/*
 * 
 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * The contents of this file are subject to the Sun Community Source
 * License v3.0/Jini Technology Specific Attachment v 1.0 (the
 * "License"). You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://wwws.sun.com/software/jini/licensing/SCSL3_JiniTSA1.html.
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Reference Code is Jini Technology Core Platform code, v2.0. The
 * Developer of the Reference Code is Sun Microsystems, Inc.
 * 
 * Contributor(s): Sun Microsystems, Inc.
 * 
 * The contents of this file comply with the Jini Technology Core
 * Platform Compatibility Kit, v2.0A.
 * 
 * Tester(s): Sun Microsystems, Inc.
 * 
 * Test Platform(s):
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Solaris(TM)SPARC/x86
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Linux (Intel x86)
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Windows (Intel Platform)
 * 
 */
package net.jini.jeri.kerberos;

import java.rmi.RemoteException;
import javax.net.SocketFactory;
import javax.security.auth.kerberos.KerberosPrincipal;
import net.jini.security.TrustVerifier;

/**
 * Trust verifier for verifying the Jini(TM) extensible remote
 * invocation (Jini ERI) endpoints of type {@link KerberosEndpoint},
 * and principals of type {@link KerberosPrincipal}. This class is
 * intended to be specified in a resource to configure the operation
 * of {@link net.jini.security.Security#verifyObjectTrust
 * Security.verifyObjectTrust}.
 *
 * @author Sun Microsystems, Inc.
 * @see KerberosEndpoint
 * @since 2.0
 */
public class KerberosTrustVerifier implements TrustVerifier {

    /**
     * Creates a <code>Security.TrustVerifier</code> for this package.
     */
    public KerberosTrustVerifier() {}

    /**
     * Returns <code>true</code> if the object is an instance of
     * {@link KerberosEndpoint} and the <code>SocketFactory</code> it
     * uses internally, if not <code>null</code>, is trusted by the
     * given <code>TrustVerifier.Context</code>, or the object is an
     * instance of {@link KerberosPrincipal}.  Returns
     * <code>false</code> otherwise.
     *
     * @throws RemoteException if a communication-related exception
     *         occurs when verifying a socket factory
     * @throws SecurityException if a security exception occurs when
     *	       verifying a socket factory
     * @throws NullPointerException {@inheritDoc}
     */
    public boolean isTrustedObject(Object obj, TrustVerifier.Context ctx)
	throws RemoteException
    {
	if (obj == null || ctx == null)
	    throw new NullPointerException("null argument encountered");

	if (obj instanceof KerberosEndpoint) {
	    SocketFactory csf =
		((KerberosEndpoint) obj).getSocketFactory();
	    if (csf == null) {
		return true;
	    } else {
		return ctx.isTrustedObject(csf);
	    }
	} else if (obj instanceof KerberosPrincipal) {
	    return true;
	}

	return false;
    }
}
