Skip to content

Commit 54d2d4f

Browse files
committed
✨ Added passing of custom http client
1 parent ce6a5a8 commit 54d2d4f

File tree

4 files changed

+42
-20
lines changed

4 files changed

+42
-20
lines changed

lib/src/oauth_chopper.dart

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

3+
import 'package:http/http.dart' as http;
34
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';
@@ -36,11 +37,15 @@ class OAuthChopper {
3637
/// See [OAuthStorage] for more information.
3738
final OAuthStorage _storage;
3839

40+
/// Provide a custom [http.Client] which will be passed to [oauth2] and used for making new requests.
41+
final http.Client? httpClient;
42+
3943
OAuthChopper({
4044
required this.authorizationEndpoint,
4145
required this.identifier,
4246
required this.secret,
4347
this.endSessionEndpoint,
48+
this.httpClient,
4449

4550
/// OAuth storage for storing credentials.
4651
/// By default it will use a in memory storage [MemoryStorage]. For persisting the credentials implement a custom [OAuthStorage].
@@ -51,9 +56,7 @@ class OAuthChopper {
5156
/// Get stored [OAuthToken].
5257
Future<OAuthToken?> get token async {
5358
final credentialsJson = await _storage.fetchCredentials();
54-
return credentialsJson != null
55-
? OAuthToken.fromJson(credentialsJson)
56-
: null;
59+
return credentialsJson != null ? OAuthToken.fromJson(credentialsJson) : null;
5760
}
5861

5962
/// Provides an [OAuthAuthenticator] instance.
@@ -75,8 +78,11 @@ class OAuthChopper {
7578
if (credentialsJson == null) return null;
7679
final credentials = oauth2.Credentials.fromJson(credentialsJson);
7780
try {
78-
final newCredentials =
79-
await credentials.refresh(identifier: identifier, secret: secret);
81+
final newCredentials = await credentials.refresh(
82+
identifier: identifier,
83+
secret: secret,
84+
httpClient: httpClient,
85+
);
8086
await _storage.saveCredentials(newCredentials.toJson());
8187
return OAuthToken.fromCredentials(newCredentials);
8288
} on oauth2.AuthorizationException {
@@ -92,8 +98,7 @@ class OAuthChopper {
9298
/// - [AuthorizationCodeGrant]
9399
/// Throws an exception if the grant fails.
94100
Future<OAuthToken> requestGrant(OAuthGrant grant) async {
95-
final credentials =
96-
await grant.handle(authorizationEndpoint, identifier, secret);
101+
final credentials = await grant.handle(authorizationEndpoint, identifier, secret, httpClient);
97102

98103
await _storage.saveCredentials(credentials);
99104

lib/src/oauth_grant.dart

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
1+
import 'package:http/http.dart' as http;
12
import 'package:oauth2/oauth2.dart' as oauth;
23

34
abstract class OAuthGrant {
45
const OAuthGrant();
56

67
Future<String> handle(
7-
Uri authorizationEndpoint, String identifier, String secret);
8+
Uri authorizationEndpoint,
9+
String identifier,
10+
String secret,
11+
http.Client? httpClient,
12+
);
813
}
914

1015
/// Obtains credentials using a [resource owner password grant](https://tools.ietf.org/html/rfc6749#section-1.3.3).
1116
class ResourceOwnerPasswordGrant extends OAuthGrant {
1217
final String username;
1318
final String password;
1419

15-
const ResourceOwnerPasswordGrant(
16-
{required this.username, required this.password});
20+
const ResourceOwnerPasswordGrant({required this.username, required this.password});
1721

1822
@override
1923
Future<String> handle(
20-
Uri authorizationEndpoint, String identifier, String secret) async {
24+
Uri authorizationEndpoint,
25+
String identifier,
26+
String secret,
27+
http.Client? httpClient,
28+
) async {
2129
final client = await oauth.resourceOwnerPasswordGrant(
2230
authorizationEndpoint,
2331
username,
2432
password,
2533
secret: secret,
2634
identifier: identifier,
35+
httpClient: httpClient,
2736
);
2837
return client.credentials.toJson();
2938
}
@@ -35,11 +44,16 @@ class ClientCredentialsGrant extends OAuthGrant {
3544

3645
@override
3746
Future<String> handle(
38-
Uri authorizationEndpoint, String identifier, String secret) async {
47+
Uri authorizationEndpoint,
48+
String identifier,
49+
String secret,
50+
http.Client? httpClient,
51+
) async {
3952
final client = await oauth.clientCredentialsGrant(
4053
authorizationEndpoint,
4154
identifier,
4255
secret,
56+
httpClient: httpClient,
4357
);
4458
return client.credentials.toJson();
4559
}
@@ -63,18 +77,21 @@ class AuthorizationCodeGrant extends OAuthGrant {
6377

6478
@override
6579
Future<String> handle(
66-
Uri authorizationEndpoint, String identifier, String secret) async {
80+
Uri authorizationEndpoint,
81+
String identifier,
82+
String secret,
83+
http.Client? httpClient,
84+
) async {
6785
final grant = oauth.AuthorizationCodeGrant(
6886
identifier,
6987
authorizationEndpoint,
7088
tokenEndpoint,
89+
httpClient: httpClient,
7190
);
72-
var authorizationUrl =
73-
grant.getAuthorizationUrl(redirectUrl, scopes: scopes);
91+
var authorizationUrl = grant.getAuthorizationUrl(redirectUrl, scopes: scopes);
7492
await redirect(authorizationUrl);
7593
var responseUrl = await listen(redirectUrl);
76-
oauth.Client client =
77-
await grant.handleAuthorizationResponse(responseUrl.queryParameters);
94+
oauth.Client client = await grant.handleAuthorizationResponse(responseUrl.queryParameters);
7895

7996
return client.credentials.toJson();
8097
}

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
lints: ">=2.1.1 <4.0.0"
1616
mocktail: ^1.0.3
1717
test: ^1.25.2

test/oauth_chopper_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void main() {
8989
test("Successful grant is stored", () async {
9090
// arrange
9191
when(() => storageMock.saveCredentials(any())).thenAnswer((_) => null);
92-
when(() => grantMock.handle(any(), any(), any()))
92+
when(() => grantMock.handle(any(), any(), any(), null))
9393
.thenAnswer((_) async => testJson);
9494
final oauthChopper = OAuthChopper(
9595
authorizationEndpoint: Uri.parse('endpoint'),
@@ -101,7 +101,7 @@ void main() {
101101
final token = await oauthChopper.requestGrant(grantMock);
102102

103103
// assert
104-
verify(() => grantMock.handle(any(), 'identifier', 'secret')).called(1);
104+
verify(() => grantMock.handle(any(), 'identifier', 'secret', null)).called(1);
105105
verify(() => storageMock.saveCredentials(testJson)).called(1);
106106
expect(token.accessToken, 'accesToken');
107107
expect(token.idToken, 'idToken');

0 commit comments

Comments
 (0)