/*
 * @(#)AuthorizeCallback.java	1.3 01/01/25
 *
 * Copyright 2000 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * This software is the proprietary information of Sun Microsystems, Inc.  
 * Use is subject to license terms.
 * 
 */

package com.sun.security.sasl.preview;

import javax.security.auth.callback.Callback;

/**
  * This callback is used by <tt>SaslServer</tt> to determine whether
  * one entity (identified by an authenticated authentication id) 
  * can act on
  * behalf of another entity (identified by an authorization id).
  *
  * @author Rosanna Lee
  * @author Rob Weltman
  */
public class AuthorizeCallback implements Callback {
    private String authenticationId;
    private String authorizationId;
    private String authorizedId;
    private boolean authorized;

    /**
     * Constructs an instance of <tt>AuthorizeCallback</tt>.
     *
     * @param authnId	The (authenticated) authentication id.
     * @param authzId   The authorization id.
     */
    public AuthorizeCallback(String authnId, String authzId) {
	authenticationId = authnId;
	authorizationId = authzId;
    }

    /**
     * Returns the authentication id to check.
     * @return The authentication id to check.
     */
    public String getAuthenticationId() {
	return authenticationId;
    }

    /**
     * Returns the authorization id to check.
     * @return The authentication id to check.
     */
    public String getAuthorizationId() {
	return authorizationId;
    }

    /**
     * Determines whether the authentication id is allowed to
     * act on behalf of the authorization id.
     *
     * @return <tt>true</tt> if authorization is allowed; <tt>false</tt> otherwise
     * @see #setAuthorized(boolean)
     * @see #getAuthorizedId()
     */
    public boolean isAuthorized() {
	return authorized;
    }

    /**
     * Sets whether the authorization is allowed.
     * @param ok <tt>true</tt> if authorization is allowed; <tt>false</tt> otherwise
     * @see #isAuthorized
     * @see #setAuthorizedId(java.lang.String)
     */
    public void setAuthorized(boolean ok) {
	authorized = ok;
    }

    /**
     * Returns the id of the authorized user.
     * @return The id of the authorized user. <tt>null</tt> means the
     * authorization failed.
     * @see #setAuthorized(boolean)
     * @see #setAuthorizedId(java.lang.String)
     */
    public String getAuthorizedId() {
	if (!authorized) {
	    return null;
	}
	return (authorizedId == null) ? authorizationId : authorizedId;
    }

    /**
     * Sets the id of the authorized entity. Called by handler only when the id
     * is different from getAuthorizationId(). For example, the id
     * might need to be canonicalized for the environment in which it
     * will be used.
     * @param The id of the authorized user.
     * @see #setAuthorized(boolean)
     * @see #getAuthorizedId
     */
    public void setAuthorizedId(String id) {
	authorizedId = id;
    }
}
