public class JWTAuthenticationHandler extends java.lang.Object implements AuthenticationHandler
JWTAuthenticationHandler implements
AuthenticationHandler to add WebSSO behavior for UIs. The expected
SSO token is a JsonWebToken (JWT). The supported algorithm is RS256 which
uses PKI between the token issuer and consumer. The flow requires a redirect
to a configured authentication server URL and a subsequent request with the
expected JWT token. This token is cryptographically verified and validated.
The user identity is then extracted from the token and used to create an
AuthenticationToken - as expected by the AuthenticationFilter.
The supported configuration properties are:
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
AUTHENTICATION_PROVIDER_URL |
static java.lang.String |
EXPECTED_JWT_AUDIENCES |
static java.lang.String |
JWT_COOKIE_NAME |
static java.lang.String |
PUBLIC_KEY_PEM |
static java.lang.String |
TYPE |
JWT_SSO_ENABLED, NON_BROWSER_USER_AGENTS, NON_BROWSER_USER_AGENTS_DEFAULT, WWW_AUTHENTICATE| Constructor and Description |
|---|
JWTAuthenticationHandler() |
| Modifier and Type | Method and Description |
|---|---|
AuthenticationToken |
authenticate(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Performs an authentication step for the given HTTP client request.
|
void |
destroy()
Destroys the authentication handler instance.
|
protected java.lang.String |
getJWTFromCookie(javax.servlet.http.HttpServletRequest req)
Encapsulate the acquisition of the JWT token from HTTP cookies within the
request.
|
java.lang.String |
getType()
Returns the authentication type of the authentication handler.
|
void |
init(java.util.Properties config)
Initializes the authentication handler instance.
|
void |
setPublicKey(java.security.interfaces.RSAPublicKey pk)
Primarily for testing, this provides a way to set the publicKey for
signature verification without needing to get a PEM encoded value.
|
boolean |
shouldAuthenticate(javax.servlet.http.HttpServletRequest request)
Should the request be authenticated by the handler or not.
|
protected boolean |
validateAudiences(com.nimbusds.jwt.SignedJWT jwtToken)
Validate whether any of the accepted audience claims is present in the
issued token claims list for audience.
|
protected boolean |
validateExpiration(com.nimbusds.jwt.SignedJWT jwtToken)
Validate that the expiration time of the JWT token has not been violated.
|
protected boolean |
validateSignature(com.nimbusds.jwt.SignedJWT jwtToken)
Verify the signature of the JWT token in this method.
|
protected boolean |
validateToken(com.nimbusds.jwt.SignedJWT jwtToken)
This method provides a single method for validating the JWT for use in
request processing.
|
public static final java.lang.String TYPE
public static final java.lang.String AUTHENTICATION_PROVIDER_URL
public static final java.lang.String PUBLIC_KEY_PEM
public static final java.lang.String EXPECTED_JWT_AUDIENCES
public static final java.lang.String JWT_COOKIE_NAME
public void setPublicKey(java.security.interfaces.RSAPublicKey pk)
pk - publicKey for the token signtature verificationpublic java.lang.String getType()
AuthenticationHandlergetType in interface AuthenticationHandlerpublic void init(java.util.Properties config)
throws javax.servlet.ServletException
This method is invoked by the AuthenticationFilter.init(javax.servlet.FilterConfig) method.
init in interface AuthenticationHandlerconfig - configuration properties to initialize the handler.javax.servlet.ServletException - thrown if the handler could not be initialized.public void destroy()
AuthenticationHandler
This method is invoked by the AuthenticationFilter.destroy() method.
destroy in interface AuthenticationHandlerpublic AuthenticationToken authenticate(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws java.io.IOException, AuthenticationException
AuthenticationHandler
This method is invoked by the AuthenticationFilter only if the HTTP client request is
not yet authenticated.
Depending upon the authentication mechanism being implemented, a particular HTTP client may end up making a sequence of invocations before authentication is successfully established (this is the case of Kerberos SPNEGO).
This method must return an AuthenticationToken only if the the HTTP client request has
been successfully and fully authenticated.
If the HTTP client request has not been completely authenticated, this method must take over
the corresponding HTTP response and it must return null.
authenticate in interface AuthenticationHandlerrequest - the HTTP client request.response - the HTTP client response.AuthenticationToken if the HTTP client request has been authenticated,
null otherwise (in this case it must take care of the response).java.io.IOException - thrown if an IO error occurred.AuthenticationException - thrown if an Authentication error occurred.public boolean shouldAuthenticate(javax.servlet.http.HttpServletRequest request)
AuthenticationHandlerThis is useful for deciding if a request should be handled by this handler or just forwarded to the next filter in chain. For example, if it's a java client in a long running application then the SSO handler can decide to not authenticate it and pass it on to the next filter
If the method returns TRUE the request will have to go through
this authentication handler
If the method returns FALSE the request is expected to be
passed on to the next filter in the chain
shouldAuthenticate in interface AuthenticationHandlerrequest - the HTTP client request.TRUE if this request should be authenticated by the handler,
FALSE otherwise.protected java.lang.String getJWTFromCookie(javax.servlet.http.HttpServletRequest req)
req - servlet request to get the JWT token fromprotected boolean validateToken(com.nimbusds.jwt.SignedJWT jwtToken)
jwtToken - the token to validateprotected boolean validateSignature(com.nimbusds.jwt.SignedJWT jwtToken)
jwtToken - the token that contains the signature to be validatedprotected boolean validateAudiences(com.nimbusds.jwt.SignedJWT jwtToken)
jwtToken - the JWT token where the allowed audiences will be foundprotected boolean validateExpiration(com.nimbusds.jwt.SignedJWT jwtToken)
jwtToken - the token that contains the expiration date to validate