Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(619)

Side by Side Diff: pkg/oauth2/lib/src/utils.dart

Issue 11420025: Add a package for authenticating via OAuth2. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library utils;
6
7 import 'dart:uri';
Adam 2012/11/17 05:13:07 At some point could these utility functions be rol
nweiz 2012/11/19 20:55:27 It's possible. See issue 2645. In the short term,
8 import 'dart:isolate';
9 import 'dart:crypto';
10
11 /// Adds additional query parameters to [url], overwriting the original
12 /// parameters if a name conflict occurs.
13 Uri addQueryParameters(Uri url, Map<String, String> parameters) {
14 var queryMap = queryToMap(url.query);
15 mapAddAll(queryMap, parameters);
16 return url.resolve("?${mapToQuery(queryMap)}");
17 }
18
19 /// Convert a URL query string (or `application/x-www-form-urlencoded` body)
20 /// into a [Map] from parameter names to values.
21 Map<String, String> queryToMap(String queryList) {
22 var map = <String>{};
23 for (var pair in queryList.split("&")) {
24 var split = split1(pair, "=");
25 if (split.isEmpty) continue;
26 var key = urlDecode(split[0]);
27 var value = split.length > 1 ? urlDecode(split[1]) : "";
28 map[key] = value;
29 }
30 return map;
31 }
32
33 /// Convert a [Map] from parameter names to values to a URL query string.
34 String mapToQuery(Map<String, String> map) {
35 var pairs = <List<String>>[];
36 map.forEach((key, value) {
37 key = encodeUriComponent(key);
38 value = (value == null || value.isEmpty) ? null : encodeUriComponent(value);
39 pairs.add([key, value]);
40 });
41 return Strings.join(pairs.map((pair) {
42 if (pair[1] == null) return pair[0];
43 return "${pair[0]}=${pair[1]}";
44 }), "&");
45 }
46
47 /// Add all key/value pairs from [source] to [destination], overwriting any
48 /// pre-existing values.
49 void mapAddAll(Map destination, Map source) =>
50 source.forEach((key, value) => destination[key] = value);
51
52 /// Decode a URL-encoded string. Unlike [decodeUriComponent], this includes
53 /// replacing `+` with ` `.
54 String urlDecode(String encoded) =>
55 decodeUriComponent(encoded.replaceAll("+", " "));
56
57 /// Like [String.split], but only splits on the first occurrence of the pattern.
58 /// This will always return a list of two elements or fewer.
59 List<String> split1(String toSplit, String pattern) {
60 if (toSplit.isEmpty) return <String>[];
61
62 var index = toSplit.indexOf(pattern);
63 if (index == -1) return [toSplit];
64 return [toSplit.substring(0, index),
65 toSplit.substring(index + pattern.length)];
66 }
67
68 /// Returns a [Future] that asynchronously completes to `null`.
69 Future get async {
70 var completer = new Completer();
71 new Timer(0, (_) => completer.complete(null));
72 return completer.future;
73 }
OLDNEW
« no previous file with comments | « pkg/oauth2/lib/src/handle_access_token_response.dart ('k') | pkg/oauth2/test/authorization_code_grant_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698