Search in sources :

Example 11 with RemoteAPIToken

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());
}
Also used : RemoteAPITokenServiceImpl(ca.corefacility.bioinformatics.irida.service.impl.RemoteAPITokenServiceImpl) RemoteAPI(ca.corefacility.bioinformatics.irida.model.RemoteAPI) RemoteAPIToken(ca.corefacility.bioinformatics.irida.model.RemoteAPIToken) UserRepository(ca.corefacility.bioinformatics.irida.repositories.user.UserRepository) User(ca.corefacility.bioinformatics.irida.model.user.User) OAuthClient(org.apache.oltu.oauth2.client.OAuthClient) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) RemoteApiTokenRepository(ca.corefacility.bioinformatics.irida.repositories.RemoteApiTokenRepository) Date(java.util.Date) Before(org.junit.Before)

Example 12 with RemoteAPIToken

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);
}
Also used : RemoteAPIToken(ca.corefacility.bioinformatics.irida.model.RemoteAPIToken) Test(org.junit.Test)

Example 13 with RemoteAPIToken

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;
}
Also used : RemoteAPIToken(ca.corefacility.bioinformatics.irida.model.RemoteAPIToken) EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException)

Example 14 with RemoteAPIToken

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;
}
Also used : RemoteAPIToken(ca.corefacility.bioinformatics.irida.model.RemoteAPIToken) User(ca.corefacility.bioinformatics.irida.model.user.User) EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException)

Example 15 with RemoteAPIToken

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);
}
Also used : RemoteAPIToken(ca.corefacility.bioinformatics.irida.model.RemoteAPIToken) Date(java.util.Date)

Aggregations

RemoteAPIToken (ca.corefacility.bioinformatics.irida.model.RemoteAPIToken)16 Date (java.util.Date)8 Test (org.junit.Test)7 RemoteAPI (ca.corefacility.bioinformatics.irida.model.RemoteAPI)6 EntityNotFoundException (ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException)5 Transactional (org.springframework.transaction.annotation.Transactional)3 User (ca.corefacility.bioinformatics.irida.model.user.User)2 URI (java.net.URI)2 OAuthClientRequest (org.apache.oltu.oauth2.client.request.OAuthClientRequest)2 OAuthJSONAccessTokenResponse (org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse)2 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)2 IridaOAuthException (ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException)1 RemoteApiTokenRepository (ca.corefacility.bioinformatics.irida.repositories.RemoteApiTokenRepository)1 UserRepository (ca.corefacility.bioinformatics.irida.repositories.user.UserRepository)1 RemoteAPITokenServiceImpl (ca.corefacility.bioinformatics.irida.service.impl.RemoteAPITokenServiceImpl)1 OAuthClient (org.apache.oltu.oauth2.client.OAuthClient)1 OAuthProblemException (org.apache.oltu.oauth2.common.exception.OAuthProblemException)1 OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)1 Before (org.junit.Before)1 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)1