1069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/* 2069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/auth/BasicScheme.java $ 3069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Revision: 658430 $ 4069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Date: 2008-05-20 14:04:27 -0700 (Tue, 20 May 2008) $ 5069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 6069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ==================================================================== 7069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 8069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Licensed to the Apache Software Foundation (ASF) under one or more 9069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * contributor license agreements. See the NOTICE file distributed with 10069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * this work for additional information regarding copyright ownership. 11069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * The ASF licenses this file to You under the Apache License, Version 2.0 12069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * (the "License"); you may not use this file except in compliance with 13069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * the License. You may obtain a copy of the License at 14069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 15069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 16069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 17069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Unless required by applicable law or agreed to in writing, software 18069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 19069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * See the License for the specific language governing permissions and 21069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * limitations under the License. 22069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ==================================================================== 23069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 24069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * This software consists of voluntary contributions made by many 25069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * individuals on behalf of the Apache Software Foundation. For more 26069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * information on the Apache Software Foundation, please see 27069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <http://www.apache.org/>. 28069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 29069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 30069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 31069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpackage org.apache.http.impl.auth; 32069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 33069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.commons.codec.binary.Base64; 34069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.Header; 35069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.HttpRequest; 36069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.auth.AuthenticationException; 37069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.auth.Credentials; 38069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.auth.AUTH; 39069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.auth.MalformedChallengeException; 40069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.auth.params.AuthParams; 41069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.message.BufferedHeader; 42069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.util.CharArrayBuffer; 43069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.util.EncodingUtils; 44069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 45069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/** 46069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <p> 47069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Basic authentication scheme as defined in RFC 2617. 48069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * </p> 49069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 50069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:remm@apache.org">Remy Maucherat</a> 51069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author Rodney Waldhoff 52069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a> 53069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author Ortwin Glueck 54069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author Sean C. Sullivan 55069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a> 56069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> 57069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> 58069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 59069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @since 4.0 60069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 61069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 62069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpublic class BasicScheme extends RFC2617Scheme { 63069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 64069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** Whether the basic authentication process is complete */ 65069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project private boolean complete; 66069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 67069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 68069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Default constructor for the basic authetication scheme. 69069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 70069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public BasicScheme() { 71069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project super(); 72069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project this.complete = false; 73069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 74069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 75069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 76069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Returns textual designation of the basic authentication scheme. 77069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 78069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @return <code>basic</code> 79069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 80069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public String getSchemeName() { 81069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return "basic"; 82069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 83069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 84069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 85069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Processes the Basic challenge. 86069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 87069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param header the challenge header 88069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 89069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @throws MalformedChallengeException is thrown if the authentication challenge 90069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * is malformed 91069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 92069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project @Override 93069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public void processChallenge( 94069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project final Header header) throws MalformedChallengeException { 95069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project super.processChallenge(header); 96069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project this.complete = true; 97069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 98069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 99069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 100069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Tests if the Basic authentication process has been completed. 101069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 102069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @return <tt>true</tt> if Basic authorization has been processed, 103069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <tt>false</tt> otherwise. 104069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 105069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public boolean isComplete() { 106069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return this.complete; 107069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 108069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 109069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 110069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Returns <tt>false</tt>. Basic authentication scheme is request based. 111069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 112069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @return <tt>false</tt>. 113069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 114069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public boolean isConnectionBased() { 115069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return false; 116069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 117069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 118069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 119069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Produces basic authorization header for the given set of {@link Credentials}. 120069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 121069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param credentials The set of credentials to be used for athentication 122069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param request The request being authenticated 123069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @throws org.apache.http.auth.InvalidCredentialsException if authentication credentials 124069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * are not valid or not applicable for this authentication scheme 125069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @throws AuthenticationException if authorization string cannot 126069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * be generated due to an authentication failure 127069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 128069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @return a basic authorization string 129069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 130069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public Header authenticate( 131069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project final Credentials credentials, 132069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project final HttpRequest request) throws AuthenticationException { 133069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 134069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (credentials == null) { 135069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project throw new IllegalArgumentException("Credentials may not be null"); 136069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 137069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (request == null) { 138069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project throw new IllegalArgumentException("HTTP request may not be null"); 139069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 140069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 141069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project String charset = AuthParams.getCredentialCharset(request.getParams()); 142069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return authenticate(credentials, charset, isProxy()); 143069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 144069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 145069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project /** 146069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Returns a basic <tt>Authorization</tt> header value for the given 147069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * {@link Credentials} and charset. 148069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 149069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param credentials The credentials to encode. 150069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @param charset The charset to use for encoding the credentials 151069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * 152069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @return a basic authorization header 153069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */ 154069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project public static Header authenticate( 155069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project final Credentials credentials, 156069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project final String charset, 157069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project boolean proxy) { 158069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (credentials == null) { 159069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project throw new IllegalArgumentException("Credentials may not be null"); 160069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 161069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (charset == null) { 162069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project throw new IllegalArgumentException("charset may not be null"); 163069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 164069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 165069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project StringBuilder tmp = new StringBuilder(); 166069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project tmp.append(credentials.getUserPrincipal().getName()); 167069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project tmp.append(":"); 168069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword()); 169069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 170069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project byte[] base64password = Base64.encodeBase64( 171069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project EncodingUtils.getBytes(tmp.toString(), charset)); 172069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 173069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project CharArrayBuffer buffer = new CharArrayBuffer(32); 174069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project if (proxy) { 175069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project buffer.append(AUTH.PROXY_AUTH_RESP); 176069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } else { 177069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project buffer.append(AUTH.WWW_AUTH_RESP); 178069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 179069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project buffer.append(": Basic "); 180069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project buffer.append(base64password, 0, base64password.length); 181069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 182069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project return new BufferedHeader(buffer); 183069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project } 184069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project 185069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project} 186