oglueck     2003/09/11 02:04:35

  Modified:    httpclient/src/test/org/apache/commons/httpclient Tag:
                        HTTPCLIENT_2_0_BRANCH TestAuthenticator.java
               httpclient/src/java/org/apache/commons/httpclient/auth Tag:
                        HTTPCLIENT_2_0_BRANCH DigestScheme.java
  Log:
  Adding support for digest auth MD5-sess
  
  PR: 22926
  Submitted by: Dustin Sallings
  Reviewed by:  Ortwin Gl�ck, Oleg Kalnichevski, Michael Becke
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.25.2.3  +37 -5     
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestAuthenticator.java
  
  Index: TestAuthenticator.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestAuthenticator.java,v
  retrieving revision 1.25.2.2
  retrieving revision 1.25.2.3
  diff -u -r1.25.2.2 -r1.25.2.3
  --- TestAuthenticator.java    8 Sep 2003 01:49:15 -0000       1.25.2.2
  +++ TestAuthenticator.java    11 Sep 2003 09:04:34 -0000      1.25.2.3
  @@ -411,7 +411,39 @@
               checkAuthorization(cred2, method.getName(), 
method.getRequestHeader("Authorization").getValue());
           }
       }
  -    
  +
  +    /** 
  +     * Test digest authentication using the MD5-sess algorithm.
  +     */
  +    public void testDigestAuthenticationMD5Sess() throws Exception {
  +        // Example using Digest auth with MD5-sess
  +
  +        String realm="realm";
  +        String username="username";
  +        String password="password";
  +        String nonce="e273f1776275974f1a120d8b92c5b3cb";
  +
  +        String challenge="Digest realm=\"" + realm + "\", "
  +            + nonce + "\"" + nonce + "\", "
  +            + "opaque=\"SomeString\", "
  +            + "stale=false, "
  +            + "algorithm=MD5-sess, "
  +            + "qop=\"auth\"";
  +
  +        HttpState state = new HttpState();
  +        UsernamePasswordCredentials cred =
  +            new UsernamePasswordCredentials(username, password);
  +        state.setCredentials(realm, null, cred);
  +        AuthScheme authscheme = new DigestScheme(challenge);
  +        HttpMethod method =
  +            new SimpleHttpMethod(new Header("WWW-Authenticate", challenge));
  +        assertTrue(HttpAuthenticator.authenticate(
  +            authscheme, method, null, state));
  +        assertTrue(null != method.getRequestHeader("Authorization"));
  +        checkAuthorization(cred, method.getName(),
  +            method.getRequestHeader("Authorization").getValue());
  +    }
  +
       // --------------------------------- Test Methods for NTLM Authentication
   
       public void testNTLMAuthenticationWithNoCreds() {
  
  
  
  No                   revision
  No                   revision
  1.4.2.3   +33 -9     
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/auth/DigestScheme.java
  
  Index: DigestScheme.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/auth/DigestScheme.java,v
  retrieving revision 1.4.2.2
  retrieving revision 1.4.2.3
  diff -u -r1.4.2.2 -r1.4.2.3
  --- DigestScheme.java 8 Sep 2003 01:49:15 -0000       1.4.2.2
  +++ DigestScheme.java 11 Sep 2003 09:04:35 -0000      1.4.2.3
  @@ -234,6 +234,12 @@
           String cnonce = (String) params.get("cnonce");
           String qop = (String) params.get("qop");
           String method = (String) params.get("methodname");
  +        String algorithm = (String) params.get("algorithm");
  +
  +        // If an algorithm is not specified, default to MD5.
  +        if(algorithm == null) {
  +            algorithm="MD5";
  +        }
   
           if (qop != null) {
               qop = "auth";
  @@ -250,16 +256,35 @@
           }
   
           // Calculating digest according to rfc 2617
  +
  +        String a1 = null;
  +        if(algorithm.equals("MD5")) {
  +            // unq(username-value) ":" unq(realm-value) ":" passwd
  +            a1 = uname + ":" + realm + ":" + pwd;
  +        } else if(algorithm.equals("MD5-sess")) {
  +            // H( unq(username-value) ":" unq(realm-value) ":" passwd )
  +            //      ":" unq(nonce-value)
  +            //      ":" unq(cnonce-value)
  +
  +            String tmp=encode(md5Helper.digest(HttpConstants.getBytes(
  +                uname + ":" + realm + ":" + pwd)));
  +
  +            a1 = tmp + ":" + nonce + ":" + cnonce;
  +        } else {
  +            LOG.warn("Unhandled algorithm " + algorithm + " requested");
  +            a1 = uname + ":" + realm + ":" + pwd;
  +        }
  +        String md5a1 = encode(md5Helper.digest(HttpConstants.getBytes(a1)));
  +        String serverDigestValue;
  +
           String a2 = method + ":" + uri;
           String md5a2 = encode(md5Helper.digest(HttpConstants.getBytes(a2)));
  -        String digestValue = uname + ":" + realm + ":" + pwd;
  -        String md5a1 
  -            = encode(md5Helper.digest(HttpConstants.getBytes(digestValue)));
  -        String serverDigestValue;
   
           if (qop == null) {
  +            LOG.debug("Using null qop method");
               serverDigestValue = md5a1 + ":" + nonce + ":" + md5a2;
           } else {
  +            LOG.debug("Using qop method " + qop);
               serverDigestValue = md5a1 + ":" + nonce + ":" + nc + ":" + cnonce
                                   + ":" + qop + ":" + md5a2;
           }
  @@ -297,12 +322,11 @@
           String opaque = (String) params.get("opaque");
           String response = digest;
           String qop = (String) params.get("qop");
  +        String algorithm = (String) params.get("algorithm");
   
           if (qop != null) {
               qop = "auth"; //we only support auth
           }
  -
  -        String algorithm = "MD5"; //we only support MD5
   
           sb.append("username=\"" + uname + "\"")
             .append(", realm=\"" + realm + "\"")
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to