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 client; | 5 library client; |
6 | 6 |
7 import 'dart:uri'; | 7 import 'dart:uri'; |
8 | 8 |
9 import '../../../http/lib/http.dart' as http; | 9 import '../../../http/lib/http.dart' as http; |
10 | 10 |
11 import 'credentials.dart'; | 11 import 'credentials.dart'; |
12 import 'expiration_exception.dart'; | 12 import 'expiration_exception.dart'; |
13 import 'utils.dart'; | 13 import 'utils.dart'; |
14 | 14 |
15 // TODO(nweiz): Add an onCredentialsRefreshed event once we have some event | 15 // TODO(nweiz): Add an onCredentialsRefreshed event once we have some event |
16 // infrastructure. | 16 // infrastructure. |
17 /// An OAuth2 client. This acts as a drop-in replacement for an | 17 /// An OAuth2 client. This acts as a drop-in replacement for an [http.Client], |
18 /// [http.BaseClient], while sending OAuth2 authorization credentials along with | 18 /// while sending OAuth2 authorization credentials along with each request. |
19 /// each request. | |
20 /// | 19 /// |
21 /// The client also automatically refreshes its credentials if possible. When it | 20 /// The client also automatically refreshes its credentials if possible. When it |
22 /// makes a request, if its credentials are expired, it will first refresh them. | 21 /// makes a request, if its credentials are expired, it will first refresh them. |
23 /// This means that any request may throw an [AuthorizationException] if the | 22 /// This means that any request may throw an [AuthorizationException] if the |
24 /// refresh is not authorized for some reason, a [FormatException] if the | 23 /// refresh is not authorized for some reason, a [FormatException] if the |
25 /// authorization server provides ill-formatted responses, or an | 24 /// authorization server provides ill-formatted responses, or an |
26 /// [ExpirationException] if the credentials are expired and can't be refreshed. | 25 /// [ExpirationException] if the credentials are expired and can't be refreshed. |
27 /// | 26 /// |
28 /// Currently this client doesn't attempt to identify errors from the resource | 27 /// Currently this client doesn't attempt to identify errors from the resource |
29 /// server that are caused by authentication failure. However, it may throw | 28 /// server that are caused by authentication failure. However, it may throw |
(...skipping 25 matching lines...) Expand all Loading... |
55 /// certainty that a client is who it claims to be. | 54 /// certainty that a client is who it claims to be. |
56 final String secret; | 55 final String secret; |
57 | 56 |
58 /// The credentials this client uses to prove to the resource server that it's | 57 /// The credentials this client uses to prove to the resource server that it's |
59 /// authorized. This may change from request to request as the credentials | 58 /// authorized. This may change from request to request as the credentials |
60 /// expire and the client refreshes them automatically. | 59 /// expire and the client refreshes them automatically. |
61 Credentials get credentials => _credentials; | 60 Credentials get credentials => _credentials; |
62 Credentials _credentials; | 61 Credentials _credentials; |
63 | 62 |
64 /// The underlying HTTP client. | 63 /// The underlying HTTP client. |
65 http.BaseClient _httpClient; | 64 http.Client _httpClient; |
66 | 65 |
67 /// Creates a new client from a pre-existing set of credentials. When | 66 /// Creates a new client from a pre-existing set of credentials. When |
68 /// authorizing a client for the first time, you should use | 67 /// authorizing a client for the first time, you should use |
69 /// [AuthorizationCodeGrant] instead of constructing a [Client] directly. | 68 /// [AuthorizationCodeGrant] instead of constructing a [Client] directly. |
70 /// | 69 /// |
71 /// [httpClient] is the underlying client that this forwards requests to after | 70 /// [httpClient] is the underlying client that this forwards requests to after |
72 /// adding authorization credentials to them. | 71 /// adding authorization credentials to them. |
73 Client( | 72 Client( |
74 this.identifier, | 73 this.identifier, |
75 this.secret, | 74 this.secret, |
76 this._credentials, | 75 this._credentials, |
77 {http.BaseClient httpClient}) | 76 {http.Client httpClient}) |
78 : _httpClient = httpClient == null ? new http.Client() : httpClient; | 77 : _httpClient = httpClient == null ? new http.Client() : httpClient; |
79 | 78 |
80 /// Sends an HTTP request with OAuth2 authorization credentials attached. This | 79 /// Sends an HTTP request with OAuth2 authorization credentials attached. This |
81 /// will also automatically refresh this client's [Credentials] before sending | 80 /// will also automatically refresh this client's [Credentials] before sending |
82 /// the request if necessary. | 81 /// the request if necessary. |
83 Future<http.StreamedResponse> send(http.BaseRequest request) { | 82 Future<http.StreamedResponse> send(http.BaseRequest request) { |
84 return async.chain((_) { | 83 return async.chain((_) { |
85 if (!credentials.isExpired) return new Future.immediate(null); | 84 if (!credentials.isExpired) return new Future.immediate(null); |
86 if (!credentials.canRefresh) throw new ExpirationException(credentials); | 85 if (!credentials.canRefresh) throw new ExpirationException(credentials); |
87 return refreshCredentials(); | 86 return refreshCredentials(); |
(...skipping 28 matching lines...) Expand all Loading... |
116 return this; | 115 return this; |
117 }); | 116 }); |
118 } | 117 } |
119 | 118 |
120 /// Closes this client and its underlying HTTP client. | 119 /// Closes this client and its underlying HTTP client. |
121 void close() { | 120 void close() { |
122 if (_httpClient != null) _httpClient.close(); | 121 if (_httpClient != null) _httpClient.close(); |
123 _httpClient = null; | 122 _httpClient = null; |
124 } | 123 } |
125 } | 124 } |
OLD | NEW |