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.client.NetworkSecurityBindingStub;
import gov.epa.cdx.axis.security.enum.AuthMethod;
import gov.epa.cdx.commons.log.LogInterface;
import gov.epa.cdx.commons.log.LogFactory;
import gov.epa.cdx.commons.exception.CDXException;
import gov.epa.cdx.commons.exception.CDXServerException;
import gov.epa.cdx.commons.exception.CDXErrorCode;
import gov.epa.cdx.commons.property.CDXPropertyManager;

import java.rmi.RemoteException;
import java.net.URL;

import org.apache.axis.client.Stub;

/**

 * 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 = getStub();
        String value = null;


        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 = getStub();
        String value = null;

        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 = getStub();;
        String value = null;

        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;
    }

    private static NetworkSecurityNodePortType getStub()  throws RemoteException
    {
        Stub clientStub = null;
        String endpointURLString = CDXPropertyManager.getProperty("NAAS", "endpointURL");

        CdxSecurityLocator locator =
                new CdxSecurityLocator();
        NetworkSecurityBindingStub _stub = null;

        try
        {
             URL portURL = new URL(endpointURLString);
            _stub = new NetworkSecurityBindingStub(portURL, locator);
        }
        catch (Exception ex)
        {
            log.error("Could not create Axis Client Stub for NAAS", ex);
            throw new RemoteException("Couldn't not connect to NAAS.", ex);
        }

        _stub.setPortName(locator.getNetworkSecurityNodePortTypeWSDDServiceName());
        clientStub = _stub;

        return (NetworkSecurityNodePortType)clientStub;
    }
}
