OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// A client library for authenticating with a remote service via OAuth2 on | 5 /// A client library for authenticating with a remote service via OAuth2 on |
6 /// behalf of a user, and making authorized HTTP requests with the user's OAuth2 | 6 /// behalf of a user, and making authorized HTTP requests with the user's OAuth2 |
7 /// credentials. | 7 /// credentials. |
8 /// | 8 /// |
9 /// OAuth2 allows a client (the program using this library) to access and | 9 /// OAuth2 allows a client (the program using this library) to access and |
10 /// manipulate a resource that's owned by a resource owner (the end user) and | 10 /// manipulate a resource that's owned by a resource owner (the end user) and |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 /// final secret = "my client secret"; | 45 /// final secret = "my client secret"; |
46 /// | 46 /// |
47 /// // This is a URL on your application's server. The authorization server | 47 /// // This is a URL on your application's server. The authorization server |
48 /// // will redirect the resource owner here once they've authorized the | 48 /// // will redirect the resource owner here once they've authorized the |
49 /// // client. The redirection will include the authorization code in the | 49 /// // client. The redirection will include the authorization code in the |
50 /// // query parameters. | 50 /// // query parameters. |
51 /// final redirectUrl = new Uri.fromString( | 51 /// final redirectUrl = new Uri.fromString( |
52 /// "http://my-site.com/oauth2-redirect"); | 52 /// "http://my-site.com/oauth2-redirect"); |
53 /// | 53 /// |
54 /// var credentialsFile = new File("~/.myapp/credentials.json"); | 54 /// var credentialsFile = new File("~/.myapp/credentials.json"); |
55 /// return credentialsFile.exists().chain((exists) { | 55 /// return credentialsFile.exists().then((exists) { |
56 /// // If the OAuth2 credentials have already been saved from a previous | 56 /// // If the OAuth2 credentials have already been saved from a previous |
57 /// // run, we just want to reload them. | 57 /// // run, we just want to reload them. |
58 /// if (exists) { | 58 /// if (exists) { |
59 /// return credentialsFile.readAsText().transform((json) { | 59 /// return credentialsFile.readAsText().then((json) { |
60 /// var credentials = new oauth2.Credentials.fromJson(json); | 60 /// var credentials = new oauth2.Credentials.fromJson(json); |
61 /// return new oauth2.Client(identifier, secret, credentials); | 61 /// return new oauth2.Client(identifier, secret, credentials); |
62 /// }); | 62 /// }); |
63 /// } | 63 /// } |
64 /// | 64 /// |
65 /// // If we don't have OAuth2 credentials yet, we need to get the | 65 /// // If we don't have OAuth2 credentials yet, we need to get the |
66 /// // resource owner to authorize us. We're assuming here that we're a | 66 /// // resource owner to authorize us. We're assuming here that we're a |
67 /// // command-line application. | 67 /// // command-line application. |
68 /// var grant = new oauth2.AuthorizationCodeGrant( | 68 /// var grant = new oauth2.AuthorizationCodeGrant( |
69 /// identifier, secret, authorizationEndpoint, tokenEndpoint); | 69 /// identifier, secret, authorizationEndpoint, tokenEndpoint); |
70 /// | 70 /// |
71 /// // Redirect the resource owner to the authorization URL. This will be | 71 /// // Redirect the resource owner to the authorization URL. This will be |
72 /// // a URL on the authorization server (authorizationEndpoint with some | 72 /// // a URL on the authorization server (authorizationEndpoint with some |
73 /// // additional query parameters). Once the resource owner has | 73 /// // additional query parameters). Once the resource owner has |
74 /// // authorized, they'll be redirected to `redirectUrl` with an | 74 /// // authorized, they'll be redirected to `redirectUrl` with an |
75 /// // authorization code. | 75 /// // authorization code. |
76 /// // | 76 /// // |
77 /// // `redirect` is an imaginary function that redirects the resource | 77 /// // `redirect` is an imaginary function that redirects the resource |
78 /// // owner's browser. | 78 /// // owner's browser. |
79 /// return redirect(grant.getAuthorizationUrl(redirectUrl)).chain((_) { | 79 /// return redirect(grant.getAuthorizationUrl(redirectUrl)).then((_) { |
80 /// // Another imaginary function that listens for a request to | 80 /// // Another imaginary function that listens for a request to |
81 /// // `redirectUrl`. | 81 /// // `redirectUrl`. |
82 /// return listen(redirectUrl); | 82 /// return listen(redirectUrl); |
83 /// }).transform((request) { | 83 /// }).then((request) { |
84 /// // Once the user is redirected to `redirectUrl`, pass the query | 84 /// // Once the user is redirected to `redirectUrl`, pass the query |
85 /// // parameters to the AuthorizationCodeGrant. It will validate them | 85 /// // parameters to the AuthorizationCodeGrant. It will validate them |
86 /// // and extract the authorization code to create a new Client. | 86 /// // and extract the authorization code to create a new Client. |
87 /// return grant.handleAuthorizationResponse(request.queryParameters); | 87 /// return grant.handleAuthorizationResponse(request.queryParameters); |
88 /// }) | 88 /// }) |
89 /// }).chain((client) { | 89 /// }).then((client) { |
90 /// // Once you have a Client, you can use it just like any other HTTP | 90 /// // Once you have a Client, you can use it just like any other HTTP |
91 /// // client. | 91 /// // client. |
92 /// return client.read("http://example.com/protected-resources.txt") | 92 /// return client.read("http://example.com/protected-resources.txt") |
93 /// .transform((result) { | 93 /// .then((result) { |
94 /// // Once we're done with the client, save the credentials file. This | 94 /// // Once we're done with the client, save the credentials file. This |
95 /// // ensures that if the credentials were automatically refreshed | 95 /// // ensures that if the credentials were automatically refreshed |
96 /// // while using the client, the new credentials are available for the | 96 /// // while using the client, the new credentials are available for the |
97 /// // next run of the program. | 97 /// // next run of the program. |
98 /// return credentialsFile.open(FileMode.WRITE).chain((file) { | 98 /// return credentialsFile.open(FileMode.WRITE).then((file) { |
99 /// return file.writeString(client.credentials.toJson()); | 99 /// return file.writeString(client.credentials.toJson()); |
100 /// }).chain((file) => file.close()).transform((_) => result); | 100 /// }).then((file) => file.close()).then((_) => result); |
101 /// }); | 101 /// }); |
102 /// }).then(print); | 102 /// }).then(print); |
103 library oauth2; | 103 library oauth2; |
104 | 104 |
105 export 'src/authorization_code_grant.dart'; | 105 export 'src/authorization_code_grant.dart'; |
106 export 'src/client.dart'; | 106 export 'src/client.dart'; |
107 export 'src/credentials.dart'; | 107 export 'src/credentials.dart'; |
108 export 'src/authorization_exception.dart'; | 108 export 'src/authorization_exception.dart'; |
109 export 'src/expiration_exception.dart'; | 109 export 'src/expiration_exception.dart'; |
OLD | NEW |