login
boolean login() throws LoginException;
The login method
is called to authenticate a Subject. This is phase
1 of authentication.
This method implementation should perform the actual authentication. For example, it may cause prompting for a user name and password, and then attempt to verify the password against a password database. Another example implementation may inform the user to insert their finger into a fingerprint reader, and then match the input fingerprint against a fingerprint database.
If
your LoginModule requires some form of user interaction
(retrieving a user name and password, for example), it should not
do so directly. That is because there are various ways of communicating
with a user, and it is desirable for LoginModules
to remain independent of the different types of user interaction.
Rather, the LoginModule's login method
should invoke the handle method of the the CallbackHandler passed
to the initialize method to perform the user interaction
and set appropriate results, such as the user name and password. The LoginModule passes
the CallbackHandler an array of appropriate Callbacks,
for example a NameCallback for the user name and a PasswordCallback
for the password, and the CallbackHandler performs
the requested user interaction and sets appropriate values in the Callbacks.
For example, to process a NameCallback, the CallbackHandler may
prompt for a name, retrieve the value from the user, and call the NameCallback's setName method
to store the name.
The authentication process may also involve communication over a network. For example, if this method implementation performs the equivalent of a kinit in Kerberos, then it would need to contact the KDC. If a password database entry itself resides in a remote naming service, then that naming service needs to be contacted, perhaps via the Java™ Naming and Directory Interface (JNDI). Implementations might also interact with an underlying operating system. For example, if a user has already logged into an operating system like Solaris or Windows NT, this method might simply import the underlying operating system's identity information.
The login method
should
- Determine whether or not this
LoginModuleshould be ignored. One example of when it should be ignored is when a user attempts to authenticate under an identity irrelevant to thisLoginModule(if a user attempts to authenticate as root using NIS, for example). If thisLoginModuleshould be ignored,loginshould returnfalse. Otherwise, it should do the following: - Call the
CallbackHandlerhandlemethod if user interaction is required. - Perform the authentication.
- Store the authentication result (success or failure).
- If authentication succeeded, save any relevant state information
that may be needed by the
commitmethod. - Return
trueif authentication succeeds, or throw aLoginExceptionsuch asFailedLoginExceptionif authentication fails.
Note that the login method implementation
should not associate any new Principal or credential
information with the saved Subject object. This method
merely performs the authentication, and then stores away the authentication
result and corresponding authentication state. This result and state
will later be accessed by the commit or abort method.
Note that the result and state should typically not be saved in the sharedState Map,
as they are not intended to be shared with other LoginModules.
An
example of where this method might find it useful to store state information
in the sharedState Map is when LoginModules
are configured to share passwords. In this case, the entered password
would be saved as shared state. By sharing passwords, the user only
enters the password once, and can still be authenticated to multiple LoginModules.
The standard conventions for saving and retrieving names and passwords
from the sharedState Map are the following:
javax.security.auth.login.name- Use this as the shared state map key for saving/retrieving a name.javax.security.auth.login.password- Use this as the shared state map key for saving/retrieving a password.
If authentication fails, the login method
should not retry the authentication. This is the responsibility of
the application. Multiple LoginContext login method
calls by an application are preferred over multiple login attempts
from within LoginModule.login().