/*
 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/*
 * @(#)JCKPrivateCred.java	1.3 02/01/12
 *
 * ------------------------------------------------------------------
 *  (C) COPYRIGHT INTERNATIONAL BUSINESS MACHINES CORPORATION 1999.
 *                       ALL RIGHTS RESERVED
 *                         IBM Confidential
 * ------------------------------------------------------------------
 */

package com.sun.cts.util.tests.jaas;

import javax.security.auth.Destroyable;
import javax.security.auth.DestroyFailedException;
import javax.security.auth.Refreshable;
import javax.security.auth.RefreshFailedException;
import javax.security.auth.AuthPermission;

/**
 * This file contains a simple private credential
 * implementation for purposes of JAAS JCK testing.
 *
 * <p> A <code>Credential</code> represents a security related attribute.
 *
 * @see javax.security.auth.Subject
 *
 * @(#)author:  D. Kent Soper
 * @(#)version: 1.0
 * @(#)date:    99/07/27
 */
public class JCKPrivateCred implements Destroyable, Refreshable {

    /**
     * @serial
     */
    private String credential;

    /**
     * @serial
     */
    private int lifespan;

    /**
     * Create a JCKPrivateCred with an identifying name.
     *
     * <p>
     *
     * @param name the name of the private credential.
     *
     * @exception NullPointerException if the <code>name</code>
     *			is <code>null</code>.
     */
    public JCKPrivateCred(String name) {
	    if (name == null)
	        throw new NullPointerException("illegal null input");

	    credential = name;
	    lifespan = 3;
	}

	/**
     * Create a JCKPrivateCred with an identifying name.
     *
     * <p>
     *
     * @param name the name of the private credential.
     *
     * @exception NullPointerException if the <code>name</code>
     *			is <code>null</code>.
     */
    public JCKPrivateCred(char[] name) {
	    if (name == null)
	        throw new NullPointerException("illegal null input");

	    credential = new String(name);
	    lifespan = 3;
	}

	/**
     * Create a JCKPrivateCred with an identifying name.
     *
     * <p>
     *
     * @param name the name of the private credential.
     *
     * @exception NullPointerException if the <code>name</code>
     *			is <code>null</code>.
     */
    public JCKPrivateCred(int number) {

	    credential = Integer.toHexString(number);
	    lifespan = 3;
	}

    /**
     * Destroy this <code>JCKPrivateCred</code>.
     *
     * <p> Sensitive information associated with this
     * <code>JCKPrivateCred</code> is
     * destroyed or cleared.  Subsequent method calls on this
     * <code>JCKPrivateCred</code> result in a
     * <code>UnsupportedOperationException</code> being thrown.
     *
     * <p>
     *
     * @exception SecurityException if the caller does not have permission
     *		to destroy this <code>JCKPrivateCred</code>.
     */
    public void destroy() throws DestroyFailedException {

        // Check for permission to destroy credentials.
        java.lang.SecurityManager sm = System.getSecurityManager();
        if (sm != null)
            sm.checkPermission(new AuthPermission("destroyCredential"));

        // Check for destroyed credential.
        if (credential == null)
            throw new DestroyFailedException ("credential previously destroyed");

        // Destroy credential.
        credential = null;
        lifespan = -1;
    }

    /**
     * Determine if this <code>JCKPrivateCred</code> has been destroyed.
     *
     * <p>
     *
     * @return true if this <code>JCKPrivateCred</code> has been destroyed,
     *		false otherwise.
     */
    public boolean isDestroyed() {
        return (lifespan < 0);
    }

    /**
     * Determine if this <code>Object</code> is current.
     *
     * <p>
     *
     * @return true if this <code>Object</code> is currently current,
     *		false otherwise.
     */
    public boolean isCurrent() {
        if (lifespan > 0) {
            lifespan--;
            return true;
        }
        return false;
    }

    /**
     * Update or extend the validity period for this
     * <code>Object</code>.
     *
     * <p>
     *
     * @exception SecurityException if the caller does not have permission
     *		to update or extend the validity period for this
     *		<code>Object</code>.
     */
    public void refresh() throws RefreshFailedException {
        // Check for permission to refresh credentials.
        java.lang.SecurityManager sm = System.getSecurityManager();
        if (sm != null)
            sm.checkPermission(new AuthPermission("refreshCredential"));

        // Is the credential destroyed?
        if (credential == null)
            throw new RefreshFailedException ("can not refresh destroyed credentials");

        lifespan = 3;
    }

    /**
     * Compares the specified Object with this <code>JCKPrivateCred</code> for
     * equality.  Returns true if the given object is also a
     * <code>JCKPrivateCred</code> and the two JCKPrivateCreds are equivalent.
     *
     * <p>
     *
     * @param o Object to be compared for equality with this
     * <code>JCKPrivateCred</code>.
     *
     * @return true if the specified Object is equal equal to this
     * <code>JCKPrivateCred</code>.
     *
     * @exception UnsupportedOperationException if the <code>JCKPrivateCred</code>
     *		has already been destroyed.
     */
    public boolean equals(Object o) {

        if (!(o instanceof JCKPrivateCred))
            return false;

        // lifespan equivalence is not important.
        return ((JCKPrivateCred)o).getName().equals(credential);
    }


    /**
     * Return the credential name for this <code>JCKPrivateCred</code>.
     *
     * <p>
     *
     * @return the credential name for this <code>JCKPrivateCred</code>
     */
    public String getName() {
	    return credential;
    }

    /**
     * Return a string representation of this <code>JCKPrincipal</code>.
     *
     * <p>
     *
     * @return a string representation of this <code>JCKPrincipal</code>.
     */
    public String toString() {
	    return("JCKPrivateCred:  " + credential);
    }
}
