FileDocCategorySizeDatePackage
AppservPasswordLoginModule.javaAPI DocGlassfish v2 API12065Fri May 04 22:32:54 BST 2007com.sun.appserv.security

AppservPasswordLoginModule

public abstract class AppservPasswordLoginModule extends Object implements LoginModule
Abstract base class for password-based login modules.

Most login modules receive a username and password from the client (possibly through HTTP BASIC auth, or FORM, or other mechanism) and then make (or delegate) an authentication decision based on this data. This class provides common methods for such password-based login modules.

Subclasses need to implement the authenticateUser() method and later call commitUserAuthentication().

Fields Summary
protected Subject
_subject
protected Map
_sharedState
protected Map
_options
protected String
_username
protected String
_password
protected com.sun.enterprise.security.auth.realm.Realm
_currentRealm
protected boolean
_succeeded
protected boolean
_commitSucceeded
protected com.sun.enterprise.deployment.PrincipalImpl
_userPrincipal
protected String[]
_groupsList
protected Logger
_logger
protected static final com.sun.enterprise.util.i18n.StringManager
sm
Constructors Summary
Methods Summary
public final booleanabort()
Abort the authentication process.

        if(_logger.isLoggable(Level.FINE)){
            _logger.log(Level.FINE,"JAAS authentication aborted.");
        }
        
        if (_succeeded == false) {
            return false;
        } else if (_succeeded == true && _commitSucceeded == false) {
            // login succeeded but overall authentication failed
            _succeeded = false;
            _username = null;
            _password = null;
            _userPrincipal = null;
            for(int i = 0; i < _groupsList.length; i++){
                _groupsList[i] = null;
            }
            _groupsList = null;
        } else {
            // overall authentication succeeded and commit succeeded,
            // but someone else's commit failed
            logout();
        }
        return true;
    
protected abstract voidauthenticateUser()
Perform authentication decision. Method returns silently on success and returns a LoginException on failure.

throws
LoginException on authentication failure.

public booleancommit()
Commit the authentication.

Commit is called after all necessary login modules have succeeded. It adds (if not present) a PrincipalImpl principal and a LocalCredentials public credential to the Subject.

throws
LoginException If commit fails.

        if (_succeeded == false) {
            return false;
        }

        // Add a Principal (authenticated identity) to the Subject
        // Assume the user we authenticated is the PrincipalImpl [RI]
        String realm_name = _currentRealm.getName();
        _userPrincipal = 
            PrincipalGroupFactory.getPrincipalInstance(_username, realm_name);
        Set principalSet = _subject.getPrincipals();
        if (!principalSet.contains(_userPrincipal)){
            principalSet.add(_userPrincipal);
        }
        /* populate the group in the subject and clean out the slate at the same
         * time
         */
        for(int i = 0; i<_groupsList.length; i++){
            if(_groupsList[i] != null){
                Group g =
                    PrincipalGroupFactory.getGroupInstance(_groupsList[i], realm_name);
                if(!principalSet.contains(g)){
                    principalSet.add(g);
                }
                
                // cleaning the slate
                _groupsList[i] = null;
            }
        }
        
        // In any case, clean out state.
        _groupsList = null;
        _username = null;
        _password = null;
        _commitSucceeded = true;
        if(_logger.isLoggable(Level.FINE)){
            _logger.log(Level.FINE,"JAAS authentication committed.");
        }
        return true;
    
public final voidcommitUserAuthentication(java.lang.String[] groups)

This is a convenience method which can be used by subclasses

Note that this method is called after the authentication has succeeded. If authentication failed do not call this method. Global instance field succeeded is set to true by this method.

param
groups String array of group memberships for user (could be empty).

        _groupsList = groups;
        _succeeded = true;
    
public javax.security.auth.SubjectgetSubject()

return
the subject being authenticated. use case: A custom login module could overwrite commit() method, and call getSubject() to get subject being authenticated inside its commit(). Custom principal then can be added to subject. By doing this,custom principal will be stored in calling thread's security context and participate in following Appserver's authorization.

        return _subject;
    
public final voidinitialize(javax.security.auth.Subject subject, javax.security.auth.callback.CallbackHandler callbackHandler, java.util.Map sharedState, java.util.Map options)
Initialize this login module.

param
subject - the Subject to be authenticated.
param
callbackHandler - a CallbackHandler for obtaining the subject username and password.
param
sharedState - state shared with other configured LoginModules.
param
options - options specified in the login Configuration for this particular LoginModule.


    
                                                             
          
                              
    
        _subject = subject;
        _sharedState = sharedState;
        _options = options;
        if(_logger.isLoggable(Level.FINE)){
            _logger.log(Level.FINE, "Login module initialized: "+
                           this.getClass().toString());
        }
    
public final booleanlogin()
Perform login.

The callback handler is used to obtain authentication info for the subject and a login is attempted. This PasswordLoginModule expects to find a PasswordCredential in the private credentials of the Subject. If not present the login fails. The callback handler is ignored as it is not really relevant on the server side. Finally, the authenticateUser() method is invoked.

returns
true if login succeeds, otherwise an exception is thrown.
throws
LoginException Thrown if login failed, or on other problems.

        if (_subject==null) {
            String msg = sm.getString("pwdlm.noinfo");
            _logger.log(Level.SEVERE, msg);
            throw new LoginException(msg);
        }

        PasswordCredential pwdCred = null;

        try {
            Iterator i = _subject.getPrivateCredentials().iterator();
            while (i.hasNext() && pwdCred==null) {
                Object privCred = i.next();
                if (privCred instanceof PasswordCredential) {
                    pwdCred = (PasswordCredential)privCred;
                }
            }
        } catch (Exception e) {
            _logger.log(Level.WARNING, "passwordlm.nocreds", e.toString());
        }

        if (pwdCred==null) {
            _logger.log(Level.SEVERE, "passwordlm.nopwdcred");
            String msg = sm.getString("pwdlm.nocreds");            
            throw new LoginException(msg);
        }

        // Need to obtain the requested realm to get parameters.

        String realm = null;
        try {
            realm = pwdCred.getRealm();
            _currentRealm = Realm.getInstance(realm);

        } catch (Exception e) {
            String msg = sm.getString("pwdlm.norealm", realm);
            _logger.log(Level.SEVERE, msg);
            throw new LoginException(msg);
        }

        if (_currentRealm == null) {
            String msg = sm.getString("pwdlm.norealmavail", realm);
            _logger.log(Level.SEVERE, msg);
            throw new LoginException(msg);
        }

        // Get username and password data from credential (ignore callback)

        _username = pwdCred.getUser();
        _password = pwdCred.getPassword();

        // Delegate the actual authentication to subclass.

        authenticateUser();
        if(_logger.isLoggable(Level.FINE)){
            _logger.log(Level.FINE, "JAAS login complete.");
        }
        return true;
    
public final booleanlogout()
Log out the subject.

        if(_logger.isLoggable(Level.FINE)){
            _logger.log(Level.FINE, "JAAS logout for: " + _subject.toString());
        }

        _subject.getPrincipals().clear();
        _subject.getPublicCredentials().clear();
        _subject.getPrivateCredentials().clear();
        
        _succeeded = false;
        _commitSucceeded = false;
        _username = null;
        _password = null;
        _userPrincipal = null;
        if(_groupsList != null){
            for (int i = 0; i < _groupsList.length; i++){
                _groupsList[i] = null;
            }
            _groupsList = null;
        }
        return true;