8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: HttpCallback.java,v 1.4 2009/07/28 19:40:45 beomsuk Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.authentication.spi;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.Serializable;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.security.auth.callback.Callback;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport javax.servlet.http.HttpServletResponse;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>HttpCallback</code> class implements <code>Callback</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * and is used by the authentication module with HTTP protocol based
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * handshaking negotiation.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @supported.all.api
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class HttpCallback implements Callback, Serializable {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private String tokenHeader = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private String authToken = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private String negoHeader = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private String negoValue = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private int errorCode = HttpServletResponse.SC_UNAUTHORIZED;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static final String HTTP_NEGOTIATE = "Negotiate";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static final String HTTP_HTTPBASIC = "BASIC realm=\"basic_realm\"";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Creates a <code>HttpCallback</code> object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param authorizationHeader Header name for the authorization string.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param negotiationHeader Negotiation header string.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param errorCode Error code set in the header for negotiation.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public HttpCallback(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String authorizationHeader,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String negotiationHeader,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String errorCode) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.tokenHeader = authorizationHeader;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.errorCode = Integer.parseInt(errorCode);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (Exception e) {}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int index = negotiationHeader.indexOf(":");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (index != -1) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.negoHeader = negotiationHeader.substring(0, index);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.negoValue = negotiationHeader.substring(index+1);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.negoHeader = negotiationHeader;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Creates a <code>HttpCallback</code> object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The negotiation header is constructed using the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * negotiation name and value in the format
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>negoName:negoValue</code>.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param authRHeader Header name for the authorization string.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param negoName Negotiation name in the negotiation header.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param negoValue Negotiation value in the negotiation header.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param errorCode Error code set in the header for negotiation.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public HttpCallback(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String authRHeader,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String negoName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String negoValue,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int errorCode) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.tokenHeader = authRHeader;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.negoHeader = negoName;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.negoValue = negoValue;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.errorCode = errorCode;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns the authorization header string.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return the authorization header string.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String getAuthorizationHeader() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return tokenHeader;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns the negotiation header name.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return the negotiation header name.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String getNegotiationHeaderName() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return negoHeader;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns the negotiation header value.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return the negotiation header value.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String getNegotiationHeaderValue() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return negoValue;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns the negotiation error code.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return the negotiation error code.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public int getNegotiationCode() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return errorCode;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns the authorization string.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return the authorization string.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String getAuthorization() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return authToken;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Set the authorization string to a <code>HttpCallback</code> object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param authorization
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void setAuthorization(String authorization) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.authToken = authorization;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns <code>true<code> if the callback is for HTTPBasic.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public boolean isForHTTPBasic() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (negoValue != null)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ? negoValue.equalsIgnoreCase(HTTP_HTTPBASIC) : false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns <code>true<code> if the callback is for WindowsDesktopSSO.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public boolean isForWindowsDesktopSSO() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (negoValue != null)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ? negoValue.equalsIgnoreCase(HTTP_NEGOTIATE) : false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster