Search in sources :

Example 6 with RemoteAPIToken

use of ca.corefacility.bioinformatics.irida.model.RemoteAPIToken in project irida by phac-nml.

the class RemoteAPITokenServiceImpl method createTokenFromAuthCode.

/**
 * Get a new token from the given auth code
 * @param authcode      the auth code to create a token for
 * @param remoteAPI     the remote api to get a token for
 * @param tokenRedirect a redirect url to get the token from
 * @return a new token
 * @throws OAuthSystemException If building the token request fails
 * @throws OAuthProblemException If the token request fails
 */
@Transactional
public RemoteAPIToken createTokenFromAuthCode(String authcode, RemoteAPI remoteAPI, String tokenRedirect) throws OAuthSystemException, OAuthProblemException {
    String serviceURI = remoteAPI.getServiceURI();
    // Build the token location for this service
    URI serviceTokenLocation = UriBuilder.fromUri(serviceURI).path("oauth").path("token").build();
    logger.debug("Remote token location: " + serviceTokenLocation);
    // Create the token request form the given auth code
    OAuthClientRequest tokenRequest = OAuthClientRequest.tokenLocation(serviceTokenLocation.toString()).setClientId(remoteAPI.getClientId()).setClientSecret(remoteAPI.getClientSecret()).setRedirectURI(tokenRedirect).setCode(authcode).setGrantType(GrantType.AUTHORIZATION_CODE).buildBodyMessage();
    // execute the request
    OAuthJSONAccessTokenResponse accessTokenResponse = oauthClient.accessToken(tokenRequest);
    // 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 and store it
    RemoteAPIToken token = new RemoteAPIToken(accessToken, refreshToken, remoteAPI, expiry);
    return create(token);
}
Also used : RemoteAPIToken(ca.corefacility.bioinformatics.irida.model.RemoteAPIToken) OAuthJSONAccessTokenResponse(org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) URI(java.net.URI) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with RemoteAPIToken

use of ca.corefacility.bioinformatics.irida.model.RemoteAPIToken in project irida by phac-nml.

the class RemoteAPITokenServiceImpl method delete.

/**
 * {@inheritDoc}
 */
@Transactional
@Override
public void delete(RemoteAPI remoteAPI) throws EntityNotFoundException {
    RemoteAPIToken token = getToken(remoteAPI);
    tokenRepository.delete(token);
}
Also used : RemoteAPIToken(ca.corefacility.bioinformatics.irida.model.RemoteAPIToken) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with RemoteAPIToken

use of ca.corefacility.bioinformatics.irida.model.RemoteAPIToken in project irida by phac-nml.

the class RemoteAPITokenServiceImpl method updateTokenFromRefreshToken.

/**
 * {@inheritDoc}
 */
@Transactional
public RemoteAPIToken updateTokenFromRefreshToken(RemoteAPI api) {
    RemoteAPIToken token = null;
    try {
        token = getToken(api);
        String refreshToken = token.getRefreshToken();
        if (refreshToken != null) {
            URI serviceTokenLocation = UriBuilder.fromUri(api.getServiceURI()).path("oauth").path("token").build();
            OAuthClientRequest tokenRequest = OAuthClientRequest.tokenLocation(serviceTokenLocation.toString()).setClientId(api.getClientId()).setClientSecret(api.getClientSecret()).setRefreshToken(refreshToken).setGrantType(GrantType.REFRESH_TOKEN).buildBodyMessage();
            OAuthJSONAccessTokenResponse accessToken = oauthClient.accessToken(tokenRequest);
            token = buildTokenFromResponse(accessToken, api);
            delete(api);
            token = create(token);
            logger.debug("Token for api " + api + " updated by refresh token.");
        } else {
            logger.debug("No refresh token for api " + api + ". Cannot update access token.");
        }
    } catch (EntityNotFoundException ex) {
        logger.debug("Token not found for api " + api + ".  Cannot update access token.");
    } catch (OAuthProblemException | OAuthSystemException ex) {
        logger.error("Updating token by refresh token failed", ex.getMessage());
    }
    return token;
}
Also used : OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) RemoteAPIToken(ca.corefacility.bioinformatics.irida.model.RemoteAPIToken) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) OAuthJSONAccessTokenResponse(org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse) EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) URI(java.net.URI) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with RemoteAPIToken

use of ca.corefacility.bioinformatics.irida.model.RemoteAPIToken in project irida by phac-nml.

the class OAuthTokenRestTemplate method createRequest.

/**
 * Add an OAuth token from the tokenRepository to the request
 */
@Override
protected ClientHttpRequest createRequest(URI uri, HttpMethod method) throws IOException {
    RemoteAPIToken token;
    try {
        token = tokenService.getToken(remoteAPI);
    } catch (EntityNotFoundException ex) {
        logger.debug("No token found for service " + remoteAPI);
        throw new IridaOAuthException("No token fround for service", remoteAPI, ex);
    }
    if (token.isExpired()) {
        logger.debug("Token for service is expired " + remoteAPI);
        throw new IridaOAuthException("Token is expired for service", remoteAPI);
    }
    ClientHttpRequest createRequest = super.createRequest(uri, method);
    createRequest.getHeaders().add("Authorization", "Bearer " + token.getTokenString());
    return createRequest;
}
Also used : IridaOAuthException(ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException) RemoteAPIToken(ca.corefacility.bioinformatics.irida.model.RemoteAPIToken) EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest)

Example 10 with RemoteAPIToken

use of ca.corefacility.bioinformatics.irida.model.RemoteAPIToken in project irida by phac-nml.

the class RemoteAPITokenServiceImplIT method testGetToken.

@Test
public void testGetToken() {
    RemoteAPI api = apiService.read(1L);
    RemoteAPIToken token = tokenService.getToken(api);
    assertNotNull(token);
    assertEquals("123456789", token.getTokenString());
}
Also used : RemoteAPI(ca.corefacility.bioinformatics.irida.model.RemoteAPI) RemoteAPIToken(ca.corefacility.bioinformatics.irida.model.RemoteAPIToken) Test(org.junit.Test)

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