ClientSession everywhere refactor phase1

This commit is contained in:
Bill Burke committed 2014-09-26 17:44:33 -04:00
1 parent 0bf6c36ca7
commit 524b423733
55 files changed
+1330 -667

No files matched your search

+6
View File
@@ -20,6 +20,12 @@
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-model-api</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
@@ -2,6 +2,7 @@ package org.keycloak.social;
import org.codehaus.jackson.map.ObjectMapper;
import org.keycloak.OAuth2Constants;
import org.keycloak.models.ClientSessionModel;
import org.keycloak.social.utils.SimpleHttp;
import java.io.IOException;
@@ -43,7 +44,7 @@ public abstract class AbstractOAuth2Provider implements SocialProvider {
protected abstract SocialUser getProfile(String accessToken) throws SocialProviderException;
@Override
public AuthRequest getAuthUrl(SocialProviderConfig config, String state) throws SocialProviderException {
public AuthRequest getAuthUrl(ClientSessionModel clientSession, SocialProviderConfig config, String state) throws SocialProviderException {
return AuthRequest.create(getAuthUrl())
.setQueryParam(CLIENT_ID, config.getKey())
.setQueryParam(RESPONSE_TYPE, CODE)
@@ -54,7 +55,7 @@ public abstract class AbstractOAuth2Provider implements SocialProvider {
}
@Override
public SocialUser processCallback(SocialProviderConfig config, AuthCallback callback) throws SocialProviderException {
public SocialUser processCallback(ClientSessionModel clientSession, SocialProviderConfig config, AuthCallback callback) throws SocialProviderException {
String error = callback.getQueryParam(OAuth2Constants.ERROR);
if (error != null) {
if (error.equals("access_denied")) {
+1 -6
View File
@@ -29,11 +29,9 @@ import java.util.Map;
public class AuthCallback {
private Map<String, String[]> queryParams;
private Map<String, String> attributes;
public AuthCallback(Map<String, String[]> queryParams, Map<String, String> attributes) {
public AuthCallback(Map<String, String[]> queryParams) {
this.queryParams = queryParams;
this.attributes = attributes;
}
public String getQueryParam(String name) {
@@ -44,8 +42,5 @@ public class AuthCallback {
return null;
}
public String getAttribute(String name) {
return attributes != null ? attributes.get(name) : null;
}
}
+2 -19
View File
@@ -35,8 +35,6 @@ public class AuthRequest {
private URI authUri;
private Map<String, String> attributes;
public static AuthRequestBuilder create(String url) {
AuthRequestBuilder req = new AuthRequestBuilder();
@@ -46,27 +44,20 @@ public class AuthRequest {
return req;
}
private AuthRequest(URI authUri, Map<String, String> attributes) {
private AuthRequest(URI authUri) {
this.authUri = authUri;
this.attributes = attributes;
}
public URI getAuthUri() {
return authUri;
}
public Map<String, String> getAttributes() {
return attributes;
}
public static class AuthRequestBuilder {
private StringBuilder b;
private char sep;
private Map<String, String> attributes;
private String id;
private AuthRequestBuilder() {
@@ -90,17 +81,9 @@ public class AuthRequest {
}
}
public AuthRequestBuilder setAttribute(String name, String value) {
if (attributes == null) {
attributes = new HashMap<String, String>();
}
attributes.put(name, value);
return this;
}
public AuthRequest build() {
try {
return new AuthRequest(new URI(b.toString()), attributes);
return new AuthRequest(new URI(b.toString()));
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
+4 -2
View File
@@ -22,6 +22,8 @@
package org.keycloak.social;
import org.keycloak.models.ClientSessionModel;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
@@ -29,10 +31,10 @@ public interface SocialProvider {
String getId();
AuthRequest getAuthUrl(SocialProviderConfig config, String state) throws SocialProviderException;
AuthRequest getAuthUrl(ClientSessionModel clientSession, SocialProviderConfig config, String state) throws SocialProviderException;
String getName();
SocialUser processCallback(SocialProviderConfig config, AuthCallback callback) throws SocialProviderException;
SocialUser processCallback(ClientSessionModel clientSession, SocialProviderConfig config, AuthCallback callback) throws SocialProviderException;
}
+6
View File
@@ -14,6 +14,12 @@
<description/>
<dependencies>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-model-api</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-social-core</artifactId>
+6
View File
@@ -14,6 +14,12 @@
<description/>
<dependencies>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-model-api</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-social-core</artifactId>
+6
View File
@@ -14,6 +14,12 @@
<description/>
<dependencies>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-model-api</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-social-core</artifactId>
+6
View File
@@ -14,6 +14,12 @@
<description/>
<dependencies>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-model-api</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-social-core</artifactId>
@@ -21,6 +21,7 @@
*/
package org.keycloak.social.twitter;
import org.keycloak.models.ClientSessionModel;
import org.keycloak.social.AuthCallback;
import org.keycloak.social.AuthRequest;
import org.keycloak.social.SocialAccessDeniedException;
@@ -45,7 +46,7 @@ public class TwitterProvider implements SocialProvider {
}
@Override
public AuthRequest getAuthUrl(SocialProviderConfig config, String state) throws SocialProviderException {
public AuthRequest getAuthUrl(ClientSessionModel clientSession, SocialProviderConfig config, String state) throws SocialProviderException {
try {
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(config.getKey(), config.getSecret());
@@ -53,8 +54,9 @@ public class TwitterProvider implements SocialProvider {
URI uri = new URI(config.getCallbackUrl() + "?state=" + state);
RequestToken requestToken = twitter.getOAuthRequestToken(uri.toString());
clientSession.setNote("twitter_token", requestToken.getToken());
clientSession.setNote("twitter_tokenSecret", requestToken.getTokenSecret());
return AuthRequest.create(requestToken.getAuthenticationURL())
.setAttribute("token", requestToken.getToken()).setAttribute("tokenSecret", requestToken.getTokenSecret())
.build();
} catch (Exception e) {
throw new SocialProviderException(e);
@@ -67,7 +69,7 @@ public class TwitterProvider implements SocialProvider {
}
@Override
public SocialUser processCallback(SocialProviderConfig config, AuthCallback callback) throws SocialProviderException {
public SocialUser processCallback(ClientSessionModel clientSession, SocialProviderConfig config, AuthCallback callback) throws SocialProviderException {
if (callback.getQueryParam("denied") != null) {
throw new SocialAccessDeniedException();
}
@@ -78,8 +80,10 @@ public class TwitterProvider implements SocialProvider {
String token = callback.getQueryParam("oauth_token");
String verifier = callback.getQueryParam("oauth_verifier");
String twitterToken = clientSession.getNote("twitter_token");
String twitterSecret = clientSession.getNote("twitter_tokenSecret");
RequestToken requestToken = new RequestToken((String)callback.getAttribute("token"), (String)callback.getAttribute("tokenSecret"));
RequestToken requestToken = new RequestToken(twitterToken, twitterSecret);
twitter.getOAuthAccessToken(requestToken, verifier);
twitter4j.User twitterUser = twitter.verifyCredentials();