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