1
1
import 'package:oauth2/oauth2.dart' as oauth;
2
2
3
- abstract class OAuthGrant {
3
+ /// {@template oauth_grant}
4
+ /// Interface for a OAuth grant.
5
+ /// Grants are used to obtain credentials from an authorization server.
6
+ /// {@endtemplate}
7
+ abstract interface class OAuthGrant {
8
+ /// {@macro oauth_grant}
4
9
const OAuthGrant ();
5
10
6
- Future < String > handle (
7
- Uri authorizationEndpoint, String identifier, String secret);
11
+ /// Obtains credentials from an authorization server.
12
+ Future < String > handle ( Uri authorizationEndpoint, String identifier, String secret);
8
13
}
9
14
15
+ /// {@template resource_owner_password_grant}
10
16
/// Obtains credentials using a [resource owner password grant] (https://tools.ietf.org/html/rfc6749#section-1.3.3).
11
- class ResourceOwnerPasswordGrant extends OAuthGrant {
17
+ ///
18
+ /// This grant uses the resource owner's [username] and [password] to obtain
19
+ /// credentials.
20
+ /// {@endtemplate}
21
+ class ResourceOwnerPasswordGrant implements OAuthGrant {
22
+ /// {@macro resource_owner_password_grant}
23
+ const ResourceOwnerPasswordGrant ({
24
+ required this .username,
25
+ required this .password,
26
+ });
27
+
28
+ /// Username used for obtaining credentials.
12
29
final String username;
13
- final String password;
14
30
15
- const ResourceOwnerPasswordGrant (
16
- { required this .username, required this . password}) ;
31
+ /// Password used for obtaining credentials.
32
+ final String password;
17
33
18
34
@override
19
- Future <String > handle (
20
- Uri authorizationEndpoint, String identifier, String secret) async {
35
+ Future <String > handle (Uri authorizationEndpoint, String identifier, String secret) async {
21
36
final client = await oauth.resourceOwnerPasswordGrant (
22
37
authorizationEndpoint,
23
38
username,
@@ -29,13 +44,15 @@ class ResourceOwnerPasswordGrant extends OAuthGrant {
29
44
}
30
45
}
31
46
47
+ /// {@template client_credentials_grant}
32
48
/// Obtains credentials using a [client credentials grant] (https://tools.ietf.org/html/rfc6749#section-1.3.4).
33
- class ClientCredentialsGrant extends OAuthGrant {
49
+ /// {@endtemplate}
50
+ class ClientCredentialsGrant implements OAuthGrant {
51
+ /// {@macro client_credentials_grant}
34
52
const ClientCredentialsGrant ();
35
53
36
54
@override
37
- Future <String > handle (
38
- Uri authorizationEndpoint, String identifier, String secret) async {
55
+ Future <String > handle (Uri authorizationEndpoint, String identifier, String secret) async {
39
56
final client = await oauth.clientCredentialsGrant (
40
57
authorizationEndpoint,
41
58
identifier,
@@ -45,8 +62,11 @@ class ClientCredentialsGrant extends OAuthGrant {
45
62
}
46
63
}
47
64
65
+ /// {@template authorization_code_grant}
48
66
/// Obtains credentials using a [authorization code grant] (https://tools.ietf.org/html/rfc6749#section-1.3.1).
49
- class AuthorizationCodeGrant extends OAuthGrant {
67
+ /// {@endtemplate}
68
+ class AuthorizationCodeGrant implements OAuthGrant {
69
+ /// {@macro authorization_code_grant}
50
70
const AuthorizationCodeGrant ({
51
71
required this .tokenEndpoint,
52
72
required this .scopes,
@@ -55,26 +75,48 @@ class AuthorizationCodeGrant extends OAuthGrant {
55
75
required this .listen,
56
76
});
57
77
78
+ /// A URL provided by the authorization server that this library uses to
79
+ /// obtain long-lasting credentials.
80
+ ///
81
+ /// This will usually be listed in the authorization server's OAuth2 API
82
+ /// documentation.
58
83
final Uri tokenEndpoint;
84
+
85
+ /// The redirect URL where the resource owner will redirect to.
59
86
final Uri redirectUrl;
87
+
88
+ /// The specific permissions being requested from the authorization server may
89
+ /// be specified via [scopes] .
60
90
final List <String > scopes;
91
+ /// Callback used for redirect the authorizationUrl given by the authorization
92
+ /// server.
61
93
final Future <void > Function (Uri authorizationUri) redirect;
94
+ /// Callback used for listening for the redirectUrl.
62
95
final Future <Uri > Function (Uri redirectUri) listen;
63
96
64
97
@override
65
98
Future <String > handle (
66
- Uri authorizationEndpoint, String identifier, String secret) async {
99
+ Uri authorizationEndpoint,
100
+ String identifier,
101
+ String secret,
102
+ ) async {
67
103
final grant = oauth.AuthorizationCodeGrant (
68
104
identifier,
69
105
authorizationEndpoint,
70
106
tokenEndpoint,
71
107
);
72
- var authorizationUrl =
73
- grant.getAuthorizationUrl (redirectUrl, scopes: scopes);
108
+
109
+ final authorizationUrl = grant.getAuthorizationUrl (
110
+ redirectUrl,
111
+ scopes: scopes,
112
+ );
113
+
74
114
await redirect (authorizationUrl);
75
- var responseUrl = await listen (redirectUrl);
76
- oauth.Client client =
77
- await grant.handleAuthorizationResponse (responseUrl.queryParameters);
115
+ final responseUrl = await listen (redirectUrl);
116
+
117
+ final oauth.Client client = await grant.handleAuthorizationResponse (
118
+ responseUrl.queryParameters,
119
+ );
78
120
79
121
return client.credentials.toJson ();
80
122
}
0 commit comments