1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 package org.eclipse.jgit.transport;
44
45 import static org.junit.Assert.fail;
46
47 import java.io.IOException;
48 import java.net.MalformedURLException;
49 import java.net.URL;
50 import java.util.ArrayList;
51 import java.util.HashMap;
52 import java.util.List;
53 import java.util.Map;
54
55 import org.eclipse.jgit.transport.http.JDKHttpConnection;
56 import org.junit.Test;
57
58 public class HttpAuthTest {
59 private static String digestHeader = "WWW-Authenticate: Digest qop=\"auth\",algorithm=MD5-sess,nonce=\"+Upgraded+v1b9...ba\",charset=utf-8,realm=\"Digest\"";
60
61 private static String basicHeader = "WWW-Authenticate: Basic realm=\"everyones.loves.git\"";
62
63 private static String ntlmHeader = "WWW-Authenticate: NTLM";
64
65 private static String bearerHeader = "WWW-Authenticate: Bearer";
66
67 private static String negotiateHeader = "WWW-Authenticate: Negotiate";
68
69 private static String URL_SAMPLE = "http://everyones.loves.git/u/2";
70
71 private static String BASIC = "Basic";
72
73 private static String DIGEST = "Digest";
74
75 private static String NEGOTIATE = "Negotiate";
76
77 @Test
78 public void testHttpAuthScanResponse() {
79 checkResponse(new String[] { basicHeader }, BASIC);
80 checkResponse(new String[] { digestHeader }, DIGEST);
81 checkResponse(new String[] { negotiateHeader }, NEGOTIATE);
82 checkResponse(new String[] { basicHeader, digestHeader }, DIGEST);
83 checkResponse(new String[] { digestHeader, basicHeader }, DIGEST);
84 checkResponse(new String[] { digestHeader, negotiateHeader }, NEGOTIATE);
85 checkResponse(new String[] { negotiateHeader, digestHeader }, NEGOTIATE);
86 checkResponse(new String[] { ntlmHeader, basicHeader, digestHeader,
87 bearerHeader }, DIGEST);
88 checkResponse(new String[] { ntlmHeader, basicHeader, bearerHeader },
89 BASIC);
90 checkResponse(new String[] { ntlmHeader, basicHeader, digestHeader,
91 negotiateHeader, bearerHeader }, NEGOTIATE);
92 }
93
94 private static void checkResponse(String[] headers,
95 String expectedAuthMethod) {
96
97 AuthHeadersResponse response = null;
98 try {
99 response = new AuthHeadersResponse(headers);
100 } catch (IOException e) {
101 fail("Couldn't instantiate AuthHeadersResponse: " + e.toString());
102 }
103 HttpAuthMethod authMethod = HttpAuthMethod.scanResponse(response, null);
104
105 if (!expectedAuthMethod.equals(getAuthMethodName(authMethod))) {
106 fail("Wrong authentication method: expected " + expectedAuthMethod
107 + ", but received " + getAuthMethodName(authMethod));
108 }
109 }
110
111 private static String getAuthMethodName(HttpAuthMethod authMethod) {
112 return authMethod.getClass().getSimpleName();
113 }
114
115 private static class AuthHeadersResponse extends JDKHttpConnection {
116 Map<String, List<String>> headerFields = new HashMap<>();
117
118 public AuthHeadersResponse(String[] authHeaders)
119 throws MalformedURLException, IOException {
120 super(new URL(URL_SAMPLE));
121 parseHeaders(authHeaders);
122 }
123
124 @Override
125 public boolean usingProxy() {
126 return false;
127 }
128
129 @Override
130 public void connect() throws IOException {
131 fail("The connect method shouldn't be invoked");
132 }
133
134 @Override
135 public String getHeaderField(String name) {
136 if (!headerFields.containsKey(name))
137 return null;
138 else {
139 int n = headerFields.get(name).size();
140
141 if (n > 0)
142 return headerFields.get(name).get(n - 1);
143 else
144 return null;
145 }
146 }
147
148 @Override
149 public Map<String, List<String>> getHeaderFields() {
150 return headerFields;
151 }
152
153 private void parseHeaders(String[] headers) {
154 for (String header : headers) {
155 int i = header.indexOf(':');
156
157 if (i < 0)
158 continue;
159
160 String key = header.substring(0, i);
161 String value = header.substring(i + 1).trim();
162
163 if (!headerFields.containsKey(key))
164 headerFields.put(key, new ArrayList<String>());
165
166 List<String> values = headerFields.get(key);
167 values.add(value);
168 }
169 }
170 }
171 }