You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HttpAuthTest.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. package org.eclipse.jgit.transport;
  43. import static org.junit.Assert.fail;
  44. import java.io.IOException;
  45. import java.net.MalformedURLException;
  46. import java.net.URL;
  47. import java.util.ArrayList;
  48. import java.util.HashMap;
  49. import java.util.List;
  50. import java.util.Map;
  51. import org.eclipse.jgit.transport.http.JDKHttpConnection;
  52. import org.junit.Test;
  53. public class HttpAuthTest {
  54. private static String digestHeader = "WWW-Authenticate: Digest qop=\"auth\",algorithm=MD5-sess,nonce=\"+Upgraded+v1b9...ba\",charset=utf-8,realm=\"Digest\"";
  55. private static String basicHeader = "WWW-Authenticate: Basic realm=\"everyones.loves.git\"";
  56. private static String ntlmHeader = "WWW-Authenticate: NTLM";
  57. private static String bearerHeader = "WWW-Authenticate: Bearer";
  58. private static String negotiateHeader = "WWW-Authenticate: Negotiate";
  59. private static String URL_SAMPLE = "http://everyones.loves.git/u/2";
  60. private static String BASIC = "Basic";
  61. private static String DIGEST = "Digest";
  62. private static String NEGOTIATE = "Negotiate";
  63. @Test
  64. public void testHttpAuthScanResponse() {
  65. checkResponse(new String[] { basicHeader }, BASIC);
  66. checkResponse(new String[] { digestHeader }, DIGEST);
  67. checkResponse(new String[] { negotiateHeader }, NEGOTIATE);
  68. checkResponse(new String[] { basicHeader, digestHeader }, DIGEST);
  69. checkResponse(new String[] { digestHeader, basicHeader }, DIGEST);
  70. checkResponse(new String[] { digestHeader, negotiateHeader }, NEGOTIATE);
  71. checkResponse(new String[] { negotiateHeader, digestHeader }, NEGOTIATE);
  72. checkResponse(new String[] { ntlmHeader, basicHeader, digestHeader,
  73. bearerHeader }, DIGEST);
  74. checkResponse(new String[] { ntlmHeader, basicHeader, bearerHeader },
  75. BASIC);
  76. checkResponse(new String[] { ntlmHeader, basicHeader, digestHeader,
  77. negotiateHeader, bearerHeader }, NEGOTIATE);
  78. }
  79. private static void checkResponse(String[] headers,
  80. String expectedAuthMethod) {
  81. AuthHeadersResponse response = null;
  82. try {
  83. response = new AuthHeadersResponse(headers);
  84. } catch (IOException e) {
  85. fail("Couldn't instantiate AuthHeadersResponse: " + e.toString());
  86. }
  87. HttpAuthMethod authMethod = HttpAuthMethod.scanResponse(response, null);
  88. if (!expectedAuthMethod.equals(getAuthMethodName(authMethod))) {
  89. fail("Wrong authentication method: expected " + expectedAuthMethod
  90. + ", but received " + getAuthMethodName(authMethod));
  91. }
  92. }
  93. private static String getAuthMethodName(HttpAuthMethod authMethod) {
  94. return authMethod.getClass().getSimpleName();
  95. }
  96. private static class AuthHeadersResponse extends JDKHttpConnection {
  97. Map<String, List<String>> headerFields = new HashMap<>();
  98. public AuthHeadersResponse(String[] authHeaders)
  99. throws MalformedURLException, IOException {
  100. super(new URL(URL_SAMPLE));
  101. parseHeaders(authHeaders);
  102. }
  103. @Override
  104. public boolean usingProxy() {
  105. return false;
  106. }
  107. @Override
  108. public void connect() throws IOException {
  109. fail("The connect method shouldn't be invoked");
  110. }
  111. @Override
  112. public String getHeaderField(String name) {
  113. if (!headerFields.containsKey(name)) {
  114. return null;
  115. }
  116. int n = headerFields.get(name).size();
  117. if (n > 0) {
  118. return headerFields.get(name).get(n - 1);
  119. }
  120. return null;
  121. }
  122. @Override
  123. public Map<String, List<String>> getHeaderFields() {
  124. return headerFields;
  125. }
  126. private void parseHeaders(String[] headers) {
  127. for (String header : headers) {
  128. int i = header.indexOf(':');
  129. if (i < 0)
  130. continue;
  131. String key = header.substring(0, i);
  132. String value = header.substring(i + 1).trim();
  133. if (!headerFields.containsKey(key))
  134. headerFields.put(key, new ArrayList<String>());
  135. List<String> values = headerFields.get(key);
  136. values.add(value);
  137. }
  138. }
  139. }
  140. }