commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient Authenticator.java
Date Wed, 10 Jul 2002 02:24:37 GMT
dion        2002/07/09 19:24:37

  Modified:    httpclient/src/java/org/apache/commons/httpclient
                        Authenticator.java
  Log:
  Proxy Authentication patch from Evert Hoff <evert.hoff@pixie.co.za>
  
  Revision  Changes    Path
  1.12      +73 -13    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.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- Authenticator.java	5 Jan 2002 11:15:59 -0000	1.11
  +++ Authenticator.java	10 Jul 2002 02:24:37 -0000	1.12
  @@ -70,28 +70,82 @@
    * <p>Utility methods for HTTP authorization and authentication.</p>
    * <p>
    * This class provides utility methods for generating
  - * responses to HTTP authentication challenges.
  + * responses to HTTP www and proxy authentication challenges.
    * </p>
    * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
    * @author Rodney Waldhoff
    * @version $Revision$ $Date$
    */
   class Authenticator {
  +    /**
  +     * The www authenticate challange header
  +     */
  +    public static final String WWW_AUTH = "WWW-Authenticate";
  +
  +    /**
  +     * The www authenticate response header
  +     */
  +    public static final String WWW_AUTH_RESP = "Authorization";
  +
  +    /**
  +     * The proxy authenticate challange header
  +     */
  +    public static final String PROXY_AUTH = "Proxy-Authenticate";
  +
  +    /**
  +     * The proxy authenticate response header
  +     */
  +    public static final String PROXY_AUTH_RESP = "Proxy-Authorization";
   
       /**
        * Add requisite authentication credentials to the given
        * {@link HttpMethod}, if possible.
        *
  +     * @see HttpState#setCredentials(String, Credentials) HttpState.setCredentials
  +     *
        * @param method a {@link HttpMethod} which requires authentication
        * @param state a {@link HttpState} object providing {@link Credentials}
        *
        * @throws HttpException when a parsing or other error occurs
        * @throws UnsupportedOperationException when the given challenge type is not supported
  +     * @return true if only if a response header was added
        */
       static boolean authenticate(HttpMethod method, HttpState state) throws HttpException
{
           log.debug("Authenticator.authenticate(HttpMethod, HttpState)");
  -        Header challengeHeader = method.getResponseHeader("WWW-Authenticate");
  +
  +        Header challengeHeader = method.getResponseHeader(WWW_AUTH);
           if(null == challengeHeader) { return false; }
  +
  +        return authenticate(method, state, challengeHeader, WWW_AUTH_RESP);
  +    }
  +
  +    /**
  +     * Add requisite proxy authentication credentials to the given
  +     * {@link HttpMethod}, if possible.
  +     *
  +     * @see HttpState#setProxyCredentials(String, Credentials) HttpState.setProxyCredentials
  +     *
  +     * @param method a {@link HttpMethod} which requires authentication
  +     * @param state a {@link HttpState} object providing {@link Credentials}
  +     *
  +     * @throws HttpException when a parsing or other error occurs
  +     * @throws UnsupportedOperationException when the given challenge type is not supported
  +     * @return true if only if a response header was added
  +     */
  +    static boolean authenticateProxy(HttpMethod method, HttpState state) throws HttpException
{
  +        log.debug("Authenticator.authenticateProxy(HttpMethod, HttpState)");
  +
  +        Header challengeHeader = method.getResponseHeader(PROXY_AUTH);
  +        if (null == challengeHeader) { return false; }
  +        return authenticate(method, state, challengeHeader, PROXY_AUTH_RESP);
  +    }
  +
  +
  +    private static boolean authenticate(HttpMethod method, HttpState state,
  +                                        Header challengeHeader,
  +                                        String respHeader)
  +                                        throws HttpException {
  +
           String challenge = challengeHeader.getValue();
           if(null == challenge) { return false; }
   
  @@ -119,7 +173,7 @@
               }
               String realm = realmstr.substring("realm=\"".length(),realmstr.length()-1);
               log.debug("Parsed realm \"" + realm + "\" from challenge \"" + challenge +
"\".");
  -            Header header = Authenticator.basic(realm,state);
  +           Header header = Authenticator.basic(realm, state, respHeader);
               if(null != header) {
                   method.addRequestHeader(header);
                   return true;
  @@ -137,19 +191,23 @@
        * Create a Basic <tt>Authorization</tt> header for the given
        * <i>realm</i> and <i>state</i> to the given <i>method</i>.
        *
  -     * @param method the {@link HttpMethod} to authenticate to
        * @param realm the basic realm to authenticate to
        * @param state a {@link HttpState} object providing {@link Credentials}
  +     * @param respHeader the header's name to store the authentication response
  +     * in. PROXY_AUTH_RESP will force the proxy credentials to be used.
        *
  -     * @return a basic <tt>Authorization</tt> value
  +     * @return a basic <tt>Authorization</tt> header
        *
        * @throws HttpException when no matching credentials are available
        */
  -    static Header basic(String realm, HttpState state) throws HttpException {
  +    static Header basic(String realm, HttpState state, String respHeader) throws HttpException
{
           log.debug("Authenticator.basic(String,HttpState)");
  +        boolean proxy = PROXY_AUTH_RESP.equals(respHeader);
           UsernamePasswordCredentials cred = null;
           try {
  -            cred = (UsernamePasswordCredentials)(state.getCredentials(realm));
  +            cred = (UsernamePasswordCredentials) ( proxy ?
  +                    state.getProxyCredentials(realm) :
  +                    state.getCredentials(realm));
           } catch(ClassCastException e) {
               throw new HttpException("UsernamePasswordCredentials required for Basic authentication.");
           }
  @@ -158,7 +216,9 @@
                   log.info("No credentials found for realm \"" + realm + "\", attempting
to use default credentials.");
               }
               try {
  -                cred = (UsernamePasswordCredentials)(state.getCredentials(null));
  +                cred = (UsernamePasswordCredentials)( proxy ?
  +                        state.getProxyCredentials(null) :
  +                        state.getCredentials(null));
               } catch(ClassCastException e) {
                   throw new HttpException("UsernamePasswordCredentials required for Basic
authentication.");
               }
  @@ -166,7 +226,7 @@
           if(null == cred) {
               throw new HttpException("No credentials available for the Basic authentication
realm \"" + realm + "\"/");
           } else {
  -            return new Header("Authorization",Authenticator.basic(cred));
  +            return new Header(respHeader, Authenticator.basic(cred));
           }
       }
   
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message