Skip to content

Commit 0141d56

Browse files
authored
Merge pull request #18 from DutchCodingCompany/feature/add_custom_client
✨ Add custom client
2 parents 4162fe5 + dfeea5f commit 0141d56

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

lib/src/oauth_chopper.dart

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:async';
22

3-
import 'package:oauth2/oauth2.dart';
3+
import 'package:http/http.dart' as http;
4+
import 'package:oauth2/oauth2.dart' as oauth2;
45
import 'package:oauth_chopper/src/oauth_authenticator.dart';
56
import 'package:oauth_chopper/src/oauth_grant.dart';
67
import 'package:oauth_chopper/src/oauth_interceptor.dart';
@@ -27,6 +28,7 @@ class OAuthChopper {
2728
required this.identifier,
2829
required this.secret,
2930
this.endSessionEndpoint,
31+
this.httpClient,
3032

3133
/// OAuth storage for storing credentials.
3234
/// By default it will use a in memory storage [MemoryStorage].
@@ -53,6 +55,10 @@ class OAuthChopper {
5355
/// See [OAuthStorage] for more information.
5456
final OAuthStorage _storage;
5557

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+
5662
/// Get stored [OAuthToken].
5763
Future<OAuthToken?> get token async {
5864
final credentialsJson = await _storage.fetchCredentials();
@@ -76,18 +82,21 @@ class OAuthChopper {
7682
/// Tries to refresh the available credentials and returns a new [OAuthToken]
7783
/// instance.
7884
/// 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]
8187
Future<OAuthToken?> refresh() async {
8288
final credentialsJson = await _storage.fetchCredentials();
8389
if (credentialsJson == null) return null;
84-
final credentials = Credentials.fromJson(credentialsJson);
90+
final credentials = oauth2.Credentials.fromJson(credentialsJson);
8591
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+
);
8897
await _storage.saveCredentials(newCredentials.toJson());
8998
return OAuthToken.fromCredentials(newCredentials);
90-
} on AuthorizationException {
99+
} on oauth2.AuthorizationException {
91100
_storage.clear();
92101
rethrow;
93102
}
@@ -99,11 +108,15 @@ class OAuthChopper {
99108
/// Currently supported grants:
100109
/// - [ResourceOwnerPasswordGrant]
101110
/// - [ClientCredentialsGrant]
102-
///
111+
/// - [AuthorizationCodeGrant]
103112
/// Throws an exception if the grant fails.
104113
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+
);
107120

108121
await _storage.saveCredentials(credentials);
109122

lib/src/oauth_grant.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:http/http.dart' as http;
12
import 'package:oauth2/oauth2.dart' as oauth;
23

34
/// {@template oauth_grant}
@@ -13,6 +14,7 @@ abstract interface class OAuthGrant {
1314
Uri authorizationEndpoint,
1415
String identifier,
1516
String secret,
17+
http.Client? httpClient,
1618
);
1719
}
1820

@@ -40,13 +42,15 @@ class ResourceOwnerPasswordGrant implements OAuthGrant {
4042
Uri authorizationEndpoint,
4143
String identifier,
4244
String secret,
45+
http.Client? httpClient,
4346
) async {
4447
final client = await oauth.resourceOwnerPasswordGrant(
4548
authorizationEndpoint,
4649
username,
4750
password,
4851
secret: secret,
4952
identifier: identifier,
53+
httpClient: httpClient,
5054
);
5155
return client.credentials.toJson();
5256
}
@@ -64,11 +68,13 @@ class ClientCredentialsGrant implements OAuthGrant {
6468
Uri authorizationEndpoint,
6569
String identifier,
6670
String secret,
71+
http.Client? httpClient,
6772
) async {
6873
final client = await oauth.clientCredentialsGrant(
6974
authorizationEndpoint,
7075
identifier,
7176
secret,
77+
httpClient: httpClient,
7278
);
7379
return client.credentials.toJson();
7480
}
@@ -113,11 +119,13 @@ class AuthorizationCodeGrant implements OAuthGrant {
113119
Uri authorizationEndpoint,
114120
String identifier,
115121
String secret,
122+
http.Client? httpClient,
116123
) async {
117124
final grant = oauth.AuthorizationCodeGrant(
118125
identifier,
119126
authorizationEndpoint,
120127
tokenEndpoint,
128+
httpClient: httpClient,
121129
);
122130

123131
final authorizationUrl = grant.getAuthorizationUrl(

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ environment:
88

99
dependencies:
1010
chopper: ^7.2.0
11+
http: ^1.2.1
1112
oauth2: ^2.0.2
1213

1314
dev_dependencies:
14-
http: ^1.2.1
1515
mocktail: ^1.0.3
1616
test: ^1.25.2
1717
very_good_analysis: ^5.1.0

test/oauth_chopper_test.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ void main() {
9393
test('Successful grant is stored', () async {
9494
// arrange
9595
when(() => storageMock.saveCredentials(any())).thenAnswer((_) => null);
96-
when(() => grantMock.handle(any(), any(), any()))
96+
when(() => grantMock.handle(any(), any(), any(), null))
9797
.thenAnswer((_) async => testJson);
9898
final oauthChopper = OAuthChopper(
9999
authorizationEndpoint: Uri.parse('endpoint'),
@@ -106,7 +106,8 @@ void main() {
106106
final token = await oauthChopper.requestGrant(grantMock);
107107

108108
// assert
109-
verify(() => grantMock.handle(any(), 'identifier', 'secret')).called(1);
109+
verify(() => grantMock.handle(any(), 'identifier', 'secret', null))
110+
.called(1);
110111
verify(() => storageMock.saveCredentials(testJson)).called(1);
111112
expect(token.accessToken, 'accesToken');
112113
expect(token.idToken, 'idToken');

0 commit comments

Comments
 (0)