Search in sources :

Example 1 with HttpCommunicationException

use of org.pac4j.core.exception.HttpCommunicationException in project pac4j by pac4j.

the class OAuth10Authenticator method retrieveAccessToken.

@Override
protected void retrieveAccessToken(final WebContext context, final OAuthCredentials credentials) {
    OAuth10Credentials oAuth10Credentials = (OAuth10Credentials) credentials;
    final OAuth1RequestToken tokenRequest = oAuth10Credentials.getRequestToken();
    final String token = oAuth10Credentials.getToken();
    final String verifier = oAuth10Credentials.getVerifier();
    logger.debug("tokenRequest: {}", tokenRequest);
    logger.debug("token: {}", token);
    logger.debug("verifier: {}", verifier);
    if (tokenRequest == null) {
        final String message = "Token request expired";
        throw new OAuthCredentialsException(message);
    }
    final String savedToken = tokenRequest.getToken();
    logger.debug("savedToken: {}", savedToken);
    if (savedToken == null || !savedToken.equals(token)) {
        final String message = "Token received: " + token + " is different from saved token: " + savedToken;
        throw new OAuthCredentialsException(message);
    }
    final OAuth1AccessToken accessToken;
    try {
        accessToken = this.configuration.buildService(context, client, null).getAccessToken(tokenRequest, verifier);
    } catch (final IOException | InterruptedException | ExecutionException e) {
        throw new HttpCommunicationException("Error getting token:" + e.getMessage());
    }
    logger.debug("accessToken: {}", accessToken);
    oAuth10Credentials.setAccessToken(accessToken);
}
Also used : OAuth1AccessToken(com.github.scribejava.core.model.OAuth1AccessToken) OAuth1RequestToken(com.github.scribejava.core.model.OAuth1RequestToken) HttpCommunicationException(org.pac4j.core.exception.HttpCommunicationException) OAuthCredentialsException(org.pac4j.oauth.exception.OAuthCredentialsException) OAuth10Credentials(org.pac4j.oauth.credentials.OAuth10Credentials) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with HttpCommunicationException

use of org.pac4j.core.exception.HttpCommunicationException in project pac4j by pac4j.

the class OAuth20Authenticator method retrieveAccessToken.

@Override
protected void retrieveAccessToken(final WebContext context, final OAuthCredentials credentials) {
    OAuth20Credentials oAuth20Credentials = (OAuth20Credentials) credentials;
    // no request token saved in context and no token (OAuth v2.0)
    final String code = oAuth20Credentials.getCode();
    logger.debug("code: {}", code);
    final OAuth2AccessToken accessToken;
    try {
        accessToken = this.configuration.buildService(context, client, null).getAccessToken(code);
    } catch (final IOException | InterruptedException | ExecutionException e) {
        throw new HttpCommunicationException("Error getting token:" + e.getMessage());
    }
    logger.debug("accessToken: {}", accessToken);
    oAuth20Credentials.setAccessToken(accessToken);
}
Also used : OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) HttpCommunicationException(org.pac4j.core.exception.HttpCommunicationException) OAuth20Credentials(org.pac4j.oauth.credentials.OAuth20Credentials) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with HttpCommunicationException

use of org.pac4j.core.exception.HttpCommunicationException 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)

Example 4 with HttpCommunicationException

use of org.pac4j.core.exception.HttpCommunicationException in project pac4j by pac4j.

the class OAuthProfileCreator method sendRequestForData.

/**
 * Make a request to get the data of the authenticated user for the provider.
 *
 * @param service the OAuth service
 * @param accessToken the access token
 * @param dataUrl     url of the data
 * @param verb        method used to request data
 * @return the user data response
 */
protected String sendRequestForData(final S service, final T accessToken, final String dataUrl, Verb verb) {
    logger.debug("accessToken: {} / dataUrl: {}", accessToken, dataUrl);
    final long t0 = System.currentTimeMillis();
    final OAuthRequest request = createOAuthRequest(dataUrl, verb);
    signRequest(service, accessToken, request);
    final String body;
    final int code;
    try {
        Response response = service.execute(request);
        code = response.getCode();
        body = response.getBody();
    } 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: " + dataUrl);
    logger.debug("response code: {} / response body: {}", code, body);
    if (code != 200) {
        throw new HttpCommunicationException(code, body);
    }
    return body;
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) HttpCommunicationException(org.pac4j.core.exception.HttpCommunicationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with HttpCommunicationException

use of org.pac4j.core.exception.HttpCommunicationException in project pac4j by pac4j.

the class YahooProfileCreator method retrieveUserProfileFromToken.

@Override
protected YahooProfile retrieveUserProfileFromToken(final WebContext context, final OAuth1AccessToken accessToken) {
    // get the guid: https://developer.yahoo.com/social/rest_api_guide/introspective-guid-resource.html
    final OAuth10ProfileDefinition<YahooProfile> profileDefinition = (OAuth10ProfileDefinition<YahooProfile>) configuration.getProfileDefinition();
    final String profileUrl = profileDefinition.getProfileUrl(accessToken, this.configuration);
    final OAuth10aService service = configuration.buildService(context, client, null);
    String body = sendRequestForData(service, accessToken, profileUrl, profileDefinition.getProfileVerb());
    final String guid = CommonHelper.substringBetween(body, "<value>", "</value>");
    logger.debug("guid : {}", guid);
    if (CommonHelper.isBlank(guid)) {
        throw new HttpCommunicationException("Cannot find guid from body : " + body);
    }
    body = sendRequestForData(service, accessToken, "https://social.yahooapis.com/v1/user/" + guid + "/profile?format=json", Verb.GET);
    final YahooProfile profile = (YahooProfile) configuration.getProfileDefinition().extractUserProfile(body);
    addAccessTokenToProfile(profile, accessToken);
    return profile;
}
Also used : OAuth10ProfileDefinition(org.pac4j.oauth.profile.definition.OAuth10ProfileDefinition) HttpCommunicationException(org.pac4j.core.exception.HttpCommunicationException) OAuth10aService(com.github.scribejava.core.oauth.OAuth10aService)

Aggregations

HttpCommunicationException (org.pac4j.core.exception.HttpCommunicationException)6 IOException (java.io.IOException)5 ExecutionException (java.util.concurrent.ExecutionException)5 OAuthException (com.github.scribejava.core.exceptions.OAuthException)2 OAuth1RequestToken (com.github.scribejava.core.model.OAuth1RequestToken)2 OAuth10aService (com.github.scribejava.core.oauth.OAuth10aService)2 DefaultApi20 (com.github.scribejava.core.builder.api.DefaultApi20)1 OAuth1AccessToken (com.github.scribejava.core.model.OAuth1AccessToken)1 OAuth2AccessToken (com.github.scribejava.core.model.OAuth2AccessToken)1 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)1 Response (com.github.scribejava.core.model.Response)1 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)1 TechnicalException (org.pac4j.core.exception.TechnicalException)1 OAuth20Configuration (org.pac4j.oauth.config.OAuth20Configuration)1 OAuth10Credentials (org.pac4j.oauth.credentials.OAuth10Credentials)1 OAuth20Credentials (org.pac4j.oauth.credentials.OAuth20Credentials)1 OAuthCredentialsException (org.pac4j.oauth.exception.OAuthCredentialsException)1 OAuth10ProfileDefinition (org.pac4j.oauth.profile.definition.OAuth10ProfileDefinition)1 OAuth20ProfileDefinition (org.pac4j.oauth.profile.definition.OAuth20ProfileDefinition)1