Skip to content

Commit a8fc9e9

Browse files
author
Bas de Vaan
committed
add Authorization code grant
1 parent 10d67b3 commit a8fc9e9

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

lib/src/oauth_grant.dart

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,18 @@ import 'package:oauth2/oauth2.dart' as oauth;
33
abstract class OAuthGrant {
44
const OAuthGrant();
55

6-
Future<String> handle(
7-
Uri authorizationEndpoint, String identifier, String secret);
6+
Future<String> handle(Uri authorizationEndpoint, String identifier, String secret);
87
}
98

109
/// Obtains credentials using a [resource owner password grant](https://tools.ietf.org/html/rfc6749#section-1.3.3).
1110
class ResourceOwnerPasswordGrant extends OAuthGrant {
1211
final String username;
1312
final String password;
1413

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

1816
@override
19-
Future<String> handle(
20-
Uri authorizationEndpoint, String identifier, String secret) async {
17+
Future<String> handle(Uri authorizationEndpoint, String identifier, String secret) async {
2118
final client = await oauth.resourceOwnerPasswordGrant(
2219
authorizationEndpoint,
2320
username,
@@ -34,8 +31,7 @@ class ClientCredentialsGrant extends OAuthGrant {
3431
const ClientCredentialsGrant();
3532

3633
@override
37-
Future<String> handle(
38-
Uri authorizationEndpoint, String identifier, String secret) async {
34+
Future<String> handle(Uri authorizationEndpoint, String identifier, String secret) async {
3935
final client = await oauth.clientCredentialsGrant(
4036
authorizationEndpoint,
4137
identifier,
@@ -45,4 +41,34 @@ class ClientCredentialsGrant extends OAuthGrant {
4541
}
4642
}
4743

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

Comments
 (0)