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 credentials; | 5 library credentials; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:json' as JSON; | 8 import 'dart:json' as JSON; |
9 import 'dart:uri'; | 9 import 'dart:uri'; |
10 | 10 |
(...skipping 26 matching lines...) Expand all Loading... |
37 /// credentials. This is optional. | 37 /// credentials. This is optional. |
38 final Uri tokenEndpoint; | 38 final Uri tokenEndpoint; |
39 | 39 |
40 /// The specific permissions being requested from the authorization server. | 40 /// The specific permissions being requested from the authorization server. |
41 /// The scope strings are specific to the authorization server and may be | 41 /// The scope strings are specific to the authorization server and may be |
42 /// found in its documentation. | 42 /// found in its documentation. |
43 final List<String> scopes; | 43 final List<String> scopes; |
44 | 44 |
45 /// The date at which these credentials will expire. This is likely to be a | 45 /// The date at which these credentials will expire. This is likely to be a |
46 /// few seconds earlier than the server's idea of the expiration date. | 46 /// few seconds earlier than the server's idea of the expiration date. |
47 final Date expiration; | 47 final DateTime expiration; |
48 | 48 |
49 /// Whether or not these credentials have expired. Note that it's possible the | 49 /// Whether or not these credentials have expired. Note that it's possible the |
50 /// credentials will expire shortly after this is called. However, since the | 50 /// credentials will expire shortly after this is called. However, since the |
51 /// client's expiration date is kept a few seconds earlier than the server's, | 51 /// client's expiration date is kept a few seconds earlier than the server's, |
52 /// there should be enough leeway to rely on this. | 52 /// there should be enough leeway to rely on this. |
53 bool get isExpired => expiration != null && new Date.now() > expiration; | 53 bool get isExpired => expiration != null && new DateTime.now() > expiration; |
54 | 54 |
55 /// Whether it's possible to refresh these credentials. | 55 /// Whether it's possible to refresh these credentials. |
56 bool get canRefresh => refreshToken != null && tokenEndpoint != null; | 56 bool get canRefresh => refreshToken != null && tokenEndpoint != null; |
57 | 57 |
58 /// Creates a new set of credentials. | 58 /// Creates a new set of credentials. |
59 /// | 59 /// |
60 /// This class is usually not constructed directly; rather, it's accessed via | 60 /// This class is usually not constructed directly; rather, it's accessed via |
61 /// [Client.credentials] after a [Client] is created by | 61 /// [Client.credentials] after a [Client] is created by |
62 /// [AuthorizationCodeGrant]. Alternately, it may be loaded from a serialized | 62 /// [AuthorizationCodeGrant]. Alternately, it may be loaded from a serialized |
63 /// form via [Credentials.fromJson]. | 63 /// form via [Credentials.fromJson]. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 'field "scopes" was not a list, was "$scopes"'); | 104 'field "scopes" was not a list, was "$scopes"'); |
105 | 105 |
106 var tokenEndpoint = parsed['tokenEndpoint']; | 106 var tokenEndpoint = parsed['tokenEndpoint']; |
107 if (tokenEndpoint != null) { | 107 if (tokenEndpoint != null) { |
108 tokenEndpoint = new Uri.fromString(tokenEndpoint); | 108 tokenEndpoint = new Uri.fromString(tokenEndpoint); |
109 } | 109 } |
110 var expiration = parsed['expiration']; | 110 var expiration = parsed['expiration']; |
111 if (expiration != null) { | 111 if (expiration != null) { |
112 validate(expiration is int, | 112 validate(expiration is int, |
113 'field "expiration" was not an int, was "$expiration"'); | 113 'field "expiration" was not an int, was "$expiration"'); |
114 expiration = new Date.fromMillisecondsSinceEpoch(expiration); | 114 expiration = new DateTime.fromMillisecondsSinceEpoch(expiration); |
115 } | 115 } |
116 | 116 |
117 return new Credentials( | 117 return new Credentials( |
118 parsed['accessToken'], | 118 parsed['accessToken'], |
119 parsed['refreshToken'], | 119 parsed['refreshToken'], |
120 tokenEndpoint, | 120 tokenEndpoint, |
121 scopes, | 121 scopes, |
122 expiration); | 122 expiration); |
123 } | 123 } |
124 | 124 |
(...skipping 20 matching lines...) Expand all Loading... |
145 Future<Credentials> refresh( | 145 Future<Credentials> refresh( |
146 String identifier, | 146 String identifier, |
147 String secret, | 147 String secret, |
148 {List<String> newScopes, | 148 {List<String> newScopes, |
149 http.Client httpClient}) { | 149 http.Client httpClient}) { |
150 var scopes = this.scopes; | 150 var scopes = this.scopes; |
151 if (newScopes != null) scopes = newScopes; | 151 if (newScopes != null) scopes = newScopes; |
152 if (scopes == null) scopes = <String>[]; | 152 if (scopes == null) scopes = <String>[]; |
153 if (httpClient == null) httpClient = new http.Client(); | 153 if (httpClient == null) httpClient = new http.Client(); |
154 | 154 |
155 var startTime = new Date.now(); | 155 var startTime = new DateTime.now(); |
156 return async.then((_) { | 156 return async.then((_) { |
157 if (refreshToken == null) { | 157 if (refreshToken == null) { |
158 throw new StateError("Can't refresh credentials without a refresh " | 158 throw new StateError("Can't refresh credentials without a refresh " |
159 "token."); | 159 "token."); |
160 } else if (tokenEndpoint == null) { | 160 } else if (tokenEndpoint == null) { |
161 throw new StateError("Can't refresh credentials without a token " | 161 throw new StateError("Can't refresh credentials without a token " |
162 "endpoint."); | 162 "endpoint."); |
163 } | 163 } |
164 | 164 |
165 var fields = { | 165 var fields = { |
(...skipping 17 matching lines...) Expand all Loading... |
183 if (credentials.refreshToken != null) return credentials; | 183 if (credentials.refreshToken != null) return credentials; |
184 return new Credentials( | 184 return new Credentials( |
185 credentials.accessToken, | 185 credentials.accessToken, |
186 this.refreshToken, | 186 this.refreshToken, |
187 credentials.tokenEndpoint, | 187 credentials.tokenEndpoint, |
188 credentials.scopes, | 188 credentials.scopes, |
189 credentials.expiration); | 189 credentials.expiration); |
190 }); | 190 }); |
191 } | 191 } |
192 } | 192 } |
OLD | NEW |