Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.jaas.spi;
 
 import java.util.List;
 import java.util.Map;
 
 
AbstractLoginModule Abstract base class for all LoginModules. Subclasses should just need to implement getUserInfo method.
 
 public abstract class AbstractLoginModule implements LoginModule
 {
     private CallbackHandler callbackHandler;
 
     private boolean authState = false;
     private boolean commitState = false;
     private JAASUserInfo currentUser;
     private Subject subject;
 
     public class JAASUserInfo
     {
         private UserInfo user;
         private Principal principal;
         private List<JAASRoleroles;
 
         public JAASUserInfo (UserInfo u)
         {
             setUserInfo(u);
         }
 
         public String getUserName ()
         {
             return this..getUserName();
         }
 
         public Principal getPrincipal()
         {
             return this.;
         }
 
         public void setUserInfo (UserInfo u)
         {
             this. = u;
             this. = new JAASPrincipal(u.getUserName());
             this. = new ArrayList<JAASRole>();
             if (u.getRoleNames() != null)
             {
                 Iterator<Stringitor = u.getRoleNames().iterator();
                 while (itor.hasNext())
                     this..add(new JAASRole((String)itor.next()));
             }
         }
 
         public void setJAASInfo (Subject subject)
         {
             subject.getPrincipals().add(this.);
             subject.getPrivateCredentials().add(this..getCredential());
             subject.getPrincipals().addAll();
         }
 
         public void unsetJAASInfo (Subject subject)
        {
            subject.getPrincipals().remove(this.);
            subject.getPrivateCredentials().remove(this..getCredential());
            subject.getPrincipals().removeAll(this.);
        }
        public boolean checkCredential (Object suppliedCredential)
        {
            return this..checkCredential(suppliedCredential);
        }
    }
    public Subject getSubject ()
    {
        return this.;
    }
    public void setSubject (Subject s)
    {
        this. = s;
    }
    public JAASUserInfo getCurrentUser()
    {
        return this.;
    }
    public void setCurrentUser (JAASUserInfo u)
    {
        this. = u;
    }
    {
        return this.;
    }
    public void setCallbackHandler(CallbackHandler h)
    {
        this. = h;
    }
    public boolean isAuthenticated()
    {
        return this.;
    }
    public boolean isCommitted ()
    {
        return this.;
    }
    public void setAuthenticated (boolean authState)
    {
        this. = authState;
    }
    public void setCommitted (boolean commitState)
    {
        this. = commitState;
    }
    
    public boolean abort() throws LoginException
    {
        this. = null;
        return (isAuthenticated() && isCommitted());
    }

    

Returns:
true if committed, false if not (likely not authenticated)
Throws:
javax.security.auth.login.LoginException
See also:
javax.security.auth.spi.LoginModule.commit()
    public boolean commit() throws LoginException
    {
        if (!isAuthenticated())
        {
             = null;
            setCommitted(false);
            return false;
        }
        setCommitted(true);
        return true;
    }
    public Callback[] configureCallbacks ()
    {
        Callback[] callbacks = new Callback[3];
        callbacks[0] = new NameCallback("Enter user name");
        callbacks[1] = new ObjectCallback();
        callbacks[2] = new PasswordCallback("Enter password"false); //only used if framework does not support the ObjectCallback
        return callbacks;
    }
    
    
    public boolean isIgnored ()
    {
        return false;
    }
    
    
    public abstract UserInfo getUserInfo (String usernamethrows Exception;



    

Returns:
true if is authenticated, false otherwise
Throws:
javax.security.auth.login.LoginException
See also:
javax.security.auth.spi.LoginModule.login()
    public boolean login() throws LoginException
    {
        try
        {  
            if (isIgnored())
                return false;
            
            if ( == null)
                throw new LoginException ("No callback handler");
            Callback[] callbacks = configureCallbacks();
            .handle(callbacks);
            String webUserName = ((NameCallback)callbacks[0]).getName();
            Object webCredential = null;
            webCredential = ((ObjectCallback)callbacks[1]).getObject(); //first check if ObjectCallback has the credential
            if (webCredential == null)
                webCredential = ((PasswordCallback)callbacks[2]).getPassword(); //use standard PasswordCallback
            if ((webUserName == null) || (webCredential == null))
            {
                setAuthenticated(false);
                throw new FailedLoginException();
            }
            UserInfo userInfo = getUserInfo(webUserName);
            if (userInfo == null)
            {
                setAuthenticated(false);
                throw new FailedLoginException();
            }
             = new JAASUserInfo(userInfo);
            setAuthenticated(.checkCredential(webCredential));
          
            if (isAuthenticated())
                return true;
            else
                throw new FailedLoginException();
        }
        catch (IOException e)
        {
            throw new LoginException (e.toString());
        }
        catch (UnsupportedCallbackException e)
        {
            throw new LoginException (e.toString());
        }
        catch (Exception e)
        {
            if (e instanceof LoginException)
                throw (LoginException)e;
            throw new LoginException (e.toString());
        }
    }

    
    public boolean logout() throws LoginException
    {
        this..unsetJAASInfo(this.);
        return true;
    }

    
    public void initialize(Subject subjectCallbackHandler callbackHandler,
            Map<String,?> sharedStateMap<String,?> options)
    {
        this. = callbackHandler;
        this. = subject;
    }
New to GrepCode? Check out our FAQ X