use of ca.corefacility.bioinformatics.irida.model.RemoteAPIToken in project irida by phac-nml.
the class RemoteAPITokenServiceImplTest method setUp.
@Before
public void setUp() {
tokenRepository = mock(RemoteApiTokenRepository.class);
userRepo = mock(UserRepository.class);
oauthClient = mock(OAuthClient.class);
service = new RemoteAPITokenServiceImpl(tokenRepository, userRepo, oauthClient);
user = new User("tom", "an@email.com", "password1", "tom", "matthews", "123456789");
remoteAPI = new RemoteAPI("apiname", "http://nowhere", "a test api", "clientId", "clientSecret");
SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(user, null));
remoteAPIToken = new RemoteAPIToken("token", remoteAPI, new Date());
}
use of ca.corefacility.bioinformatics.irida.model.RemoteAPIToken in project irida by phac-nml.
the class RemoteAPITokenServiceImplTest method testGetToken.
@Test
public void testGetToken() {
when(userRepo.loadUserByUsername(user.getUsername())).thenReturn(user);
when(tokenRepository.readTokenForApiAndUser(remoteAPI, user)).thenReturn(remoteAPIToken);
RemoteAPIToken token = service.getToken(remoteAPI);
assertEquals(remoteAPIToken, token);
verify(userRepo).loadUserByUsername(user.getUsername());
verify(tokenRepository).readTokenForApiAndUser(remoteAPI, user);
}
use of ca.corefacility.bioinformatics.irida.model.RemoteAPIToken in project irida by phac-nml.
the class RemoteAPITokenServiceImpl method getOldTokenId.
/**
* Remove any old token for this user from the database
*
* @param apiToken
* the api token to remove.
* @return the token that was found.
*/
protected RemoteAPIToken getOldTokenId(RemoteAPIToken apiToken) {
RemoteAPIToken oldToken = null;
try {
oldToken = getToken(apiToken.getRemoteApi());
logger.trace("Old token found for service " + apiToken.getRemoteApi());
apiToken.setId(oldToken.getId());
} catch (EntityNotFoundException ex) {
logger.trace("No token found for service " + apiToken.getRemoteApi());
}
return apiToken;
}
use of ca.corefacility.bioinformatics.irida.model.RemoteAPIToken in project irida by phac-nml.
the class RemoteAPITokenServiceImpl method getToken.
/**
* {@inheritDoc}
*/
@Override
public RemoteAPIToken getToken(RemoteAPI remoteAPI) throws EntityNotFoundException {
User user = userRepository.loadUserByUsername(getUserName());
RemoteAPIToken readTokenForApiAndUser = tokenRepository.readTokenForApiAndUser(remoteAPI, user);
if (readTokenForApiAndUser == null) {
throw new EntityNotFoundException("Couldn't find an OAuth2 token for this API and User");
}
return readTokenForApiAndUser;
}
use of ca.corefacility.bioinformatics.irida.model.RemoteAPIToken in project irida by phac-nml.
the class RemoteAPITokenServiceImpl method buildTokenFromResponse.
private RemoteAPIToken buildTokenFromResponse(OAuthJSONAccessTokenResponse accessTokenResponse, RemoteAPI remoteAPI) {
// read the response for the access token
String accessToken = accessTokenResponse.getAccessToken();
// Handle Refresh Tokens
String refreshToken = accessTokenResponse.getRefreshToken();
// check the token expiry
Long expiresIn = accessTokenResponse.getExpiresIn();
Long currentTime = System.currentTimeMillis();
Date expiry = new Date(currentTime + (expiresIn * ONE_SECOND_IN_MS));
logger.debug("Token expiry: " + expiry);
// create the OAuth2 token
return new RemoteAPIToken(accessToken, refreshToken, remoteAPI, expiry);
}
Aggregations