001/**
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
005 *
006 * The contents of this file are subject to the terms
007 * of the Common Development and Distribution License
008 * (the License). You may not use this file except in
009 * compliance with the License.
010 *
011 * You can obtain a copy of the License at
012 * https://opensso.dev.java.net/public/CDDLv1.0.html or
013 * opensso/legal/CDDLv1.0.txt
014 * See the License for the specific language governing
015 * permission and limitations under the License.
016 *
017 * When distributing Covered Code, include this CDDL
018 * Header Notice in each file and include the License file
019 * at opensso/legal/CDDLv1.0.txt.
020 * If applicable, add the following below the CDDL Header,
021 * with the fields enclosed by brackets [] replaced by
022 * your own identifying information:
023 * "Portions Copyrighted [year] [name of copyright owner]"
024 *
025 * $Id: HttpCallback.java,v 1.4 2009/07/28 19:40:45 beomsuk Exp $
026 *
027 */
028
029
030package com.sun.identity.authentication.spi;
031
032import java.io.Serializable;
033import javax.security.auth.callback.Callback;
034import javax.servlet.http.HttpServletResponse;
035
036/**
037 * <code>HttpCallback</code> class implements <code>Callback</code>
038 * and is used by the authentication module with HTTP protocol based
039 * handshaking negotiation.
040 *
041 * @supported.all.api
042 */
043public class HttpCallback implements Callback, Serializable {
044    private String tokenHeader = null;
045    private String authToken = null;
046    private String negoHeader = null;
047    private String negoValue = null;
048    private int errorCode = HttpServletResponse.SC_UNAUTHORIZED;
049    static final String HTTP_NEGOTIATE = "Negotiate";
050    static final String HTTP_HTTPBASIC = "BASIC realm=\"basic_realm\"";
051    
052    /**
053     * Creates a <code>HttpCallback</code> object.
054     * @param authorizationHeader Header name for the authorization string.
055     * @param negotiationHeader  Negotiation header string.
056     * @param errorCode Error code set in the header for negotiation.
057     */
058    public HttpCallback(
059        String authorizationHeader,
060        String negotiationHeader,
061        String errorCode) {
062        this.tokenHeader = authorizationHeader;
063        try {
064            this.errorCode   = Integer.parseInt(errorCode);
065        } catch (Exception e) {}
066        
067        int index = negotiationHeader.indexOf(":");
068        if (index != -1) {
069            this.negoHeader = negotiationHeader.substring(0, index);
070            this.negoValue = negotiationHeader.substring(index+1);
071        } else {
072            this.negoHeader = negotiationHeader;
073        }
074    }
075
076    /**
077     * Creates a <code>HttpCallback</code> object.
078     * The negotiation header is constructed using the
079     * negotiation name and value in the format 
080     * <code>negoName:negoValue</code>.
081     *
082     * @param authRHeader Header name for the authorization string.
083     * @param negoName Negotiation name in the negotiation header.
084     * @param negoValue Negotiation value in the negotiation header.
085     * @param errorCode Error code set in the header for negotiation.
086     */
087    public HttpCallback(
088        String authRHeader,
089        String negoName,
090        String negoValue,
091        int errorCode) {
092        this.tokenHeader = authRHeader;
093        this.negoHeader = negoName;
094        this.negoValue = negoValue;
095        this.errorCode = errorCode;
096    }
097    
098    /**
099     * Returns the authorization header string.
100     *
101     * @return the authorization header string.
102     */
103    public String getAuthorizationHeader() {
104        return tokenHeader;
105    }
106    
107    /**
108     * Returns the negotiation header name.
109     *
110     * @return the negotiation header name.
111     */
112    public String getNegotiationHeaderName() {
113        return negoHeader;
114    }
115    
116    /**
117     * Returns the negotiation header value.
118     *
119     * @return the negotiation header value.
120     */
121    public String getNegotiationHeaderValue() {
122        return negoValue;
123    }
124    
125    /**
126     * Returns the negotiation error code.
127     *
128     * @return the negotiation error code.
129     */
130    public int getNegotiationCode() {
131        return errorCode;
132    }
133    
134    /**
135     * Returns the authorization string.
136     * @return the authorization string.
137     */
138    public String getAuthorization() {
139        return authToken;
140    }
141    
142    /**
143     * Set the authorization string to a <code>HttpCallback</code> object.
144     * @param authorization 
145     */
146    public void setAuthorization(String authorization) {
147        this.authToken = authorization;
148    }
149
150    /**
151     * Returns <code>true<code> if the callback is for HTTPBasic.
152     */
153    public boolean isForHTTPBasic() {
154        return (negoValue != null)
155            ? negoValue.equalsIgnoreCase(HTTP_HTTPBASIC) : false;
156    }
157
158    /**
159     * Returns <code>true<code> if the callback is for WindowsDesktopSSO.
160     */
161    public boolean isForWindowsDesktopSSO() {
162        return (negoValue != null)
163            ? negoValue.equalsIgnoreCase(HTTP_NEGOTIATE) : false;
164    }
165}
166