View Javadoc
1   /*
2    * Copyright (C) 2013, Microsoft Corporation
3    *
4    * This program and the accompanying materials are made available
5    * under the terms of the Eclipse Distribution License v1.0 which
6    * accompanies this distribution, is reproduced below, and is
7    * available at http://www.eclipse.org/org/documents/edl-v10.php
8    *
9    * All rights reserved.
10   *
11   * Redistribution and use in source and binary forms, with or
12   * without modification, are permitted provided that the following
13   * conditions are met:
14   *
15   * - Redistributions of source code must retain the above copyright
16   *   notice, this list of conditions and the following disclaimer.
17   *
18   * - Redistributions in binary form must reproduce the above
19   *   copyright notice, this list of conditions and the following
20   *   disclaimer in the documentation and/or other materials provided
21   *   with the distribution.
22   *
23   * - Neither the name of the Eclipse Foundation, Inc. nor the
24   *   names of its contributors may be used to endorse or promote
25   *   products derived from this software without specific prior
26   *   written permission.
27   *
28   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
29   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
30   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
33   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
37   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
40   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 }