The Form Based AuthenticationHandler has two authentication phases: The first phase is presenting a login form to the user and passing the entered user name and password to the server. The second phase is storing successful authentication in a Cookie or an HTTP Session.
The implementation of the Form Based Authentication Handler follows the guidelines of the Servlet API 2.4 specification for Form Based Authentication in section SRV.12.5.3. Specifically the following requirements are implemented:
/j_security_check
and the user name and password names must be j_username
and j_password
, resp.HttpServletRequest.getAuthType()
is set to HttpServletRequest.FORM_AUTH
.The Form Based Authentication Handler is maintained in the the Sling repo.
extractCredentials
-- Prepares credentials for the form entered data or from the Cookie or HTTP Session attribute. Returns null
if neither data is provided in the requestrequestCredentials
-- Redirects the client (browser) to the login formdropCredentials
-- Remove the Cookie or remove the HTTP Session attributeauthenticationFailed
-- Remove the Cookie or remove the HTTP Session attributeauthenticationSucceeded
-- Set (or update) the Cookie or HTTP Session attributeThe login form submitted in phase 1 to validate the user name and password must be provided in an HTTP POST
request to an URL whose last segment is j_security_check
. The request is ignored as a form submission if either the method is not POST
or the last segment is no j_security_check
.
The form is rendered by redirecting the client to the URL indicated by the form.login.form
configuration parameter. This redirection request may accompanyied by the following parameters:
resource
-- The resource to which the user should be redirected after successful login. This request parameter should be submitted back to the server as the resource
parameter.j_reason
-- This parameter indicates the reason for rendering the login form. If this parameter is set, it is set to INVALID_CREDENTIALS
indicating a previous form submission presented invalid username and password or TIMEOUT
indicating a login session has timed out. The login form servlet/script can present the user with an appropriate message.The Form Based Authentication Handlers supports the following request parameters submitted by the HTML form:
j_username
-- Name of the user to authenticatej_password
-- Password to authenticate the userj_validate
-- Flag indicating whether to just validate the credentialsresource
-- The location to go to on successful loginsling.auth.redirect
-- The location to redirect to on successful loginThe j_username
and j_password
parameters are used to create a JCR SimpleCredentials
object to log into the JCR Repository.
The j_validate
parameter may be used to implement login form submission using AJAX. If this parameter is set to true
(case-insensitive) the credentials are used to login and after success or failure to return a status code:
Status | Description |
---|---|
200 OK | Authentication succeeded; credentials are valid for login; the Cookie or HTTP Session attribute is now set |
403 FORBIDDEN | Authentication failed; credentials are invalid for login; the Cookie or HTTP Session attribute is not set (if it was set, it is now cleared) |
If the j_validate
parameter is not set or is set to any value other than true
, the request processing depends on authentication success or failure:
Authentication | Description |
---|---|
Success | Client is redirected to the authenticated resource; the Cookie or HTTP Session attribute is now set. |
Failure | The request is redirected to the login form again; the Cookie or HTTP Session attribute is not set (if it was set, it is now cleared) |
The resource
and sling.auth.redirect
parameters provide similar functionality but with differing historical backgrounds. The resource
parameter is based on the resource
request attribute which is set by the login servlet to indicate the original target resource the client desired when it was forced to authenticate. The sling.auth.redirect
parameter can be used by clients (applications like cURL or plain HTML forms) to request being redirected after successful login. If both parameters are set, the sling.auth.redirect
parameter takes precedence.
The Form Based Authentication Handler contains a default form servlet and HTML form template.
After the successful authentication of the user in phase 1, the authentication state is stored in a Cookie or an HTTP Session. The stored value is a security token with the following contents:
HmacSHA1(securetoken, <securetokennumber><expirytime>@<userID>)@<securetokennumber><expirytime>@<userID>
The securetoken
and securetokennumber
are related in that an table of secure tokens is maintained where the securetoken
is an entry in the table and the securetokennumber
is the index in of the token in the table.
The secure tokens are refreshed periodically causing the authentication state stored in the Cookie or the HTTP Session to be updated peridocally. This periodic update has two advantages:
The authentication state may be transmitted with a Cookie which is configured as follows:
ServletRequest.isSecure()
methodIf the authentication state is kept in an HTTP Session the setup of the session ID cookie is maintained by the servlet container and is outside of the control of the Form Based AuthenticationHandler.
The Form Based Authentication Handler is configured with configuration provided by the OSGi Configuration Admin Service using the org.apache.sling.formauth.FormAuthenticationHandler
service PID.
Parameter | Default | Description |
---|---|---|
form.login.form | /system/sling/form/login | The URL (without any context path prefix) to redirect the client to to present the login form. |
form.auth.storage | cookie | The type of storage used to provide the authentication state. Valid values are cookie and session . The default value also applies if any setting other than the supported values is configured. |
form.auth.name | sling.formauth | The name of the Cookie or HTTP Session attribute providing the authentication state. |
form.auth.timeout | 30 | The number of minutes after which a login session times out. This value is used as the expiry time set in the authentication data. |
form.credentials.name | sling.formauth | The name of the SimpleCredentials attribute used to provide the authentication data to the LoginModulePlugin . |
form.token.file | cookie-tokens.bin | The name of the file used to persist the security tokens. |
form.default.cookie.domain | The domain on which cookies will be set, unless overridden in the AuthenticationInfo object. |
Note: The form.token.file
parameter currently refers to a file stored in the file system. If the path is a relative path, the file is either stored in the Authentication Handler bundle private data area or -- if not possible -- below the location indicated by the sling.home
framework property or -- if sling.home
is not set -- the current working directory. In the future this file may be store in the JCR Repository to support clustering scenarios.
Form Based Authentication has some limitations in terms of security:
To prevent eavesdroppers from sniffing the credentials or stealing the Cookie a secure transport layer should be used such as TLS/SSL, VPN or IPSec.