Search in sources :

Example 1 with OAuthJSONAccessTokenResponse

use of org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse in project incubator-gobblin by apache.

the class SalesforceRestWriter method onConnect.

/**
 * Retrieve access token, if needed, retrieve instance url, and set server host URL
 * {@inheritDoc}
 * @see org.apache.gobblin.writer.http.HttpWriter#onConnect(org.apache.http.HttpHost)
 */
@Override
public void onConnect(URI serverHost) throws IOException {
    if (!StringUtils.isEmpty(accessToken)) {
        // No need to be called if accessToken is active.
        return;
    }
    try {
        getLog().info("Getting Oauth2 access token.");
        OAuthClientRequest request = OAuthClientRequest.tokenLocation(serverHost.toString()).setGrantType(GrantType.PASSWORD).setClientId(clientId).setClientSecret(clientSecret).setUsername(userId).setPassword(password + securityToken).buildQueryMessage();
        OAuthClient client = new OAuthClient(new URLConnectionClient());
        OAuthJSONAccessTokenResponse response = client.accessToken(request, OAuth.HttpMethod.POST);
        accessToken = response.getAccessToken();
        setCurServerHost(new URI(response.getParam("instance_url")));
    } catch (OAuthProblemException e) {
        throw new NonTransientException("Error while authenticating with Oauth2", e);
    } catch (OAuthSystemException e) {
        throw new RuntimeException("Failed getting access token", e);
    } catch (URISyntaxException e) {
        throw new RuntimeException("Failed due to invalid instance url", e);
    }
}
Also used : OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) NonTransientException(org.apache.gobblin.exception.NonTransientException) URLConnectionClient(org.apache.oltu.oauth2.client.URLConnectionClient) OAuthClient(org.apache.oltu.oauth2.client.OAuthClient) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) OAuthJSONAccessTokenResponse(org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse) URISyntaxException(java.net.URISyntaxException) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) URI(java.net.URI)

Example 2 with OAuthJSONAccessTokenResponse

use of org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse 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 3 with OAuthJSONAccessTokenResponse

use of org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse 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 4 with OAuthJSONAccessTokenResponse

use of org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse in project irida by phac-nml.

the class OltuAuthorizationController method getToken.

/**
 * Receive the OAuth2 authorization code and request an OAuth2 token
 *
 * @param request
 *            The incoming request
 * @param response
 *            The response to redirect
 * @param apiId
 *            the Long ID of the API we're requesting from
 * @param redirect
 *            The URL location to redirect to after completion
 * @return A ModelAndView redirecting back to the resource that was
 *         requested
 * @throws IOException
 * @throws OAuthSystemException
 * @throws OAuthProblemException
 * @throws URISyntaxException
 */
@RequestMapping("/token")
public ModelAndView getToken(HttpServletRequest request, HttpServletResponse response, @RequestParam("redirect") String redirect) throws IOException, OAuthSystemException, OAuthProblemException, URISyntaxException {
    // Get the OAuth2 auth code
    OAuthAuthzResponse oar = OAuthAuthzResponse.oauthCodeAuthzResponse(request);
    String code = oar.getCode();
    logger.debug("got code " + code);
    // Read the RemoteAPI from the RemoteAPIService and get the base URI
    // Build the token location for this service
    URI serviceTokenLocation = UriBuilder.fromUri(serviceURI).path("oauth").path("token").build();
    logger.debug("token loc " + serviceTokenLocation);
    // Build the redirect URI to request a token from
    String tokenRedirect = buildRedirectURI(redirect);
    // Create the token request form the given auth code
    OAuthClientRequest tokenRequest = OAuthClientRequest.tokenLocation(serviceTokenLocation.toString()).setClientId(clientId).setClientSecret(clientSecret).setRedirectURI(tokenRedirect).setCode(code).setGrantType(GrantType.AUTHORIZATION_CODE).buildBodyMessage();
    // execute the request
    OAuthClient client = new OAuthClient(new URLConnectionClient());
    // read the response for the access token
    OAuthJSONAccessTokenResponse accessTokenResponse = client.accessToken(tokenRequest, OAuthJSONAccessTokenResponse.class);
    String accessToken = accessTokenResponse.getAccessToken();
    // check the token expiry
    Long expiresIn = accessTokenResponse.getExpiresIn();
    logger.debug("Token expires in " + expiresIn);
    // adding the token to the response page. This is just a demo to show
    // how to get an oauth token. NEVER DO THIS!!!
    redirect = redirect + "?token=" + accessToken;
    // redirect the response back to the requested resource
    return new ModelAndView(new RedirectView(redirect));
}
Also used : URLConnectionClient(org.apache.oltu.oauth2.client.URLConnectionClient) OAuthClient(org.apache.oltu.oauth2.client.OAuthClient) ModelAndView(org.springframework.web.servlet.ModelAndView) RedirectView(org.springframework.web.servlet.view.RedirectView) OAuthJSONAccessTokenResponse(org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) OAuthAuthzResponse(org.apache.oltu.oauth2.client.response.OAuthAuthzResponse) URI(java.net.URI) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

URI (java.net.URI)4 OAuthClientRequest (org.apache.oltu.oauth2.client.request.OAuthClientRequest)4 OAuthJSONAccessTokenResponse (org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse)4 RemoteAPIToken (ca.corefacility.bioinformatics.irida.model.RemoteAPIToken)2 OAuthClient (org.apache.oltu.oauth2.client.OAuthClient)2 URLConnectionClient (org.apache.oltu.oauth2.client.URLConnectionClient)2 OAuthProblemException (org.apache.oltu.oauth2.common.exception.OAuthProblemException)2 OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)2 Transactional (org.springframework.transaction.annotation.Transactional)2 EntityNotFoundException (ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException)1 URISyntaxException (java.net.URISyntaxException)1 Date (java.util.Date)1 NonTransientException (org.apache.gobblin.exception.NonTransientException)1 OAuthAuthzResponse (org.apache.oltu.oauth2.client.response.OAuthAuthzResponse)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1 RedirectView (org.springframework.web.servlet.view.RedirectView)1