001: /*
002: * $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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient.auth;
032:
033: import org.apache.commons.codec.binary.Base64;
034: import org.apache.commons.httpclient.Credentials;
035: import org.apache.commons.httpclient.HttpMethod;
036: import org.apache.commons.httpclient.UsernamePasswordCredentials;
037: import org.apache.commons.httpclient.util.EncodingUtil;
038: import org.apache.commons.logging.Log;
039: import org.apache.commons.logging.LogFactory;
040:
041: /**
042: * <p>
043: * Basic authentication scheme as defined in RFC 2617.
044: * </p>
045: *
046: * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
047: * @author Rodney Waldhoff
048: * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
049: * @author Ortwin Gl?ck
050: * @author Sean C. Sullivan
051: * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a>
052: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
053: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
054: */
055:
056: public class BasicScheme extends RFC2617Scheme {
057:
058: /** Log object for this class. */
059: private static final Log LOG = LogFactory.getLog(BasicScheme.class);
060:
061: /** Whether the basic authentication process is complete */
062: private boolean complete;
063:
064: /**
065: * Default constructor for the basic authetication scheme.
066: *
067: * @since 3.0
068: */
069: public BasicScheme() {
070: super ();
071: this .complete = false;
072: }
073:
074: /**
075: * Constructor for the basic authetication scheme.
076: *
077: * @param challenge authentication challenge
078: *
079: * @throws MalformedChallengeException is thrown if the authentication challenge
080: * is malformed
081: *
082: * @deprecated Use parameterless constructor and {@link AuthScheme#processChallenge(String)}
083: * method
084: */
085: public BasicScheme(final String challenge)
086: throws MalformedChallengeException {
087: super (challenge);
088: this .complete = true;
089: }
090:
091: /**
092: * Returns textual designation of the basic authentication scheme.
093: *
094: * @return <code>basic</code>
095: */
096: public String getSchemeName() {
097: return "basic";
098: }
099:
100: /**
101: * Processes the Basic challenge.
102: *
103: * @param challenge the challenge string
104: *
105: * @throws MalformedChallengeException is thrown if the authentication challenge
106: * is malformed
107: *
108: * @since 3.0
109: */
110: public void processChallenge(String challenge)
111: throws MalformedChallengeException {
112: super .processChallenge(challenge);
113: this .complete = true;
114: }
115:
116: /**
117: * Tests if the Basic authentication process has been completed.
118: *
119: * @return <tt>true</tt> if Basic authorization has been processed,
120: * <tt>false</tt> otherwise.
121: *
122: * @since 3.0
123: */
124: public boolean isComplete() {
125: return this .complete;
126: }
127:
128: /**
129: * Produces basic authorization string for the given set of
130: * {@link Credentials}.
131: *
132: * @param credentials The set of credentials to be used for athentication
133: * @param method Method name is ignored by the basic authentication scheme
134: * @param uri URI is ignored by the basic authentication scheme
135: * @throws InvalidCredentialsException if authentication credentials
136: * are not valid or not applicable for this authentication scheme
137: * @throws AuthenticationException if authorization string cannot
138: * be generated due to an authentication failure
139: *
140: * @return a basic authorization string
141: *
142: * @deprecated Use {@link #authenticate(Credentials, HttpMethod)}
143: */
144: public String authenticate(Credentials credentials, String method,
145: String uri) throws AuthenticationException {
146:
147: LOG
148: .trace("enter BasicScheme.authenticate(Credentials, String, String)");
149:
150: UsernamePasswordCredentials usernamepassword = null;
151: try {
152: usernamepassword = (UsernamePasswordCredentials) credentials;
153: } catch (ClassCastException e) {
154: throw new InvalidCredentialsException(
155: "Credentials cannot be used for basic authentication: "
156: + credentials.getClass().getName());
157: }
158: return BasicScheme.authenticate(usernamepassword);
159: }
160:
161: /**
162: * Returns <tt>false</tt>. Basic authentication scheme is request based.
163: *
164: * @return <tt>false</tt>.
165: *
166: * @since 3.0
167: */
168: public boolean isConnectionBased() {
169: return false;
170: }
171:
172: /**
173: * Produces basic authorization string for the given set of {@link Credentials}.
174: *
175: * @param credentials The set of credentials to be used for athentication
176: * @param method The method being authenticated
177: * @throws InvalidCredentialsException if authentication credentials
178: * are not valid or not applicable for this authentication scheme
179: * @throws AuthenticationException if authorization string cannot
180: * be generated due to an authentication failure
181: *
182: * @return a basic authorization string
183: *
184: * @since 3.0
185: */
186: public String authenticate(Credentials credentials,
187: HttpMethod method) throws AuthenticationException {
188:
189: LOG
190: .trace("enter BasicScheme.authenticate(Credentials, HttpMethod)");
191:
192: if (method == null) {
193: throw new IllegalArgumentException("Method may not be null");
194: }
195: UsernamePasswordCredentials usernamepassword = null;
196: try {
197: usernamepassword = (UsernamePasswordCredentials) credentials;
198: } catch (ClassCastException e) {
199: throw new InvalidCredentialsException(
200: "Credentials cannot be used for basic authentication: "
201: + credentials.getClass().getName());
202: }
203: return BasicScheme.authenticate(usernamepassword, method
204: .getParams().getCredentialCharset());
205: }
206:
207: /**
208: * @deprecated Use {@link #authenticate(UsernamePasswordCredentials, String)}
209: *
210: * Returns a basic <tt>Authorization</tt> header value for the given
211: * {@link UsernamePasswordCredentials}.
212: *
213: * @param credentials The credentials to encode.
214: *
215: * @return a basic authorization string
216: */
217: public static String authenticate(
218: UsernamePasswordCredentials credentials) {
219: return authenticate(credentials, "ISO-8859-1");
220: }
221:
222: /**
223: * Returns a basic <tt>Authorization</tt> header value for the given
224: * {@link UsernamePasswordCredentials} and charset.
225: *
226: * @param credentials The credentials to encode.
227: * @param charset The charset to use for encoding the credentials
228: *
229: * @return a basic authorization string
230: *
231: * @since 3.0
232: */
233: public static String authenticate(
234: UsernamePasswordCredentials credentials, String charset) {
235:
236: LOG
237: .trace("enter BasicScheme.authenticate(UsernamePasswordCredentials, String)");
238:
239: if (credentials == null) {
240: throw new IllegalArgumentException(
241: "Credentials may not be null");
242: }
243: if (charset == null || charset.length() == 0) {
244: throw new IllegalArgumentException(
245: "charset may not be null or empty");
246: }
247: StringBuffer buffer = new StringBuffer();
248: buffer.append(credentials.getUserName());
249: buffer.append(":");
250: buffer.append(credentials.getPassword());
251:
252: return "Basic "
253: + EncodingUtil.getAsciiString(Base64
254: .encodeBase64(EncodingUtil.getBytes(buffer
255: .toString(), charset)));
256: }
257:
258: }
|