OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
| 5 import "package:convert/convert.dart"; |
5 import "package:crypto/crypto.dart"; | 6 import "package:crypto/crypto.dart"; |
6 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
7 import 'dart:async'; | 8 import 'dart:async'; |
8 import 'dart:io'; | 9 import 'dart:io'; |
9 import 'dart:isolate'; | 10 import 'dart:isolate'; |
10 | 11 |
11 class Server { | 12 class Server { |
12 HttpServer server; | 13 HttpServer server; |
13 int unauthCount = 0; // Counter of the 401 responses. | 14 int unauthCount = 0; // Counter of the 401 responses. |
14 int successCount = 0; // Counter of the successful responses. | 15 int successCount = 0; // Counter of the successful responses. |
15 int nonceCount = 0; // Counter of use of current nonce. | 16 int nonceCount = 0; // Counter of use of current nonce. |
16 var ha1; | 17 var ha1; |
17 | 18 |
18 static Future<Server> start(String algorithm, | 19 static Future<Server> start(String algorithm, |
19 String qop, | 20 String qop, |
20 {int nonceStaleAfter, | 21 {int nonceStaleAfter, |
21 bool useNextNonce: false}) { | 22 bool useNextNonce: false}) { |
22 return new Server()._start(algorithm, qop, nonceStaleAfter, useNextNonce); | 23 return new Server()._start(algorithm, qop, nonceStaleAfter, useNextNonce); |
23 } | 24 } |
24 | 25 |
25 Future<Server> _start(String serverAlgorithm, | 26 Future<Server> _start(String serverAlgorithm, |
26 String serverQop, | 27 String serverQop, |
27 int nonceStaleAfter, | 28 int nonceStaleAfter, |
28 bool useNextNonce) { | 29 bool useNextNonce) { |
29 Set ncs = new Set(); | 30 Set ncs = new Set(); |
30 // Calculate ha1. | 31 // Calculate ha1. |
31 String realm = "test"; | 32 String realm = "test"; |
32 String username = "dart"; | 33 String username = "dart"; |
33 String password = "password"; | 34 String password = "password"; |
34 var hasher = new MD5(); | 35 var hasher = md5.convert("${username}:${realm}:${password}".codeUnits); |
35 hasher.add("${username}:${realm}:${password}".codeUnits); | 36 ha1 = hex.encode(hasher.bytes); |
36 ha1 = CryptoUtils.bytesToHex(hasher.close()); | |
37 | 37 |
38 var nonce = "12345678"; // No need for random nonce in test. | 38 var nonce = "12345678"; // No need for random nonce in test. |
39 | 39 |
40 var completer = new Completer(); | 40 var completer = new Completer(); |
41 HttpServer.bind("127.0.0.1", 0).then((s) { | 41 HttpServer.bind("127.0.0.1", 0).then((s) { |
42 server = s; | 42 server = s; |
43 server.listen((HttpRequest request) { | 43 server.listen((HttpRequest request) { |
44 sendUnauthorizedResponse(HttpResponse response, {stale: false}) { | 44 sendUnauthorizedResponse(HttpResponse response, {stale: false}) { |
45 response.statusCode = HttpStatus.UNAUTHORIZED; | 45 response.statusCode = HttpStatus.UNAUTHORIZED; |
46 StringBuffer authHeader = new StringBuffer(); | 46 StringBuffer authHeader = new StringBuffer(); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 Expect.isNotNull(cnonce); | 89 Expect.isNotNull(cnonce); |
90 Expect.isNotNull(nc); | 90 Expect.isNotNull(nc); |
91 Expect.isFalse(ncs.contains(nc)); | 91 Expect.isFalse(ncs.contains(nc)); |
92 ncs.add(nc); | 92 ncs.add(nc); |
93 } else { | 93 } else { |
94 Expect.isNull(cnonce); | 94 Expect.isNull(cnonce); |
95 Expect.isNull(nc); | 95 Expect.isNull(nc); |
96 } | 96 } |
97 Expect.isNotNull(header.parameters["response"]); | 97 Expect.isNotNull(header.parameters["response"]); |
98 | 98 |
99 var hasher = new MD5(); | 99 var hasher = md5.convert("${request.method}:${uri}".codeUnits); |
100 hasher.add("${request.method}:${uri}".codeUnits); | 100 var ha2 = hex.encode(hasher.bytes); |
101 var ha2 = CryptoUtils.bytesToHex(hasher.close()); | |
102 | 101 |
103 var x; | 102 var x; |
104 hasher = new MD5(); | 103 Digest digest; |
105 if (qop == null || qop == "" || qop == "none") { | 104 if (qop == null || qop == "" || qop == "none") { |
106 hasher.add("$ha1:${nonce}:$ha2".codeUnits); | 105 digest = md5.convert("$ha1:${nonce}:$ha2".codeUnits); |
107 } else { | 106 } else { |
108 hasher.add("$ha1:${nonce}:${nc}:${cnonce}:${qop}:$ha2".codeUnits); | 107 digest = md5.convert("$ha1:${nonce}:${nc}:${cnonce}:${qop}:$ha2".c
odeUnits); |
109 } | 108 } |
110 Expect.equals(CryptoUtils.bytesToHex(hasher.close()), | 109 Expect.equals(hex.encode(digest.bytes), |
111 header.parameters["response"]); | 110 header.parameters["response"]); |
112 | 111 |
113 successCount++; | 112 successCount++; |
114 nonceCount++; | 113 nonceCount++; |
115 | 114 |
116 // Add a bogus Authentication-Info for testing. | 115 // Add a bogus Authentication-Info for testing. |
117 var info = 'rspauth="77180d1ab3d6c9de084766977790f482", ' | 116 var info = 'rspauth="77180d1ab3d6c9de084766977790f482", ' |
118 'cnonce="8f971178", ' | 117 'cnonce="8f971178", ' |
119 'nc=000002c74, ' | 118 'nc=000002c74, ' |
120 'qop=auth'; | 119 'qop=auth'; |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 testAuthenticateCallback(null, null); | 388 testAuthenticateCallback(null, null); |
390 testAuthenticateCallback("MD5", null); | 389 testAuthenticateCallback("MD5", null); |
391 testAuthenticateCallback("MD5", "auth"); | 390 testAuthenticateCallback("MD5", "auth"); |
392 testAuthenticateCallback("MD5", "auth-int"); | 391 testAuthenticateCallback("MD5", "auth-int"); |
393 testStaleNonce(); | 392 testStaleNonce(); |
394 testNextNonce(); | 393 testNextNonce(); |
395 // These teste are not normally run. They can be used for locally | 394 // These teste are not normally run. They can be used for locally |
396 // testing with another web server (e.g. Apache). | 395 // testing with another web server (e.g. Apache). |
397 //testLocalServerDigest(); | 396 //testLocalServerDigest(); |
398 } | 397 } |
OLD | NEW |