OLD | NEW |
(Empty) | |
| 1 /* |
| 2 Copyright 2013 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.Collections.Generic; |
| 18 using System.IO; |
| 19 using System.Threading; |
| 20 using System.Threading.Tasks; |
| 21 |
| 22 using Google.Apis.Util.Store; |
| 23 |
| 24 namespace Google.Apis.Auth.OAuth2 |
| 25 { |
| 26 /// <summary>A helper utility to manage the authorization code flow.</summar
y> |
| 27 public class GoogleWebAuthenticationBroker |
| 28 { |
| 29 /// <summary>The folder which is used by the <seealso cref="Google.Apis.
Util.Store.FileDataStore"/>.</summary> |
| 30 public static string Folder = "Google.Apis.Auth"; |
| 31 |
| 32 /// <summary>Asynchronously authenticates the specified user.</summary> |
| 33 /// <remarks> |
| 34 /// In case no data store is specified, <seealso cref="Google.Apis.Util.
Store.FileDataStore"/> will be used by· |
| 35 /// default. |
| 36 /// </remarks> |
| 37 /// <param name="clientSecrets">The client secrets.</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 authenticate.</param> |
| 42 /// <param name="taskCancellationToken">Cancellation token to cancel an
operation.</param> |
| 43 /// <param name="dataStore">The data store, if not specified a file data
store will be used.</param> |
| 44 /// <returns>User credential.</returns> |
| 45 public static async Task<UserCredential> AuthenticateAsync(ClientSecrets
clientSecrets, |
| 46 IEnumerable<string> scopes, string user, CancellationToken taskCance
llationToken, |
| 47 IDataStore dataStore = null) |
| 48 { |
| 49 var initializer = new GoogleAuthorizationCodeFlow.Initializer |
| 50 { |
| 51 ClientSecrets = clientSecrets, |
| 52 }; |
| 53 return await AuthenticateAsyncCore(initializer, scopes, user, taskCa
ncellationToken, dataStore); |
| 54 } |
| 55 |
| 56 /// <summary>Asynchronously authenticates the specified user.</summary> |
| 57 /// <remarks> |
| 58 /// In case no data store is specified, <seealso cref="Google.Apis.Util.
Store.FileDataStore"/> will be used by· |
| 59 /// default. |
| 60 /// </remarks> |
| 61 /// <param name="clientSecretsStream"> |
| 62 /// The client secrets stream. The authorization code flow constructor i
s responsible for disposing the stream. |
| 63 /// </remarks> |
| 64 /// <param name="scopes"> |
| 65 /// The scopes which indicate the Google API access your application is
requesting. |
| 66 /// </param> |
| 67 /// <param name="user">The user to authenticate.</param> |
| 68 /// <param name="taskCancellationToken">Cancellation token to cancel an
operation.</param> |
| 69 /// <param name="dataStore">The data store, if not specified a file data
store will be used.</param> |
| 70 /// <returns>User credential.</returns> |
| 71 public static async Task<UserCredential> AuthenticateAsync(Stream client
SecretsStream, |
| 72 IEnumerable<string> scopes, string user, CancellationToken taskCance
llationToken, |
| 73 IDataStore dataStore = null) |
| 74 { |
| 75 var initializer = new GoogleAuthorizationCodeFlow.Initializer |
| 76 { |
| 77 ClientSecretsStream = clientSecretsStream, |
| 78 }; |
| 79 return await AuthenticateAsyncCore(initializer, scopes, user, taskCa
ncellationToken, dataStore); |
| 80 } |
| 81 |
| 82 /// <summary>The core logic for asynchronously authenticating the specif
ied user.</summary> |
| 83 /// <param name="initializer">The authorization code initializer.</param
> |
| 84 /// <param name="scopes"> |
| 85 /// The scopes which indicate the Google API access your application is
requesting. |
| 86 /// </param> |
| 87 /// <param name="user">The user to authenticate.</param> |
| 88 /// <param name="taskCancellationToken">Cancellation token to cancel an
operation.</param> |
| 89 /// <param name="dataStore">The data store, if not specified a file data
store will be used.</param> |
| 90 /// <returns>User credential.</returns> |
| 91 private static async Task<UserCredential> AuthenticateAsyncCore(Authoriz
ationCodeFlow.Initializer initializer, |
| 92 IEnumerable<string> scopes, string user, CancellationToken taskCance
llationToken, |
| 93 IDataStore dataStore = null) |
| 94 { |
| 95 initializer.Scopes = scopes; |
| 96 initializer.DataStore = dataStore ?? new FileDataStore(Folder); |
| 97 var flow = new GoogleAuthorizationCodeFlow(initializer); |
| 98 |
| 99 // Create authorization code installed app instance and authorize th
e user. |
| 100 return await new AuthorizationCodeInstalledApp(flow, new LocalServer
CodeReceiver()).Authorize |
| 101 (user, taskCancellationToken); |
| 102 } |
| 103 } |
| 104 } |
OLD | NEW |