Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.31 KB | None | 0 0
  1.  
  2. package la.oarf.componente.openam.login;
  3.  
  4. import java.security.Principal;
  5. import java.util.Map;
  6. import javax.security.auth.Subject;
  7. import javax.security.auth.callback.Callback;
  8. import javax.security.auth.callback.NameCallback;
  9. import javax.security.auth.callback.PasswordCallback;
  10. import javax.security.auth.login.LoginException;
  11. import weblogic.security.principal.WLSGroupImpl;
  12. import com.sun.identity.authentication.spi.AMLoginModule;
  13. import com.sun.identity.authentication.util.ISAuthConstants;
  14. import com.sun.identity.shared.debug.Debug;
  15.  
  16. public class MyCustomAuth extends AMLoginModule
  17. {
  18.    private String userLogin;
  19.    private Subject subject;
  20.    private final static Debug debug = Debug.getInstance("MyCustomAuth");
  21.  
  22.    public MyCustomAuth()
  23.    {
  24.       super();
  25.    }
  26.    
  27.    @Override
  28.    public Principal getPrincipal()
  29.    {
  30.       return criaPrincipalUsuario(userLogin);
  31.    }
  32.  
  33.    @SuppressWarnings("rawtypes")
  34.    @Override
  35.    public void init(Subject subject, Map sharedState, Map options)
  36.    {
  37.       if (debug.messageEnabled())
  38.       {
  39.           debug.message("MyCustomAuth::init");
  40.       }
  41.      
  42.       this.subject = subject;
  43.    }
  44.  
  45.    @Override
  46.    public int process(Callback[] callbacks, int state) throws LoginException
  47.    {
  48.       NameCallback nameCB = (NameCallback) callbacks[0];
  49.       PasswordCallback passwordCB = (PasswordCallback) callbacks[1];
  50.  
  51.       String user = nameCB.getName();
  52.       String senha = new String(passwordCB.getPassword());
  53.  
  54.       if (debug.messageEnabled())
  55.       {
  56.           debug.message("MyCustomAuth::process user: " + user + " password: " + senha);
  57.       }
  58.  
  59.       // TODO fazer autenticacao no LDAP
  60.  
  61.       this.userLogin = user;
  62.  
  63.       return ISAuthConstants.LOGIN_SUCCEED;
  64.  
  65.       // throw new InvalidPasswordException("Senha inválida");
  66.    }
  67.  
  68.    private BRBAuthPrincipal criaPrincipalUsuario(String userName)
  69.    {
  70.       // TODO recuperar grupos do BD
  71.       subject.getPrincipals().add(new WLSGroupImpl("my-group-1"));
  72.       subject.getPrincipals().add(new WLSGroupImpl("my-group-2"));
  73.       subject.getPrincipals().add(new WLSGroupImpl("my-group-3"));
  74.      
  75.       if (debug.messageEnabled())
  76.       {
  77.           debug.message("MyCustomAuth::get principal user: " + userName);
  78.       }
  79.  
  80.       return new BRBAuthPrincipal(userLogin);
  81.    }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement