1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/auth/BasicScheme.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,179 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * 1.7 + * Licensed to the Apache Software Foundation (ASF) under one or more 1.8 + * contributor license agreements. See the NOTICE file distributed with 1.9 + * this work for additional information regarding copyright ownership. 1.10 + * The ASF licenses this file to You under the Apache License, Version 2.0 1.11 + * (the "License"); you may not use this file except in compliance with 1.12 + * the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, software 1.17 + * distributed under the License is distributed on an "AS IS" BASIS, 1.18 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.19 + * See the License for the specific language governing permissions and 1.20 + * limitations under the License. 1.21 + * ==================================================================== 1.22 + * 1.23 + * This software consists of voluntary contributions made by many 1.24 + * individuals on behalf of the Apache Software Foundation. For more 1.25 + * information on the Apache Software Foundation, please see 1.26 + * <http://www.apache.org/>. 1.27 + * 1.28 + */ 1.29 + 1.30 +package ch.boye.httpclientandroidlib.impl.auth; 1.31 + 1.32 +import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; 1.33 + 1.34 +import org.mozilla.apache.commons.codec.binary.Base64; 1.35 +import ch.boye.httpclientandroidlib.Header; 1.36 +import ch.boye.httpclientandroidlib.HttpRequest; 1.37 +import ch.boye.httpclientandroidlib.auth.AuthenticationException; 1.38 +import ch.boye.httpclientandroidlib.auth.Credentials; 1.39 +import ch.boye.httpclientandroidlib.auth.AUTH; 1.40 +import ch.boye.httpclientandroidlib.auth.InvalidCredentialsException; 1.41 +import ch.boye.httpclientandroidlib.auth.MalformedChallengeException; 1.42 +import ch.boye.httpclientandroidlib.auth.params.AuthParams; 1.43 +import ch.boye.httpclientandroidlib.message.BufferedHeader; 1.44 +import ch.boye.httpclientandroidlib.util.CharArrayBuffer; 1.45 +import ch.boye.httpclientandroidlib.util.EncodingUtils; 1.46 + 1.47 +/** 1.48 + * Basic authentication scheme as defined in RFC 2617. 1.49 + * <p> 1.50 + * The following parameters can be used to customize the behavior of this 1.51 + * class: 1.52 + * <ul> 1.53 + * <li>{@link ch.boye.httpclientandroidlib.auth.params.AuthPNames#CREDENTIAL_CHARSET}</li> 1.54 + * </ul> 1.55 + * 1.56 + * @since 4.0 1.57 + */ 1.58 +@NotThreadSafe 1.59 +public class BasicScheme extends RFC2617Scheme { 1.60 + 1.61 + /** Whether the basic authentication process is complete */ 1.62 + private boolean complete; 1.63 + 1.64 + /** 1.65 + * Default constructor for the basic authentication scheme. 1.66 + */ 1.67 + public BasicScheme() { 1.68 + super(); 1.69 + this.complete = false; 1.70 + } 1.71 + 1.72 + /** 1.73 + * Returns textual designation of the basic authentication scheme. 1.74 + * 1.75 + * @return <code>basic</code> 1.76 + */ 1.77 + public String getSchemeName() { 1.78 + return "basic"; 1.79 + } 1.80 + 1.81 + /** 1.82 + * Processes the Basic challenge. 1.83 + * 1.84 + * @param header the challenge header 1.85 + * 1.86 + * @throws MalformedChallengeException is thrown if the authentication challenge 1.87 + * is malformed 1.88 + */ 1.89 + @Override 1.90 + public void processChallenge( 1.91 + final Header header) throws MalformedChallengeException { 1.92 + super.processChallenge(header); 1.93 + this.complete = true; 1.94 + } 1.95 + 1.96 + /** 1.97 + * Tests if the Basic authentication process has been completed. 1.98 + * 1.99 + * @return <tt>true</tt> if Basic authorization has been processed, 1.100 + * <tt>false</tt> otherwise. 1.101 + */ 1.102 + public boolean isComplete() { 1.103 + return this.complete; 1.104 + } 1.105 + 1.106 + /** 1.107 + * Returns <tt>false</tt>. Basic authentication scheme is request based. 1.108 + * 1.109 + * @return <tt>false</tt>. 1.110 + */ 1.111 + public boolean isConnectionBased() { 1.112 + return false; 1.113 + } 1.114 + 1.115 + /** 1.116 + * Produces basic authorization header for the given set of {@link Credentials}. 1.117 + * 1.118 + * @param credentials The set of credentials to be used for authentication 1.119 + * @param request The request being authenticated 1.120 + * @throws InvalidCredentialsException if authentication credentials are not 1.121 + * valid or not applicable for this authentication scheme 1.122 + * @throws AuthenticationException if authorization string cannot 1.123 + * be generated due to an authentication failure 1.124 + * 1.125 + * @return a basic authorization string 1.126 + */ 1.127 + public Header authenticate( 1.128 + final Credentials credentials, 1.129 + final HttpRequest request) throws AuthenticationException { 1.130 + 1.131 + if (credentials == null) { 1.132 + throw new IllegalArgumentException("Credentials may not be null"); 1.133 + } 1.134 + if (request == null) { 1.135 + throw new IllegalArgumentException("HTTP request may not be null"); 1.136 + } 1.137 + 1.138 + String charset = AuthParams.getCredentialCharset(request.getParams()); 1.139 + return authenticate(credentials, charset, isProxy()); 1.140 + } 1.141 + 1.142 + /** 1.143 + * Returns a basic <tt>Authorization</tt> header value for the given 1.144 + * {@link Credentials} and charset. 1.145 + * 1.146 + * @param credentials The credentials to encode. 1.147 + * @param charset The charset to use for encoding the credentials 1.148 + * 1.149 + * @return a basic authorization header 1.150 + */ 1.151 + public static Header authenticate( 1.152 + final Credentials credentials, 1.153 + final String charset, 1.154 + boolean proxy) { 1.155 + if (credentials == null) { 1.156 + throw new IllegalArgumentException("Credentials may not be null"); 1.157 + } 1.158 + if (charset == null) { 1.159 + throw new IllegalArgumentException("charset may not be null"); 1.160 + } 1.161 + 1.162 + StringBuilder tmp = new StringBuilder(); 1.163 + tmp.append(credentials.getUserPrincipal().getName()); 1.164 + tmp.append(":"); 1.165 + tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword()); 1.166 + 1.167 + byte[] base64password = Base64.encodeBase64( 1.168 + EncodingUtils.getBytes(tmp.toString(), charset)); 1.169 + 1.170 + CharArrayBuffer buffer = new CharArrayBuffer(32); 1.171 + if (proxy) { 1.172 + buffer.append(AUTH.PROXY_AUTH_RESP); 1.173 + } else { 1.174 + buffer.append(AUTH.WWW_AUTH_RESP); 1.175 + } 1.176 + buffer.append(": Basic "); 1.177 + buffer.append(base64password, 0, base64password.length); 1.178 + 1.179 + return new BufferedHeader(buffer); 1.180 + } 1.181 + 1.182 +}