1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/auth/BasicScheme.java $
3 * $Revision: 658430 $
4 * $Date: 2008-05-20 14:04:27 -0700 (Tue, 20 May 2008) $
5 *
6 * ====================================================================
7 *
8 *  Licensed to the Apache Software Foundation (ASF) under one or more
9 *  contributor license agreements.  See the NOTICE file distributed with
10 *  this work for additional information regarding copyright ownership.
11 *  The ASF licenses this file to You under the Apache License, Version 2.0
12 *  (the "License"); you may not use this file except in compliance with
13 *  the License.  You may obtain a copy of the License at
14 *
15 *      http://www.apache.org/licenses/LICENSE-2.0
16 *
17 *  Unless required by applicable law or agreed to in writing, software
18 *  distributed under the License is distributed on an "AS IS" BASIS,
19 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 *  See the License for the specific language governing permissions and
21 *  limitations under the License.
22 * ====================================================================
23 *
24 * This software consists of voluntary contributions made by many
25 * individuals on behalf of the Apache Software Foundation.  For more
26 * information on the Apache Software Foundation, please see
27 * <http://www.apache.org/>.
28 *
29 */
30
31package org.apache.http.impl.auth;
32
33import org.apache.commons.codec.binary.Base64;
34import org.apache.http.Header;
35import org.apache.http.HttpRequest;
36import org.apache.http.auth.AuthenticationException;
37import org.apache.http.auth.Credentials;
38import org.apache.http.auth.AUTH;
39import org.apache.http.auth.MalformedChallengeException;
40import org.apache.http.auth.params.AuthParams;
41import org.apache.http.message.BufferedHeader;
42import org.apache.http.util.CharArrayBuffer;
43import org.apache.http.util.EncodingUtils;
44
45/**
46 * <p>
47 * Basic authentication scheme as defined in RFC 2617.
48 * </p>
49 *
50 * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
51 * @author Rodney Waldhoff
52 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
53 * @author Ortwin Glueck
54 * @author Sean C. Sullivan
55 * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a>
56 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
57 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
58 *
59 * @since 4.0
60 *
61 * @deprecated Please use {@link java.net.URL#openConnection} instead.
62 *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
63 *     for further details.
64 */
65
66@Deprecated
67public class BasicScheme extends RFC2617Scheme {
68
69    /** Whether the basic authentication process is complete */
70    private boolean complete;
71
72    /**
73     * Default constructor for the basic authetication scheme.
74     */
75    public BasicScheme() {
76        super();
77        this.complete = false;
78    }
79
80    /**
81     * Returns textual designation of the basic authentication scheme.
82     *
83     * @return <code>basic</code>
84     */
85    public String getSchemeName() {
86        return "basic";
87    }
88
89    /**
90     * Processes the Basic challenge.
91     *
92     * @param header the challenge header
93     *
94     * @throws MalformedChallengeException is thrown if the authentication challenge
95     * is malformed
96     */
97    @Override
98    public void processChallenge(
99            final Header header) throws MalformedChallengeException {
100        super.processChallenge(header);
101        this.complete = true;
102    }
103
104    /**
105     * Tests if the Basic authentication process has been completed.
106     *
107     * @return <tt>true</tt> if Basic authorization has been processed,
108     *   <tt>false</tt> otherwise.
109     */
110    public boolean isComplete() {
111        return this.complete;
112    }
113
114    /**
115     * Returns <tt>false</tt>. Basic authentication scheme is request based.
116     *
117     * @return <tt>false</tt>.
118     */
119    public boolean isConnectionBased() {
120        return false;
121    }
122
123    /**
124     * Produces basic authorization header for the given set of {@link Credentials}.
125     *
126     * @param credentials The set of credentials to be used for athentication
127     * @param request The request being authenticated
128     * @throws org.apache.http.auth.InvalidCredentialsException if authentication credentials
129     *         are not valid or not applicable for this authentication scheme
130     * @throws AuthenticationException if authorization string cannot
131     *   be generated due to an authentication failure
132     *
133     * @return a basic authorization string
134     */
135    public Header authenticate(
136            final Credentials credentials,
137            final HttpRequest request) throws AuthenticationException {
138
139        if (credentials == null) {
140            throw new IllegalArgumentException("Credentials may not be null");
141        }
142        if (request == null) {
143            throw new IllegalArgumentException("HTTP request may not be null");
144        }
145
146        String charset = AuthParams.getCredentialCharset(request.getParams());
147        return authenticate(credentials, charset, isProxy());
148    }
149
150    /**
151     * Returns a basic <tt>Authorization</tt> header value for the given
152     * {@link Credentials} and charset.
153     *
154     * @param credentials The credentials to encode.
155     * @param charset The charset to use for encoding the credentials
156     *
157     * @return a basic authorization header
158     */
159    public static Header authenticate(
160            final Credentials credentials,
161            final String charset,
162            boolean proxy) {
163        if (credentials == null) {
164            throw new IllegalArgumentException("Credentials may not be null");
165        }
166        if (charset == null) {
167            throw new IllegalArgumentException("charset may not be null");
168        }
169
170        StringBuilder tmp = new StringBuilder();
171        tmp.append(credentials.getUserPrincipal().getName());
172        tmp.append(":");
173        tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword());
174
175        byte[] base64password = Base64.encodeBase64(
176                EncodingUtils.getBytes(tmp.toString(), charset));
177
178        CharArrayBuffer buffer = new CharArrayBuffer(32);
179        if (proxy) {
180            buffer.append(AUTH.PROXY_AUTH_RESP);
181        } else {
182            buffer.append(AUTH.WWW_AUTH_RESP);
183        }
184        buffer.append(": Basic ");
185        buffer.append(base64password, 0, base64password.length);
186
187        return new BufferedHeader(buffer);
188    }
189
190}
191