Skip to content

Commit 6649c87

Browse files
authored
Add imaginary function examples and OAuth documentation links to README (flutter#72)
1 parent 5e56c31 commit 6649c87

File tree

1 file changed

+71
-15
lines changed

1 file changed

+71
-15
lines changed

README.md

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,10 @@ client has permission to access resources on behalf of the resource owner.
1212

1313
OAuth2 provides several different methods for the client to obtain
1414
authorization. At the time of writing, this library only supports the
15-
[AuthorizationCodeGrant][], [clientCredentialsGrant][] and [resourceOwnerPasswordGrant][] methods, but
16-
further methods may be added in the future. The following example uses this
17-
method to authenticate, and assumes that the library is being used by a
18-
server-side application.
19-
20-
[AuthorizationCodeGrant]: http://www.dartdocs.org/documentation/oauth2/latest/index.html#oauth2/oauth2.AuthorizationCodeGrant
21-
[clientCredentialsGrant]: http://www.dartdocs.org/documentation/oauth2/latest/index.html#oauth2/oauth2.clientCredentialsGrant
22-
[resourceOwnerPasswordGrant]: http://www.dartdocs.org/documentation/oauth2/latest/index.html#oauth2/oauth2.resourceOwnerPasswordGrant
15+
[Authorization Code Grant][authorizationCodeGrantSection], [Client Credentials Grant][clientCredentialsGrantSection] and [Resource Owner Password Grant][resourceOwnerPasswordGrantSection] flows, but more may be added in the future.
2316

2417
## Authorization Code Grant
18+
**Resources:** [Class summary][authorizationCodeGrantMethod], [OAuth documentation][authorizationCodeGrantDocs]
2519

2620
```dart
2721
import 'dart:io';
@@ -79,22 +73,25 @@ Future<oauth2.Client> getClient() async {
7973
identifier, authorizationEndpoint, tokenEndpoint,
8074
secret: secret);
8175
82-
// Redirect the resource owner to the authorization URL. This will be a URL on
83-
// the authorization server (authorizationEndpoint with some additional query
84-
// parameters). Once the resource owner has authorized, they'll be redirected
85-
// to `redirectUrl` with an authorization code.
76+
// A URL on the authorization server (authorizationEndpoint with some additional
77+
// query parameters). Scopes and state can optionally be passed into this method.
78+
var authorizationUrl = grant.getAuthorizationUrl(redirectUrl);
79+
80+
// Redirect the resource owner to the authorization URL. Once the resource
81+
// owner has authorized, they'll be redirected to `redirectUrl` with an
82+
// authorization code.
8683
//
8784
// `redirect` is an imaginary function that redirects the resource
8885
// owner's browser.
89-
await redirect(grant.getAuthorizationUrl(redirectUrl));
86+
await redirect(authorizationUrl);
9087
9188
// Another imaginary function that listens for a request to `redirectUrl`.
92-
var request = await listen(redirectUrl);
89+
var responseUrl = await listen(redirectUrl);
9390
9491
// Once the user is redirected to `redirectUrl`, pass the query parameters to
9592
// the AuthorizationCodeGrant. It will validate them and extract the
9693
// authorization code to create a new Client.
97-
return await grant.handleAuthorizationResponse(request.uri.queryParameters);
94+
return await grant.handleAuthorizationResponse(responseUrl.queryParameters);
9895
}
9996
10097
main() async {
@@ -113,7 +110,52 @@ main() async {
113110
}
114111
```
115112

113+
<details>
114+
<summary>Click here to learn how to implement the imaginary functions mentioned above.</summary>
115+
116+
-----
117+
118+
Unfortunately, there's not a universal example for implementing the imaginary functions, `redirect` and `listen`, because different options exist for each platform.
119+
120+
For Flutter apps, there's two popular approaches:
121+
1. Launch a browser using [url_launcher][] and listen for a redirect using [uni_links][].
122+
```dart
123+
if (await canLaunch(authorizationUrl.toString())) {
124+
await launch(authorizationUrl.toString());
125+
}
126+
127+
...
128+
129+
final linksStream = getLinksStream().listen((Uri uri) async {
130+
if (uri.toString().startsWith(redirectUrl)) {
131+
responseUrl = uri;
132+
}
133+
});
134+
```
135+
136+
2. Launch a WebView inside the app and listen for a redirect using [webview_flutter][].
137+
```dart
138+
WebView(
139+
javascriptMode: JavascriptMode.unrestricted,
140+
initialUrl: authorizationUrl.toString(),
141+
navigationDelegate: (navReq) {
142+
if (navReq.url.startsWith(redirectUrl)) {
143+
responseUrl = Uri.parse(navReq.url);
144+
return NavigationDecision.prevent;
145+
}
146+
147+
return NavigationDecision.navigate;
148+
},
149+
...
150+
);
151+
```
152+
153+
For Dart apps, the best approach depends on the available options for accessing a browser. In general, you'll need to launch the authorization URL through the client's browser and listen for the redirect URL.
154+
</details>
155+
116156
## Client Credentials Grant
157+
**Resources:** [Method summary][clientCredentialsGrantMethod], [OAuth documentation][clientCredentialsGrantDocs]
158+
117159
```dart
118160
// This URL is an endpoint that's provided by the authorization server. It's
119161
// usually included in the server's documentation of its OAuth2 API.
@@ -149,6 +191,7 @@ await credentialsFile.writeAsString(client.credentials.toJson());
149191
```
150192

151193
## Resource Owner Password Grant
194+
**Resources:** [Method summary][resourceOwnerPasswordGrantMethod], [OAuth documentation][resourceOwnerPasswordGrantDocs]
152195

153196
```dart
154197
// This URL is an endpoint that's provided by the authorization server. It's
@@ -185,3 +228,16 @@ var result = await client.read("http://example.com/protected-resources.txt");
185228
new File("~/.myapp/credentials.json")
186229
.writeAsString(client.credentials.toJson());
187230
```
231+
232+
[authorizationCodeGrantDocs]: https://oauth.net/2/grant-types/authorization-code/
233+
[authorizationCodeGrantMethod]: https://pub.dev/documentation/oauth2/latest/oauth2/AuthorizationCodeGrant-class.html
234+
[authorizationCodeGrantSection]: #authorization-code-grant
235+
[clientCredentialsGrantDocs]: https://oauth.net/2/grant-types/client-credentials/
236+
[clientCredentialsGrantMethod]: https://pub.dev/documentation/oauth2/latest/oauth2/clientCredentialsGrant.html
237+
[clientCredentialsGrantSection]: #client-credentials-grant
238+
[resourceOwnerPasswordGrantDocs]: https://oauth.net/2/grant-types/password/
239+
[resourceOwnerPasswordGrantMethod]: https://pub.dev/documentation/oauth2/latest/oauth2/resourceOwnerPasswordGrant.html
240+
[resourceOwnerPasswordGrantSection]: #resource-owner-password-grant
241+
[uni_links]: https://pub.dev/packages/uni_links
242+
[url_launcher]: https://pub.dev/packages/url_launcher
243+
[webview_flutter]: https://pub.dev/packages/webview_flutter

0 commit comments

Comments
 (0)