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));
}
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());
}
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;
}
Aggregations