Search in sources :

Example 1 with OAuth20ProfileDefinition

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;
}
Also used : HttpCommunicationException(org.pac4j.core.exception.HttpCommunicationException) OAuthException(com.github.scribejava.core.exceptions.OAuthException) IOException(java.io.IOException) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service) OAuth20ProfileDefinition(org.pac4j.oauth.profile.definition.OAuth20ProfileDefinition) DefaultApi20(com.github.scribejava.core.builder.api.DefaultApi20) ExecutionException(java.util.concurrent.ExecutionException) OAuth20Configuration(org.pac4j.oauth.config.OAuth20Configuration)

Aggregations

DefaultApi20 (com.github.scribejava.core.builder.api.DefaultApi20)1 OAuthException (com.github.scribejava.core.exceptions.OAuthException)1 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)1 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 HttpCommunicationException (org.pac4j.core.exception.HttpCommunicationException)1 OAuth20Configuration (org.pac4j.oauth.config.OAuth20Configuration)1 OAuth20ProfileDefinition (org.pac4j.oauth.profile.definition.OAuth20ProfileDefinition)1