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