Skip to content

Commit

Permalink
setProfileFactory in ProfileCreator
Browse files Browse the repository at this point in the history
  • Loading branch information
leleuj committed Sep 20, 2016
1 parent 5ee7e4a commit 61c5375
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 41 deletions.
2 changes: 2 additions & 0 deletions documentation/docs/authenticators.md
Expand Up @@ -59,3 +59,5 @@ In practice:
- all the clients are configured by default with the [`AuthenticatorProfileCreator`](https://github.com/pac4j/pac4j/blob/master/pac4j-core/src/main/java/org/pac4j/core/profile/creator/AuthenticatorProfileCreator.java) which retrieves the user profile from the current `Credentials` and returns it.

So it works out of the box, even if providing a specific `ProfileCreator` is perfectly feasible.

Notice that you can change the returned profile from the `AuthenticatorProfileCreator` by using the `setProfileFactory` method to build the appropriate profile.
@@ -0,0 +1,30 @@
package org.pac4j.core.profile.creator;

import org.pac4j.core.credentials.Credentials;
import org.pac4j.core.profile.CommonProfile;
import org.pac4j.core.util.CommonHelper;
import org.pac4j.core.util.InitializableWebObject;

import java.util.function.Supplier;

/**
* Abstract profile creator where you can define the profile created.
*
* @author Jerome Leleu
* @since 1.9.3
*/
public abstract class AbstractProfileCreator<C extends Credentials, P extends CommonProfile> extends InitializableWebObject implements ProfileCreator<C, P> {

private Supplier<P> profileFactory;

public Supplier<P> getProfileFactory() {
return profileFactory;
}

public void setProfileFactory(final Supplier<P> profileFactory) {
CommonHelper.assertNotNull("profileFactory", profileFactory);
if (this.profileFactory == null) {
this.profileFactory = profileFactory;
}
}
}
Expand Up @@ -7,17 +7,29 @@

/**
* This profile creator retrieves the user profile attached in the {@link org.pac4j.core.credentials.Credentials}.
* Using the {@link #setProfileFactory(java.util.function.Supplier)} method, a new type of profile can be returned.
*
* @author Jerome Leleu
* @since 1.8.0
*/
public class AuthenticatorProfileCreator<C extends Credentials, P extends CommonProfile>
implements ProfileCreator<C, P> {
public class AuthenticatorProfileCreator<C extends Credentials, P extends CommonProfile> extends AbstractProfileCreator<C, P> {

public final static AuthenticatorProfileCreator INSTANCE = new AuthenticatorProfileCreator<>();

@Override
protected void internalInit(final WebContext context) {}

@Override
public P create(final C credentials, final WebContext context) throws HttpAction {
return (P) credentials.getUserProfile();
final P profile = (P) credentials.getUserProfile();
if (getProfileFactory() == null) {
return profile;
} else {
// rebuild new profile type
final P newProfile = getProfileFactory().get();
newProfile.setId(profile.getId());
newProfile.addAttributes(profile.getAttributes());
return newProfile;
}
}
}
@@ -1,6 +1,7 @@
package org.pac4j.oidc.client;

import org.pac4j.core.context.WebContext;
import org.pac4j.core.util.CommonHelper;
import org.pac4j.oidc.client.azuread.AzureAdResourceRetriever;
import org.pac4j.oidc.config.OidcConfiguration;
import org.pac4j.oidc.profile.azuread.AzureAdProfile;
Expand Down Expand Up @@ -42,13 +43,14 @@ public AzureAdClient(final String clientId, final String secret, final String di

@Override
protected void internalInit(final WebContext context) {
CommonHelper.assertNotNull("configuration", getConfiguration());
getConfiguration().setResourceRetriever(new AzureAdResourceRetriever());
setProfileCreator(new AzureAdProfileCreator(getConfiguration()));

super.internalInit(context);
}

@Override
protected Class<AzureAdProfile> getProfileClass() {
return AzureAdProfile.class;
protected void createProfileCreator() {
setProfileCreator(new AzureAdProfileCreator(getConfiguration()));
}
}
@@ -1,7 +1,9 @@
package org.pac4j.oidc.client;

import org.pac4j.core.context.WebContext;
import org.pac4j.core.util.CommonHelper;
import org.pac4j.oidc.config.OidcConfiguration;
import org.pac4j.oidc.profile.creator.OidcProfileCreator;
import org.pac4j.oidc.profile.google.GoogleOidcProfile;

/**
Expand All @@ -13,20 +15,25 @@
*/
public class GoogleOidcClient extends OidcClient<GoogleOidcProfile> {

public GoogleOidcClient() {}
public GoogleOidcClient() {
}

public GoogleOidcClient(final OidcConfiguration configuration) {
super(configuration);
}

@Override
protected void internalInit(final WebContext context) {
CommonHelper.assertNotNull("configuration", getConfiguration());
getConfiguration().setDiscoveryURI("https://accounts.google.com/.well-known/openid-configuration");

super.internalInit(context);
}

@Override
protected Class<GoogleOidcProfile> getProfileClass() {
return GoogleOidcProfile.class;
protected void createProfileCreator() {
final OidcProfileCreator<GoogleOidcProfile> profileCreator = new OidcProfileCreator<>(getConfiguration());
profileCreator.setProfileFactory(GoogleOidcProfile::new);
setProfileCreator(profileCreator);
}
}
14 changes: 6 additions & 8 deletions pac4j-oidc/src/main/java/org/pac4j/oidc/client/OidcClient.java
Expand Up @@ -32,7 +32,6 @@ public class OidcClient<U extends OidcProfile> extends IndirectClientV2<OidcCred
public OidcClient() { }

public OidcClient(final OidcConfiguration oidcConfiguration) {
setConfiguration(oidcConfiguration);
this.configuration = oidcConfiguration;
}

Expand All @@ -41,7 +40,6 @@ public OidcConfiguration getConfiguration() {
}

public void setConfiguration(final OidcConfiguration oidcConfiguration) {
CommonHelper.assertNotNull("oidcConfiguration", oidcConfiguration);
this.configuration = oidcConfiguration;
}

Expand Down Expand Up @@ -82,22 +80,22 @@ public void setCustomParams(final Map<String, String> customParams) {
configuration.setCustomParams(customParams);
}

@SuppressWarnings("uncheked")
protected Class<U> getProfileClass() {
return (Class<U>) OidcProfile.class;
}

@Override
protected void internalInit(final WebContext context) {
super.internalInit(context);

CommonHelper.assertNotNull("configuration", configuration);
configuration.setCallbackUrl(computeFinalCallbackUrl(context));
configuration.init(context);

setRedirectActionBuilder(new OidcRedirectActionBuilder(configuration));
setCredentialsExtractor(new OidcExtractor(configuration, getName()));
setAuthenticator(new OidcAuthenticator(configuration));
setProfileCreator(new OidcProfileCreator<U>(configuration, getProfileClass()));
createProfileCreator();
}

protected void createProfileCreator() {
setProfileCreator(new OidcProfileCreator<>(configuration));
}

@Deprecated
Expand Down
Expand Up @@ -85,7 +85,6 @@ public class OidcConfiguration extends InitializableWebObject {

@Override
protected void internalInit(final WebContext context) {

// checks
CommonHelper.assertNotBlank("clientId", clientId);
CommonHelper.assertNotBlank("secret", secret);
Expand Down
Expand Up @@ -16,7 +16,8 @@
public class AzureAdProfileCreator extends OidcProfileCreator<AzureAdProfile> {

public AzureAdProfileCreator(final OidcConfiguration configuration) {
super(configuration, AzureAdProfile.class);
super(configuration);
setProfileFactory(() -> new AzureAdProfile());
}

@Override
Expand Down
Expand Up @@ -19,10 +19,8 @@
import org.pac4j.core.context.WebContext;
import org.pac4j.core.exception.HttpAction;
import org.pac4j.core.exception.TechnicalException;
import org.pac4j.core.profile.ProfileHelper;
import org.pac4j.core.profile.creator.ProfileCreator;
import org.pac4j.core.profile.creator.AbstractProfileCreator;
import org.pac4j.core.util.CommonHelper;
import org.pac4j.core.util.InitializableWebObject;
import org.pac4j.oidc.config.OidcConfiguration;
import org.pac4j.oidc.credentials.OidcCredentials;
import org.pac4j.oidc.profile.OidcProfile;
Expand All @@ -42,28 +40,23 @@
* @author Jerome Leleu
* @since 1.9.2
*/
public class OidcProfileCreator<U extends OidcProfile> extends InitializableWebObject implements ProfileCreator<OidcCredentials, U> {
public class OidcProfileCreator<U extends OidcProfile> extends AbstractProfileCreator<OidcCredentials, U> {

private static final Logger logger = LoggerFactory.getLogger(OidcProfileCreator.class);

private OidcConfiguration configuration;

private Class<U> clazz;

protected IDTokenValidator idTokenValidator;

public OidcProfileCreator() {}

public OidcProfileCreator(final OidcConfiguration configuration, final Class<U> clazz) {
public OidcProfileCreator(final OidcConfiguration configuration) {
this.configuration = configuration;
this.clazz = clazz;
}

@Override
protected void internalInit(final WebContext context) {
assertNotNull("configuration", configuration);
assertNotNull("clazz", clazz);

configuration.init(context);

// check algorithms
Expand Down Expand Up @@ -92,6 +85,8 @@ protected void internalInit(final WebContext context) {
this.idTokenValidator = createRSATokenValidator(jwsAlgorithm, _clientID);
}
this.idTokenValidator.setMaxClockSkew(configuration.getMaxClockSkew());

setProfileFactory(() -> (U) new OidcProfile());
}

protected IDTokenValidator createRSATokenValidator(final JWSAlgorithm jwsAlgorithm, final ClientID clientID) {
Expand All @@ -115,7 +110,7 @@ public U create(final OidcCredentials credentials, final WebContext context) thr
final AccessToken accessToken = credentials.getAccessToken();

// Create profile
final U profile = (U) ProfileHelper.buildUserProfileByClassCompleteName(clazz.getName());
final U profile = getProfileFactory().get();
profile.setAccessToken(accessToken);
final JWT idToken = credentials.getIdToken();
profile.setIdTokenString(idToken.getParsedString());
Expand Down Expand Up @@ -187,20 +182,12 @@ public void setConfiguration(final OidcConfiguration configuration) {
this.configuration = configuration;
}

public Class<U> getClazz() {
return clazz;
}

public void setClazz(final Class<U> clazz) {
this.clazz = clazz;
}

public IDTokenValidator getIdTokenValidator() {
return idTokenValidator;
}

@Override
public String toString() {
return CommonHelper.toString(this.getClass(), "configuration", configuration, "clazz", clazz);
return CommonHelper.toString(this.getClass(), "configuration", configuration);
}
}
Expand Up @@ -81,7 +81,7 @@ protected void verifyProfile(final CommonProfile userProfile) {
assertNotNull(profile.getIpaddr());
assertNotNull(profile.getIssuedAt());
assertNotNull(profile.getAttribute("sub"));
assertEquals("0", profile.getAttribute("pwd_exp"));
assertEquals(22, profile.getAttributes().size());
assertNull(profile.getAttribute("pwd_exp"));
assertEquals(20, profile.getAttributes().size());
}
}

0 comments on commit 61c5375

Please sign in to comment.