LEFT | RIGHT |
1 /* | 1 /* |
2 Copyright 2013 Google Inc | 2 Copyright 2013 Google Inc |
3 | 3 |
4 Licensed under the Apache License, Version 2.0 (the "License"); | 4 Licensed under the Apache License, Version 2.0 (the "License"); |
5 you may not use this file except in compliance with the License. | 5 you may not use this file except in compliance with the License. |
6 You may obtain a copy of the License at | 6 You may obtain a copy of the License at |
7 | 7 |
8 http://www.apache.org/licenses/LICENSE-2.0 | 8 http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 |
10 Unless required by applicable law or agreed to in writing, software | 10 Unless required by applicable law or agreed to in writing, software |
11 distributed under the License is distributed on an "AS IS" BASIS, | 11 distributed under the License is distributed on an "AS IS" BASIS, |
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 See the License for the specific language governing permissions and | 13 See the License for the specific language governing permissions and |
14 limitations under the License. | 14 limitations under the License. |
15 */ | 15 */ |
16 | 16 |
17 using System.Collections.Generic; | 17 using System.Collections.Generic; |
18 using System.IO; | 18 using System.IO; |
19 using System.Threading; | 19 using System.Threading; |
20 using System.Threading.Tasks; | 20 using System.Threading.Tasks; |
21 | 21 |
22 using Google.Apis.Auth.OAuth2.Flows; | 22 using Google.Apis.Auth.OAuth2.Flows; |
23 using Google.Apis.Util.Store; | 23 using Google.Apis.Util.Store; |
24 | 24 |
25 namespace Google.Apis.Auth.OAuth2 | 25 namespace Google.Apis.Auth.OAuth2 |
26 { | 26 { |
27 /// <summary>A helper utility to manage the authorization code flow.</summar
y> | 27 /// <summary>A helper utility to manage the authorization code flow.</summar
y> |
28 public class GoogleWebAuthorizationBroker | 28 public class GoogleWebAuthorizationBroker |
29 { | 29 { |
30 /// <summary>The folder which is used by the <see cref="Google.Apis.Util
.Store.FileDataStore"/>.</summary> | 30 /// <summary>The folder which is used by the <seealso cref="Google.Apis.
Util.Store.FileDataStore"/>.</summary> |
31 public static string Folder = "Google.Apis.Auth"; | 31 public static string Folder = "Google.Apis.Auth"; |
32 | 32 |
33 /// <summary>Asynchronously authorizes the specified user.</summary> | 33 /// <summary>Asynchronously authorizes the specified user.</summary> |
34 /// <remarks> | 34 /// <remarks> |
35 /// In case no data store is specified, <see cref="Google.Apis.Util.Stor
e.FileDataStore"/> will be used by· | 35 /// In case no data store is specified, <seealso cref="Google.Apis.Util.
Store.FileDataStore"/> will be used by· |
36 /// default. | 36 /// default. |
37 /// </remarks> | 37 /// </remarks> |
38 /// <param name="clientSecrets">The client secrets.</param> | 38 /// <param name="clientSecrets">The client secrets.</param> |
39 /// <param name="scopes"> | 39 /// <param name="scopes"> |
40 /// The scopes which indicate the Google API access your application is
requesting. | 40 /// The scopes which indicate the Google API access your application is
requesting. |
41 /// </param> | 41 /// </param> |
42 /// <param name="user">The user to authorize.</param> | 42 /// <param name="user">The user to authorize.</param> |
43 /// <param name="taskCancellationToken">Cancellation token to cancel an
operation.</param> | 43 /// <param name="taskCancellationToken">Cancellation token to cancel an
operation.</param> |
44 /// <param name="dataStore">The data store, if not specified a file data
store will be used.</param> | 44 /// <param name="dataStore">The data store, if not specified a file data
store will be used.</param> |
45 /// <returns>User credential.</returns> | 45 /// <returns>User credential.</returns> |
46 public static async Task<UserCredential> AuthorizeAsync(ClientSecrets cl
ientSecrets, | 46 public static async Task<UserCredential> AuthorizeAsync(ClientSecrets cl
ientSecrets, |
47 IEnumerable<string> scopes, string user, CancellationToken taskCance
llationToken, | 47 IEnumerable<string> scopes, string user, CancellationToken taskCance
llationToken, |
48 IDataStore dataStore = null) | 48 IDataStore dataStore = null) |
49 { | 49 { |
50 var initializer = new GoogleAuthorizationCodeFlow.Initializer | 50 var initializer = new GoogleAuthorizationCodeFlow.Initializer |
51 { | 51 { |
52 ClientSecrets = clientSecrets, | 52 ClientSecrets = clientSecrets, |
53 }; | 53 }; |
54 return await AuthorizeAsyncCore(initializer, scopes, user, taskCance
llationToken, dataStore) | 54 return await AuthorizeAsyncCore(initializer, scopes, user, taskCance
llationToken, dataStore) |
55 .ConfigureAwait(false); | 55 .ConfigureAwait(false); |
56 } | 56 } |
57 | 57 |
58 /// <summary>Asynchronously authorizes the specified user.</summary> | 58 /// <summary>Asynchronously authorizes the specified user.</summary> |
59 /// <remarks> | 59 /// <remarks> |
60 /// In case no data store is specified, <see cref="Google.Apis.Util.Stor
e.FileDataStore"/> will be used by· | 60 /// In case no data store is specified, <seealso cref="Google.Apis.Util.
Store.FileDataStore"/> will be used by· |
61 /// default. | 61 /// default. |
62 /// </remarks> | 62 /// </remarks> |
63 /// <param name="clientSecretsStream"> | 63 /// <param name="clientSecretsStream"> |
64 /// The client secrets stream. The authorization code flow constructor i
s responsible for disposing the stream. | 64 /// The client secrets stream. The authorization code flow constructor i
s responsible for disposing the stream. |
65 /// </param> | 65 /// </param> |
66 /// <param name="scopes"> | 66 /// <param name="scopes"> |
67 /// The scopes which indicate the Google API access your application is
requesting. | 67 /// The scopes which indicate the Google API access your application is
requesting. |
68 /// </param> | 68 /// </param> |
69 /// <param name="user">The user to authorize.</param> | 69 /// <param name="user">The user to authorize.</param> |
70 /// <param name="taskCancellationToken">Cancellation token to cancel an
operation.</param> | 70 /// <param name="taskCancellationToken">Cancellation token to cancel an
operation.</param> |
71 /// <param name="dataStore">The data store, if not specified a file data
store will be used.</param> | 71 /// <param name="dataStore">The data store, if not specified a file data
store will be used.</param> |
72 /// <returns>User credential.</returns> | 72 /// <returns>User credential.</returns> |
73 public static async Task<UserCredential> AuthorizeAsync(Stream clientSec
retsStream, | 73 public static async Task<UserCredential> AuthorizeAsync(Stream clientSec
retsStream, |
74 IEnumerable<string> scopes, string user, CancellationToken taskCance
llationToken, | 74 IEnumerable<string> scopes, string user, CancellationToken taskCance
llationToken, |
75 IDataStore dataStore = null) | 75 IDataStore dataStore = null) |
76 { | 76 { |
77 var initializer = new GoogleAuthorizationCodeFlow.Initializer | 77 var initializer = new GoogleAuthorizationCodeFlow.Initializer |
78 { | 78 { |
79 ClientSecretsStream = clientSecretsStream, | 79 ClientSecretsStream = clientSecretsStream, |
80 }; | 80 }; |
81 return await AuthorizeAsyncCore(initializer, scopes, user, taskCance
llationToken, dataStore) | 81 return await AuthorizeAsyncCore(initializer, scopes, user, taskCance
llationToken, dataStore) |
82 .ConfigureAwait(false); | 82 .ConfigureAwait(false); |
83 } | 83 } |
84 | 84 |
| 85 /// <summary> |
| 86 /// Asynchronously reauthorizes the user. This method should be called i
f the users want to authorize after· |
| 87 /// they revoked the token. |
| 88 /// </summary> |
| 89 /// <param name="userCredential">The current user credential. Its <see c
ref="UserCredential.Token"/> will be |
| 90 /// updated. </param> |
| 91 /// <param name="taskCancellationToken">Cancellation token to cancel an
operation.</param> |
| 92 public static async Task ReauthorizeAsync(UserCredential userCredential, |
| 93 CancellationToken taskCancellationToken) |
| 94 { |
| 95 // Create an authorization code installed app instance and authorize
the user. |
| 96 UserCredential newUserCredential = await new AuthorizationCodeInstal
ledApp(userCredential.Flow, |
| 97 new LocalServerCodeReceiver()).AuthorizeAsync |
| 98 (userCredential.UderId, taskCancellationToken).ConfigureAwait(fa
lse); |
| 99 userCredential.Token = newUserCredential.Token; |
| 100 } |
| 101 |
85 /// <summary>The core logic for asynchronously authorizing the specified
user.</summary> | 102 /// <summary>The core logic for asynchronously authorizing the specified
user.</summary> |
86 /// <param name="initializer">The authorization code initializer.</param
> | 103 /// <param name="initializer">The authorization code initializer.</param
> |
87 /// <param name="scopes"> | 104 /// <param name="scopes"> |
88 /// The scopes which indicate the Google API access your application is
requesting. | 105 /// The scopes which indicate the Google API access your application is
requesting. |
89 /// </param> | 106 /// </param> |
90 /// <param name="user">The user to authorize.</param> | 107 /// <param name="user">The user to authorize.</param> |
91 /// <param name="taskCancellationToken">Cancellation token to cancel an
operation.</param> | 108 /// <param name="taskCancellationToken">Cancellation token to cancel an
operation.</param> |
92 /// <param name="dataStore">The data store, if not specified a file data
store will be used.</param> | 109 /// <param name="dataStore">The data store, if not specified a file data
store will be used.</param> |
93 /// <returns>User credential.</returns> | 110 /// <returns>User credential.</returns> |
94 private static async Task<UserCredential> AuthorizeAsyncCore( | 111 private static async Task<UserCredential> AuthorizeAsyncCore( |
95 GoogleAuthorizationCodeFlow.Initializer initializer, | 112 GoogleAuthorizationCodeFlow.Initializer initializer, IEnumerable<str
ing> scopes, string user, |
96 IEnumerable<string> scopes, | 113 CancellationToken taskCancellationToken, IDataStore dataStore = null
) |
97 string user, | |
98 CancellationToken taskCancellationToken, | |
99 IDataStore dataStore = null) | |
100 { | 114 { |
101 initializer.Scopes = scopes; | 115 initializer.Scopes = scopes; |
102 initializer.DataStore = dataStore ?? new FileDataStore(Folder); | 116 initializer.DataStore = dataStore ?? new FileDataStore(Folder); |
103 var flow = new GoogleAuthorizationCodeFlow(initializer); | 117 var flow = new GoogleAuthorizationCodeFlow(initializer); |
104 | 118 |
105 // Create authorization code installed app instance and authorize th
e user. | 119 // Create an authorization code installed app instance and authorize
the user. |
106 return await new AuthorizationCodeInstalledApp(flow, new LocalServer
CodeReceiver()).AuthorizeAsync | 120 return await new AuthorizationCodeInstalledApp(flow, new LocalServer
CodeReceiver()).AuthorizeAsync |
107 (user, taskCancellationToken).ConfigureAwait(false); | 121 (user, taskCancellationToken).ConfigureAwait(false); |
108 } | 122 } |
109 } | 123 } |
110 } | 124 } |
LEFT | RIGHT |