rwaldhoff 01/08/14 11:02:49 Modified: httpclient/src/java/org/apache/commons/httpclient Tag: rlwrefactoring Authenticator.java Log: removing deprecated methods, javadoc Revision Changes Path No revision No revision 1.6.2.2 +38 -66 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Authenticator.java Index: Authenticator.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Authenticator.java,v retrieving revision 1.6.2.1 retrieving revision 1.6.2.2 diff -u -r1.6.2.1 -r1.6.2.2 --- Authenticator.java 2001/08/13 15:50:13 1.6.2.1 +++ Authenticator.java 2001/08/14 18:02:49 1.6.2.2 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Authenticator.java,v 1.6.2.1 2001/08/13 15:50:13 rwaldhoff Exp $ - * $Revision: 1.6.2.1 $ - * $Date: 2001/08/13 15:50:13 $ + * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Authenticator.java,v 1.6.2.2 2001/08/14 18:02:49 rwaldhoff Exp $ + * $Revision: 1.6.2.2 $ + * $Date: 2001/08/14 18:02:49 $ * * ==================================================================== * @@ -68,67 +68,33 @@ import org.apache.commons.httpclient.log.*; /** - * Authenticate helper. + * Authorization helper. + * <p> + * This class provides utility methods for generating + * responses to HTTP authentication challenges. * * @author <a href="mailto:[EMAIL PROTECTED]">Remy Maucherat</a> + * @version $Id: Authenticator.java,v 1.6.2.2 2001/08/14 18:02:49 rwaldhoff Exp $ */ public class Authenticator { - static private final Log log = LogSource.getInstance("org.apache.commons.httpclient.Authenticator"); - - - // ----------------------------------------------------- Instance Variables - - - /** - * Base 64 encoder. - */ - protected static Base64 base64 = new Base64(); - - - // ------------------------------------------------------------- Properties - /** - * Generate a response to the given challenge. + * Generate a response (suitable for use in an <tt>Authorization</tt> + * header) for the given <i>challenge</i> and <i>state</i>. * - * @param state State - * @param credentials Credentials to use to answser the challenge - * @return String response to the challenge - * @deprecated Use {@link #challengeResponse(java.lang.String,org.apache.commons.httpclient.State)} + * @param challenge the authentication challenge + * (e.g., the <tt>WWW-Authenticate</tt> header value) + * @param state a {@link #State} object providing {@link Credentials} + * + * @return an <tt>Authorization</tt> value + * + * @throws HttpException when a parsing or other error occurs + * @throws UnsupportedOperationException when the given challenge type is not supported */ - public static String challengeResponse(State state, - Credentials credentials) - throws HttpException { - log.debug("Authenticator.challengeResponse(State,Credentials)"); - - if (credentials == null) - throw new HttpException(HttpException.NO_CREDENTIALS_GIVEN); - - String challenge = state.getAuthenticateToken(); - if (challenge == null) { - return null; - } - - int space = challenge.indexOf(' '); - if (space < 0) - return null; - - String authScheme = challenge.substring(0, space); - - if ("basic".equalsIgnoreCase(authScheme)) { - return basic(state, credentials); - } else if ("digest".equalsIgnoreCase(authScheme)) { - throw new UnsupportedOperationException("Digest authentication is not supported."); - } else { - throw new UnsupportedOperationException("Authentication type \"" + authScheme + "\" is not recognized."); - } - } - - public static String challengeResponse(String challenge, State state) throws HttpException { log.debug("Authenticator.challengeResponse(String, State)"); - if(challenge == null) { return null; } + if(null == challenge) { return null; } int space = challenge.indexOf(' '); if(space < 0) { @@ -137,18 +103,18 @@ String authScheme = challenge.substring(0, space); if ("basic".equalsIgnoreCase(authScheme)) { - // parse the realm from the authentication challenge // XXX FIX ME XXX // Note that this won't work if there is more than one // realm within the challenge // We could probably make it a bit more flexible in // parsing as well. + + // parse the realm from the authentication challenge if(challenge.length() < space + 1) { throw new HttpException("Unable to parse authentication challenge \"" + challenge + "\", expected realm"); } String realmstr = challenge.substring(space+1,challenge.length()); realmstr.trim(); - log.debug("Parsing realm from \"" + realmstr + "\"."); String realm = realmstr.substring("realm=\"".length(),realmstr.length()-1); log.debug("Parsed realm \"" + realm + "\" from challenge \"" + challenge + "\"."); @@ -161,19 +127,17 @@ } /** - * Generate a basic response. + * Generate a basic response (suitable for use in + * an <tt>Authorization</tt> header) for the given + * <i>realm</i> and <i>state</i>. + * + * @param realm the basic realm to authenticate to + * @param state a {@link #State} object providing {@link Credentials} * - * @param credentials Credentials to use to answser the challenge - * @deprecated Use {@link #basic(java.lang.String,org.apache.commons.httpclient.State)} + * @return a basic <tt>Authorization</tt> value + * + * @throws HttpException when no matching credentials are available */ - public static String basic(State state, Credentials credentials) { - log.debug("Authenticator.basic(State,Credentials)"); - String authString = credentials.getUserName() + ":" - + credentials.getPassword(); - return "Basic " + new String(base64.encode(authString.getBytes())); - - } - public static String basic(String realm, State state) throws HttpException { log.debug("Authenticator.basic(String,State)"); Credentials cred = state.getCredentials(realm); @@ -190,4 +154,12 @@ return "Basic " + new String(base64.encode(authString.getBytes())); } + + // -------------------------------------------------------- Class Variables + + /** <tt>org.apache.commons.httpclient.Authenticator</tt> log. */ + static private final Log log = LogSource.getInstance("org.apache.commons.httpclient.Authenticator"); + + /** Base 64 encoder. */ + protected static Base64 base64 = new Base64(); }