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

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

Issue 812253002: Delete a bunch of packages that are now on GitHub. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Un-delete http Created 6 years 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
« no previous file with comments | « pkg/oauth2/lib/src/authorization_exception.dart ('k') | pkg/oauth2/lib/src/credentials.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 oauth2_client;
6
7 import 'dart:async';
8
9 import 'package:http/http.dart' as http;
10
11 import 'authorization_exception.dart';
12 import 'credentials.dart';
13 import 'expiration_exception.dart';
14 import 'utils.dart';
15
16 // TODO(nweiz): Add an onCredentialsRefreshed event once we have some event
17 // infrastructure.
18 /// An OAuth2 client. This acts as a drop-in replacement for an [http.Client],
19 /// while sending OAuth2 authorization credentials along with each request.
20 ///
21 /// 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.
23 /// This means that any request may throw an [AuthorizationException] if the
24 /// refresh is not authorized for some reason, a [FormatException] if the
25 /// authorization server provides ill-formatted responses, or an
26 /// [ExpirationException] if the credentials are expired and can't be refreshed.
27 ///
28 /// The client will also throw an [AuthorizationException] if the resource
29 /// server returns a 401 response with a WWW-Authenticate header indicating that
30 /// the current credentials are invalid.
31 ///
32 /// If you already have a set of [Credentials], you can construct a [Client]
33 /// directly. However, in order to first obtain the credentials, you must
34 /// authorize. At the time of writing, the only authorization method this
35 /// library supports is [AuthorizationCodeGrant].
36 class Client extends http.BaseClient {
37 /// The client identifier for this client. The authorization server will issue
38 /// each client a separate client identifier and secret, which allows the
39 /// server to tell which client is accessing it. Some servers may also have an
40 /// anonymous identifier/secret pair that any client may use.
41 ///
42 /// This is usually global to the program using this library.
43 final String identifier;
44
45 /// The client secret for this client. The authorization server will issue
46 /// each client a separate client identifier and secret, which allows the
47 /// server to tell which client is accessing it. Some servers may also have an
48 /// anonymous identifier/secret pair that any client may use.
49 ///
50 /// This is usually global to the program using this library.
51 ///
52 /// Note that clients whose source code or binary executable is readily
53 /// available may not be able to make sure the client secret is kept a secret.
54 /// This is fine; OAuth2 servers generally won't rely on knowing with
55 /// certainty that a client is who it claims to be.
56 final String secret;
57
58 /// 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
60 /// expire and the client refreshes them automatically.
61 Credentials get credentials => _credentials;
62 Credentials _credentials;
63
64 /// The underlying HTTP client.
65 http.Client _httpClient;
66
67 /// Creates a new client from a pre-existing set of credentials. When
68 /// authorizing a client for the first time, you should use
69 /// [AuthorizationCodeGrant] instead of constructing a [Client] directly.
70 ///
71 /// [httpClient] is the underlying client that this forwards requests to after
72 /// adding authorization credentials to them.
73 Client(
74 this.identifier,
75 this.secret,
76 this._credentials,
77 {http.Client httpClient})
78 : _httpClient = httpClient == null ? new http.Client() : httpClient;
79
80 /// Sends an HTTP request with OAuth2 authorization credentials attached. This
81 /// will also automatically refresh this client's [Credentials] before sending
82 /// the request if necessary.
83 Future<http.StreamedResponse> send(http.BaseRequest request) {
84 return async.then((_) {
85 if (!credentials.isExpired) return new Future.value();
86 if (!credentials.canRefresh) throw new ExpirationException(credentials);
87 return refreshCredentials();
88 }).then((_) {
89 request.headers['authorization'] = "Bearer ${credentials.accessToken}";
90 return _httpClient.send(request);
91 }).then((response) {
92 if (response.statusCode != 401 ||
93 !response.headers.containsKey('www-authenticate')) {
94 return response;
95 }
96
97 var authenticate;
98 try {
99 authenticate = new AuthenticateHeader.parse(
100 response.headers['www-authenticate']);
101 } on FormatException catch (e) {
102 return response;
103 }
104
105 if (authenticate.scheme != 'bearer') return response;
106
107 var params = authenticate.parameters;
108 if (!params.containsKey('error')) return response;
109
110 throw new AuthorizationException(
111 params['error'], params['error_description'],
112 params['error_uri'] == null ? null : Uri.parse(params['error_uri']));
113 });
114 }
115
116 /// Explicitly refreshes this client's credentials. Returns this client.
117 ///
118 /// This will throw a [StateError] if the [Credentials] can't be refreshed, an
119 /// [AuthorizationException] if refreshing the credentials fails, or a
120 /// [FormatError] if the authorization server returns invalid responses.
121 ///
122 /// You may request different scopes than the default by passing in
123 /// [newScopes]. These must be a subset of the scopes in the
124 /// [Credentials.scopes] field of [Client.credentials].
125 Future<Client> refreshCredentials([List<String> newScopes]) {
126 return async.then((_) {
127 if (!credentials.canRefresh) {
128 var prefix = "OAuth credentials";
129 if (credentials.isExpired) prefix = "$prefix have expired and";
130 throw new StateError("$prefix can't be refreshed.");
131 }
132
133 return credentials.refresh(identifier, secret,
134 newScopes: newScopes, httpClient: _httpClient);
135 }).then((credentials) {
136 _credentials = credentials;
137 return this;
138 });
139 }
140
141 /// Closes this client and its underlying HTTP client.
142 void close() {
143 if (_httpClient != null) _httpClient.close();
144 _httpClient = null;
145 }
146 }
OLDNEW
« no previous file with comments | « pkg/oauth2/lib/src/authorization_exception.dart ('k') | pkg/oauth2/lib/src/credentials.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698