Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2012 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.security;
 
 import java.util.Map;
 
 
 
 
 
 /* ------------------------------------------------------------ */
A login service that keeps UserIdentities in a concurrent map either as the source or a cache of the users.
 
 public abstract class MappedLoginService extends AbstractLifeCycle implements LoginService
 {
     private static final Logger LOG = Log.getLogger(MappedLoginService.class);
 
     protected String _name;
 
     /* ------------------------------------------------------------ */
     protected MappedLoginService()
     {
     }
     
     /* ------------------------------------------------------------ */
    
Get the name.

Returns:
the name
 
     public String getName()
     {
         return ;
     }
     
     /* ------------------------------------------------------------ */
    
Get the identityService.

Returns:
the identityService
 
     {
         return ;
     }
     
     /* ------------------------------------------------------------ */
    
Get the users.

Returns:
the users
 
     {
         return ;
     }
     
     /* ------------------------------------------------------------ */
    
Set the identityService.

Parameters:
identityService the identityService to set
 
     public void setIdentityService(IdentityService identityService)
     {
         if (isRunning())
             throw new IllegalStateException("Running");
          = identityService;
     }
 
     /* ------------------------------------------------------------ */
    
Set the name.

Parameters:
name the name to set
 
    public void setName(String name)
    {
        if (isRunning())
            throw new IllegalStateException("Running");
         = name;
    }
    /* ------------------------------------------------------------ */
    
Set the users.

Parameters:
users the users to set
    public void setUsers(Map<StringUserIdentityusers)
    {
        if (isRunning())
            throw new IllegalStateException("Running");
        .clear();
        .putAll(users);
    }
    /* ------------------------------------------------------------ */
    
    @Override
    protected void doStart() throws Exception
    {
        loadUsers();
        super.doStart();
    }
    /* ------------------------------------------------------------ */
    @Override
    protected void doStop() throws Exception
    {
        super.doStop();
    }
    /* ------------------------------------------------------------ */
    public void logout(UserIdentity identity)
    {   
        .debug("logout {}",identity);
    }
    
    /* ------------------------------------------------------------ */
    @Override
    public String toString()
    {
        return this.getClass().getSimpleName()+"["++"]";
    }
    
    /* ------------------------------------------------------------ */
    
Put user into realm. Called by implementations to put the user data loaded from file/db etc into the user structure.

Parameters:
userName User name
info a UserIdentity instance, or a String password or Credential instance
Returns:
User instance
    protected synchronized UserIdentity putUser(String userNameObject info)
    {
        final UserIdentity identity;
        if (info instanceof UserIdentity)
            identity=(UserIdentity)info;
        else
        {
            Credential credential = (info instanceof Credential)?(Credential)info:Credential.getCredential(info.toString());
            
            Principal userPrincipal = new KnownUser(userName,credential);
            Subject subject = new Subject();
            subject.getPrincipals().add(userPrincipal);
            subject.getPrivateCredentials().add(credential);
            subject.setReadOnly();
            identity=.newUserIdentity(subject,userPrincipal,.);
        }
        
        .put(userName,identity);
        return identity;
    }
    
    /* ------------------------------------------------------------ */
    
Put user into realm.

Parameters:
userName The user to add
credential The users Credentials
roles The users roles
Returns:
UserIdentity
    public synchronized UserIdentity putUser(String userNameCredential credentialString[] roles)
    {
        Principal userPrincipal = new KnownUser(userName,credential);
        Subject subject = new Subject();
        subject.getPrincipals().add(userPrincipal);
        subject.getPrivateCredentials().add(credential);
        
        if (roles!=null)
            for (String role : roles)
                subject.getPrincipals().add(new RolePrincipal(role));
        subject.setReadOnly();
        UserIdentity identity=.newUserIdentity(subject,userPrincipal,roles);
        .put(userName,identity);
        return identity;
    } 
    
    /* ------------------------------------------------------------ */
    public void removeUser(String username)
    {
        .remove(username);
    }   
    /* ------------------------------------------------------------ */
    
    public UserIdentity login(String usernameObject credentials)
    {
        UserIdentity user = .get(username);
        
        if (user==null)
            user = loadUser(username);
        
        if (user!=null)
        {
            UserPrincipal principal = (UserPrincipal)user.getUserPrincipal();
            if (principal.authenticate(credentials))
                return user;
        }
        return null;
    }
    /* ------------------------------------------------------------ */
    public boolean validate(UserIdentity user)
    {
        if (.containsKey(user.getUserPrincipal().getName()))
            return true;
        
        if (loadUser(user.getUserPrincipal().getName())!=null)
            return true;
                
        return false;
    }
    /* ------------------------------------------------------------ */
    protected abstract UserIdentity loadUser(String username);
    
    /* ------------------------------------------------------------ */
    protected abstract void loadUsers() throws IOException;
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    public interface UserPrincipal extends Principal,Serializable
    {
        boolean authenticate(Object credentials);
        public boolean isAuthenticated();
    }
    
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    public static class RolePrincipal implements Principal,Serializable
    {
        private static final long serialVersionUID = 2998397924051854402L;
        private final String _roleName;
        public RolePrincipal(String name)
        {
            =name;
        }
        public String getName()
        {
            return ;
        }
    }
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    public static class Anonymous implements UserPrincipal,Serializable
    {
        private static final long serialVersionUID = 1097640442553284845L;
        public boolean isAuthenticated()
        {
            return false;
        }
        public String getName()
        {
            return "Anonymous";
        }
        
        public boolean authenticate(Object credentials)
        {
            return false;
        }
        
    }
    
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    /* ------------------------------------------------------------ */
    public static class KnownUser implements UserPrincipal,Serializable
    {
        private static final long serialVersionUID = -6226920753748399662L;
        private final String _name;
        private final Credential _credential;
        
        /* -------------------------------------------------------- */
        public KnownUser(String name,Credential credential)
        {
            =name;
            =credential;
        }
        /* -------------------------------------------------------- */
        public boolean authenticate(Object credentials)
        {
            return !=null && .check(credentials);
        }
        
        /* ------------------------------------------------------------ */
        public String getName()
        {
            return ;
        }
        
        /* -------------------------------------------------------- */
        public boolean isAuthenticated()
        {
            return true;
        }
        /* -------------------------------------------------------- */
        @Override
        public String toString()
        {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X