<commit_before>/*
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id: LoginServiceImpl.java 5032 2006-08-17 18:15:06Z novotny $
 */
package org.gridsphere.services.core.security.auth.impl;

import org.gridsphere.portlet.PortletLog;
import org.gridsphere.portlet.User;
import org.gridsphere.portlet.PortletRequest;
import org.gridsphere.portlet.impl.SportletLog;
import org.gridsphere.portlet.service.PortletServiceUnavailableException;
import org.gridsphere.portlet.service.PortletServiceNotFoundException;
import org.gridsphere.portlet.service.spi.PortletServiceConfig;
import org.gridsphere.portlet.service.spi.PortletServiceProvider;
import org.gridsphere.portlet.service.spi.PortletServiceFactory;
import org.gridsphere.services.core.security.auth.AuthorizationException;
import org.gridsphere.services.core.security.auth.AuthenticationException;
import org.gridsphere.services.core.security.auth.LoginUserModule;
import org.gridsphere.services.core.security.auth.modules.LoginAuthModule;
import org.gridsphere.services.core.security.auth.modules.impl.descriptor.AuthModulesDescriptor;
import org.gridsphere.services.core.security.auth.modules.impl.descriptor.AuthModuleCollection;
import org.gridsphere.services.core.security.auth.modules.impl.descriptor.AuthModuleDefinition;
import org.gridsphere.services.core.security.auth.LoginService;
import org.gridsphere.services.core.user.UserManagerService;
import org.gridsphere.services.core.persistence.PersistenceManagerRdbms;
import org.gridsphere.services.core.persistence.PersistenceManagerException;
import org.gridsphere.services.core.persistence.PersistenceManagerService;

import java.lang.reflect.Constructor;
import java.util.*;
import java.security.cert.X509Certificate;
import java.net.URL;

/**
 * The <code>LoginService</code> is the primary interface that defines the login method used to obtain a
 * <code>User</code> from a username and password. The <code>LoginService</code> is configured
 * dynamically at run-time with login authorization modules. By default the PASSWORD_AUTH_MODULE is
 * selected which uses the GridSphere database to store passwords. Other authorization modules
 * can use external directory servers such as LDAP, etc
 */
public class LoginServiceImpl implements LoginService, PortletServiceProvider {

    private PortletLog log = SportletLog.getInstance(LoginServiceImpl.class);
    private UserManagerService userManagerService = null;
    private boolean inited = false;
    private List authModules = new ArrayList();
    private LoginUserModule activeLoginModule = null;

    private PersistenceManagerRdbms pm = null;

    private URL authMappingStream = getClass().getResource("/org/gridsphere/services/core/security/auth/modules/impl/descriptor/auth-modules-mapping.xml");


    public LoginServiceImpl() {
    }

    public List<LoginAuthModule> getAuthModules() {
        return authModules;
    }

    public List<LoginAuthModule> getActiveAuthModules() {
        List activeMods = new ArrayList();
        Iterator it = authModules.iterator();
        while (it.hasNext()) {
            LoginAuthModule authModule = (LoginAuthModule)it.next();
            if (authModule.isModuleActive()) activeMods.add(authModule);
        }
        return activeMods;
    }

    public LoginUserModule getActiveLoginModule() {
        return activeLoginModule;
    }

    public void setActiveLoginModule(LoginUserModule loginModule) {
        activeLoginModule = loginModule;
    }

    /**
     * Initializes the portlet service.
     * The init method is invoked by the portlet container immediately after a portlet service has
     * been instantiated and before it is passed to the requestor.
     *
     * @param config the service configuration
     * @throws PortletServiceUnavailableException
     *          if an error occurs during initialization
     */
    public void init(PortletServiceConfig config) throws PortletServiceUnavailableException {
        log.debug("in login service init");
        if (!inited) {
            PersistenceManagerService pmservice = (PersistenceManagerService)PortletServiceFactory.createPortletService(PersistenceManagerService.class, true);
            pm = pmservice.createGridSphereRdbms();
            String loginClassName = config.getInitParameter("LOGIN_MODULE");
            try {
                Class loginModClass = Class.forName(loginClassName);
                activeLoginModule = (LoginUserModule)PortletServiceFactory.createPortletService(loginModClass, true);
            } catch (ClassNotFoundException e) {
                log.error("Unable to create class from class name: " + loginClassName, e);
            } catch (PortletServiceNotFoundException e) {
                log.error("Unable to get service from portlet service factory: " + loginClassName, e);
            }
            log.debug("Created a login module service: " + loginClassName);
            String authModulesPath = config.getServletContext().getRealPath("/WEB-INF/authmodules.xml");
            loadAuthModules(authModulesPath, Thread.currentThread().getContextClassLoader());
            userManagerService = (UserManagerService)PortletServiceFactory.createPortletService(UserManagerService.class, true);
            inited = true;
        }
    }

    public void loadAuthModules(String authModsPath, ClassLoader classloader) {

        AuthModulesDescriptor desc;
        try {
            desc = new AuthModulesDescriptor(authModsPath, authMappingStream);

            AuthModuleCollection coll = desc.getCollection();
            List modList = coll.getAuthModulesList();
            Iterator it = modList.iterator();
            log.info("loading auth modules:");<commit_msg>this was loading in auth modules twice<commit_after>/*
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id: LoginServiceImpl.java 5032 2006-08-17 18:15:06Z novotny $
 */
package org.gridsphere.services.core.security.auth.impl;

import org.gridsphere.portlet.PortletLog;
import org.gridsphere.portlet.User;
import org.gridsphere.portlet.PortletRequest;
import org.gridsphere.portlet.impl.SportletLog;
import org.gridsphere.portlet.service.PortletServiceUnavailableException;
import org.gridsphere.portlet.service.PortletServiceNotFoundException;
import org.gridsphere.portlet.service.spi.PortletServiceConfig;
import org.gridsphere.portlet.service.spi.PortletServiceProvider;
import org.gridsphere.portlet.service.spi.PortletServiceFactory;
import org.gridsphere.services.core.security.auth.AuthorizationException;
import org.gridsphere.services.core.security.auth.AuthenticationException;
import org.gridsphere.services.core.security.auth.LoginUserModule;
import org.gridsphere.services.core.security.auth.modules.LoginAuthModule;
import org.gridsphere.services.core.security.auth.modules.impl.descriptor.AuthModulesDescriptor;
import org.gridsphere.services.core.security.auth.modules.impl.descriptor.AuthModuleCollection;
import org.gridsphere.services.core.security.auth.modules.impl.descriptor.AuthModuleDefinition;
import org.gridsphere.services.core.security.auth.LoginService;
import org.gridsphere.services.core.user.UserManagerService;
import org.gridsphere.services.core.persistence.PersistenceManagerRdbms;
import org.gridsphere.services.core.persistence.PersistenceManagerException;
import org.gridsphere.services.core.persistence.PersistenceManagerService;

import java.lang.reflect.Constructor;
import java.util.*;
import java.security.cert.X509Certificate;
import java.net.URL;

/**
 * The <code>LoginService</code> is the primary interface that defines the login method used to obtain a
 * <code>User</code> from a username and password. The <code>LoginService</code> is configured
 * dynamically at run-time with login authorization modules. By default the PASSWORD_AUTH_MODULE is
 * selected which uses the GridSphere database to store passwords. Other authorization modules
 * can use external directory servers such as LDAP, etc
 */
public class LoginServiceImpl implements LoginService, PortletServiceProvider {

    private PortletLog log = SportletLog.getInstance(LoginServiceImpl.class);
    private UserManagerService userManagerService = null;
    private boolean inited = false;
    private List authModules = new ArrayList();
    private LoginUserModule activeLoginModule = null;

    private PersistenceManagerRdbms pm = null;

    private URL authMappingStream = getClass().getResource("/org/gridsphere/services/core/security/auth/modules/impl/descriptor/auth-modules-mapping.xml");


    public LoginServiceImpl() {
    }

    public List<LoginAuthModule> getAuthModules() {
        return authModules;
    }

    public List<LoginAuthModule> getActiveAuthModules() {
        List activeMods = new ArrayList();
        Iterator it = authModules.iterator();
        while (it.hasNext()) {
            LoginAuthModule authModule = (LoginAuthModule)it.next();
            if (authModule.isModuleActive()) activeMods.add(authModule);
        }
        return activeMods;
    }

    public LoginUserModule getActiveLoginModule() {
        return activeLoginModule;
    }

    public void setActiveLoginModule(LoginUserModule loginModule) {
        activeLoginModule = loginModule;
    }

    /**
     * Initializes the portlet service.
     * The init method is invoked by the portlet container immediately after a portlet service has
     * been instantiated and before it is passed to the requestor.
     *
     * @param config the service configuration
     * @throws PortletServiceUnavailableException
     *          if an error occurs during initialization
     */
    public void init(PortletServiceConfig config) throws PortletServiceUnavailableException {
        log.debug("in login service init");
        if (!inited) {
            PersistenceManagerService pmservice = (PersistenceManagerService)PortletServiceFactory.createPortletService(PersistenceManagerService.class, true);
            pm = pmservice.createGridSphereRdbms();
            String loginClassName = config.getInitParameter("LOGIN_MODULE");
            try {
                Class loginModClass = Class.forName(loginClassName);
                activeLoginModule = (LoginUserModule)PortletServiceFactory.createPortletService(loginModClass, true);
            } catch (ClassNotFoundException e) {
                log.error("Unable to create class from class name: " + loginClassName, e);
            } catch (PortletServiceNotFoundException e) {
                log.error("Unable to get service from portlet service factory: " + loginClassName, e);
            }
            log.debug("Created a login module service: " + loginClassName);
            //String authModulesPath = config.getServletContext().getRealPath("/WEB-INF/authmodules.xml");
            //loadAuthModules(authModulesPath, Thread.currentThread().getContextClassLoader());
            userManagerService = (UserManagerService)PortletServiceFactory.createPortletService(UserManagerService.class, true);
            inited = true;
        }
    }

    public void loadAuthModules(String authModsPath, ClassLoader classloader) {

        AuthModulesDescriptor desc;
        try {
            desc = new AuthModulesDescriptor(authModsPath, authMappingStream);

            AuthModuleCollection coll = desc.getCollection();
            List modList = coll.getAuthModulesList();
            Iterator it = modList.iterator();
            log.info("loading auth modules:");
{'fraction_non_alphanumeric': 0.08597087801900775, 'fraction_numerical': 0.0036620455139942452, 'mean_word_length': 3.9589277993947256, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7662206', 'n_tokens_mistral': 3003, 'n_tokens_neox': 2914, 'n_words': 665}