Skip to content

Commit

Permalink
update all clients
Browse files Browse the repository at this point in the history
  • Loading branch information
leleuj committed Jan 28, 2016
1 parent e8f09ae commit b8cb84f
Show file tree
Hide file tree
Showing 48 changed files with 382 additions and 1,529 deletions.
Expand Up @@ -15,7 +15,6 @@
*/
package org.pac4j.oauth.client;

import com.github.scribejava.core.builder.api.DefaultApi20;
import com.github.scribejava.core.model.OAuthConfig;
import com.github.scribejava.core.oauth.OAuth20Service;
import org.apache.commons.lang3.RandomStringUtils;
Expand Down Expand Up @@ -50,34 +49,18 @@ protected String getState() {
@Override
protected String retrieveAuthorizationUrl(final WebContext context) {
// create a specific configuration with state
final OAuthConfig config = getOAuthConfig(context);
final OAuthConfig config = buildOAuthConfig(context);
final String state = getState();
config.setState(state);
// save state
context.setSessionAttribute(getName() + STATE_PARAMETER, state);
// create a specific service
final OAuth20Service newService = new OAuth20Service(getApi(), config);
final OAuth20Service newService = (OAuth20Service) getApi().createService(config);
final String authorizationUrl = newService.getAuthorizationUrl();
logger.debug("authorizationUrl: {}", authorizationUrl);
return authorizationUrl;
}

/**
* Define the OAuth API for this client.
*
* @return the OAuth API
*/
protected abstract DefaultApi20 getApi();


/**
* Define the OAuth configuration for this client.
*
* @param context the web context
* @return the OAuth configuration
*/
protected abstract OAuthConfig getOAuthConfig(final WebContext context);

@Override
protected OAuthCredentials getOAuthCredentials(final WebContext context) {
// check state parameter if required
Expand All @@ -87,13 +70,13 @@ protected OAuthCredentials getOAuthCredentials(final WebContext context) {
final String sessionState = (String) context.getSessionAttribute(getName() + STATE_PARAMETER);
// clean from session after retrieving it
context.setSessionAttribute(getName() + STATE_PARAMETER, null);
logger.debug("sessionState : {} / stateParameter : {}", sessionState, stateParameter);
logger.debug("sessionState: {} / stateParameter: {}", sessionState, stateParameter);
if (!stateParameter.equals(sessionState)) {
final String message = "State parameter mismatch : session expired or possible threat of cross-site request forgery";
final String message = "State parameter mismatch: session expired or possible threat of cross-site request forgery";
throw new OAuthCredentialsException(message);
}
} else {
final String message = "Missing state parameter : session expired or possible threat of cross-site request forgery";
final String message = "Missing state parameter: session expired or possible threat of cross-site request forgery";
throw new OAuthCredentialsException(message);
}

Expand Down
Expand Up @@ -15,6 +15,7 @@
*/
package org.pac4j.oauth.client;

import com.github.scribejava.core.builder.api.Api;
import com.github.scribejava.core.exceptions.OAuthException;
import com.github.scribejava.core.model.*;
import com.github.scribejava.core.oauth.OAuthService;
Expand Down Expand Up @@ -60,6 +61,45 @@ protected void internalInit(final WebContext context) {
CommonHelper.assertNotBlank("key", this.key);
CommonHelper.assertNotBlank("secret", this.secret);
CommonHelper.assertNotBlank("callbackUrl", this.callbackUrl);

this.service = getApi().createService(buildOAuthConfig(context));
}

/**
* Build an OAuth configuration.
*
* @param context the web context
* @return the OAuth configuration
*/
protected OAuthConfig buildOAuthConfig(final WebContext context) {
return new OAuthConfig(this.key, this.secret, computeFinalCallbackUrl(context),
SignatureType.Header, getOAuthScope(), null, this.connectTimeout, this.readTimeout, hasOAuthGrantType() ? "authorization_code" : null);
}

/**
* Define the OAuth API for this client.
*
* @return the OAuth API
*/
protected abstract Api getApi();

/**
* Define the OAuth scope for this client.
*
* @return the OAuth scope
*/
protected String getOAuthScope() {
return null;
}


/**
* Whether the grant type must be added.
*
* @return Whether the grant type must be added
*/
protected boolean hasOAuthGrantType() {
return false;
}

@Override
Expand Down Expand Up @@ -171,7 +211,7 @@ public U getUserProfile(final WebContext context, final String accessToken) {
protected U retrieveUserProfileFromToken(final Token accessToken) {
final String body = sendRequestForData(accessToken, getProfileUrl(accessToken));
if (body == null) {
throw new HttpCommunicationException("Not data found for accessToken : " + accessToken);
throw new HttpCommunicationException("Not data found for accessToken: " + accessToken);
}
final U profile = extractUserProfile(body);
addAccessTokenToProfile(profile, accessToken);
Expand All @@ -194,7 +234,7 @@ protected U retrieveUserProfileFromToken(final Token accessToken) {
* @return the user data response
*/
protected String sendRequestForData(final Token accessToken, final String dataUrl) {
logger.debug("accessToken : {} / dataUrl : {}", accessToken, dataUrl);
logger.debug("accessToken: {} / dataUrl: {}", accessToken, dataUrl);
final long t0 = System.currentTimeMillis();
final OAuthRequest request = createOAuthRequest(dataUrl);
this.service.signRequest(accessToken, request);
Expand All @@ -206,8 +246,8 @@ protected String sendRequestForData(final Token accessToken, final String dataUr
final int code = response.getCode();
final String body = response.getBody();
final long t1 = System.currentTimeMillis();
logger.debug("Request took : " + (t1 - t0) + " ms for : " + dataUrl);
logger.debug("response code : {} / response body : {}", code, body);
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);
}
Expand Down Expand Up @@ -255,24 +295,11 @@ public String sendRequestForData(final OAuth10Profile profile, final String data
protected void addAccessTokenToProfile(final U profile, final Token accessToken) {
if (profile != null) {
final String token = accessToken.getToken();
logger.debug("add access_token : {} to profile", token);
logger.debug("add access_token: {} to profile", token);
profile.setAccessToken(token);
}
}

/**
* Build an OAuth configuration.
*
* @param context the web context
* @param type the signature type
* @param scope the scope
* @return the OAuth configuration
*/
protected OAuthConfig buildOAuthConfig(final WebContext context, final SignatureType type, final String scope) {
return new OAuthConfig(this.key, this.secret, computeFinalCallbackUrl(context),
type, scope, null, this.connectTimeout, this.readTimeout, null);
}

public void setKey(final String key) {
this.key = key;
}
Expand Down
Expand Up @@ -16,10 +16,8 @@
package org.pac4j.oauth.client;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.scribejava.core.model.SignatureType;
import com.github.scribejava.core.builder.api.Api;
import com.github.scribejava.core.model.Token;
import com.github.scribejava.core.oauth.OAuth10aService;
import org.pac4j.core.context.WebContext;
import org.pac4j.oauth.profile.JsonHelper;
import org.pac4j.oauth.profile.bitbucket.BitbucketProfile;
import org.pac4j.scribe.builder.api.BitBucketApi;
Expand All @@ -43,9 +41,8 @@ public BitbucketClient(final String key, final String secret) {
}

@Override
protected void internalInit(final WebContext context) {
super.internalInit(context);
this.service = new OAuth10aService(new BitBucketApi(), buildOAuthConfig(context, SignatureType.Header, null));
protected Api getApi() {
return new BitBucketApi();
}

@Override
Expand Down
Expand Up @@ -17,9 +17,8 @@

import java.util.Iterator;

import com.github.scribejava.core.model.SignatureType;
import com.github.scribejava.core.builder.api.Api;
import com.github.scribejava.core.model.Token;
import com.github.scribejava.core.oauth.OAuth20Service;
import org.pac4j.core.context.WebContext;
import org.pac4j.core.util.CommonHelper;
import org.pac4j.oauth.profile.JsonHelper;
Expand Down Expand Up @@ -54,12 +53,15 @@ public CasOAuthWrapperClient(final String key, final String secret, final String

@Override
protected void internalInit(final WebContext context) {
super.internalInit(context);
CommonHelper.assertNotBlank("casOAuthUrl", this.casOAuthUrl);
this.service = new OAuth20Service(new CasOAuthWrapperApi20(this.casOAuthUrl, this.springSecurityCompliant),
buildOAuthConfig(context, SignatureType.Header, null));
super.internalInit(context);
}


@Override
protected Api getApi() {
return new CasOAuthWrapperApi20(this.casOAuthUrl, this.springSecurityCompliant);
}

@Override
protected String getProfileUrl(final Token accessToken) {
return this.casOAuthUrl + "/profile";
Expand Down
Expand Up @@ -16,9 +16,8 @@
package org.pac4j.oauth.client;

import com.github.scribejava.apis.DropBoxApi;
import com.github.scribejava.core.model.SignatureType;
import com.github.scribejava.core.builder.api.Api;
import com.github.scribejava.core.model.Token;
import com.github.scribejava.core.oauth.OAuth10aService;
import org.pac4j.core.context.WebContext;
import org.pac4j.core.profile.AttributesDefinition;
import org.pac4j.oauth.credentials.OAuthCredentials;
Expand All @@ -44,13 +43,12 @@ public DropBoxClient(final String key, final String secret) {
setKey(key);
setSecret(secret);
}

@Override
protected void internalInit(final WebContext context) {
super.internalInit(context);
this.service = new OAuth10aService(DropBoxApi.instance(), buildOAuthConfig(context, SignatureType.Header, null));
protected Api getApi() {
return DropBoxApi.instance();
}

@Override
protected String getProfileUrl(final Token accessToken) {
return "https://api.dropbox.com/1/account/info";
Expand Down
Expand Up @@ -20,9 +20,9 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.github.scribejava.apis.FacebookApi;
import com.github.scribejava.core.builder.api.Api;
import com.github.scribejava.core.builder.api.DefaultApi20;
import com.github.scribejava.core.model.*;
import com.github.scribejava.core.oauth.OAuth20Service;
import org.pac4j.core.context.WebContext;
import org.pac4j.core.exception.HttpCommunicationException;
import org.pac4j.core.exception.TechnicalException;
Expand Down Expand Up @@ -89,19 +89,18 @@ public FacebookClient(final String key, final String secret) {

@Override
protected void internalInit(final WebContext context) {
super.internalInit(context);
CommonHelper.assertNotBlank("fields", this.fields);
this.service = new OAuth20Service(getApi(), getOAuthConfig(context));
super.internalInit(context);
}

@Override
protected DefaultApi20 getApi() {
protected Api getApi() {
return FacebookApi.instance();
}

@Override
protected OAuthConfig getOAuthConfig(final WebContext context) {
return buildOAuthConfig(context, SignatureType.Header, this.scope);
protected String getOAuthScope() {
return this.scope;
}

@Override
Expand Down Expand Up @@ -139,7 +138,7 @@ protected FacebookProfile retrieveUserProfileFromToken(final Token accessToken)
logger.debug("response code: {} / response body: {}", code, body);
if (code == 200) {
logger.debug("Retrieve extended token from {}", body);
final Token extendedAccessToken = getApi().getAccessTokenExtractor().extract(body);
final Token extendedAccessToken = ((DefaultApi20) getApi()).getAccessTokenExtractor().extract(body);
logger.debug("Extended token: {}", extendedAccessToken);
addAccessTokenToProfile(profile, extendedAccessToken);
} else {
Expand Down
Expand Up @@ -17,12 +17,13 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.github.scribejava.apis.Foursquare2Api;
import com.github.scribejava.core.model.SignatureType;
import com.github.scribejava.core.builder.api.Api;
import com.github.scribejava.core.builder.api.DefaultApi20;
import com.github.scribejava.core.model.Token;
import com.github.scribejava.core.oauth.OAuth20Service;
import org.pac4j.core.context.WebContext;
import org.pac4j.oauth.profile.JsonHelper;
import org.pac4j.oauth.profile.foursquare.FoursquareProfile;
import org.pac4j.scribe.oauth.Foursquare20Service;

/**
* <p>This class is the OAuth client to authenticate users in Foursquare.
Expand All @@ -33,6 +34,7 @@
* @since 1.5.0
*/
public class FoursquareClient extends BaseOAuth20Client<FoursquareProfile>{

public FoursquareClient() {}

public FoursquareClient(String key, String secret) {
Expand All @@ -43,7 +45,22 @@ public FoursquareClient(String key, String secret) {
@Override
protected void internalInit(final WebContext context) {
super.internalInit(context);
this.service = new OAuth20Service(Foursquare2Api.instance(), buildOAuthConfig(context, SignatureType.Header, "user"));
this.service = new Foursquare20Service((DefaultApi20) getApi(), buildOAuthConfig(context));
}

@Override
protected Api getApi() {
return Foursquare2Api.instance();
}

@Override
protected String getOAuthScope() {
return "user";
}

@Override
protected String getProfileUrl(final Token accessToken) {
return "https://api.foursquare.com/v2/users/self?v=20131118";
}

@Override
Expand All @@ -67,9 +84,4 @@ protected FoursquareProfile extractUserProfile(String body) {
}
return profile;
}

@Override
protected String getProfileUrl(Token accessToken) {
return "https://api.foursquare.com/v2/users/self?v=20131118";
}
}
Expand Up @@ -16,10 +16,8 @@
package org.pac4j.oauth.client;

import com.github.scribejava.apis.GitHubApi;
import com.github.scribejava.core.model.SignatureType;
import com.github.scribejava.core.builder.api.Api;
import com.github.scribejava.core.model.Token;
import com.github.scribejava.core.oauth.OAuth20Service;
import org.pac4j.core.context.WebContext;
import org.pac4j.oauth.profile.JsonHelper;
import org.pac4j.oauth.profile.github.GitHubProfile;

Expand Down Expand Up @@ -50,11 +48,15 @@ public GitHubClient(final String key, final String secret) {
}

@Override
protected void internalInit(final WebContext context) {
super.internalInit(context);
this.service = new OAuth20Service(GitHubApi.instance(), buildOAuthConfig(context, SignatureType.Header, this.scope));
protected Api getApi() {
return GitHubApi.instance();
}


@Override
protected String getOAuthScope() {
return this.scope;
}

@Override
protected String getProfileUrl(final Token accessToken) {
return "https://api.github.com/user";
Expand Down

0 comments on commit b8cb84f

Please sign in to comment.