Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * ==================================================================== |
michael@0 | 3 | * |
michael@0 | 4 | * Licensed to the Apache Software Foundation (ASF) under one or more |
michael@0 | 5 | * contributor license agreements. See the NOTICE file distributed with |
michael@0 | 6 | * this work for additional information regarding copyright ownership. |
michael@0 | 7 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
michael@0 | 8 | * (the "License"); you may not use this file except in compliance with |
michael@0 | 9 | * the License. You may obtain a copy of the License at |
michael@0 | 10 | * |
michael@0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 12 | * |
michael@0 | 13 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 16 | * See the License for the specific language governing permissions and |
michael@0 | 17 | * limitations under the License. |
michael@0 | 18 | * ==================================================================== |
michael@0 | 19 | * |
michael@0 | 20 | * This software consists of voluntary contributions made by many |
michael@0 | 21 | * individuals on behalf of the Apache Software Foundation. For more |
michael@0 | 22 | * information on the Apache Software Foundation, please see |
michael@0 | 23 | * <http://www.apache.org/>. |
michael@0 | 24 | * |
michael@0 | 25 | */ |
michael@0 | 26 | |
michael@0 | 27 | package ch.boye.httpclientandroidlib.impl.auth; |
michael@0 | 28 | |
michael@0 | 29 | import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; |
michael@0 | 30 | |
michael@0 | 31 | import org.mozilla.apache.commons.codec.binary.Base64; |
michael@0 | 32 | import ch.boye.httpclientandroidlib.Header; |
michael@0 | 33 | import ch.boye.httpclientandroidlib.HttpRequest; |
michael@0 | 34 | import ch.boye.httpclientandroidlib.auth.AuthenticationException; |
michael@0 | 35 | import ch.boye.httpclientandroidlib.auth.Credentials; |
michael@0 | 36 | import ch.boye.httpclientandroidlib.auth.AUTH; |
michael@0 | 37 | import ch.boye.httpclientandroidlib.auth.InvalidCredentialsException; |
michael@0 | 38 | import ch.boye.httpclientandroidlib.auth.MalformedChallengeException; |
michael@0 | 39 | import ch.boye.httpclientandroidlib.auth.params.AuthParams; |
michael@0 | 40 | import ch.boye.httpclientandroidlib.message.BufferedHeader; |
michael@0 | 41 | import ch.boye.httpclientandroidlib.util.CharArrayBuffer; |
michael@0 | 42 | import ch.boye.httpclientandroidlib.util.EncodingUtils; |
michael@0 | 43 | |
michael@0 | 44 | /** |
michael@0 | 45 | * Basic authentication scheme as defined in RFC 2617. |
michael@0 | 46 | * <p> |
michael@0 | 47 | * The following parameters can be used to customize the behavior of this |
michael@0 | 48 | * class: |
michael@0 | 49 | * <ul> |
michael@0 | 50 | * <li>{@link ch.boye.httpclientandroidlib.auth.params.AuthPNames#CREDENTIAL_CHARSET}</li> |
michael@0 | 51 | * </ul> |
michael@0 | 52 | * |
michael@0 | 53 | * @since 4.0 |
michael@0 | 54 | */ |
michael@0 | 55 | @NotThreadSafe |
michael@0 | 56 | public class BasicScheme extends RFC2617Scheme { |
michael@0 | 57 | |
michael@0 | 58 | /** Whether the basic authentication process is complete */ |
michael@0 | 59 | private boolean complete; |
michael@0 | 60 | |
michael@0 | 61 | /** |
michael@0 | 62 | * Default constructor for the basic authentication scheme. |
michael@0 | 63 | */ |
michael@0 | 64 | public BasicScheme() { |
michael@0 | 65 | super(); |
michael@0 | 66 | this.complete = false; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | /** |
michael@0 | 70 | * Returns textual designation of the basic authentication scheme. |
michael@0 | 71 | * |
michael@0 | 72 | * @return <code>basic</code> |
michael@0 | 73 | */ |
michael@0 | 74 | public String getSchemeName() { |
michael@0 | 75 | return "basic"; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | /** |
michael@0 | 79 | * Processes the Basic challenge. |
michael@0 | 80 | * |
michael@0 | 81 | * @param header the challenge header |
michael@0 | 82 | * |
michael@0 | 83 | * @throws MalformedChallengeException is thrown if the authentication challenge |
michael@0 | 84 | * is malformed |
michael@0 | 85 | */ |
michael@0 | 86 | @Override |
michael@0 | 87 | public void processChallenge( |
michael@0 | 88 | final Header header) throws MalformedChallengeException { |
michael@0 | 89 | super.processChallenge(header); |
michael@0 | 90 | this.complete = true; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | /** |
michael@0 | 94 | * Tests if the Basic authentication process has been completed. |
michael@0 | 95 | * |
michael@0 | 96 | * @return <tt>true</tt> if Basic authorization has been processed, |
michael@0 | 97 | * <tt>false</tt> otherwise. |
michael@0 | 98 | */ |
michael@0 | 99 | public boolean isComplete() { |
michael@0 | 100 | return this.complete; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | /** |
michael@0 | 104 | * Returns <tt>false</tt>. Basic authentication scheme is request based. |
michael@0 | 105 | * |
michael@0 | 106 | * @return <tt>false</tt>. |
michael@0 | 107 | */ |
michael@0 | 108 | public boolean isConnectionBased() { |
michael@0 | 109 | return false; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | /** |
michael@0 | 113 | * Produces basic authorization header for the given set of {@link Credentials}. |
michael@0 | 114 | * |
michael@0 | 115 | * @param credentials The set of credentials to be used for authentication |
michael@0 | 116 | * @param request The request being authenticated |
michael@0 | 117 | * @throws InvalidCredentialsException if authentication credentials are not |
michael@0 | 118 | * valid or not applicable for this authentication scheme |
michael@0 | 119 | * @throws AuthenticationException if authorization string cannot |
michael@0 | 120 | * be generated due to an authentication failure |
michael@0 | 121 | * |
michael@0 | 122 | * @return a basic authorization string |
michael@0 | 123 | */ |
michael@0 | 124 | public Header authenticate( |
michael@0 | 125 | final Credentials credentials, |
michael@0 | 126 | final HttpRequest request) throws AuthenticationException { |
michael@0 | 127 | |
michael@0 | 128 | if (credentials == null) { |
michael@0 | 129 | throw new IllegalArgumentException("Credentials may not be null"); |
michael@0 | 130 | } |
michael@0 | 131 | if (request == null) { |
michael@0 | 132 | throw new IllegalArgumentException("HTTP request may not be null"); |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | String charset = AuthParams.getCredentialCharset(request.getParams()); |
michael@0 | 136 | return authenticate(credentials, charset, isProxy()); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | /** |
michael@0 | 140 | * Returns a basic <tt>Authorization</tt> header value for the given |
michael@0 | 141 | * {@link Credentials} and charset. |
michael@0 | 142 | * |
michael@0 | 143 | * @param credentials The credentials to encode. |
michael@0 | 144 | * @param charset The charset to use for encoding the credentials |
michael@0 | 145 | * |
michael@0 | 146 | * @return a basic authorization header |
michael@0 | 147 | */ |
michael@0 | 148 | public static Header authenticate( |
michael@0 | 149 | final Credentials credentials, |
michael@0 | 150 | final String charset, |
michael@0 | 151 | boolean proxy) { |
michael@0 | 152 | if (credentials == null) { |
michael@0 | 153 | throw new IllegalArgumentException("Credentials may not be null"); |
michael@0 | 154 | } |
michael@0 | 155 | if (charset == null) { |
michael@0 | 156 | throw new IllegalArgumentException("charset may not be null"); |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | StringBuilder tmp = new StringBuilder(); |
michael@0 | 160 | tmp.append(credentials.getUserPrincipal().getName()); |
michael@0 | 161 | tmp.append(":"); |
michael@0 | 162 | tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword()); |
michael@0 | 163 | |
michael@0 | 164 | byte[] base64password = Base64.encodeBase64( |
michael@0 | 165 | EncodingUtils.getBytes(tmp.toString(), charset)); |
michael@0 | 166 | |
michael@0 | 167 | CharArrayBuffer buffer = new CharArrayBuffer(32); |
michael@0 | 168 | if (proxy) { |
michael@0 | 169 | buffer.append(AUTH.PROXY_AUTH_RESP); |
michael@0 | 170 | } else { |
michael@0 | 171 | buffer.append(AUTH.WWW_AUTH_RESP); |
michael@0 | 172 | } |
michael@0 | 173 | buffer.append(": Basic "); |
michael@0 | 174 | buffer.append(base64password, 0, base64password.length); |
michael@0 | 175 | |
michael@0 | 176 | return new BufferedHeader(buffer); |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | } |