1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.proxy;
21
22 import static org.junit.Assert.assertEquals;
23
24 import java.util.HashMap;
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 import org.junit.Test;
30
31
32
33
34
35
36
37
38 public class HttpAuthTest {
39
40
41
42
43 private final static String CHARSET_IN_USE = "ISO-8859-1";
44
45
46
47
48 @Test
49 public void testBasicAuthResponse() {
50 String USER = "Aladdin";
51 String PWD = "open sesame";
52
53 assertEquals("QWxhZGRpbjpvcGVuIHNlc2FtZQ==", HttpBasicAuthLogicHandler.createAuthorization(USER, PWD));
54 }
55
56
57
58
59 @Test
60 public void testDigestAuthResponse() {
61 String USER = "Mufasa";
62 String PWD = "Circle Of Life";
63 String METHOD = "GET";
64
65 HashMap<String, String> map = new HashMap<>();
66
67 map.put("realm", "testrealm@host.com");
68 map.put("qop", "auth");
69 map.put("nc", "00000001");
70
71 map.put("cnonce", "0a4f113b");
72
73 map.put("nonce", "dcd98b7102dd2f0e8b11d0f600bfb0c093");
74 map.put("opaque", "5ccc069c403ebaf9f0171e9517f40e41");
75 map.put("uri", "/dir/index.html");
76 map.put("username", USER);
77
78 String response = null;
79 try {
80 response = DigestUtilities.computeResponseValue(new DummySession(), map, METHOD, PWD, CHARSET_IN_USE, null);
81 assertEquals("6629fae49393a05397450978507c4ef1", response);
82 writeResponse(map, response);
83 } catch (Exception e) {
84
85 }
86 }
87
88
89
90
91
92
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
117 }
118 }