use of org.apache.oltu.oauth2.client.request.OAuthClientRequest in project mbed-cloud-sdk-java by ARMmbed.
the class OAuthOkHttpClient method execute.
public <T extends OAuthClientResponse> T execute(OAuthClientRequest request, Map<String, String> headers, String requestMethod, Class<T> responseClass) throws OAuthSystemException, OAuthProblemException {
MediaType mediaType = MediaType.parse("application/json");
Request.Builder requestBuilder = new Request.Builder().url(request.getLocationUri());
if (headers != null) {
for (Entry<String, String> entry : headers.entrySet()) {
if (entry.getKey().equalsIgnoreCase("Content-Type")) {
mediaType = MediaType.parse(entry.getValue());
} else {
requestBuilder.addHeader(entry.getKey(), entry.getValue());
}
}
}
RequestBody body = request.getBody() != null ? RequestBody.create(mediaType, request.getBody()) : null;
requestBuilder.method(requestMethod, body);
try {
Response response = client.newCall(requestBuilder.build()).execute();
return OAuthClientResponseFactory.createCustomResponse(response.body().string(), response.body().contentType().toString(), response.code(), responseClass);
} catch (IOException e) {
throw new OAuthSystemException(e);
}
}
use of org.apache.oltu.oauth2.client.request.OAuthClientRequest in project irida by phac-nml.
the class OltuAuthorizationController method authenticate.
/**
* Begin authentication procedure by redirecting to remote authorization
* location
*
* @param remoteAPI
* The API we need to authenticate with
* @param redirect
* The location to redirect back to after authentication is
* complete
* @return A ModelAndView beginning the authentication procedure
* @throws OAuthSystemException
*/
/**
* Begin authentication procedure by redirecting to remote authorization
* location
*
* @param serviceURI
* The base URI of the rest api service
* @param clientID
* The Client ID to connect with
* @param clientSecret
* The client secret to connect with
* @param redirect
* Page to redirect to after auth is complete
* @return ModelAndView redirecting to the authorization location
* @throws OAuthSystemException
*/
public ModelAndView authenticate(String serviceURI, String clientID, String clientSecret, String redirect) throws OAuthSystemException {
// save the client credentials and information
this.clientId = clientID;
this.clientSecret = clientSecret;
this.serviceURI = serviceURI;
// build the authorization path
URI serviceAuthLocation = UriBuilder.fromUri(serviceURI).path("oauth").path("authorize").build();
logger.debug("redirect: " + redirect);
// build a redirect URI to redirect to after auth flow is completed
String tokenRedirect = buildRedirectURI(redirect);
// build the redirect query to request an authorization code from the
// remote API
OAuthClientRequest request = OAuthClientRequest.authorizationLocation(serviceAuthLocation.toString()).setClientId(clientID).setRedirectURI(tokenRedirect).setResponseType(ResponseType.CODE.toString()).setScope("read").buildQueryMessage();
String locURI = request.getLocationUri();
logger.debug("authorization request location:" + locURI);
// create the redirection
ModelAndView modelAndView = new ModelAndView(new RedirectView(locURI));
return modelAndView;
}
use of org.apache.oltu.oauth2.client.request.OAuthClientRequest 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.oltu.oauth2.client.request.OAuthClientRequest in project irida by phac-nml.
the class OltuAuthorizationController method authenticate.
/**
* Begin authentication procedure by redirecting to remote authorization
* location
*
* @param remoteAPI
* The API we need to authenticate with
* @param redirect
* The location to redirect back to after authentication is
* complete
* @return A ModelAndView beginning the authentication procedure
* @throws OAuthSystemException
* if we can't read from the authorization server.
*/
public String authenticate(RemoteAPI remoteAPI, String redirect) throws OAuthSystemException {
// get the URI for the remote service we'll be requesting from
String serviceURI = remoteAPI.getServiceURI();
// build the authorization path
URI serviceAuthLocation = UriBuilder.fromUri(serviceURI).path("oauth").path("authorize").build();
logger.debug("Authenticating for service: " + remoteAPI);
logger.debug("Redirect after authentication: " + redirect);
// build a redirect URI to redirect to after auth flow is completed
String tokenRedirect = buildRedirectURI(remoteAPI.getId(), redirect);
// build the redirect query to request an authorization code from the
// remote API
OAuthClientRequest request = OAuthClientRequest.authorizationLocation(serviceAuthLocation.toString()).setClientId(remoteAPI.getClientId()).setRedirectURI(tokenRedirect).setResponseType(ResponseType.CODE.toString()).setScope("read").buildQueryMessage();
String locURI = request.getLocationUri();
logger.trace("Authorization request location: " + locURI);
return "redirect:" + locURI;
}
use of org.apache.oltu.oauth2.client.request.OAuthClientRequest 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