JaasAuthenticator.java :  » Content-Management-System » openedit » com » openedit » users » authenticate » Java Open Source

Java Open Source » Content Management System » openedit 
openedit » com » openedit » users » authenticate » JaasAuthenticator.java
/*
 * Created on Jul 20, 2006
 */
package com.openedit.users.authenticate;

import java.io.IOException;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
import javax.security.sasl.RealmCallback;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.openedit.users.Authenticator;
import com.openedit.users.User;
import com.openedit.users.UserManagerException;

public class JaasAuthenticator implements Authenticator
{
  private static final Log log = LogFactory.getLog(JaasAuthenticator.class);

  public boolean authenticate(User inUser, String inPassword) throws UserManagerException
  {
    // Obtain a LoginContext, needed for authentication. Tell it 
    // to use the LoginModule implementation specified by the 
    // entry named "JaasSample" in the JAAS login configuration 
    // file and to also use the specified CallbackHandler.
    try
    {
      LoginContext loginContext = new LoginContext("OpenEditConfig", new UserPasswordCallbackHandler(inUser, inPassword));
      loginContext.login();

      //       Now we're logged in, so we can get the current subject.
      //Subject subject = loginContext.getSubject();
    }
    catch (LoginException le)
    {
      log.error("Cannot create LoginContext. " + le.getMessage());
      return false;
    }
    catch (SecurityException se)
    {
      log.error("Cannot create LoginContext. " + se.getMessage());
      return false;
    }
    return true;
  }

  public class UserPasswordCallbackHandler implements CallbackHandler
  {
    private User fieldUser;
    private String mPassword;

    /**
     *  We need a stateful handler to return the username and password.
     */
    public UserPasswordCallbackHandler(User username, String password)
    {
      fieldUser = username;
      mPassword = password;
    }

    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException
    {
      for (int ii = 0; ii < callbacks.length; ii++)
      {
        if (callbacks[ii] instanceof NameCallback)
        {
          NameCallback ncb = (NameCallback) callbacks[ii];
          ncb.setName(fieldUser.getUserName());
        }
        else if (callbacks[ii] instanceof PasswordCallback)
        {
          PasswordCallback pcb = (PasswordCallback) callbacks[ii];
          pcb.setPassword(mPassword.toCharArray());
        }
        else if (callbacks[ii] instanceof RealmCallback)
        {
          RealmCallback rcb = (RealmCallback) callbacks[ii];
          String realm = fieldUser.getString("realm");
          if( realm != null)
          {
            rcb.setText(realm);
          }
        }
      }
    }

  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.