OLD | NEW |
(Empty) | |
| 1 /* |
| 2 Copyright 2014 Google Inc |
| 3 |
| 4 Licensed under the Apache License, Version 2.0 (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 |
| 7 |
| 8 http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
| 10 Unless required by applicable law or agreed to in writing, software |
| 11 distributed under the License is distributed on an "AS IS" BASIS, |
| 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 See the License for the specific language governing permissions and |
| 14 limitations under the License. |
| 15 */ |
| 16 |
| 17 using System; |
| 18 using System.Collections.Generic; |
| 19 using System.IO; |
| 20 using System.Text; |
| 21 using System.Threading; |
| 22 using System.Threading.Tasks; |
| 23 using Windows.Storage; |
| 24 |
| 25 using Google.Apis.Auth.OAuth2.Flows; |
| 26 using Google.Apis.Util.Store; |
| 27 |
| 28 namespace Google.Apis.Auth.OAuth2 |
| 29 { |
| 30 /// <summary>A helper utility to manage the authorization code flow.</summar
y> |
| 31 public class GoogleWebAuthorizationBroker |
| 32 { |
| 33 /// <summary>Asynchronously authorizes the specified user.</summary> |
| 34 /// <remarks> |
| 35 /// It uses <see cref="Google.Apis.Util.Store.PasswordVaultDataStore"/>
as the flow's data store by default. |
| 36 /// </remarks> |
| 37 /// <param name="clientSecretsUri">The client secrets URI.</param> |
| 38 /// <param name="scopes"> |
| 39 /// The scopes which indicate the Google API access your application is
requesting. |
| 40 /// </param> |
| 41 /// <param name="user">The user to authorize.</param> |
| 42 /// <param name="taskCancellationToken">Cancellation token to cancel an
operation.</param> |
| 43 /// <returns>User credential.</returns> |
| 44 public static async Task<UserCredential> AuthorizeAsync(Uri clientSecret
sUri, IEnumerable<string> scopes, |
| 45 string user, CancellationToken taskCancellationToken) |
| 46 { |
| 47 var clientSecrets = await LoadClientSecrets(clientSecretsUri).Config
ureAwait(false); |
| 48 return await AuthorizeAsync(clientSecrets, scopes, user, taskCancell
ationToken).ConfigureAwait(false); |
| 49 } |
| 50 |
| 51 /// <summary> |
| 52 /// Asynchronously reauthorizes the user. This method should be called i
f the users want to authorize after· |
| 53 /// they revoked the token. |
| 54 /// </summary> |
| 55 /// <param name="userCredential">The current user credential. Its <see c
ref="UserCredential.Token"/> will be |
| 56 /// updated. </param> |
| 57 /// <param name="taskCancellationToken">Cancellation token to cancel an
operation.</param> |
| 58 public static async Task ReauthorizeAsync(UserCredential userCredential, |
| 59 CancellationToken taskCancellationToken) |
| 60 { |
| 61 var installedApp = new AuthorizationCodeWPInstalledApp(userCredentia
l.Flow); |
| 62 // Create an authorization code installed app instance and authorize
the user. |
| 63 UserCredential newUserCredential = await installedApp.AuthorizeAsync
( |
| 64 userCredential.UserId, taskCancellationToken).ConfigureAwait(fal
se); |
| 65 userCredential.Token = newUserCredential.Token; |
| 66 } |
| 67 |
| 68 /// <summary>Asynchronously authorizes the specified user.</summary> |
| 69 /// <remarks> |
| 70 /// It uses <see cref="Google.Apis.Util.Store.PasswordVaultDataStore"/>
as the flow's data store by default. |
| 71 /// </remarks> |
| 72 /// <param name="clientSecrets">The client secrets URI.</param> |
| 73 /// <param name="scopes"> |
| 74 /// The scopes which indicate the Google API access your application is
requesting. |
| 75 /// </param> |
| 76 /// <param name="user">The user to authorize.</param> |
| 77 /// <param name="taskCancellationToken">Cancellation token to cancel an
operation.</param> |
| 78 /// <returns>User credential.</returns> |
| 79 private static async Task<UserCredential> AuthorizeAsync(ClientSecrets c
lientSecrets, |
| 80 IEnumerable<string> scopes, string user, CancellationToken taskCance
llationToken) |
| 81 { |
| 82 var initializer = new GoogleAuthorizationCodeFlow.Initializer |
| 83 { |
| 84 ClientSecrets = clientSecrets, |
| 85 Scopes = scopes, |
| 86 DataStore = new PasswordVaultDataStore() |
| 87 }; |
| 88 |
| 89 var installedApp = new AuthorizationCodeWPInstalledApp(new GoogleAut
horizationCodeFlow(initializer)); |
| 90 return await installedApp.AuthorizeAsync(user, taskCancellationToken
).ConfigureAwait(false); |
| 91 } |
| 92 |
| 93 /// <summary>Loads the client secrets from the given URI.</summary> |
| 94 /// <param name="clientSecretsUri">The client secrets URI.</param> |
| 95 /// <returns>Client secrets.</returns> |
| 96 private static async Task<ClientSecrets> LoadClientSecrets(Uri clientSec
retsUri) |
| 97 { |
| 98 var file = await StorageFile.GetFileFromApplicationUriAsync(clientSe
cretsUri); |
| 99 var content = await FileIO.ReadTextAsync(file); |
| 100 |
| 101 using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(content)
)) |
| 102 { |
| 103 return GoogleClientSecrets.Load(stream).Secrets; |
| 104 } |
| 105 } |
| 106 } |
| 107 } |
OLD | NEW |