11import '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' ;
35
46/// {@template oauth_grant}
57/// Interface for a OAuth grant.
@@ -20,10 +22,15 @@ abstract interface class OAuthGrant {
2022 /// Obtains credentials from an authorization server.
2123 Future <String > handle (
2224 Uri authorizationEndpoint,
23- String identifier,
24- String secret,
25+ String identifier, {
26+ String ? secret,
2527 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+ });
2734}
2835
2936/// {@template resource_owner_password_grant}
@@ -37,6 +44,7 @@ class ResourceOwnerPasswordGrant implements OAuthGrant {
3744 const ResourceOwnerPasswordGrant ({
3845 required this .username,
3946 required this .password,
47+ this .onCredentialsRefreshed,
4048 });
4149
4250 /// Username used for obtaining credentials.
@@ -45,20 +53,36 @@ class ResourceOwnerPasswordGrant implements OAuthGrant {
4553 /// Password used for obtaining credentials.
4654 final String password;
4755
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+
4862 @override
4963 Future <String > handle (
5064 Uri authorizationEndpoint,
51- String identifier,
52- String secret,
65+ String identifier, {
66+ String ? secret,
5367 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 (
5675 authorizationEndpoint,
5776 username,
5877 password,
5978 secret: secret,
6079 identifier: identifier,
80+ scopes: scopes,
81+ basicAuth: basicAuth,
82+ delimiter: delimiter,
6183 httpClient: httpClient,
84+ getParameters: getParameters,
85+ onCredentialsRefreshed: onCredentialsRefreshed,
6286 );
6387 return client.credentials.toJson ();
6488 }
@@ -74,15 +98,24 @@ class ClientCredentialsGrant implements OAuthGrant {
7498 @override
7599 Future <String > handle (
76100 Uri authorizationEndpoint,
77- String identifier,
78- String secret,
101+ String identifier, {
102+ String ? secret,
79103 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 (
82111 authorizationEndpoint,
83112 identifier,
84113 secret,
114+ scopes: scopes,
115+ basicAuth: basicAuth,
116+ delimiter: delimiter,
85117 httpClient: httpClient,
118+ getParameters: getParameters,
86119 );
87120 return client.credentials.toJson ();
88121 }
@@ -95,10 +128,11 @@ class AuthorizationCodeGrant implements OAuthGrant {
95128 /// {@macro authorization_code_grant}
96129 const AuthorizationCodeGrant ({
97130 required this .tokenEndpoint,
98- required this .scopes,
99131 required this .redirectUrl,
100132 required this .redirect,
101133 required this .listen,
134+ this .onCredentialsRefreshed,
135+ this .codeVerifier,
102136 });
103137
104138 /// A URL provided by the authorization server that this library uses to
@@ -111,9 +145,16 @@ class AuthorizationCodeGrant implements OAuthGrant {
111145 /// The redirect URL where the resource owner will redirect to.
112146 final Uri redirectUrl;
113147
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;
117158
118159 /// Callback used for redirect the authorizationUrl given by the authorization
119160 /// server.
@@ -125,15 +166,26 @@ class AuthorizationCodeGrant implements OAuthGrant {
125166 @override
126167 Future <String > handle (
127168 Uri authorizationEndpoint,
128- String identifier,
129- String secret,
169+ String identifier, {
170+ String ? secret,
130171 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 (
133179 identifier,
134180 authorizationEndpoint,
135181 tokenEndpoint,
182+ basicAuth: basicAuth,
183+ delimiter: delimiter,
184+ getParameters: getParameters,
185+ secret: secret,
136186 httpClient: httpClient,
187+ onCredentialsRefreshed: onCredentialsRefreshed,
188+ codeVerifier: codeVerifier,
137189 );
138190
139191 final authorizationUrl = grant.getAuthorizationUrl (
0 commit comments