1
1
import 'package:http/http.dart' as http;
2
- import 'package:oauth2/oauth2.dart' as oauth;
2
+ import 'package:http_parser/http_parser.dart' ;
3
+ import 'package:oauth2/oauth2.dart' as oauth2;
4
+ import 'package:oauth2/oauth2.dart' ;
3
5
4
6
/// {@template oauth_grant}
5
7
/// Interface for a OAuth grant.
@@ -20,10 +22,15 @@ abstract interface class OAuthGrant {
20
22
/// Obtains credentials from an authorization server.
21
23
Future <String > handle (
22
24
Uri authorizationEndpoint,
23
- String identifier,
24
- String secret,
25
+ String identifier, {
26
+ String ? secret,
25
27
http.Client ? httpClient,
26
- );
28
+ Iterable <String >? scopes,
29
+ bool basicAuth = true ,
30
+ String ? delimiter,
31
+ Map <String , dynamic > Function (MediaType ? contentType, String body)?
32
+ getParameters,
33
+ });
27
34
}
28
35
29
36
/// {@template resource_owner_password_grant}
@@ -37,6 +44,7 @@ class ResourceOwnerPasswordGrant implements OAuthGrant {
37
44
const ResourceOwnerPasswordGrant ({
38
45
required this .username,
39
46
required this .password,
47
+ this .onCredentialsRefreshed,
40
48
});
41
49
42
50
/// Username used for obtaining credentials.
@@ -45,20 +53,36 @@ class ResourceOwnerPasswordGrant implements OAuthGrant {
45
53
/// Password used for obtaining credentials.
46
54
final String password;
47
55
56
+ /// Callback to be invoked whenever the credentials are refreshed.
57
+ ///
58
+ /// This will be passed as-is to the constructed [Client] .
59
+ /// Will be passed to [oauth2] .
60
+ final CredentialsRefreshedCallback ? onCredentialsRefreshed;
61
+
48
62
@override
49
63
Future <String > handle (
50
64
Uri authorizationEndpoint,
51
- String identifier,
52
- String secret,
65
+ String identifier, {
66
+ String ? secret,
53
67
http.Client ? httpClient,
54
- ) async {
55
- final client = await oauth.resourceOwnerPasswordGrant (
68
+ Iterable <String >? scopes,
69
+ bool basicAuth = true ,
70
+ String ? delimiter,
71
+ Map <String , dynamic > Function (MediaType ? contentType, String body)?
72
+ getParameters,
73
+ }) async {
74
+ final client = await oauth2.resourceOwnerPasswordGrant (
56
75
authorizationEndpoint,
57
76
username,
58
77
password,
59
78
secret: secret,
60
79
identifier: identifier,
80
+ scopes: scopes,
81
+ basicAuth: basicAuth,
82
+ delimiter: delimiter,
61
83
httpClient: httpClient,
84
+ getParameters: getParameters,
85
+ onCredentialsRefreshed: onCredentialsRefreshed,
62
86
);
63
87
return client.credentials.toJson ();
64
88
}
@@ -74,15 +98,24 @@ class ClientCredentialsGrant implements OAuthGrant {
74
98
@override
75
99
Future <String > handle (
76
100
Uri authorizationEndpoint,
77
- String identifier,
78
- String secret,
101
+ String identifier, {
102
+ String ? secret,
79
103
http.Client ? httpClient,
80
- ) async {
81
- final client = await oauth.clientCredentialsGrant (
104
+ Iterable <String >? scopes,
105
+ bool basicAuth = true ,
106
+ String ? delimiter,
107
+ Map <String , dynamic > Function (MediaType ? contentType, String body)?
108
+ getParameters,
109
+ }) async {
110
+ final client = await oauth2.clientCredentialsGrant (
82
111
authorizationEndpoint,
83
112
identifier,
84
113
secret,
114
+ scopes: scopes,
115
+ basicAuth: basicAuth,
116
+ delimiter: delimiter,
85
117
httpClient: httpClient,
118
+ getParameters: getParameters,
86
119
);
87
120
return client.credentials.toJson ();
88
121
}
@@ -95,10 +128,11 @@ class AuthorizationCodeGrant implements OAuthGrant {
95
128
/// {@macro authorization_code_grant}
96
129
const AuthorizationCodeGrant ({
97
130
required this .tokenEndpoint,
98
- required this .scopes,
99
131
required this .redirectUrl,
100
132
required this .redirect,
101
133
required this .listen,
134
+ this .onCredentialsRefreshed,
135
+ this .codeVerifier,
102
136
});
103
137
104
138
/// A URL provided by the authorization server that this library uses to
@@ -111,9 +145,16 @@ class AuthorizationCodeGrant implements OAuthGrant {
111
145
/// The redirect URL where the resource owner will redirect to.
112
146
final Uri redirectUrl;
113
147
114
- /// The specific permissions being requested from the authorization server may
115
- /// be specified via [scopes] .
116
- final List <String > scopes;
148
+ /// Callback to be invoked whenever the credentials are refreshed.
149
+ ///
150
+ /// This will be passed as-is to the constructed [Client] .
151
+ /// Will be passed to [oauth2] .
152
+ final CredentialsRefreshedCallback ? onCredentialsRefreshed;
153
+
154
+ /// The PKCE code verifier. Will be generated if one is not provided in the
155
+ /// constructor.
156
+ /// Will be passed to [oauth2] .
157
+ final String ? codeVerifier;
117
158
118
159
/// Callback used for redirect the authorizationUrl given by the authorization
119
160
/// server.
@@ -125,15 +166,26 @@ class AuthorizationCodeGrant implements OAuthGrant {
125
166
@override
126
167
Future <String > handle (
127
168
Uri authorizationEndpoint,
128
- String identifier,
129
- String secret,
169
+ String identifier, {
170
+ String ? secret,
130
171
http.Client ? httpClient,
131
- ) async {
132
- final grant = oauth.AuthorizationCodeGrant (
172
+ Iterable <String >? scopes,
173
+ bool basicAuth = true ,
174
+ String ? delimiter,
175
+ Map <String , dynamic > Function (MediaType ? contentType, String body)?
176
+ getParameters,
177
+ }) async {
178
+ final grant = oauth2.AuthorizationCodeGrant (
133
179
identifier,
134
180
authorizationEndpoint,
135
181
tokenEndpoint,
182
+ basicAuth: basicAuth,
183
+ delimiter: delimiter,
184
+ getParameters: getParameters,
185
+ secret: secret,
136
186
httpClient: httpClient,
187
+ onCredentialsRefreshed: onCredentialsRefreshed,
188
+ codeVerifier: codeVerifier,
137
189
);
138
190
139
191
final authorizationUrl = grant.getAuthorizationUrl (
0 commit comments