Skip to content

Commit

Permalink
Make the Context available to the auth modules.
Browse files Browse the repository at this point in the history
Patch by fjodorver

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1689058 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
markt-asf committed Jul 3, 2015
1 parent 3fb779c commit 45eb436
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 36 deletions.
Expand Up @@ -26,6 +26,7 @@
import javax.security.auth.message.config.ServerAuthConfig; import javax.security.auth.message.config.ServerAuthConfig;
import javax.security.auth.message.config.ServerAuthContext; import javax.security.auth.message.config.ServerAuthContext;


import org.apache.catalina.Context;
import org.apache.catalina.Realm; import org.apache.catalina.Realm;
import org.apache.catalina.authenticator.jaspic.provider.modules.BasicAuthModule; import org.apache.catalina.authenticator.jaspic.provider.modules.BasicAuthModule;
import org.apache.catalina.authenticator.jaspic.provider.modules.DigestAuthModule; import org.apache.catalina.authenticator.jaspic.provider.modules.DigestAuthModule;
Expand All @@ -41,17 +42,20 @@ public class TomcatAuthConfig implements ServerAuthConfig {
private String appContext; private String appContext;
private CallbackHandler handler; private CallbackHandler handler;
private TomcatServerAuthContext tomcatServerAuthContext; private TomcatServerAuthContext tomcatServerAuthContext;
private Realm realm;
private Context context;
private LoginConfig loginConfig; private LoginConfig loginConfig;
private Realm realm;




public TomcatAuthConfig(String layer, String appContext, CallbackHandler callbackHandler, public TomcatAuthConfig(String layer, String appContext, CallbackHandler callbackHandler,
Realm realm, LoginConfig loginConfig) { Context context) {
this.messageLayer = layer; this.messageLayer = layer;
this.appContext = appContext; this.appContext = appContext;
this.handler = callbackHandler; this.handler = callbackHandler;
this.realm = realm; this.context = context;
this.loginConfig = loginConfig; this.realm = context.getRealm();
this.loginConfig = context.getLoginConfig();
} }




Expand Down Expand Up @@ -108,13 +112,13 @@ private TomcatAuthModule getModule() throws AuthException {
String authMethod = getAuthMethod(); String authMethod = getAuthMethod();
switch (authMethod) { switch (authMethod) {
case "BASIC": { case "BASIC": {
return new BasicAuthModule(); return new BasicAuthModule(context);
} }
case "DIGEST": { case "DIGEST": {
return new DigestAuthModule(realm); return new DigestAuthModule(context);
} }
case "FORM": { case "FORM": {
return new FormAuthModule(); return new FormAuthModule(context);
} }
default: { default: {
throw new AuthException( throw new AuthException(
Expand Down
Expand Up @@ -26,24 +26,20 @@
import javax.security.auth.message.config.ServerAuthConfig; import javax.security.auth.message.config.ServerAuthConfig;


import org.apache.catalina.Context; import org.apache.catalina.Context;
import org.apache.catalina.Realm;
import org.apache.tomcat.util.descriptor.web.LoginConfig;


/** /**
* Tomcat's context based JASPIC authentication provider. It returns authentication * Tomcat's context based JASPIC authentication provider. It returns
* modules depending on context login-config setup. * authentication modules depending on context login-config setup.
*/ */
public class TomcatAuthConfigProvider implements AuthConfigProvider { public class TomcatAuthConfigProvider implements AuthConfigProvider {


private Map<String, String> providerProperties; private Map<String, String> providerProperties;
private ServerAuthConfig serverAuthConfig; private ServerAuthConfig serverAuthConfig;
private Realm realm; private Context context;
private LoginConfig loginConfig;




public TomcatAuthConfigProvider(Context context) { public TomcatAuthConfigProvider(Context context) {
this.realm = context.getRealm(); this.context = context;
this.loginConfig = context.getLoginConfig();
} }




Expand All @@ -66,7 +62,7 @@ public ClientAuthConfig getClientAuthConfig(String layer, String appContext,
public synchronized ServerAuthConfig getServerAuthConfig(String layer, String appContext, public synchronized ServerAuthConfig getServerAuthConfig(String layer, String appContext,
CallbackHandler handler) throws AuthException { CallbackHandler handler) throws AuthException {
if (this.serverAuthConfig == null) { if (this.serverAuthConfig == null) {
this.serverAuthConfig = new TomcatAuthConfig(layer, appContext, handler, realm, loginConfig); this.serverAuthConfig = new TomcatAuthConfig(layer, appContext, handler, context);
} }
return this.serverAuthConfig; return this.serverAuthConfig;
} }
Expand Down
Expand Up @@ -35,6 +35,7 @@
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;


import org.apache.catalina.Context;
import org.apache.catalina.realm.GenericPrincipal; import org.apache.catalina.realm.GenericPrincipal;
import org.apache.tomcat.util.buf.ByteChunk; import org.apache.tomcat.util.buf.ByteChunk;
import org.apache.tomcat.util.buf.MessageBytes; import org.apache.tomcat.util.buf.MessageBytes;
Expand All @@ -48,6 +49,12 @@ public class BasicAuthModule extends TomcatAuthModule {
private Class<?>[] supportedMessageTypes = new Class[] { HttpServletRequest.class, private Class<?>[] supportedMessageTypes = new Class[] { HttpServletRequest.class,
HttpServletResponse.class }; HttpServletResponse.class };



public BasicAuthModule(Context context) {
super(context);
}


@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
@Override @Override
public void initializeModule(MessagePolicy requestPolicy, MessagePolicy responsePolicy, public void initializeModule(MessagePolicy requestPolicy, MessagePolicy responsePolicy,
Expand Down Expand Up @@ -114,8 +121,8 @@ private AuthStatus sendUnauthorizedError(HttpServletResponse response, String re




private GenericPrincipal getPrincipal(PasswordValidationCallback passwordCallback) { private GenericPrincipal getPrincipal(PasswordValidationCallback passwordCallback) {
Iterator<Object> credentials = Iterator<Object> credentials = passwordCallback.getSubject().getPrivateCredentials()
passwordCallback.getSubject().getPrivateCredentials().iterator(); .iterator();
return (GenericPrincipal) credentials.next(); return (GenericPrincipal) credentials.next();
} }


Expand Down Expand Up @@ -147,7 +154,6 @@ public Class<?>[] getSupportedMessageTypes() {
return supportedMessageTypes; return supportedMessageTypes;
} }



/** /**
* Parser for an HTTP Authorization header for BASIC authentication as per * Parser for an HTTP Authorization header for BASIC authentication as per
* RFC 2617 section 2, and the Base64 encoded credentials as per RFC 2045 * RFC 2617 section 2, and the Base64 encoded credentials as per RFC 2045
Expand All @@ -167,14 +173,15 @@ protected static class BasicCredentials {
private String username = null; private String username = null;
private String password = null; private String password = null;



/** /**
* Parse the HTTP Authorization header for BASIC authentication as per * Parse the HTTP Authorization header for BASIC authentication as per
* RFC 2617 section 2, and the Base64 encoded credentials as per RFC * RFC 2617 section 2, and the Base64 encoded credentials as per RFC
* 2045 section 6.8. * 2045 section 6.8.
* *
* @param input The header value to parse in-place * @param input The header value to parse in-place
* @throws IllegalArgumentException If the header does not conform to RFC * @throws IllegalArgumentException If the header does not conform to
* 2617 * RFC 2617
*/ */
public BasicCredentials(ByteChunk input) throws IllegalArgumentException { public BasicCredentials(ByteChunk input) throws IllegalArgumentException {
authorization = input; authorization = input;
Expand All @@ -184,6 +191,7 @@ public BasicCredentials(ByteChunk input) throws IllegalArgumentException {
parseCredentials(decoded); parseCredentials(decoded);
} }



/** /**
* Trivial accessor. * Trivial accessor.
* *
Expand All @@ -194,6 +202,7 @@ public String getUsername() {
return username; return username;
} }



/** /**
* Trivial accessor. * Trivial accessor.
* *
Expand All @@ -204,6 +213,7 @@ public String getPassword() {
return password; return password;
} }



/* /*
* The authorization method string is case-insensitive and must have at * The authorization method string is case-insensitive and must have at
* least one space character as a delimiter. * least one space character as a delimiter.
Expand All @@ -219,6 +229,7 @@ private void parseMethod() throws IllegalArgumentException {
} }
} }



/* /*
* Decode the base64-user-pass token, which RFC 2617 states can be * Decode the base64-user-pass token, which RFC 2617 states can be
* longer than the 76 characters per line limit defined in RFC 2045. The * longer than the 76 characters per line limit defined in RFC 2045. The
Expand All @@ -236,6 +247,7 @@ private byte[] parseBase64() throws IllegalArgumentException {
return decoded; return decoded;
} }



/* /*
* Extract the mandatory username token and separate it from the * Extract the mandatory username token and separate it from the
* optional password token. Tolerate surplus surrounding white space. * optional password token. Tolerate surplus surrounding white space.
Expand Down
Expand Up @@ -37,6 +37,7 @@
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;


import org.apache.catalina.Context;
import org.apache.catalina.Realm; import org.apache.catalina.Realm;
import org.apache.catalina.util.StandardSessionIdGenerator; import org.apache.catalina.util.StandardSessionIdGenerator;
import org.apache.juli.logging.Log; import org.apache.juli.logging.Log;
Expand Down Expand Up @@ -109,8 +110,9 @@ public class DigestAuthModule extends TomcatAuthModule {


// ------------------------------------------------------------- Properties // ------------------------------------------------------------- Properties


public DigestAuthModule(Realm realm) { public DigestAuthModule(Context context) {
this.realm = realm; super(context);
this.realm = context.getRealm();
} }




Expand Down
Expand Up @@ -27,6 +27,7 @@
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;


import org.apache.catalina.Context;
import org.apache.juli.logging.Log; import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory; import org.apache.juli.logging.LogFactory;


Expand All @@ -42,12 +43,8 @@ public class FormAuthModule extends TomcatAuthModule {
private String landingPage; private String landingPage;




public FormAuthModule() { public FormAuthModule(Context context) {
} super(context);


public FormAuthModule(String landingPage) {
this.landingPage = landingPage;
} }




Expand Down
Expand Up @@ -25,6 +25,7 @@
import javax.security.auth.message.MessagePolicy; import javax.security.auth.message.MessagePolicy;
import javax.security.auth.message.module.ServerAuthModule; import javax.security.auth.message.module.ServerAuthModule;


import org.apache.catalina.Context;
import org.apache.catalina.authenticator.jaspic.MessageInfoImpl; import org.apache.catalina.authenticator.jaspic.MessageInfoImpl;
import org.apache.tomcat.util.res.StringManager; import org.apache.tomcat.util.res.StringManager;


Expand All @@ -48,20 +49,27 @@ public abstract class TomcatAuthModule implements ServerAuthModule {


protected CallbackHandler handler; protected CallbackHandler handler;


protected Context context;


public TomcatAuthModule(Context context) {
this.context = context;
}



protected boolean isMandatory(MessageInfo messageInfo) { protected boolean isMandatory(MessageInfo messageInfo) {
String mandatory = (String) messageInfo.getMap().get(MessageInfoImpl.IS_MANDATORY); String mandatory = (String) messageInfo.getMap().get(MessageInfoImpl.IS_MANDATORY);
return Boolean.parseBoolean(mandatory); return Boolean.parseBoolean(mandatory);
} }




@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
@Override @Override
public final void initialize(MessagePolicy requestPolicy, MessagePolicy responsePolicy, public final void initialize(MessagePolicy requestPolicy, MessagePolicy responsePolicy,
CallbackHandler handler, Map options) throws AuthException { CallbackHandler handler, Map options) throws AuthException {
this.handler = handler; this.handler = handler;
this.realmName = (String) options.get(REALM_NAME); this.realmName = (String) options.get(REALM_NAME);
initializeModule(requestPolicy, responsePolicy, handler, options); initializeModule(requestPolicy, responsePolicy, handler, options);
} }




Expand Down

0 comments on commit 45eb436

Please sign in to comment.