1
1
import 'dart:async' ;
2
2
3
- import 'package:oauth2/oauth2.dart' ;
3
+ import 'package:http/http.dart' as http;
4
+ import 'package:oauth2/oauth2.dart' as oauth2;
4
5
import 'package:oauth_chopper/src/oauth_authenticator.dart' ;
5
6
import 'package:oauth_chopper/src/oauth_grant.dart' ;
6
7
import 'package:oauth_chopper/src/oauth_interceptor.dart' ;
@@ -27,6 +28,7 @@ class OAuthChopper {
27
28
required this .identifier,
28
29
required this .secret,
29
30
this .endSessionEndpoint,
31
+ this .httpClient,
30
32
31
33
/// OAuth storage for storing credentials.
32
34
/// By default it will use a in memory storage [MemoryStorage] .
@@ -53,6 +55,10 @@ class OAuthChopper {
53
55
/// See [OAuthStorage] for more information.
54
56
final OAuthStorage _storage;
55
57
58
+ /// Provide a custom [http.Client] which will be passed to [oauth2] and used
59
+ /// for making new requests.
60
+ final http.Client ? httpClient;
61
+
56
62
/// Get stored [OAuthToken] .
57
63
Future <OAuthToken ?> get token async {
58
64
final credentialsJson = await _storage.fetchCredentials ();
@@ -76,18 +82,21 @@ class OAuthChopper {
76
82
/// Tries to refresh the available credentials and returns a new [OAuthToken]
77
83
/// instance.
78
84
/// Throws an exception when refreshing fails. If the exception is a
79
- /// [AuthorizationException] it clears the storage.
80
- /// See [Credentials.refresh]
85
+ /// [oauth2. AuthorizationException] it clears the storage.
86
+ /// See [oauth2. Credentials.refresh]
81
87
Future <OAuthToken ?> refresh () async {
82
88
final credentialsJson = await _storage.fetchCredentials ();
83
89
if (credentialsJson == null ) return null ;
84
- final credentials = Credentials .fromJson (credentialsJson);
90
+ final credentials = oauth2. Credentials .fromJson (credentialsJson);
85
91
try {
86
- final newCredentials =
87
- await credentials.refresh (identifier: identifier, secret: secret);
92
+ final newCredentials = await credentials.refresh (
93
+ identifier: identifier,
94
+ secret: secret,
95
+ httpClient: httpClient,
96
+ );
88
97
await _storage.saveCredentials (newCredentials.toJson ());
89
98
return OAuthToken .fromCredentials (newCredentials);
90
- } on AuthorizationException {
99
+ } on oauth2. AuthorizationException {
91
100
_storage.clear ();
92
101
rethrow ;
93
102
}
@@ -99,11 +108,15 @@ class OAuthChopper {
99
108
/// Currently supported grants:
100
109
/// - [ResourceOwnerPasswordGrant]
101
110
/// - [ClientCredentialsGrant]
102
- ///
111
+ /// - [AuthorizationCodeGrant]
103
112
/// Throws an exception if the grant fails.
104
113
Future <OAuthToken > requestGrant (OAuthGrant grant) async {
105
- final credentials =
106
- await grant.handle (authorizationEndpoint, identifier, secret);
114
+ final credentials = await grant.handle (
115
+ authorizationEndpoint,
116
+ identifier,
117
+ secret,
118
+ httpClient,
119
+ );
107
120
108
121
await _storage.saveCredentials (credentials);
109
122
0 commit comments