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.client; | 5 library oauth2.client; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:http/http.dart' as http; | 9 import 'package:http/http.dart' as http; |
10 | 10 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 final String secret; | 62 final String secret; |
63 | 63 |
64 /// The credentials this client uses to prove to the resource server that it's | 64 /// The credentials this client uses to prove to the resource server that it's |
65 /// authorized. | 65 /// authorized. |
66 /// | 66 /// |
67 /// This may change from request to request as the credentials expire and the | 67 /// This may change from request to request as the credentials expire and the |
68 /// client refreshes them automatically. | 68 /// client refreshes them automatically. |
69 Credentials get credentials => _credentials; | 69 Credentials get credentials => _credentials; |
70 Credentials _credentials; | 70 Credentials _credentials; |
71 | 71 |
72 /// Whether to use HTTP Basic authentication for authorizing the client. | |
73 final bool _basicAuth; | |
74 | |
72 /// The underlying HTTP client. | 75 /// The underlying HTTP client. |
73 http.Client _httpClient; | 76 http.Client _httpClient; |
74 | 77 |
75 /// Creates a new client from a pre-existing set of credentials. | 78 /// Creates a new client from a pre-existing set of credentials. |
76 /// | 79 /// |
77 /// When authorizing a client for the first time, you should use | 80 /// When authorizing a client for the first time, you should use |
78 /// [AuthorizationCodeGrant] instead of constructing a [Client] directly. | 81 /// [AuthorizationCodeGrant] instead of constructing a [Client] directly. |
79 /// | 82 /// |
80 /// [httpClient] is the underlying client that this forwards requests to after | 83 /// [httpClient] is the underlying client that this forwards requests to after |
81 /// adding authorization credentials to them. | 84 /// adding authorization credentials to them. |
82 Client( | 85 /// |
83 this.identifier, | 86 /// Thrwos an [ArgumentError] if [secret] is passed without [identifier]. |
Bob Nystrom
2015/08/26 16:52:44
"Throws"
nweiz
2015/08/26 20:47:28
Done.
| |
84 this.secret, | 87 Client(this._credentials, {this.identifier, this.secret, |
85 this._credentials, | 88 bool basicAuth: true, http.Client httpClient}) |
86 {http.Client httpClient}) | 89 : _basicAuth = basicAuth, |
87 : _httpClient = httpClient == null ? new http.Client() : httpClient; | 90 _httpClient = httpClient == null ? new http.Client() : httpClient { |
91 if (identifier == null && secret != null) { | |
92 throw new ArgumentError("secret may not be passed without identifier."); | |
93 } | |
94 } | |
88 | 95 |
89 /// Sends an HTTP request with OAuth2 authorization credentials attached. | 96 /// Sends an HTTP request with OAuth2 authorization credentials attached. |
90 /// | 97 /// |
91 /// This will also automatically refresh this client's [Credentials] before | 98 /// This will also automatically refresh this client's [Credentials] before |
92 /// sending the request if necessary. | 99 /// sending the request if necessary. |
93 Future<http.StreamedResponse> send(http.BaseRequest request) async { | 100 Future<http.StreamedResponse> send(http.BaseRequest request) async { |
94 if (credentials.isExpired) { | 101 if (credentials.isExpired) { |
95 if (!credentials.canRefresh) throw new ExpirationException(credentials); | 102 if (!credentials.canRefresh) throw new ExpirationException(credentials); |
96 await refreshCredentials(); | 103 await refreshCredentials(); |
97 } | 104 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
130 /// [newScopes]. These must be a subset of the scopes in the | 137 /// [newScopes]. These must be a subset of the scopes in the |
131 /// [Credentials.scopes] field of [Client.credentials]. | 138 /// [Credentials.scopes] field of [Client.credentials]. |
132 Future<Client> refreshCredentials([List<String> newScopes]) async { | 139 Future<Client> refreshCredentials([List<String> newScopes]) async { |
133 if (!credentials.canRefresh) { | 140 if (!credentials.canRefresh) { |
134 var prefix = "OAuth credentials"; | 141 var prefix = "OAuth credentials"; |
135 if (credentials.isExpired) prefix = "$prefix have expired and"; | 142 if (credentials.isExpired) prefix = "$prefix have expired and"; |
136 throw new StateError("$prefix can't be refreshed."); | 143 throw new StateError("$prefix can't be refreshed."); |
137 } | 144 } |
138 | 145 |
139 _credentials = await credentials.refresh( | 146 _credentials = await credentials.refresh( |
140 identifier, secret, | 147 identifier: identifier, |
141 newScopes: newScopes, httpClient: _httpClient); | 148 secret: secret, |
149 newScopes: newScopes, | |
150 basicAuth: _basicAuth, | |
151 httpClient: _httpClient); | |
142 | 152 |
143 return this; | 153 return this; |
144 } | 154 } |
145 | 155 |
146 /// Closes this client and its underlying HTTP client. | 156 /// Closes this client and its underlying HTTP client. |
147 void close() { | 157 void close() { |
148 if (_httpClient != null) _httpClient.close(); | 158 if (_httpClient != null) _httpClient.close(); |
149 _httpClient = null; | 159 _httpClient = null; |
150 } | 160 } |
151 } | 161 } |
OLD | NEW |