Skip to content

Commit

Permalink
setRedirectActionBuilder() is a one-time op? (pac4j#823)
Browse files Browse the repository at this point in the history
  • Loading branch information
leleuj authored Feb 14, 2017
1 parent 15278e4 commit 81b5a86
Show file tree
Hide file tree
Showing 55 changed files with 160 additions and 127 deletions.
8 changes: 4 additions & 4 deletions pac4j-cas/src/main/java/org/pac4j/cas/client/CasClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ protected void clientInit(final WebContext context) {
configuration.setCallbackUrlResolver(this.getCallbackUrlResolver());
configuration.init(context);

setRedirectActionBuilder(new CasRedirectActionBuilder(configuration, callbackUrl));
setCredentialsExtractor(new TicketAndLogoutRequestExtractor(configuration, getName()));
setAuthenticator(new CasAuthenticator(configuration, callbackUrl));
setLogoutActionBuilder(new CasLogoutActionBuilder<>(configuration.getPrefixUrl() + "logout", configuration.getPostLogoutUrlParameter()));
defaultRedirectActionBuilder(new CasRedirectActionBuilder(configuration, callbackUrl));
defaultCredentialsExtractor(new TicketAndLogoutRequestExtractor(configuration, getName()));
defaultAuthenticator(new CasAuthenticator(configuration, callbackUrl));
defaultLogoutActionBuilder(new CasLogoutActionBuilder<>(configuration.getPrefixUrl() + "logout", configuration.getPostLogoutUrlParameter()));
addAuthorizationGenerator(new DefaultCasAuthorizationGenerator<>());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public final class CasProxyReceptor extends IndirectClient<TokenCredentials, Com
protected void clientInit(final WebContext context) {
CommonHelper.assertNotNull("store", this.store);

setRedirectActionBuilder(ctx -> { throw new TechnicalException("Not supported by the CAS proxy receptor"); });
setCredentialsExtractor(ctx -> {
defaultRedirectActionBuilder(ctx -> { throw new TechnicalException("Not supported by the CAS proxy receptor"); });
defaultCredentialsExtractor(ctx -> {
// like CommonUtils.readAndRespondToProxyReceptorRequest in CAS client
final String proxyGrantingTicketIou = ctx.getRequestParameter(PARAM_PROXY_GRANTING_TICKET_IOU);
logger.debug("proxyGrantingTicketIou: {}", proxyGrantingTicketIou);
Expand All @@ -61,7 +61,7 @@ protected void clientInit(final WebContext context) {
logger.debug(message);
throw HttpAction.ok(message, ctx, "");
});
setAuthenticator((credentials, ctx) -> { throw new TechnicalException("Not supported by the CAS proxy receptor"); });
defaultAuthenticator((credentials, ctx) -> { throw new TechnicalException("Not supported by the CAS proxy receptor"); });
}

public Store<String, String> getStore() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ protected void clientInit(final WebContext context) {
CommonHelper.assertTrue(!configuration.isGateway(), "the DirectCasClient can not support gateway to avoid infinite loops");
configuration.init(context);

setCredentialsExtractor(new ParameterExtractor(CasConfiguration.TICKET_PARAMETER, true, false, getName()));
defaultCredentialsExtractor(new ParameterExtractor(CasConfiguration.TICKET_PARAMETER, true, false, getName()));
// only a fake one for the initialization as we will build a new one with the current url for each request
super.setAuthenticator(new CasAuthenticator(configuration, "fake"));
super.defaultAuthenticator(new CasAuthenticator(configuration, "fake"));
addAuthorizationGenerator(new DefaultCasAuthorizationGenerator<>());
}

Expand All @@ -96,7 +96,7 @@ public void setConfiguration(final CasConfiguration configuration) {
}

@Override
public void setAuthenticator(final Authenticator authenticator) {
protected void defaultAuthenticator(final Authenticator authenticator) {
throw new TechnicalException("You can not set an Authenticator for the DirectCasClient at startup. A new CasAuthenticator is automatically created for each request");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ protected void clientInit(final WebContext context) {
CommonHelper.assertTrue(protocol == CasProtocol.CAS20_PROXY || protocol == CasProtocol.CAS30_PROXY, "The DirectCasProxyClient must be configured with a CAS proxy protocol (CAS20_PROXY or CAS30_PROXY)");
configuration.init(context);

setCredentialsExtractor(new ParameterExtractor(CasConfiguration.TICKET_PARAMETER, true, false, getName()));
setAuthenticator(new CasAuthenticator(configuration, this.serviceUrl));
defaultCredentialsExtractor(new ParameterExtractor(CasConfiguration.TICKET_PARAMETER, true, false, getName()));
defaultAuthenticator(new CasAuthenticator(configuration, this.serviceUrl));
addAuthorizationGenerator(new DefaultCasAuthorizationGenerator<>());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ protected void clientInit(final WebContext context) {
CommonHelper.assertNotNull("configuration", this.configuration);
configuration.init(context);

setCredentialsExtractor(new BasicAuthExtractor(this.headerName, this.prefixHeader, getName()));
setAuthenticator(new CasRestAuthenticator(this.configuration));
defaultCredentialsExtractor(new BasicAuthExtractor(this.headerName, this.prefixHeader, getName()));
defaultAuthenticator(new CasRestAuthenticator(this.configuration));
}

public String getHeaderName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ protected void clientInit(final WebContext context) {
CommonHelper.assertNotNull("configuration", this.configuration);
configuration.init(context);

setCredentialsExtractor(new FormExtractor(this.usernameParameter, this.passwordParameter, getName()));
setAuthenticator(new CasRestAuthenticator(this.configuration));
defaultCredentialsExtractor(new FormExtractor(this.usernameParameter, this.passwordParameter, getName()));
defaultAuthenticator(new CasRestAuthenticator(this.configuration));
}

public String getUsernameParameter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected void internalInit(final WebContext context) {
CommonHelper.assertNotNull("configuration", configuration);
configuration.init(context);

setProfileDefinition(new CasProfileDefinition());
defaultProfileDefinition(new CasProfileDefinition());
}

@Override
Expand Down
18 changes: 15 additions & 3 deletions pac4j-core/src/main/java/org/pac4j/core/client/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public CredentialsExtractor<C> getCredentialsExtractor() {
return credentialsExtractor;
}

public void setCredentialsExtractor(final CredentialsExtractor<C> credentialsExtractor) {
protected void defaultCredentialsExtractor(final CredentialsExtractor<C> credentialsExtractor) {
if (this.credentialsExtractor == null) {
this.credentialsExtractor = credentialsExtractor;
}
Expand All @@ -173,7 +173,7 @@ public Authenticator<C> getAuthenticator() {
return authenticator;
}

public void setAuthenticator(final Authenticator<C> authenticator) {
protected void defaultAuthenticator(final Authenticator<C> authenticator) {
if (this.authenticator == null) {
this.authenticator = authenticator;
}
Expand All @@ -183,12 +183,24 @@ public ProfileCreator<C, U> getProfileCreator() {
return profileCreator;
}

public void setProfileCreator(final ProfileCreator<C, U> profileCreator) {
protected void defaultProfileCreator(final ProfileCreator<C, U> profileCreator) {
if (this.profileCreator == null || this.profileCreator == AuthenticatorProfileCreator.INSTANCE) {
this.profileCreator = profileCreator;
}
}

public void setCredentialsExtractor(final CredentialsExtractor<C> credentialsExtractor) {
this.credentialsExtractor = credentialsExtractor;
}

public void setAuthenticator(final Authenticator<C> authenticator) {
this.authenticator = authenticator;
}

public void setProfileCreator(final ProfileCreator<C, U> profileCreator) {
this.profileCreator = profileCreator;
}

@Override
public String toString() {
return CommonHelper.toString(this.getClass(), "name", getName(), "credentialsExtractor", this.credentialsExtractor,
Expand Down
12 changes: 10 additions & 2 deletions pac4j-core/src/main/java/org/pac4j/core/client/IndirectClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public RedirectActionBuilder getRedirectActionBuilder() {
return redirectActionBuilder;
}

public void setRedirectActionBuilder(final RedirectActionBuilder redirectActionBuilder) {
protected void defaultRedirectActionBuilder(final RedirectActionBuilder redirectActionBuilder) {
if (this.redirectActionBuilder == null) {
this.redirectActionBuilder = redirectActionBuilder;
}
Expand All @@ -184,12 +184,20 @@ public LogoutActionBuilder<U> getLogoutActionBuilder() {
return logoutActionBuilder;
}

public void setLogoutActionBuilder(final LogoutActionBuilder<U> logoutActionBuilder) {
protected void defaultLogoutActionBuilder(final LogoutActionBuilder<U> logoutActionBuilder) {
if (this.logoutActionBuilder == null || this.logoutActionBuilder == NoLogoutActionBuilder.INSTANCE) {
this.logoutActionBuilder = logoutActionBuilder;
}
}

public void setRedirectActionBuilder(final RedirectActionBuilder redirectActionBuilder) {
this.redirectActionBuilder = redirectActionBuilder;
}

public void setLogoutActionBuilder(final LogoutActionBuilder<U> logoutActionBuilder) {
this.logoutActionBuilder = logoutActionBuilder;
}

@Override
public String toString() {
return CommonHelper.toString(this.getClass(), "name", getName(), "callbackUrl", this.callbackUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public AnonymousClient() {

@Override
protected void clientInit(final WebContext context) {
setCredentialsExtractor(ctx -> AnonymousCredentials.INSTANCE);
setAuthenticator((cred, ctx )-> {
defaultCredentialsExtractor(ctx -> AnonymousCredentials.INSTANCE);
defaultAuthenticator((cred, ctx )-> {
cred.setUserProfile(AnonymousProfile.INSTANCE);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public ProfileDefinition<P> getProfileDefinition() {
}

public void setProfileDefinition(final ProfileDefinition<P> profileDefinition) {
CommonHelper.assertNotNull("profileDefinition", profileDefinition);
this.profileDefinition = profileDefinition;
}

protected void defaultProfileDefinition(final ProfileDefinition<P> profileDefinition) {
CommonHelper.assertNotNull("profileDefinition", profileDefinition);
if (this.profileDefinition == null) {
this.profileDefinition = profileDefinition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public MockDirectClient(final String name, final ReturnCredentials returnCredent

@Override
protected void clientInit(final WebContext context) {
setCredentialsExtractor(ctx -> returnCredentials.get());
setAuthenticator((cred, ctx) -> cred.setUserProfile(profile));
defaultCredentialsExtractor(ctx -> returnCredentials.get());
defaultAuthenticator((cred, ctx) -> cred.setUserProfile(profile));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public MockIndirectClient(final String name, final RedirectAction redirectAction

@Override
protected void clientInit(final WebContext context) {
setRedirectActionBuilder(ctx -> redirectAction);
setCredentialsExtractor(ctx -> returnCredentials.get());
setAuthenticator((cred, ctx) -> cred.setUserProfile(profile));
setLogoutActionBuilder(getLogoutActionBuilder());
defaultRedirectActionBuilder(ctx -> redirectAction);
defaultCredentialsExtractor(ctx -> returnCredentials.get());
defaultAuthenticator((cred, ctx) -> cred.setUserProfile(profile));
defaultLogoutActionBuilder(getLogoutActionBuilder());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ public class GaeUserServiceClient extends IndirectClient<GaeUserCredentials, Gae
protected void clientInit(final WebContext context) {
service = UserServiceFactory.getUserService();
CommonHelper.assertNotNull("service", this.service);
setRedirectActionBuilder(ctx -> {
defaultRedirectActionBuilder(ctx -> {
final String destinationUrl = computeFinalCallbackUrl(ctx);
final String loginUrl = authDomain == null ? service.createLoginURL(destinationUrl) : service.createLoginURL(destinationUrl, authDomain);
return RedirectAction.redirect(loginUrl);
});
setCredentialsExtractor(ctx -> {
defaultCredentialsExtractor(ctx -> {
final GaeUserCredentials credentials = new GaeUserCredentials();
credentials.setUser(service.getCurrentUser());
return credentials;
});
setAuthenticator((credentials, ctx) -> {
defaultAuthenticator((credentials, ctx) -> {
final User user = credentials.getUser();
if (user != null) {
final GaeUserServiceProfile profile = PROFILE_DEFINITION.newProfile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public CookieClient() {}

public CookieClient(final String cookieName, final Authenticator cookieAuthenticator) {
this.cookieName = cookieName;
setAuthenticator(cookieAuthenticator);
defaultAuthenticator(cookieAuthenticator);
}

@Override
protected void clientInit(final WebContext context) {
CommonHelper.assertNotBlank("cookieName", this.cookieName);

setCredentialsExtractor(new CookieExtractor(this.cookieName, getName()));
defaultCredentialsExtractor(new CookieExtractor(this.cookieName, getName()));
}

public String getCookieName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ public class DirectBasicAuthClient extends DirectClient<UsernamePasswordCredenti
public DirectBasicAuthClient() {}

public DirectBasicAuthClient(final Authenticator usernamePasswordAuthenticator) {
setAuthenticator(usernamePasswordAuthenticator);
defaultAuthenticator(usernamePasswordAuthenticator);
}

public DirectBasicAuthClient(final Authenticator usernamePasswordAuthenticator,
final ProfileCreator profileCreator) {
setAuthenticator(usernamePasswordAuthenticator);
setProfileCreator(profileCreator);
defaultAuthenticator(usernamePasswordAuthenticator);
defaultProfileCreator(profileCreator);
}

@Override
protected void clientInit(final WebContext context) {
setCredentialsExtractor(new BasicAuthExtractor(getName()));
defaultCredentialsExtractor(new BasicAuthExtractor(getName()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ public DirectDigestAuthClient() {
}

public DirectDigestAuthClient(final Authenticator digestAuthenticator) {
setAuthenticator(digestAuthenticator);
defaultAuthenticator(digestAuthenticator);
}

public DirectDigestAuthClient(final Authenticator digestAuthenticator,
final ProfileCreator profileCreator) {
setAuthenticator(digestAuthenticator);
setProfileCreator(profileCreator);
defaultAuthenticator(digestAuthenticator);
defaultProfileCreator(profileCreator);
}

@Override
protected void clientInit(final WebContext context) {
setCredentialsExtractor(new DigestAuthExtractor(getName()));
defaultCredentialsExtractor(new DigestAuthExtractor(getName()));
}

/** Per RFC 2617
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@ public class DirectFormClient extends DirectClient<UsernamePasswordCredentials,
public DirectFormClient() {}

public DirectFormClient(final Authenticator usernamePasswordAuthenticator) {
setAuthenticator(usernamePasswordAuthenticator);
defaultAuthenticator(usernamePasswordAuthenticator);
}

public DirectFormClient(final String usernameParameter, final String passwordParameter,
final Authenticator usernamePasswordAuthenticator) {
this.usernameParameter = usernameParameter;
this.passwordParameter = passwordParameter;
setAuthenticator(usernamePasswordAuthenticator);
defaultAuthenticator(usernamePasswordAuthenticator);
}

public DirectFormClient(final Authenticator usernamePasswordAuthenticator,
final ProfileCreator profileCreator) {
setAuthenticator(usernamePasswordAuthenticator);
setProfileCreator(profileCreator);
defaultAuthenticator(usernamePasswordAuthenticator);
defaultProfileCreator(profileCreator);
}

@Override
protected void clientInit(final WebContext context) {
CommonHelper.assertNotBlank("usernameParameter", usernameParameter);
CommonHelper.assertNotBlank("passwordParameter", passwordParameter);

setCredentialsExtractor(new FormExtractor(usernameParameter, passwordParameter, getName()));
defaultCredentialsExtractor(new FormExtractor(usernameParameter, passwordParameter, getName()));
}

public String getUsernameParameter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,37 @@ public HeaderClient() {}

public HeaderClient(final String headerName, final Authenticator tokenAuthenticator) {
this.headerName = headerName;
setAuthenticator(tokenAuthenticator);
defaultAuthenticator(tokenAuthenticator);
}

public HeaderClient(final String headerName, final String prefixHeader,
final Authenticator tokenAuthenticator) {
this.headerName = headerName;
this.prefixHeader = prefixHeader;
setAuthenticator(tokenAuthenticator);
defaultAuthenticator(tokenAuthenticator);
}

public HeaderClient(final String headerName, final Authenticator tokenAuthenticator,
final ProfileCreator profileCreator) {
this.headerName = headerName;
setAuthenticator(tokenAuthenticator);
setProfileCreator(profileCreator);
defaultAuthenticator(tokenAuthenticator);
defaultProfileCreator(profileCreator);
}

public HeaderClient(final String headerName, final String prefixHeader,
final Authenticator tokenAuthenticator, final ProfileCreator profileCreator) {
this.headerName = headerName;
this.prefixHeader = prefixHeader;
setAuthenticator(tokenAuthenticator);
setProfileCreator(profileCreator);
defaultAuthenticator(tokenAuthenticator);
defaultProfileCreator(profileCreator);
}

@Override
protected void clientInit(final WebContext context) {
CommonHelper.assertNotBlank("headerName", this.headerName);
CommonHelper.assertNotNull("prefixHeader", this.prefixHeader);

setCredentialsExtractor(new HeaderExtractor(this.headerName, this.prefixHeader, getName()));
defaultCredentialsExtractor(new HeaderExtractor(this.headerName, this.prefixHeader, getName()));
}

public String getHeaderName() {
Expand Down
Loading

0 comments on commit 81b5a86

Please sign in to comment.