use of org.pac4j.oauth.profile.definition.OAuth20ProfileDefinition in project pac4j by pac4j.
the class FacebookProfileCreator method retrieveUserProfileFromToken.
@Override
protected FacebookProfile retrieveUserProfileFromToken(final WebContext context, final OAuth2AccessToken accessToken) {
final OAuth20ProfileDefinition<FacebookProfile, OAuth20Configuration> profileDefinition = (OAuth20ProfileDefinition<FacebookProfile, OAuth20Configuration>) configuration.getProfileDefinition();
final FacebookConfiguration facebookConfiguration = (FacebookConfiguration) configuration;
final String profileUrl = profileDefinition.getProfileUrl(accessToken, configuration);
final OAuth20Service service = this.configuration.buildService(context, client, null);
String body = sendRequestForData(service, accessToken, profileUrl, Verb.GET);
if (body == null) {
throw new HttpCommunicationException("Not data found for accessToken: " + accessToken);
}
final FacebookProfile profile = profileDefinition.extractUserProfile(body);
addAccessTokenToProfile(profile, accessToken);
if (profile != null && facebookConfiguration.isRequiresExtendedToken()) {
String url = CommonHelper.addParameter(EXCHANGE_TOKEN_URL, OAuthConstants.CLIENT_ID, configuration.getKey());
url = CommonHelper.addParameter(url, OAuthConstants.CLIENT_SECRET, configuration.getSecret());
url = addExchangeToken(url, accessToken);
final OAuthRequest request = createOAuthRequest(url, Verb.GET);
final long t0 = System.currentTimeMillis();
final Response response;
final int code;
try {
response = service.execute(request);
body = response.getBody();
code = response.getCode();
} catch (final IOException | InterruptedException | ExecutionException e) {
throw new HttpCommunicationException("Error getting body:" + e.getMessage());
}
final long t1 = System.currentTimeMillis();
logger.debug("Request took: " + (t1 - t0) + " ms for: " + url);
logger.debug("response code: {} / response body: {}", code, body);
if (code == 200) {
logger.debug("Retrieve extended token from {}", body);
final OAuth2AccessToken extendedAccessToken;
try {
extendedAccessToken = ((DefaultApi20) configuration.getApi()).getAccessTokenExtractor().extract(response);
} catch (IOException | OAuthException ex) {
throw new HttpCommunicationException("Error extracting token: " + ex.getMessage());
}
logger.debug("Extended token: {}", extendedAccessToken);
addAccessTokenToProfile(profile, extendedAccessToken);
} else {
logger.error("Cannot get extended token: {} / {}", code, body);
}
}
return profile;
}
Aggregations