8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: LoginContext.java,v 1.2 2008/06/25 05:41:53 qcheng Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.authentication.internal;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.HashMap;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.StringTokenizer;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.security.auth.callback.Callback;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.security.auth.callback.CallbackHandler;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.security.auth.callback.NameCallback;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.security.auth.callback.PasswordCallback;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.security.auth.login.LoginException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.am.util.SystemProperties;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.authentication.util.ISAuthConstants;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class LoginContext {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private AuthSubject subject;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private CallbackHandler cbHandler;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private String organization;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private LoginModule module;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
7ffff39c9d3a5fc02ccf432e74d74418ee5b96fbSachiko Wallace private HashMap sharedState = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public final static String LDAP_AUTH_URL = "ldap://";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public final static String LDAPS_AUTH_URL = "ldaps://";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public final static String ORGNAME = "ORGANIZATION";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public final static String PASSWORD = "PASSWORD";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public final static String AUTH_MODULES = "admin.auth.classname";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public final static String DONOT_INCLUDE_SMS_MODULE =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "admin.auth.donotIncludeSMSModule";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /* Pre-configured login modules */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private final static String LDAP_LOGIN_MODULE =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "com.sun.identity.authentication.internal.server.LocalLdapAuthModule";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private final static String SMS_LOGIN_MODULE =
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "com.sun.identity.authentication.internal.server.SMSAuthModule";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster protected LoginContext(String name, CallbackHandler handler)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws LoginException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster organization = name;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster subject = new AuthSubject();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cbHandler = handler;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster protected LoginContext(String name, AuthSubject subject,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster CallbackHandler handler) throws LoginException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster organization = name;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.subject = subject;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cbHandler = handler;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster protected AuthSubject getSubject() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (subject);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster protected void login() throws LoginException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster HashMap map = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (organization != null)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster map.put(ORGNAME, organization);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Check for DPro auth login module, unless
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // LDAP authenication is explicitly called
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster module = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster LoginModule[] modules = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster modules = getLoginModules();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (Exception e) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (AuthContext.authDebug.warningEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster AuthContext.authDebug.warning("LoginContext::login() "
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + "Got exception while getting auth module", e);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster AuthContext.authDebug.warning("LoginContext::login() "
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + "Using default auth modules");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster modules = getDefaultLoginModules();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (Exception ee) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw (new LoginException(e.getMessage() + "\n"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + ee.getMessage()));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Try each module, stop at first successful auth module
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster LoginException loginException = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean success = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (int i = 0; i < modules.length; i++) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster module = modules[i];
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster module.initialize(subject, cbHandler, sharedState, map);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster success = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster success = module.login();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (LoginException le) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster loginException = le;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (success) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster module.commit();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster break;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster module.abort();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!success && (loginException != null)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // None of the modules were successful
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw (loginException);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Package protected method called by AuthLoginThread to set
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // variables in shared state
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster void updateSharedState(Callback[] callbacks) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (int i = 0; callbacks != null && i < callbacks.length; i++) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (callbacks[i] instanceof NameCallback) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String username = ((NameCallback) callbacks[i]).getName();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (username != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sharedState.put(ISAuthConstants.SHARED_STATE_USERNAME,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster username);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else if (callbacks[i] instanceof PasswordCallback) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster char[] passwd = ((PasswordCallback) callbacks[i]).getPassword();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (passwd != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sharedState.put(ISAuthConstants.SHARED_STATE_PASSWORD,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster new String(passwd));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Package protected method called by AuthContext to set
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // variables in shared state
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster void updateSharedState(String username, char[] passwd) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sharedState.put(ISAuthConstants.SHARED_STATE_USERNAME, username);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sharedState.put(ISAuthConstants.SHARED_STATE_PASSWORD, new String(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster passwd));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster protected LoginModule[] getLoginModules() throws Exception {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster LoginModule[] answer = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String modules = SystemProperties.get(AUTH_MODULES);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (modules == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster answer = getDefaultLoginModules();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (AuthContext.authDebug.messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster AuthContext.authDebug.message("LoginContext:getLoginModules() "
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + "Using default modules");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringTokenizer st = new StringTokenizer(modules, "|");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String donotIncludeSMSModule = SystemProperties
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .get(DONOT_INCLUDE_SMS_MODULE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (donotIncludeSMSModule != null
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster && donotIncludeSMSModule.equalsIgnoreCase("true")) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster answer = new LoginModule[st.countTokens()];
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster answer = new LoginModule[st.countTokens() + 1];
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster answer[answer.length - 1] = (LoginModule) Class.forName(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster SMS_LOGIN_MODULE).newInstance();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (int i = 0; st.hasMoreTokens(); i++) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String moduleClass = st.nextToken();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster answer[i] = (LoginModule) Class.forName(moduleClass)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .newInstance();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (AuthContext.authDebug.messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster AuthContext.authDebug
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .message("LoginContext:getLoginModules() "
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + "Adding module: " + moduleClass);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (answer);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster protected LoginModule[] getDefaultLoginModules() throws Exception {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster LoginModule[] answer = new LoginModule[2];
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster answer[0] = (LoginModule) Class.forName(SMS_LOGIN_MODULE).newInstance();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster answer[1] = (LoginModule) Class.forName(LDAP_LOGIN_MODULE)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .newInstance();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (answer);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster protected void logout() throws LoginException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster module.logout();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}