use of org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse in project components by Talend.
the class Oauth2ImplicitClient method getToken.
public <T extends OAuthAccessTokenResponse> T getToken(Class<T> tokenResponseClass) {
try {
TokenRequestBuilder builder = //
OAuthClientRequest.tokenLocation(//
tokenLocation.toString()).setGrantType(//
grantType).setClientId(//
clientID).setClientSecret(clientSecret);
if (GrantType.AUTHORIZATION_CODE == grantType) {
builder = //
builder.setRedirectURI(callbackURL.toString()).setCode(getAuthorizationCode());
} else if (GrantType.REFRESH_TOKEN == grantType) {
builder = builder.setRefreshToken(refreshToken);
}
OAuthClientRequest request = builder.buildQueryMessage();
OAuthClient oauthClient = new OAuthClient(new URLConnectionClient());
return oauthClient.accessToken(request, tokenResponseClass);
} catch (OAuthSystemException e) {
throw new RuntimeException(e);
} catch (OAuthProblemException e) {
throw new RuntimeException(e);
}
}
use of org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse in project intermine by intermine.
the class Callback method getAccessToken.
private String getAccessToken(String redirect, OAuthAuthzResponse oar, OAuthProvider provider) throws OAuthSystemException, OAuthProblemException {
OAuthAccessTokenResponse oauthResponse = getTokenResponse(redirect, oar, provider);
String accessToken = oauthResponse.getAccessToken();
return accessToken;
}
use of org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse in project intermine by intermine.
the class Callback method googleProviderFlow.
/**
* Google's OpenID2.0 -> OpenIDConnect (ie. Open Auth 2.0) migration makes this
* special branch necessary.
*/
private ActionMessages googleProviderFlow(HttpServletRequest request, String providerName, String redirectUri, OAuthProvider provider, OAuthAuthzResponse oar) throws ForseenProblem, OAuthSystemException, OAuthProblemException, JSONException {
// Special flow just for Google, because Google is special (not in a good way).
OAuthAccessTokenResponse resp = getTokenResponse(redirectUri, oar, provider);
LOG.debug("GOOGLE RESPONSE: " + resp.getBody());
MigrationMapping migrationMapping = null;
Base64 decoder = new Base64();
String accessToken = resp.getAccessToken();
JSONObject respData;
try {
respData = new JSONObject(resp.getBody());
} catch (JSONException e) {
throw new ForseenProblem("oauth2.error.bad-json");
}
String jwt = respData.optString("id_token");
if (jwt != null) {
String[] pieces = jwt.split("\\.");
if (pieces.length == 3) {
JSONObject claims = new JSONObject(new String(decoder.decode(pieces[1])));
String openidID = claims.optString("openid_id");
String sub = claims.optString("sub");
migrationMapping = new MigrationMapping(openidID, sub);
} else {
LOG.error("id_token is not a valid JWT - has Google changed their API?");
}
} else {
LOG.debug("No id_token (and thus migration info) provided by Google");
}
DelegatedIdentity identity = getDelegatedIdentity(providerName, accessToken);
return loginUser(request, identity, migrationMapping);
}
use of org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse in project intermine by intermine.
the class CallbackService method getAccessToken.
private String getAccessToken(String redirect, OAuthAuthzResponse oar, OAuthProvider provider) throws OAuthSystemException, OAuthProblemException {
OAuthClient oauthClient = new OAuthClient(new URLConnectionClient());
OAuthClientRequest clientReq;
OAuthClientRequest.TokenRequestBuilder requestBuilder = OAuthClientRequest.tokenLocation(provider.getTokenUrl()).setGrantType(GrantType.AUTHORIZATION_CODE).setClientId(provider.getClientId()).setClientSecret(provider.getClientSecret()).setRedirectURI(redirect).setCode(oar.getCode());
switch(provider.getMessageFormat()) {
case BODY:
clientReq = requestBuilder.buildBodyMessage();
break;
case QUERY:
clientReq = requestBuilder.buildQueryMessage();
break;
default:
throw new RuntimeException("Unknown message format");
}
LOG.info("Requesting access token: URI = " + clientReq.getLocationUri() + " BODY = " + clientReq.getBody());
try {
OAuthAccessTokenResponse tokenResponse = null;
switch(provider.getResponseType()) {
case FORM:
tokenResponse = oauthClient.accessToken(clientReq, GitHubTokenResponse.class);
break;
case JSON:
tokenResponse = oauthClient.accessToken(clientReq);
break;
default:
throw new RuntimeException("Unknown response type");
}
return tokenResponse.getAccessToken();
} catch (OAuthProblemException ex) {
throw new BadRequestException(ex.getMessage());
}
}
use of org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse 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)).setScope(getScope()).setCode(getCode(request)).buildBodyMessage();
if (isVerboseLoggingEnabled()) {
logger.info("Request body: {}", clientReq.getBody());
}
final OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
tokenResponse = oAuthClient.accessToken(clientReq, tokenResponseClass);
if (isVerboseLoggingEnabled()) {
logger.info("Access token response: {}", tokenResponse.getBody());
}
return tokenResponse;
} catch (Throwable t) {
logger.error("Could not get access token response", t);
}
return null;
}
Aggregations