Search in sources :

Example 6 with OAuthClient

use of org.apache.amber.oauth2.client.OAuthClient 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)

Example 7 with OAuthClient

use of org.apache.amber.oauth2.client.OAuthClient 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 8 with OAuthClient

use of org.apache.amber.oauth2.client.OAuthClient in project structr by structr.

the class StructrOAuthClient method getAccessTokenResponse.

private OAuthAccessTokenResponse getAccessTokenResponse(final HttpServletRequest request) {
    if (tokenResponse != null) {
        return tokenResponse;
    }
    try {
        String code = getCode(request);
        if (code == null) {
            logger.error("Could not get code from request, cancelling authorization process");
            return null;
        }
        OAuthClientRequest clientReq = OAuthClientRequest.tokenLocation(tokenLocation).setGrantType(getGrantType()).setClientId(clientId).setClientSecret(clientSecret).setRedirectURI(getAbsoluteUrl(request, redirectUri)).setCode(getCode(request)).buildBodyMessage();
        logger.info("Request body: {}", clientReq.getBody());
        OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
        tokenResponse = oAuthClient.accessToken(clientReq, tokenResponseClass);
        logger.info("Access token response: {}", tokenResponse.getBody());
        return tokenResponse;
    } catch (Throwable t) {
        logger.error("Could not get access token response", t);
    }
    return null;
}
Also used : URLConnectionClient(org.apache.oltu.oauth2.client.URLConnectionClient) OAuthClient(org.apache.oltu.oauth2.client.OAuthClient) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest)

Aggregations

OAuthClient (org.apache.oltu.oauth2.client.OAuthClient)7 URLConnectionClient (org.apache.oltu.oauth2.client.URLConnectionClient)5 OAuthClientRequest (org.apache.oltu.oauth2.client.request.OAuthClientRequest)5 URI (java.net.URI)2 OAuthJSONAccessTokenResponse (org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse)2 OAuthProblemException (org.apache.oltu.oauth2.common.exception.OAuthProblemException)2 OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)2 Before (org.junit.Before)2 RemoteAPI (ca.corefacility.bioinformatics.irida.model.RemoteAPI)1 RemoteAPIToken (ca.corefacility.bioinformatics.irida.model.RemoteAPIToken)1 User (ca.corefacility.bioinformatics.irida.model.user.User)1 RemoteApiTokenRepository (ca.corefacility.bioinformatics.irida.repositories.RemoteApiTokenRepository)1 UserRepository (ca.corefacility.bioinformatics.irida.repositories.user.UserRepository)1 OltuAuthorizationController (ca.corefacility.bioinformatics.irida.ria.web.oauth.OltuAuthorizationController)1 RemoteAPIService (ca.corefacility.bioinformatics.irida.service.RemoteAPIService)1 RemoteAPITokenService (ca.corefacility.bioinformatics.irida.service.RemoteAPITokenService)1 RemoteAPITokenServiceImpl (ca.corefacility.bioinformatics.irida.service.impl.RemoteAPITokenServiceImpl)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 Date (java.util.Date)1