1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.proxy;
21  
22  import java.util.HashMap;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.mina.core.session.DummySession;
27  import org.apache.mina.proxy.handlers.http.basic.HttpBasicAuthLogicHandler;
28  import org.apache.mina.proxy.handlers.http.digest.DigestUtilities;
29  
30  /**
31   * HttpAuthTest.java - JUNIT tests of the HTTP Basic & Digest authentication mechanisms.
32   * See <a href="http://www.ietf.org/rfc/rfc2617.txt">RFC 2617</a> .
33   * 
34   * @author The Apache MINA Project (dev@mina.apache.org)
35   * @version $Rev: 751473 $, $Date: 2009-03-08 18:22:14 +0100 (Sun, 08 Mar 2009) $
36   * @since MINA 2.0.0-M3
37   */
38  public class HttpAuthTest extends TestCase {
39  
40      /**
41       * The charset in use.
42       */
43      private final static String CHARSET_IN_USE = "ISO-8859-1";
44  
45      /**
46       * Tests Basic authentication mechanism.
47       */
48      public void testBasicAuthResponse() {
49          String USER = "Aladdin";
50          String PWD = "open sesame";
51  
52          assertEquals("QWxhZGRpbjpvcGVuIHNlc2FtZQ==", HttpBasicAuthLogicHandler
53                  .createAuthorization(USER, PWD));
54      }
55  
56      /**
57       * Tests Http Digest authentication mechanism. 
58       */
59      public void testDigestAuthResponse() {
60          String USER = "Mufasa";
61          String PWD = "Circle Of Life";
62          String METHOD = "GET";
63  
64          HashMap<String, String> map = new HashMap<String, String>();
65  
66          map.put("realm", "testrealm@host.com");
67          map.put("qop", "auth");
68          map.put("nc", "00000001");
69  
70          map.put("cnonce", "0a4f113b");
71  
72          map.put("nonce", "dcd98b7102dd2f0e8b11d0f600bfb0c093");
73          map.put("opaque", "5ccc069c403ebaf9f0171e9517f40e41");
74          map.put("uri", "/dir/index.html");
75          map.put("username", USER);
76  
77          String response = null;
78          try {
79              response = DigestUtilities.computeResponseValue(new DummySession(),
80                      map, METHOD, PWD, CHARSET_IN_USE, null);
81              assertEquals("6629fae49393a05397450978507c4ef1", response);
82              writeResponse(map, response);
83          } catch (Exception e) {
84              //e.printStackTrace();
85          }
86      }
87  
88      /**
89       * Pretty prints the digest response header .
90       * 
91       * @param map the map holding the authentication parameters
92       * @param response the built digest response string
93       */
94      private void writeResponse(HashMap<String, String> map, String response) {
95          map.put("response", response);
96          StringBuilder sb = new StringBuilder("Digest ");
97          boolean addSeparator = false;
98  
99          for (String key : map.keySet()) {
100 
101             if (addSeparator) {
102                 sb.append(",\n\t\t\t ");
103             } else {
104                 addSeparator = true;
105             }
106 
107             boolean quotedValue = !"qop".equals(key) && !"nc".equals(key);
108             sb.append(key);
109             if (quotedValue) {
110                 sb.append("=\"").append(map.get(key)).append('\"');
111             } else {
112                 sb.append('=').append(map.get(key));
113             }
114         }
115 
116         //System.out.println("Proxy-Authorization: " + sb.toString());
117     }
118 }