Class AuthUtil
AuthUtil provides utility functions for implementations of
AuthenticationHandler services and
users of the Sling authentication infrastructure.
This utility class can neither be extended from nor can it be instantiated.
- Since:
- 1.1 (bundle version 1.0.8)
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleancheckReferer(javax.servlet.http.HttpServletRequest request, String loginForm) Check if the request is for this authentication handler.static StringgetAttributeOrParameter(javax.servlet.http.HttpServletRequest request, String name, String defaultValue) Returns the value of the named request attribute or parameter as a string as follows: If there is a request attribute of that name, which is a non-empty string, it is returned. If there is a non-empty request parameter of that name, this parameter is returned.static StringgetLoginResource(javax.servlet.http.HttpServletRequest request, String defaultLoginResource) Returns any resource target to redirect to after successful authentication.static booleanisAjaxRequest(javax.servlet.http.HttpServletRequest request) Returnstrueif the request is to be considered an AJAX request placed using theXMLHttpRequestbrowser host object.static booleanisBrowserRequest(javax.servlet.http.HttpServletRequest request) Returnstrueif the given request can be assumed to be sent by a client browser such as Firefix, Internet Explorer, etc.static booleanisRedirectValid(javax.servlet.http.HttpServletRequest request, String target) Returnstrueif the given redirecttargetis valid according to the following list of requirements: Thetargetis neithernullnor an empty string Thetargetis not an URL which is identified by the character sequence://separating the scheme from the host Thetargetis normalized such that it contains no consecutive slashes and no path segment contains a single or double dot Thetargetmust be prefixed with the servlet context path If aResourceResolveris available as a request attribute thetarget(without the servlet context path prefix) must resolve to an existing resource If aResourceResolveris not available as a request attribute thetargetmust be an absolute path starting with a slash character does not contain any of the characters<,>,', or"in plain or URL encodingstatic booleanisValidateRequest(javax.servlet.http.HttpServletRequest request) Returnstrueif the the client just asks for validation of submitted username/password credentials.static voidsendInvalid(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) Sends a 403/FORBIDDEN response optionally stating the reason for this response code in theAuthConstants.X_REASONheader.static voidsendRedirect(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String target, Map<String, String> params) Redirects to the given target path appending any parameters provided in the parameter map.static voidsendValid(javax.servlet.http.HttpServletResponse response) Sends a 200/OK response to a credential validation request.static StringsetLoginResourceAttribute(javax.servlet.http.HttpServletRequest request, String defaultValue) Ensures and returns theAuthenticator.LOGIN_RESOURCErequest attribute is set to a non-null, non-empty string.
-
Method Details
-
getAttributeOrParameter
public static String getAttributeOrParameter(javax.servlet.http.HttpServletRequest request, String name, String defaultValue) Returns the value of the named request attribute or parameter as a string as follows:- If there is a request attribute of that name, which is a non-empty string, it is returned.
- If there is a non-empty request parameter of that name, this parameter is returned.
- Otherwise the
defaultValueis returned.
- Parameters:
request- The request from which to return the attribute or request parametername- The name of the attribute/parameterdefaultValue- The default value to use if neither a non-empty string attribute or a non-empty parameter exists in the request.- Returns:
- The attribute, parameter or
defaultValueas defined above.
-
getLoginResource
public static String getLoginResource(javax.servlet.http.HttpServletRequest request, String defaultLoginResource) Returns any resource target to redirect to after successful authentication. This method either returns a non-empty string or thedefaultLoginResourceparameter. First theresourcerequest attribute is checked. If it is a non-empty string, it is returned. Second theresourcerequest parameter is checked and returned if it is a non-empty string.- Parameters:
request- The request providing the attribute or parameterdefaultLoginResource- The default login resource value- Returns:
- The non-empty redirection target or
defaultLoginResource.
-
setLoginResourceAttribute
public static String setLoginResourceAttribute(javax.servlet.http.HttpServletRequest request, String defaultValue) Ensures and returns theAuthenticator.LOGIN_RESOURCErequest attribute is set to a non-null, non-empty string. If the attribute is not currently set, this method sets it as follows:- If the
Authenticator.LOGIN_RESOURCErequest parameter is set to a non-empty string, that parameter is set - Otherwise if the
defaultValueis a non-empty string the default value is used - Otherwise the attribute is set to "/"
- Parameters:
request- The request to check for the resource attributedefaultValue- The default value to use if the attribute is not set and the request parameter is not set. This parameter is ignored if it isnullor an empty string.- Returns:
- returns the value of resource request attribute
- If the
-
sendRedirect
public static void sendRedirect(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String target, Map<String, String> params) throws IOExceptionRedirects to the given target path appending any parameters provided in the parameter map.This method implements the following functionality:
- If the
paramsmap does not contain a (non-null) value for theresourceentry, such an entry is generated from the request URI and the (optional) query string of the givenrequest. - The parameters from the
paramsmap or at least a singleresourceparameter are added to the target path for the redirect. Each parameter value is encoded using thejava.net.URLEncoderwith UTF-8 encoding to make it safe for requests
After checking the redirect target and creating the target URL from the parameter map, the response buffer is reset and the
HttpServletResponse.sendRedirectis called. Any headers already set before calling this method are preserved.- Parameters:
request- The request object used to get the current request URI and request query string if theparamsmap does not have theresourceparameter set.response- The response used to send the redirect to the client.target- The redirect target to validate. This path must be prefixed with the request's servlet context path. If this parameter is not a valid target request as per theisRedirectValid(HttpServletRequest, String)method the target is modified to be the root of the request's context.params- The map of parameters to be added to the target path. This may benull.- Throws:
IOException- If an error occurs sending the redirect requestIllegalStateException- If the response was committed or if a partial URL is given and cannot be converted into a valid URLInternalError- If the UTF-8 character encoding is not supported by the platform. This should not be caught, because it is a real problem if the encoding required by the specification is missing.
- If the
-
isValidateRequest
public static boolean isValidateRequest(javax.servlet.http.HttpServletRequest request) Returnstrueif the the client just asks for validation of submitted username/password credentials.This implementation returns
trueif the request parameterAuthConstants.PAR_J_VALIDATEis set totrue(case-insensitve). If the request parameter is not set or to any value other thantruethis method returnsfalse.- Parameters:
request- The request to provide the parameter to check- Returns:
trueif theAuthConstants.PAR_J_VALIDATEparameter is set totrue.
-
sendValid
public static void sendValid(javax.servlet.http.HttpServletResponse response) Sends a 200/OK response to a credential validation request.This method just overwrites the response status to 200/OK, sends no content (content length header set to zero) and prevents caching on clients and proxies. Any other response headers set before calling this methods are preserved and sent along with the response.
- Parameters:
response- The response object- Throws:
IllegalStateException- if the response has already been committed
-
sendInvalid
public static void sendInvalid(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) Sends a 403/FORBIDDEN response optionally stating the reason for this response code in theAuthConstants.X_REASONheader. The value for theAuthConstants.X_REASONheader is taken fromAuthenticationHandler.FAILURE_REASONrequest attribute if set.This method just overwrites the response status to 403/FORBIDDEN, adds the
AuthConstants.X_REASONheader and sends the reason as result back. Any other response headers set before calling this methods are preserved and sent along with the response.- Parameters:
request- The request objectresponse- The response object- Throws:
IllegalStateException- if the response has already been committed
-
checkReferer
Check if the request is for this authentication handler.- Parameters:
request- the current requestloginForm- Path to the login form- Returns:
- true if the referrer matches this handler, or false otherwise
-
isRedirectValid
Returnstrueif the given redirecttargetis valid according to the following list of requirements:- The
targetis neithernullnor an empty string - The
targetis not an URL which is identified by the character sequence://separating the scheme from the host - The
targetis normalized such that it contains no consecutive slashes and no path segment contains a single or double dot - The
targetmust be prefixed with the servlet context path - If a
ResourceResolveris available as a request attribute thetarget(without the servlet context path prefix) must resolve to an existing resource - If a
ResourceResolveris not available as a request attribute thetargetmust be an absolute path starting with a slash character does not contain any of the characters<,>,', or"in plain or URL encoding
If any of the conditions does not hold, the method returns
falseand logs a warning level message with the org.apache.sling.auth.core.AuthUtil logger.- Parameters:
request- Providing theResourceResolverattribute and the context to resolve the resource from thetarget. This may benullwhich causes the target to not be validated with aResoureResolvertarget- The redirect target to validate. This path must be prefixed with the request's servlet context path.- Returns:
trueif the redirect target can be considered valid
- The
-
isBrowserRequest
public static boolean isBrowserRequest(javax.servlet.http.HttpServletRequest request) Returnstrueif the given request can be assumed to be sent by a client browser such as Firefix, Internet Explorer, etc.This method inspects the
User-Agentheader and returnstrueif the header contains the string Mozilla (known to be contained in Firefox, Internet Explorer, WebKit-based browsers User-Agent) or Opera (known to be contained in the Opera User-Agent).- Parameters:
request- The request to inspect- Returns:
trueif the request is assumed to be sent by a browser.
-
isAjaxRequest
public static boolean isAjaxRequest(javax.servlet.http.HttpServletRequest request) Returnstrueif the request is to be considered an AJAX request placed using theXMLHttpRequestbrowser host object. Currently a request is considered an AJAX request if the client sends the X-Requested-With request header set toXMLHttpRequest.- Parameters:
request- The current request- Returns:
trueif the request can be considered an AJAX request.
-