package gov.epa.state.sample.security;

import gov.epa.cdx.axis.security.NetworkSecurityNodePortType;
import gov.epa.cdx.axis.security.client.CdxSecurityLocator;
import gov.epa.cdx.axis.security.enum.AuthMethod;
import gov.epa.cdx.commons.log.LogInterface;
import gov.epa.cdx.commons.log.LogFactory;

import java.rmi.RemoteException;

/**

 * User: David Dundua
 * Date: May 22, 2003
 * Time: 3:28:52 PM
 */
public class SecurityMgr
{
    protected static LogInterface log = LogFactory.getLog(SecurityMgr.class.getName());

    public static String auth(String uid, String cred, String clientHost) throws java.rmi.RemoteException
    {
        NetworkSecurityNodePortType binding = null;
        String value = null;

        try
        {
            binding = new CdxSecurityLocator().getNetworkSecurityNodePortType();
        }
        catch (javax.xml.rpc.ServiceException jre)
        {
            if(jre.getLinkedCause()!=null)
                jre.getLinkedCause().printStackTrace();
            log.debug("JAX-RPC ServiceException caught: " + jre);
        }


        try
        {
            if (binding != null)
                value = binding.centralAuth(uid, cred, AuthMethod.password, clientHost);
            log.debug("Auth (" + uid + "," + cred + "," + clientHost + ") results = " + value);
        }
        catch (java.rmi.RemoteException re)
        {
            log.debug("Remote Exception caught: " + re);
        }
        return value;
    }

    public static String centralAuth(String uid, String cred, AuthMethod authMethod, String clientHost) throws java.rmi.RemoteException
    {
        NetworkSecurityNodePortType binding = null;
        String value = null;

        try
        {
            binding = new CdxSecurityLocator().getNetworkSecurityNodePortType();
        }
        catch (javax.xml.rpc.ServiceException jre)
        {
            if(jre.getLinkedCause()!=null)
                jre.getLinkedCause().printStackTrace();
            log.debug("JAX-RPC ServiceException caught: " + jre);
        }

        try
        {

            log.debug("Before Auth (" + uid + "," + cred + "," + clientHost + ") results = " + value);
            if (binding != null)
            {
                value = binding.centralAuth(uid, cred, AuthMethod.password, clientHost);
            }

            log.debug("Auth (" + uid + "," + cred + "," + clientHost + ") results = " + value);
        }
        catch (java.rmi.RemoteException re)
        {
            log.debug("Remote Exception caught: " + re);
            //re-throw
            throw re;
        }
        return value;
    }

    public static boolean isTokenValid(String authToken, String clientHost) throws RemoteException
    {

        boolean status = true;
        String validationResults = null;
        log.debug("Validating: " + authToken + "," + clientHost);

        // Validate token and IP using central security
        try
        {
            validationResults = SecurityMgr.validate(authToken, clientHost, "Resource URI");

        }
        catch (java.rmi.RemoteException e)
        {
            status = false;
            log.debug("-----------------------------------> Validate call failed");
        }

        log.debug("Results of validation = " + validationResults);

        return status;

    }

    private static String validate(String authToken, String clientHost, String resourceURI) throws java.rmi.RemoteException
    {
        NetworkSecurityNodePortType binding = null;
        String value = null;

        try
        {
            binding = new CdxSecurityLocator().getNetworkSecurityNodePortType();
        }
        catch (javax.xml.rpc.ServiceException jre)
        {
            if(jre.getLinkedCause()!=null)
                jre.getLinkedCause().printStackTrace();

            log.debug("JAX-RPC ServiceException caught: " + jre);
        }
        try
        {
            log.debug("Trying to Validate (" + authToken + "," + clientHost + "," + resourceURI + ")");
            if (binding != null)
                value = binding.validate(authToken, clientHost, resourceURI);

            log.debug("After Validation (" + authToken + "," + clientHost + "," + resourceURI + ") results = " + value);
        }
        catch (java.rmi.RemoteException re)
        {
            log.debug("Remote Exception caught: " + re);
            throw new java.rmi.RemoteException ("Remote Exception caught: " + re);
        }
        return value;
    }


}
