@@ -3,21 +3,18 @@ import 'package:oauth2/oauth2.dart' as oauth;
3
3
abstract class OAuthGrant {
4
4
const OAuthGrant ();
5
5
6
- Future <String > handle (
7
- Uri authorizationEndpoint, String identifier, String secret);
6
+ Future <String > handle (Uri authorizationEndpoint, String identifier, String secret);
8
7
}
9
8
10
9
/// Obtains credentials using a [resource owner password grant] (https://tools.ietf.org/html/rfc6749#section-1.3.3).
11
10
class ResourceOwnerPasswordGrant extends OAuthGrant {
12
11
final String username;
13
12
final String password;
14
13
15
- const ResourceOwnerPasswordGrant (
16
- {required this .username, required this .password});
14
+ const ResourceOwnerPasswordGrant ({required this .username, required this .password});
17
15
18
16
@override
19
- Future <String > handle (
20
- Uri authorizationEndpoint, String identifier, String secret) async {
17
+ Future <String > handle (Uri authorizationEndpoint, String identifier, String secret) async {
21
18
final client = await oauth.resourceOwnerPasswordGrant (
22
19
authorizationEndpoint,
23
20
username,
@@ -34,8 +31,7 @@ class ClientCredentialsGrant extends OAuthGrant {
34
31
const ClientCredentialsGrant ();
35
32
36
33
@override
37
- Future <String > handle (
38
- Uri authorizationEndpoint, String identifier, String secret) async {
34
+ Future <String > handle (Uri authorizationEndpoint, String identifier, String secret) async {
39
35
final client = await oauth.clientCredentialsGrant (
40
36
authorizationEndpoint,
41
37
identifier,
@@ -45,4 +41,34 @@ class ClientCredentialsGrant extends OAuthGrant {
45
41
}
46
42
}
47
43
48
- //TODO: Add AuthorizationCodeGrant
44
+ /// Obtains credentials using a [authorization code grant] (https://tools.ietf.org/html/rfc6749#section-1.3.1).
45
+ class AuthorizationCodeGrant extends OAuthGrant {
46
+ const AuthorizationCodeGrant (
47
+ this .tokenEndpoint,
48
+ this .scopes,
49
+ this .redirectUrl,
50
+ this .redirect,
51
+ this .listen,
52
+ );
53
+
54
+ final Uri tokenEndpoint;
55
+ final Uri redirectUrl;
56
+ final List <String > scopes;
57
+ final Future <void > Function (Uri authorizationUri) redirect;
58
+ final Future <Uri > Function (Uri redirectUri) listen;
59
+
60
+ @override
61
+ Future <String > handle (Uri authorizationEndpoint, String identifier, String secret) async {
62
+ final grant = oauth.AuthorizationCodeGrant (
63
+ identifier,
64
+ authorizationEndpoint,
65
+ tokenEndpoint,
66
+ );
67
+ var authorizationUrl = grant.getAuthorizationUrl (redirectUrl, scopes: scopes);
68
+ await redirect (authorizationUrl);
69
+ var responseUrl = await listen (redirectUrl);
70
+ oauth.Client client = await grant.handleAuthorizationResponse (responseUrl.queryParameters);
71
+
72
+ return client.credentials.toJson ();
73
+ }
74
+ }
0 commit comments