2
* $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/auth/BasicScheme.java,v 1.4.2.3 2004/02/22 18:21:14 olegk Exp $
4
* $Date: 2004/02/22 18:21:14 $
2
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/auth/BasicScheme.java,v 1.17 2004/05/13 04:02:00 mbecke Exp $
4
* $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
6
6
* ====================================================================
25
25
* information on the Apache Software Foundation, please see
26
26
* <http://www.apache.org/>.
28
* [Additional notices, if required by prior licensing conditions]
32
30
package org.apache.commons.httpclient.auth;
34
import org.apache.commons.httpclient.HttpConstants;
32
import org.apache.commons.codec.binary.Base64;
35
33
import org.apache.commons.httpclient.Credentials;
34
import org.apache.commons.httpclient.HttpMethod;
36
35
import org.apache.commons.httpclient.UsernamePasswordCredentials;
37
import org.apache.commons.httpclient.util.Base64;
36
import org.apache.commons.httpclient.util.EncodingUtil;
38
37
import org.apache.commons.logging.Log;
39
38
import org.apache.commons.logging.LogFactory;
46
45
* @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
47
46
* @author Rodney Waldhoff
48
47
* @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
49
* @author Ortwin Gl�ck
48
* @author Ortwin Gl?ck
50
49
* @author Sean C. Sullivan
51
50
* @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a>
52
51
* @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
58
57
/** Log object for this class. */
59
58
private static final Log LOG = LogFactory.getLog(BasicScheme.class);
60
/** Whether the basic authentication process is complete */
61
private boolean complete;
64
* Default constructor for the basic authetication scheme.
68
public BasicScheme() {
70
this.complete = false;
62
74
* Constructor for the basic authetication scheme.
66
78
* @throws MalformedChallengeException is thrown if the authentication challenge
81
* @deprecated Use parameterless constructor and {@link AuthScheme#processChallenge(String)}
69
84
public BasicScheme(final String challenge) throws MalformedChallengeException {
75
90
* Returns textual designation of the basic authentication scheme.
99
* Processes the Basic challenge.
101
* @param challenge the challenge string
103
* @throws MalformedChallengeException is thrown if the authentication challenge
108
public void processChallenge(String challenge)
109
throws MalformedChallengeException
111
super.processChallenge(challenge);
112
this.complete = true;
116
* Tests if the Basic authentication process has been completed.
118
* @return <tt>true</tt> if Basic authorization has been processed,
119
* <tt>false</tt> otherwise.
123
public boolean isComplete() {
124
return this.complete;
84
128
* Produces basic authorization string for the given set of
85
129
* {@link Credentials}.
87
131
* @param credentials The set of credentials to be used for athentication
88
132
* @param method Method name is ignored by the basic authentication scheme
89
133
* @param uri URI is ignored by the basic authentication scheme
134
* @throws InvalidCredentialsException if authentication credentials
135
* are not valid or not applicable for this authentication scheme
90
136
* @throws AuthenticationException if authorization string cannot
91
137
* be generated due to an authentication failure
93
139
* @return a basic authorization string
141
* @deprecated Use {@link #authenticate(Credentials, HttpMethod)}
95
143
public String authenticate(Credentials credentials, String method, String uri)
96
144
throws AuthenticationException {
102
150
usernamepassword = (UsernamePasswordCredentials) credentials;
103
151
} catch (ClassCastException e) {
104
throw new AuthenticationException(
152
throw new InvalidCredentialsException(
105
153
"Credentials cannot be used for basic authentication: "
106
154
+ credentials.getClass().getName());
112
* Return a basic <tt>Authorization</tt> header value for the given
160
* Returns <tt>false</tt>. Basic authentication scheme is request based.
162
* @return <tt>false</tt>.
166
public boolean isConnectionBased() {
171
* Produces basic authorization string for the given set of {@link Credentials}.
173
* @param credentials The set of credentials to be used for athentication
174
* @param method The method being authenticated
175
* @throws InvalidCredentialsException if authentication credentials
176
* are not valid or not applicable for this authentication scheme
177
* @throws AuthenticationException if authorization string cannot
178
* be generated due to an authentication failure
180
* @return a basic authorization string
184
public String authenticate(Credentials credentials, HttpMethod method) throws AuthenticationException {
186
LOG.trace("enter BasicScheme.authenticate(Credentials, HttpMethod)");
188
if (method == null) {
189
throw new IllegalArgumentException("Method may not be null");
191
UsernamePasswordCredentials usernamepassword = null;
193
usernamepassword = (UsernamePasswordCredentials) credentials;
194
} catch (ClassCastException e) {
195
throw new InvalidCredentialsException(
196
"Credentials cannot be used for basic authentication: "
197
+ credentials.getClass().getName());
199
return BasicScheme.authenticate(
201
method.getParams().getCredentialCharset());
205
* @deprecated Use {@link #authenticate(UsernamePasswordCredentials, String)}
207
* Returns a basic <tt>Authorization</tt> header value for the given
113
208
* {@link UsernamePasswordCredentials}.
115
210
* @param credentials The credentials to encode.
117
212
* @return a basic authorization string
119
214
public static String authenticate(UsernamePasswordCredentials credentials) {
121
LOG.trace("enter BasicScheme.authenticate(UsernamePasswordCredentials)");
215
return authenticate(credentials, "ISO-8859-1");
219
* Returns a basic <tt>Authorization</tt> header value for the given
220
* {@link UsernamePasswordCredentials} and charset.
222
* @param credentials The credentials to encode.
223
* @param charset The charset to use for encoding the credentials
225
* @return a basic authorization string
229
public static String authenticate(UsernamePasswordCredentials credentials, String charset) {
231
LOG.trace("enter BasicScheme.authenticate(UsernamePasswordCredentials, String)");
123
233
if (credentials == null) {
124
234
throw new IllegalArgumentException("Credentials may not be null");
236
if (charset == null || charset.length() == 0) {
237
throw new IllegalArgumentException("charset may not be null or empty");
126
239
StringBuffer buffer = new StringBuffer();
127
240
buffer.append(credentials.getUserName());
128
241
buffer.append(":");
129
242
buffer.append(credentials.getPassword());
131
return "Basic " + HttpConstants.getAsciiString(
132
Base64.encode(HttpConstants.getContentBytes(buffer.toString())));
244
return "Basic " + EncodingUtil.getAsciiString(
245
Base64.encodeBase64(EncodingUtil.getBytes(buffer.toString(), charset)));