Search in sources :

Example 1 with IndexType

use of com.sun.identity.authentication.AuthContext.IndexType in project OpenAM by OpenRock.

the class AMLoginContext method executeLogin.

/**
     * Starts login process, the map passed to this method is the parameters
     * required to start the login process. These parameters are
     * <code>indexType</code>, <code>indexName</code> , <code>principal</code>,
     * <code>subject</code>, <code>password</code>,
     * <code>organization name</code>. Based on these parameters Module
     * Configuration name is retrieved using Configuration component. Creates
     * a new LoginContext and starts login process and returns. On error
     * LoginException is thrown.
     *
     * @param loginParamsMap login parameters HashMap
     * @throws AuthLoginException if execute login fails
     */
public void executeLogin(Map<String, Object> loginParamsMap) throws AuthLoginException {
    boolean errorState = false;
    internalAuthError = false;
    processDone = false;
    isFailed = false;
    setLoginHash();
    /*
         * Ensure loginState created and loginParamsMap provided
         */
    if (loginState == null || loginParamsMap == null) {
        debug.error("Error: loginState or loginParams is null");
        loginStatus.setStatus(LoginStatus.AUTH_FAILED);
        if (loginState != null) {
            loginState.setErrorCode(AMAuthErrorCode.AUTH_ERROR);
        }
        setErrorMsgAndTemplate();
        internalAuthError = true;
        throw new AuthLoginException(BUNDLE_NAME, AMAuthErrorCode.AUTH_ERROR, null);
    }
    /*
         * Lookup resource bundle and locale specific settings based on locale associated with LoginState
         */
    java.util.Locale loginLocale = com.sun.identity.shared.locale.Locale.getLocale(loginState.getLocale());
    bundle = AMResourceBundleCache.getInstance().getResBundle(BUNDLE_NAME, loginLocale);
    exceedRetryLimit = AMResourceBundleCache.getInstance().getResBundle("amAuthLDAP", loginLocale).getString(ISAuthConstants.EXCEED_RETRY_LIMIT);
    if (debug.messageEnabled()) {
        debug.message("LoginState : " + loginState);
    }
    /*
         * Handle redirection if applicable
         */
    String redirectUrl = (String) loginParamsMap.get(AuthContextLocal.REDIRECT_URL);
    if (redirectUrl != null) {
        // Resource/IP/Env based auth case with Redirection Advice
        Callback[] redirectCallback = new Callback[1];
        redirectCallback[0] = new RedirectCallback(redirectUrl, null, "GET");
        if (isPureJAAS()) {
            loginState.setReceivedCallback_NoThread(redirectCallback);
        } else {
            loginState.setReceivedCallback(redirectCallback, this);
        }
        return;
    }
    /*
         * Initialize instance fields from loginParamsMap
         */
    parseLoginParams(loginParamsMap);
    /*
         * Copy orgDN and clientType values from LoginState
         */
    if (authContext.getOrgDN() != null && !authContext.getOrgDN().isEmpty()) {
        orgDN = authContext.getOrgDN();
        loginState.setQualifiedOrgDN(orgDN);
    } else {
        orgDN = loginState.getOrgDN();
    }
    clientType = loginState.getClientType();
    if (debug.messageEnabled()) {
        debug.message("orgDN : " + orgDN);
        debug.message("clientType : " + clientType);
    }
    /*
         * Throw an exception if module-based authentication is disabled and an authentication module other
         * than APPLICATION_MODULE or FEDERATION_MODULE is explicitly requested.
         */
    if (indexType == IndexType.MODULE_INSTANCE && !loginState.getEnableModuleBasedAuth() && !indexName.equals(ISAuthConstants.APPLICATION_MODULE)) {
        String moduleClassName = null;
        try {
            AMAuthenticationManager authManager = new AMAuthenticationManager(AccessController.doPrivileged(AdminTokenAction.getInstance()), orgDN);
            AMAuthenticationInstance authInstance = authManager.getAuthenticationInstance(indexName);
            moduleClassName = authInstance.getType();
        } catch (AMConfigurationException amce) {
            debug.error("AMLoginContext.executeLogin(): Unable to get authentication config", amce);
        }
        if (moduleClassName != null && !moduleClassName.equalsIgnoreCase(ISAuthConstants.FEDERATION_MODULE)) {
            throwExceptionIfModuleBasedAuthenticationDisabled();
        }
    }
    /*
         * Update LoginState indexType and indexName
         * (after storing current loginState indexType if required for HTTP callback processing)
         */
    IndexType prevIndexType = loginState.getIndexType();
    if (prevIndexType == IndexType.LEVEL || prevIndexType == IndexType.COMPOSITE_ADVICE) {
        loginState.setPreviousIndexType(prevIndexType);
    }
    loginState.setIndexType(indexType);
    loginState.setIndexName(indexName);
    /*
         * Delegate actual processing of requested authentication type to the dispatch method 'processIndexType'
         */
    try {
        if (processIndexType(indexType, indexName, orgDN)) {
            return;
        }
    } catch (AuthLoginException le) {
        if (MonitoringUtil.isRunning()) {
            if (authImpl == null) {
                authImpl = Agent.getAuthSvcMBean();
            }
            if (authImpl != null) {
                authImpl.incSsoServerAuthenticationFailureCount();
            }
        }
        debug.message("Error  : ", le);
        throw le;
    } catch (Exception e) {
        if (MonitoringUtil.isRunning()) {
            if (authImpl == null) {
                authImpl = Agent.getAuthSvcMBean();
            }
            if (authImpl != null) {
                authImpl.incSsoServerAuthenticationFailureCount();
            }
        }
        debug.message("Error : ", e);
        throw new AuthLoginException(e);
    }
    /*
         * Establish configName based on indexType, indexName, orgDN and clientType
         *
         * If configName can't be established, throw an exception
         */
    configName = getConfigName(indexType, indexName, orgDN, clientType);
    if (configName == null) {
        loginState.setErrorCode(AMAuthErrorCode.AUTH_CONFIG_NOT_FOUND);
        debug.message("Config not found");
        setErrorMsgAndTemplate();
        internalAuthError = true;
        loginStatus.setStatus(LoginStatus.AUTH_FAILED);
        loginState.logFailed(bundle.getString("noConfig"), "NOCONFIG");
        auditor.auditLoginFailure(loginState, NO_CONFIG);
        if (MonitoringUtil.isRunning()) {
            if (authImpl == null) {
                authImpl = Agent.getAuthSvcMBean();
            }
            if (authImpl != null) {
                authImpl.incSsoServerAuthenticationFailureCount();
            }
        }
        throw new AuthLoginException(BUNDLE_NAME, AMAuthErrorCode.AUTH_CONFIG_NOT_FOUND, null);
    }
    /*
         * Create the LoginContext object that actually handles login/logout
         */
    if (debug.messageEnabled()) {
        debug.message("Creating login context object\n" + "\n orgDN : " + orgDN + "\n configName : " + configName);
    }
    try {
        jaasCheck = AuthUtils.isPureJAASModulePresent(configName, this);
        if (isPureJAAS()) {
            debug.message("Using pure jaas mode.");
            if (authThread == null) {
                authThread = new AuthThreadManager();
                authThread.start();
            }
        }
        DSAMECallbackHandler dsameCallbackHandler = new DSAMECallbackHandler(this);
        if (isPureJAAS()) {
            if (subject != null) {
                loginContext = new javax.security.auth.login.LoginContext(configName, subject, dsameCallbackHandler);
            } else {
                loginContext = new javax.security.auth.login.LoginContext(configName, dsameCallbackHandler);
            }
        } else {
            debug.message("Using non pure jaas mode.");
            if (subject != null) {
                jaasLoginContext = new com.sun.identity.authentication.jaas.LoginContext(entries, subject, dsameCallbackHandler);
            } else {
                jaasLoginContext = new com.sun.identity.authentication.jaas.LoginContext(entries, dsameCallbackHandler);
            }
        }
    } catch (AuthLoginException ae) {
        debug.error("JAAS module for config: " + configName + ", " + ae.getMessage());
        if (debug.messageEnabled()) {
            debug.message("AuthLoginException", ae);
        }
        /* The user based authentication errors should not be different
             * for users who exist and who don't, which can lead to
             * possiblity of enumerating existing users.
             * The AMAuthErrorCode.AUTH_LOGIN_FAILED error code is used for
             * all user based authentication errors.
             * Refer issue3278
             */
        if (indexType == IndexType.USER && AMAuthErrorCode.AUTH_CONFIG_NOT_FOUND.equals(ae.getErrorCode())) {
            loginState.setErrorCode(AMAuthErrorCode.AUTH_LOGIN_FAILED);
        } else {
            loginState.setErrorCode(ae.getErrorCode());
        }
        setErrorMsgAndTemplate();
        loginState.logFailed(bundle.getString("loginContextCreateFailed"));
        auditor.auditLoginFailure(loginState);
        internalAuthError = true;
        loginStatus.setStatus(LoginStatus.AUTH_FAILED);
        if (MonitoringUtil.isRunning()) {
            if (authImpl == null) {
                authImpl = Agent.getAuthSvcMBean();
            }
            if (authImpl != null) {
                authImpl.incSsoServerAuthenticationFailureCount();
            }
        }
        throw ae;
    } catch (LoginException le) {
        debug.error("in creating LoginContext.");
        if (debug.messageEnabled()) {
            debug.message("Exception ", le);
        }
        loginState.setErrorCode(AMAuthErrorCode.AUTH_ERROR);
        loginState.logFailed(bundle.getString("loginContextCreateFailed"));
        auditor.auditLoginFailure(loginState);
        setErrorMsgAndTemplate();
        loginStatus.setStatus(LoginStatus.AUTH_FAILED);
        internalAuthError = true;
        if (MonitoringUtil.isRunning()) {
            if (authImpl == null) {
                authImpl = Agent.getAuthSvcMBean();
            }
            if (authImpl != null) {
                authImpl.incSsoServerAuthenticationFailureCount();
            }
        }
        throw new AuthLoginException(BUNDLE_NAME, AMAuthErrorCode.AUTH_ERROR, null, le);
    } catch (SecurityException se) {
        debug.error("security in creating LoginContext.");
        if (debug.messageEnabled()) {
            debug.message("Exception ", se);
        }
        loginState.setErrorCode(AMAuthErrorCode.AUTH_ERROR);
        setErrorMsgAndTemplate();
        loginState.logFailed(bundle.getString("loginContextCreateFailed"));
        auditor.auditLoginFailure(loginState);
        internalAuthError = true;
        loginStatus.setStatus(LoginStatus.AUTH_FAILED);
        if (MonitoringUtil.isRunning()) {
            if (authImpl == null) {
                authImpl = Agent.getAuthSvcMBean();
            }
            if (authImpl != null) {
                authImpl.incSsoServerAuthenticationFailureCount();
            }
        }
        throw new AuthLoginException(BUNDLE_NAME, AMAuthErrorCode.AUTH_ERROR, null);
    } catch (Exception e) {
        debug.error("Creating DSAMECallbackHandler: " + e.getMessage());
        loginState.setErrorCode(AMAuthErrorCode.AUTH_ERROR);
        setErrorMsgAndTemplate();
        loginState.logFailed(bundle.getString("loginContextCreateFailed"));
        auditor.auditLoginFailure(loginState);
        internalAuthError = true;
        if (MonitoringUtil.isRunning()) {
            if (authImpl == null) {
                authImpl = Agent.getAuthSvcMBean();
            }
            if (authImpl != null) {
                authImpl.incSsoServerAuthenticationFailureCount();
            }
        }
        loginStatus.setStatus(LoginStatus.AUTH_FAILED);
        throw new AuthLoginException(BUNDLE_NAME, AMAuthErrorCode.AUTH_ERROR, null, e);
    }
    /*
         * Perform the login using the objects this method has setup
         */
    try {
        if (isPureJAAS()) {
            if (jaasThread != null) {
                jaasThread.interrupt();
                jaasThread = null;
                errorState = true;
            } else {
                jaasThread = new JAASLoginThread(this);
                jaasThread.start();
            }
        } else {
            runLogin();
        }
    } catch (IllegalThreadStateException ite) {
        errorState = true;
    } catch (Exception e) {
        errorState = true;
    }
    if (errorState) {
        loginStatus.setStatus(LoginStatus.AUTH_RESET);
        loginState.setErrorCode(AMAuthErrorCode.AUTH_ERROR);
        setErrorMsgAndTemplate();
        internalAuthError = true;
        if (MonitoringUtil.isRunning()) {
            if (authImpl == null) {
                authImpl = Agent.getAuthSvcMBean();
            }
            if (authImpl != null) {
                authImpl.incSsoServerAuthenticationFailureCount();
            }
        }
        throw new AuthLoginException(BUNDLE_NAME, AMAuthErrorCode.AUTH_ERROR, null);
    }
    debug.message("AMLoginContext:Thread started... returning.");
}
Also used : AMConfigurationException(com.sun.identity.authentication.config.AMConfigurationException) IndexType(com.sun.identity.authentication.AuthContext.IndexType) AMAuthenticationManager(com.sun.identity.authentication.config.AMAuthenticationManager) RedirectCallback(com.sun.identity.authentication.spi.RedirectCallback) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) LoginException(javax.security.auth.login.LoginException) MessageLoginException(com.sun.identity.authentication.spi.MessageLoginException) AuthErrorCodeException(com.sun.identity.authentication.spi.AuthErrorCodeException) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) InvalidPasswordException(com.sun.identity.authentication.spi.InvalidPasswordException) SSOException(com.iplanet.sso.SSOException) AMConfigurationException(com.sun.identity.authentication.config.AMConfigurationException) RedirectCallback(com.sun.identity.authentication.spi.RedirectCallback) Callback(javax.security.auth.callback.Callback) LoginException(javax.security.auth.login.LoginException) MessageLoginException(com.sun.identity.authentication.spi.MessageLoginException) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) AMAuthenticationInstance(com.sun.identity.authentication.config.AMAuthenticationInstance)

Example 2 with IndexType

use of com.sun.identity.authentication.AuthContext.IndexType in project OpenAM by OpenRock.

the class AMLoginContext method processIndexType.

/* do the required process for different indextypes
     * return true if needs to return back
     * false if needs to continue
     * Exception if error
     */
boolean processIndexType(IndexType indexType, String indexName, String orgDN) throws AuthLoginException {
    boolean ignoreProfile = false;
    IndexType previousType = loginState.getPreviousIndexType();
    /*
         * Throw an exception if org specified in query does not match org specified in authContext/loginState
         *
         * (unless previous index type was LEVEL or COMPOSITE_ADVICE, or current index type is MODULE_INSTANCE)
         */
    String normOrgDN = DNUtils.normalizeDN(orgDN);
    if ((previousType != IndexType.LEVEL && previousType != IndexType.COMPOSITE_ADVICE) || indexType != IndexType.MODULE_INSTANCE) {
        // proceed only when the org in the auth context matches
        // that in the query. otherwise it means a call with a new org.
        HttpServletRequest hreq = loginState.getHttpServletRequest();
        boolean isTokenValid = false;
        final boolean isFederation = indexType == IndexType.MODULE_INSTANCE && ISAuthConstants.FEDERATION_MODULE.equals(indexName);
        if (hreq != null && !isFederation) {
            try {
                SSOTokenManager manager = SSOTokenManager.getInstance();
                SSOToken ssoToken = manager.createSSOToken(hreq);
                if (manager.isValidToken(ssoToken)) {
                    debug.message("Existing Valid session");
                    isTokenValid = true;
                }
            } catch (Exception e) {
                debug.message("ERROR processIndexType/SSOToken validation - " + e.toString());
            }
            if (!isTokenValid) {
                debug.message("No existing valid session");
                Hashtable requestHash = loginState.getRequestParamHash();
                String newOrgDN = AuthUtils.getDomainNameByRequest(hreq, requestHash);
                if (debug.messageEnabled()) {
                    debug.message("orgDN from existing auth context: " + orgDN + ", orgDN from query string: " + newOrgDN);
                }
                if (normOrgDN != null) {
                    if (!normOrgDN.equals(newOrgDN)) {
                        loginStatus.setStatus(LoginStatus.AUTH_RESET);
                        loginState.setErrorCode(AMAuthErrorCode.AUTH_ERROR);
                        setErrorMsgAndTemplate();
                        internalAuthError = true;
                        throw new AuthLoginException(BUNDLE_NAME, AMAuthErrorCode.AUTH_ERROR, null);
                    }
                }
            }
        }
    }
    if (indexType == IndexType.COMPOSITE_ADVICE) {
        /*
             * Configure login following COMPOSITE_ADVICE
             */
        debug.message("IndexType is COMPOSITE_ADVICE");
        // Set the Composite Advice in Login State after decoding
        String compositeAdvice = URLEncDec.decode(indexName);
        loginState.setCompositeAdvice(compositeAdvice);
        // else continue with login process
        try {
            if (processCompositeAdvice(indexType, indexName, orgDN, clientType)) {
                debug.message("multiple modules found");
                return true;
            } else {
                return false;
            }
        } catch (AuthException ae) {
            // no modules configured
            loginState.setErrorCode(ae.getErrorCode());
            loginState.logFailed(ae.getMessage());
            auditor.auditLoginFailure(loginState);
            setErrorMsgAndTemplate();
            loginStatus.setStatus(LoginStatus.AUTH_FAILED);
            throw new AuthLoginException(ae);
        }
    } else if (indexType == IndexType.LEVEL) {
        /*
             * Configure login so that successful authentication achieve specified authentication LEVEL
             */
        debug.message("IndexType is level");
        // else continue with login process
        try {
            if (processLevel(indexType, indexName, orgDN, clientType)) {
                debug.message("multiple modules found");
                return true;
            } else {
                return false;
            }
        } catch (AuthException ae) {
            // no modules configured
            loginState.setErrorCode(ae.getErrorCode());
            loginState.logFailed(ae.getMessage());
            auditor.auditLoginFailure(loginState);
            setErrorMsgAndTemplate();
            loginStatus.setStatus(LoginStatus.AUTH_FAILED);
            throw new AuthLoginException(ae);
        }
    } else if (indexType == IndexType.USER) {
        /*
             * Configure login for specified user
             */
        debug.message("IndexType is user");
        // if user is not active throw exception
        // else continue with login
        boolean userValid = false;
        if (!loginState.ignoreProfile()) {
            userValid = validateUser(indexName);
        } else {
            ignoreProfile = true;
        }
        if ((!userValid) && (!ignoreProfile)) {
            debug.message("User is not active");
            loginState.logFailed(bundle.getString("userInactive"), "USERINACTIVE");
            auditor.auditLoginFailure(loginState, USER_INACTIVE);
            /* The user based authentication errors should not be different
                 * for users who exist and who don't, which can lead to
                 * possibility of enumerating existing users.
                 * The AMAuthErrorCode.AUTH_LOGIN_FAILED error code is used for
                 * all user based authentication errors.
                 * Refer issue3278
                 */
            loginState.setErrorCode(AMAuthErrorCode.AUTH_LOGIN_FAILED);
            setErrorMsgAndTemplate();
            //destroySession();
            loginStatus.setStatus(LoginStatus.AUTH_FAILED);
            throw new AuthLoginException(BUNDLE_NAME, AMAuthErrorCode.AUTH_USER_INACTIVE, null);
        } else if (ignoreProfile) {
            setAuthError(AMAuthErrorCode.AUTH_PROFILE_ERROR, "loginDenied");
            throw new AuthLoginException(BUNDLE_NAME, AMAuthErrorCode.AUTH_PROFILE_ERROR, null);
        } else {
            return false;
        }
    } else if (indexType == IndexType.MODULE_INSTANCE) {
        /*
             * Configure login for specified authentication module
             */
        // check if module exists in the allowed modules list
        debug.message("indexType is module");
        boolean instanceExists = loginState.getDomainAuthenticators().contains(indexName);
        if (!indexName.equals(ISAuthConstants.APPLICATION_MODULE) && !instanceExists) {
            debug.message("Module denied!!");
            loginState.setErrorCode(AMAuthErrorCode.AUTH_MODULE_DENIED);
            loginState.logFailed(bundle.getString("moduleDenied"), "MODULEDENIED");
            auditor.auditLoginFailure(loginState, MODULE_DENIED);
            setErrorMsgAndTemplate();
            loginStatus.setStatus(LoginStatus.AUTH_FAILED);
            throw new AuthLoginException(BUNDLE_NAME, AMAuthErrorCode.AUTH_MODULE_DENIED, null);
        } else {
            return false;
        }
    } else if (indexType == IndexType.ROLE) {
        /*
             * Configure login for specified role - No longer supported, throw an exception
             */
        debug.message("indexType is Role");
        if (loginState.ignoreProfile()) {
            setAuthError(AMAuthErrorCode.AUTH_TYPE_DENIED, "loginDenied");
            throw new AuthLoginException(BUNDLE_NAME, AMAuthErrorCode.AUTH_TYPE_DENIED, null);
        }
    }
    /*
         * IndexType not processed by this method
         */
    return false;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SSOTokenManager(com.iplanet.sso.SSOTokenManager) SSOToken(com.iplanet.sso.SSOToken) Hashtable(java.util.Hashtable) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) IndexType(com.sun.identity.authentication.AuthContext.IndexType) LoginException(javax.security.auth.login.LoginException) MessageLoginException(com.sun.identity.authentication.spi.MessageLoginException) AuthErrorCodeException(com.sun.identity.authentication.spi.AuthErrorCodeException) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) InvalidPasswordException(com.sun.identity.authentication.spi.InvalidPasswordException) SSOException(com.iplanet.sso.SSOException) AMConfigurationException(com.sun.identity.authentication.config.AMConfigurationException)

Aggregations

SSOException (com.iplanet.sso.SSOException)2 IndexType (com.sun.identity.authentication.AuthContext.IndexType)2 AMConfigurationException (com.sun.identity.authentication.config.AMConfigurationException)2 AuthErrorCodeException (com.sun.identity.authentication.spi.AuthErrorCodeException)2 AuthLoginException (com.sun.identity.authentication.spi.AuthLoginException)2 InvalidPasswordException (com.sun.identity.authentication.spi.InvalidPasswordException)2 MessageLoginException (com.sun.identity.authentication.spi.MessageLoginException)2 LoginException (javax.security.auth.login.LoginException)2 SSOToken (com.iplanet.sso.SSOToken)1 SSOTokenManager (com.iplanet.sso.SSOTokenManager)1 AMAuthenticationInstance (com.sun.identity.authentication.config.AMAuthenticationInstance)1 AMAuthenticationManager (com.sun.identity.authentication.config.AMAuthenticationManager)1 RedirectCallback (com.sun.identity.authentication.spi.RedirectCallback)1 Hashtable (java.util.Hashtable)1 Callback (javax.security.auth.callback.Callback)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1