<commit_before>/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2003-2011 The eXist Project
 *  http://exist-db.org
 *  
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  $Id$
 */
package org.exist.security.internal;

import org.exist.security.AbstractRealm;
import org.exist.security.AbstractAccount;
import org.apache.log4j.Logger;
import org.exist.config.Configuration;
import org.exist.config.ConfigurationException;
import org.exist.config.Configurator;
import org.exist.config.annotation.ConfigurationClass;
import org.exist.config.annotation.ConfigurationFieldAsElement;
import org.exist.security.Group;
import org.exist.security.PermissionDeniedException;
import org.exist.security.SchemaType;
import org.exist.security.SecurityManager;
import org.exist.security.Subject;
import org.exist.security.Account;
import org.exist.security.internal.aider.UserAider;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import org.exist.storage.DBBroker;

/**
 * Represents a user within the database.
 * 
 * @author <NAME> <<EMAIL>>
 * @author {Marco.Tampucci, Massimo.Martinelli} @<EMAIL>
 */
@ConfigurationClass("account")
public class AccountImpl extends AbstractAccount {

    private final static Logger LOG = Logger.getLogger(AccountImpl.class);
    public static boolean CHECK_PASSWORDS = true;

    private final static SecurityProperties securityProperties = new SecurityProperties();

    public static SecurityProperties getSecurityProperties() {
        return securityProperties;
    }

    /*
    static {
        Properties props = new Properties();
        try {
            props.load(AccountImpl.class.getClassLoader().getResourceAsStream(
                    "org/exist/security/security.properties"));
        } catch(IOException e) {
        }
        String option = props.getProperty("passwords.encoding", "md5");
        setPasswordEncoding(option);
        option = props.getProperty("passwords.check", "yes");
        CHECK_PASSWORDS = option.equalsIgnoreCase("yes")
                || option.equalsIgnoreCase("true");
    }

    static public void enablePasswordChecks(boolean check) {
        CHECK_PASSWORDS = check;
    }

    static public void setPasswordEncoding(String encoding) {
        if(encoding != null) {
            LOG.equals("Setting password encoding to " + encoding);
            if(encoding.equalsIgnoreCase("plain")) {
                PASSWORD_ENCODING = PLAIN_ENCODING;
            } else if(encoding.equalsIgnoreCase("md5")) {
                PASSWORD_ENCODING = MD5_ENCODING;
            } else {
                PASSWORD_ENCODING = SIMPLE_MD5_ENCODING;
            }
        }
    }*/

    static public Subject getUserFromServletRequest(HttpServletRequest request) {
        Principal principal = request.getUserPrincipal();
        if(principal instanceof Subject) {
            return (Subject) principal;

            //workaroud strange jetty authentication method, why encapsulate user object??? -shabanovd
        } else if(principal != null && "org.eclipse.jetty.plus.jaas.JAASUserPrincipal".equals(principal.getClass().getName())) {
            try {
                Method method = principal.getClass().getMethod("getSubject");
                Object obj = method.invoke(principal);
                if(obj instanceof javax.security.auth.Subject) {
                    javax.security.auth.Subject subject = (javax.security.auth.Subject) obj;
                    for(Principal _principal_ : subject.getPrincipals()) {
                        if(_principal_ instanceof Subject) {
                            return (Subject) _principal_;
                        }
                    }
                }
            } catch(SecurityException e) {
            } catch(IllegalArgumentException e) {
            } catch(IllegalAccessException e) {
            } catch(NoSuchMethodException e) {
            } catch(InvocationTargetException e) {
            }
        }

        return null;
    }
    @ConfigurationFieldAsElement("password")
    private String password = null;
    @ConfigurationFieldAsElement("digestPassword")
    private String digestPassword = null;

    /**
     * Create a new user with name and password
     *
     * @param realm
     * @param name
     * @param password
     * @throws ConfigurationException
     */
    public AccountImpl(AbstractRealm realm, int id, String name, String password) throws ConfigurationException {
        super(realm, id, name);
        setPassword(password);
    }

    public AccountImpl(AbstractRealm realm, int id, String name, String password, Group group, boolean hasDbaRole) throws ConfigurationException {
        super(realm, id, name);
        setPassword(password);
        this.groups.add(group);
        this.hasDbaRole = hasDbaRole;
    }

    public AccountImpl(AbstractRealm realm, int id, String name, String password, Group group) throws ConfigurationException {
        super(realm, id, name);
        setPassword(password);
        this.groups.add(group);
    }

    /**
     * Create a new user with name
     *
     * @param name
     *            The account name
     * @throws ConfigurationException
     */
    public AccountImpl(AbstractRealm realm, String name) throws ConfigurationException {
        super(realm, Account.UNDEFINED_ID, name);
    }

//    /**
//     * Create a new user with name, password and primary group
//     *
//     * @param name
//     * @param password
//     * @param primaryGroup
//     * @throws ConfigurationException
//     * @throws PermissionDeniedException
//     */
//	public AccountImpl(AbstractRealm realm, int id, String name, String password, String primaryGroup) throws ConfigurationException {
//		this(realm, id, name, password);
//		addGroup(primaryGroup);
//	}
    public AccountImpl(AbstractRealm realm, int id, Account from_user) throws ConfigurationException, PermissionDeniedException {
        super(realm, id, from_user.getName());
        instantiate(from_user);
    }
    
    public AccountImpl(DBBroker broker, AbstractRealm realm, int id, Account from_user) throws ConfigurationException, PermissionDeniedException {
        super(broker, realm, id, from_user.getName());
        instantiate(from_user);
    }
    
    private void instantiate(Account from_user) throws PermissionDeniedException {
        home = from_user.getHome();

        //copy metadata
        for(SchemaType metadataKey : from_user.getMetadataKeys()) {
            String metadataValue = from_user.getMetadataValue(metadataKey);
            setMetadataValue(metadataKey, metadataValue);
        }

        if(from_user instanceof AccountImpl) {
            AccountImpl user = (AccountImpl) from_user;

            groups = new ArrayList<Group>(user.groups);

            password = <PASSWORD>;
            digestPassword = <PASSWORD>;

            hasDbaRole = user.hasDbaRole;

            _cred = user._cred;
        } else if(from_user instanceof UserAider) {
            UserAider user = (UserAider) from_user;

            String[] gl = user.getGroups();
            for(int i = 0; i < gl.length; i++) {
                addGroup(gl[i]);
            }

            setPassword(user.getPassword());
            digestPassword = user.getDigestPassword();
        } else {
            addGroup(from_user.getDefaultGroup());
            //TODO: groups
        }<commit_msg>[ignore] immutable<commit_after>/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2003-2011 The eXist Project
 *  http://exist-db.org
 *  
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  $Id$
 */
package org.exist.security.internal;

import org.exist.security.AbstractRealm;
import org.exist.security.AbstractAccount;
import org.apache.log4j.Logger;
import org.exist.config.Configuration;
import org.exist.config.ConfigurationException;
import org.exist.config.Configurator;
import org.exist.config.annotation.ConfigurationClass;
import org.exist.config.annotation.ConfigurationFieldAsElement;
import org.exist.security.Group;
import org.exist.security.PermissionDeniedException;
import org.exist.security.SchemaType;
import org.exist.security.SecurityManager;
import org.exist.security.Subject;
import org.exist.security.Account;
import org.exist.security.internal.aider.UserAider;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import org.exist.storage.DBBroker;

/**
 * Represents a user within the database.
 * 
 * @author <NAME> <<EMAIL>>
 * @author {Marco.Tampucci, Massimo.Martinelli} @isti.<EMAIL>
 */
@ConfigurationClass("account")
public class AccountImpl extends AbstractAccount {

    private final static Logger LOG = Logger.getLogger(AccountImpl.class);
    public static boolean CHECK_PASSWORDS = true;

    private final static SecurityProperties securityProperties = new SecurityProperties();

    public static SecurityProperties getSecurityProperties() {
        return securityProperties;
    }

    /*
    static {
        Properties props = new Properties();
        try {
            props.load(AccountImpl.class.getClassLoader().getResourceAsStream(
                    "org/exist/security/security.properties"));
        } catch(IOException e) {
        }
        String option = props.getProperty("passwords.encoding", "md5");
        setPasswordEncoding(option);
        option = props.getProperty("passwords.check", "yes");
        CHECK_PASSWORDS = option.equalsIgnoreCase("yes")
                || option.equalsIgnoreCase("true");
    }

    static public void enablePasswordChecks(boolean check) {
        CHECK_PASSWORDS = check;
    }

    static public void setPasswordEncoding(String encoding) {
        if(encoding != null) {
            LOG.equals("Setting password encoding to " + encoding);
            if(encoding.equalsIgnoreCase("plain")) {
                PASSWORD_ENCODING = PLAIN_ENCODING;
            } else if(encoding.equalsIgnoreCase("md5")) {
                PASSWORD_ENCODING = MD5_ENCODING;
            } else {
                PASSWORD_ENCODING = SIMPLE_MD5_ENCODING;
            }
        }
    }*/

    static public Subject getUserFromServletRequest(HttpServletRequest request) {
        Principal principal = request.getUserPrincipal();
        if(principal instanceof Subject) {
            return (Subject) principal;

            //workaroud strange jetty authentication method, why encapsulate user object??? -shabanovd
        } else if(principal != null && "org.eclipse.jetty.plus.jaas.JAASUserPrincipal".equals(principal.getClass().getName())) {
            try {
                Method method = principal.getClass().getMethod("getSubject");
                Object obj = method.invoke(principal);
                if(obj instanceof javax.security.auth.Subject) {
                    javax.security.auth.Subject subject = (javax.security.auth.Subject) obj;
                    for(Principal _principal_ : subject.getPrincipals()) {
                        if(_principal_ instanceof Subject) {
                            return (Subject) _principal_;
                        }
                    }
                }
            } catch(SecurityException e) {
            } catch(IllegalArgumentException e) {
            } catch(IllegalAccessException e) {
            } catch(NoSuchMethodException e) {
            } catch(InvocationTargetException e) {
            }
        }

        return null;
    }
    @ConfigurationFieldAsElement("password")
    private String password = null;
    @ConfigurationFieldAsElement("digestPassword")
    private String digestPassword = null;

    /**
     * Create a new user with name and password
     *
     * @param realm
     * @param name
     * @param password
     * @throws ConfigurationException
     */
    public AccountImpl(AbstractRealm realm, int id, String name, String password) throws ConfigurationException {
        super(realm, id, name);
        setPassword(password);
    }

    public AccountImpl(AbstractRealm realm, int id, String name, String password, Group group, boolean hasDbaRole) throws ConfigurationException {
        super(realm, id, name);
        setPassword(password);
        this.groups.add(group);
        this.hasDbaRole = hasDbaRole;
    }

    public AccountImpl(AbstractRealm realm, int id, String name, String password, Group group) throws ConfigurationException {
        super(realm, id, name);
        setPassword(password);
        this.groups.add(group);
    }

    /**
     * Create a new user with name
     *
     * @param name
     *            The account name
     * @throws ConfigurationException
     */
    public AccountImpl(AbstractRealm realm, String name) throws ConfigurationException {
        super(realm, Account.UNDEFINED_ID, name);
    }

//    /**
//     * Create a new user with name, password and primary group
//     *
//     * @param name
//     * @param password
//     * @param primaryGroup
//     * @throws ConfigurationException
//     * @throws PermissionDeniedException
//     */
//	public AccountImpl(AbstractRealm realm, int id, String name, String password, String primaryGroup) throws ConfigurationException {
//		this(realm, id, name, password);
//		addGroup(primaryGroup);
//	}
    public AccountImpl(AbstractRealm realm, int id, Account from_user) throws ConfigurationException, PermissionDeniedException {
        super(realm, id, from_user.getName());
        instantiate(from_user);
    }
    
    public AccountImpl(DBBroker broker, AbstractRealm realm, int id, Account from_user) throws ConfigurationException, PermissionDeniedException {
        super(broker, realm, id, from_user.getName());
        instantiate(from_user);
    }
    
    private void instantiate(Account from_user) throws PermissionDeniedException {
        home = from_user.getHome();

        //copy metadata
        for(final SchemaType metadataKey : from_user.getMetadataKeys()) {
            final String metadataValue = from_user.getMetadataValue(metadataKey);
            setMetadataValue(metadataKey, metadataValue);
        }

        if(from_user instanceof AccountImpl) {
            AccountImpl user = (AccountImpl) from_user;

            groups = new ArrayList<Group>(user.groups);

            password = <PASSWORD>;
            digestPassword = <PASSWORD>;

            hasDbaRole = user.hasDbaRole;

            _cred = user._cred;
        } else if(from_user instanceof UserAider) {
            UserAider user = (UserAider) from_user;

            String[] gl = user.getGroups();
            for(int i = 0; i < gl.length; i++) {
                addGroup(gl[i]);
            }

            setPassword(user.<PASSWORD>());
            digestPassword = <PASSWORD>();
        } else {
            addGroup(from_user.getDefaultGroup());
            //TODO: groups
        }
{'fraction_non_alphanumeric': 0.09247298847105692, 'fraction_numerical': 0.0027766040924729883, 'mean_word_length': 2.6834148510449087, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3100499', 'n_tokens_mistral': 4376, 'n_tokens_neox': 4217, 'n_words': 1141}