
// Copyright (c) 2000 Oracle Corporation
package oracle.portal.provider.v2.externalApp;

import java.util.*;
import java.security.Principal;

/**
 * ExternalUser encapsulates the information necessary for an external
 * application to authenticate a Portal user
 */
public class ExternalPrincipal implements Principal
{

    /**
     * Constructor
     */
    public ExternalPrincipal(String name, String password, String authMethod, 
                             String userField, String passField, String authUrl,
                             Map fields)
    {
        mName = name;
        mPassword = password;
        mUserFieldName = userField;
        mPassFieldName = passField;
        mAuthMethod = authMethod;
        mAuthUrl = authUrl;
        mFieldMap = fields;
    }

    /**
     * Constructor. Assumes a default user field name of <code>"username"</code>
     * and a default password field name of <code>"password"</code>.
     * @deprecated Use {@link #ExternalPrincipal (String, String, String, String,
     * String, String, Map} instead.
     */
    public ExternalPrincipal(String name, String password, String authMethod, 
                             String authUrl, Map fields)
    {
        this(name, password, authMethod, "username", "password", authUrl, fields);
    }

    /**
     *  tests whether this object represents the same user as the passed object.
     *  if either or both objects do not have a value for Name (ie name == null)
     *  then the objects do not represent users and therefore cannot be equal.
     *
     *  @return true if the portal user name's are the same (case-sensitive).
     */
    public boolean equals(Object another)
    {
        if (!(another instanceof ExternalPrincipal))
            return false;

        ExternalPrincipal test = (ExternalPrincipal) another;

        // if one or both names are null, return false.
        if (getName() == null || test.getName() == null)
            return false;

        return getName().equals(test.getName());
    }

    /**
     * Returns a string representation of this external application user
     * @return a string representation of this User.  Currently the external
     * application user name.
     */
    public String toString()
    {
        if (mName == null)
            return "";
        return mName;
    }

    /**
     *  @return a hashcode representing this user
     */
    public int hashCode()
    {
        if (mName != null)
            return mName.hashCode();
        return Integer.MIN_VALUE;
    }

    /**
     *  Returns the external application user name
     *  @return external application user name
     */
    public String getName()
    {
        return mName;
    }

    /**
     *  Returns the external application password
     *  @return external application password
     */
    public String getPassword()
    {
        return mPassword;
    }

    /**
     *  Returns the name of the form field that should contain the username
     *  @return name of the field containing the external application user name 
     */
    public String getUserFieldName()
    {
        return mUserFieldName;
    }

    /**
     *  Returns name of the form field that should contain the password
     *  @return name of the field containing the external application password 
     */

    public String getPasswordFieldName()
    {
        return mPassFieldName;
    }

    /**
     *  Returns the external application authorisation Method
     *  @return external application auth. method
     */
    public String getAuthMethod()
    {
        return mAuthMethod;
    }

    /**
     *  Returns the external application authoriation Url
     *  @return external application auth. Url
     */
    public String getAuthUrl()
    {
        return mAuthUrl;
    }   
    
    /**
     *  Returns the value of the named field
     *  @return value associated with the named field
     */
    public String getField(String name)
    {
        if (mFieldMap == null)
            return null;
        return (String)mFieldMap.get(name);
    }


    /**
     *  Returns an iterator over the field names associated with the external
     *  application.
     *  @return an iterator over the field names
     */
    public Iterator getFields()
    {
        if (mFieldMap == null)
            return null;

        return mFieldMap.keySet().iterator();
    } 

    private String mName = null;
    private String mPassword = null;
    private String mUserFieldName = null;
    private String mPassFieldName = null;
    private String mAuthMethod = null;
    private String mAuthUrl = null;
    private Map mFieldMap = null;
}
