Index: Src/GoogleApis.Auth.WP81/OAuth2/GoogleWebAuthorizationBroker.cs =================================================================== new file mode 100644 --- /dev/null +++ b/Src/GoogleApis.Auth.WP81/OAuth2/GoogleWebAuthorizationBroker.cs @@ -0,0 +1,107 @@ +/* +Copyright 2014 Google Inc + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Windows.Storage; + +using Google.Apis.Auth.OAuth2.Flows; +using Google.Apis.Util.Store; + +namespace Google.Apis.Auth.OAuth2 +{ + /// A helper utility to manage the authorization code flow. + public class GoogleWebAuthorizationBroker + { + /// Asynchronously authorizes the specified user. + /// + /// It uses as the flow's data store by default. + /// + /// The client secrets URI. + /// + /// The scopes which indicate the Google API access your application is requesting. + /// + /// The user to authorize. + /// Cancellation token to cancel an operation. + /// User credential. + public static async Task AuthorizeAsync(Uri clientSecretsUri, IEnumerable scopes, + string user, CancellationToken taskCancellationToken) + { + var clientSecrets = await LoadClientSecrets(clientSecretsUri).ConfigureAwait(false); + return await AuthorizeAsync(clientSecrets, scopes, user, taskCancellationToken).ConfigureAwait(false); + } + + /// + /// Asynchronously reauthorizes the user. This method should be called if the users want to authorize after + /// they revoked the token. + /// + /// The current user credential. Its will be + /// updated. + /// Cancellation token to cancel an operation. + public static async Task ReauthorizeAsync(UserCredential userCredential, + CancellationToken taskCancellationToken) + { + var installedApp = new AuthorizationCodeWPInstalledApp(userCredential.Flow); + // Create an authorization code installed app instance and authorize the user. + UserCredential newUserCredential = await installedApp.AuthorizeAsync( + userCredential.UserId, taskCancellationToken).ConfigureAwait(false); + userCredential.Token = newUserCredential.Token; + } + + /// Asynchronously authorizes the specified user. + /// + /// It uses as the flow's data store by default. + /// + /// The client secrets URI. + /// + /// The scopes which indicate the Google API access your application is requesting. + /// + /// The user to authorize. + /// Cancellation token to cancel an operation. + /// User credential. + private static async Task AuthorizeAsync(ClientSecrets clientSecrets, + IEnumerable scopes, string user, CancellationToken taskCancellationToken) + { + var initializer = new GoogleAuthorizationCodeFlow.Initializer + { + ClientSecrets = clientSecrets, + Scopes = scopes, + DataStore = new PasswordVaultDataStore() + }; + + var installedApp = new AuthorizationCodeWPInstalledApp(new GoogleAuthorizationCodeFlow(initializer)); + return await installedApp.AuthorizeAsync(user, taskCancellationToken).ConfigureAwait(false); + } + + /// Loads the client secrets from the given URI. + /// The client secrets URI. + /// Client secrets. + private static async Task LoadClientSecrets(Uri clientSecretsUri) + { + var file = await StorageFile.GetFileFromApplicationUriAsync(clientSecretsUri); + var content = await FileIO.ReadTextAsync(file); + + using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(content))) + { + return GoogleClientSecrets.Load(stream).Secrets; + } + } + } +}