OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 library oauth2.utils; | 5 library oauth2.utils; |
6 | 6 |
| 7 import 'dart:convert'; |
| 8 |
| 9 import 'package:crypto/crypto.dart'; |
| 10 |
7 /// Adds additional query parameters to [url], overwriting the original | 11 /// Adds additional query parameters to [url], overwriting the original |
8 /// parameters if a name conflict occurs. | 12 /// parameters if a name conflict occurs. |
9 Uri addQueryParameters(Uri url, Map<String, String> parameters) => url.replace( | 13 Uri addQueryParameters(Uri url, Map<String, String> parameters) => url.replace( |
10 queryParameters: new Map.from(url.queryParameters)..addAll(parameters)); | 14 queryParameters: new Map.from(url.queryParameters)..addAll(parameters)); |
11 | 15 |
| 16 String basicAuthHeader(String identifier, String secret) { |
| 17 var userPass = Uri.encodeFull(identifier) + ":" + Uri.encodeFull(secret); |
| 18 return "Basic " + CryptoUtils.bytesToBase64(ASCII.encode(userPass)); |
| 19 } |
| 20 |
12 /// Like [String.split], but only splits on the first occurrence of the pattern. | 21 /// Like [String.split], but only splits on the first occurrence of the pattern. |
13 /// | 22 /// |
14 /// This will always return a list of two elements or fewer. | 23 /// This will always return a list of two elements or fewer. |
15 List<String> split1(String toSplit, String pattern) { | 24 List<String> split1(String toSplit, String pattern) { |
16 if (toSplit.isEmpty) return []; | 25 if (toSplit.isEmpty) return []; |
17 | 26 |
18 var index = toSplit.indexOf(pattern); | 27 var index = toSplit.indexOf(pattern); |
19 if (index == -1) return [toSplit]; | 28 if (index == -1) return [toSplit]; |
20 return [toSplit.substring(0, index), | 29 return [toSplit.substring(0, index), |
21 toSplit.substring(index + pattern.length)]; | 30 toSplit.substring(index + pattern.length)]; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 parameters[match.group(1).toLowerCase()] = match.group(2); | 68 parameters[match.group(1).toLowerCase()] = match.group(2); |
60 } while (match.group(3) != null); | 69 } while (match.group(3) != null); |
61 | 70 |
62 if (!paramString.trim().isEmpty) { | 71 if (!paramString.trim().isEmpty) { |
63 throw new FormatException('Invalid WWW-Authenticate header: "$header"'); | 72 throw new FormatException('Invalid WWW-Authenticate header: "$header"'); |
64 } | 73 } |
65 | 74 |
66 return new AuthenticateHeader(scheme, parameters); | 75 return new AuthenticateHeader(scheme, parameters); |
67 } | 76 } |
68 } | 77 } |
OLD | NEW |