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>Asynchronously authenticates the specified user.</summary> |
| 30 /// <remarks> |
| 31 /// It uses <seealso cref="Google.Apis.Util.Store.StorageDataStore"/> as
the flow's data store by default. |
| 32 /// </remarks> |
| 33 /// <param name="clientSecrets">The client secrets.</param> |
| 34 /// <param name="scopes"> |
| 35 /// The scopes which indicate the Google API access your application is
requesting. |
| 36 /// </param> |
| 37 /// <param name="user">The user to authenticate.</param> |
| 38 /// <param name="taskCancellationToken">Cancellation token to cancel an
operation.</param> |
| 39 /// <returns>User credential.</returns> |
| 40 public static async Task<UserCredential> AuthenticateAsync(ClientSecrets
clientSecrets, |
| 41 IEnumerable<string> scopes, string user, CancellationToken taskCance
llationToken) |
| 42 { |
| 43 var initializer = new GoogleAuthorizationCodeFlow.Initializer |
| 44 { |
| 45 ClientSecrets = clientSecrets, |
| 46 }; |
| 47 |
| 48 return await AuthenticateAsyncCore(initializer, scopes, user, taskCa
ncellationToken); |
| 49 } |
| 50 |
| 51 /// <summary> Asynchronously authenticates the specified user.</summary> |
| 52 /// <remarks> |
| 53 /// It uses <seealso cref="Google.Apis.Util.Store.StorageDataStore"/> as
the flow's data store by default. |
| 54 /// </remarks> |
| 55 /// <param name="clientSecretsStream"> |
| 56 /// The client secrets stream. The AuthorizationCodeFlow constructor is
responsible for disposing the stream. |
| 57 /// Note that it's more secured to use the authenticate method which get
s <seealso cref="ClientSecrets"/>, and |
| 58 /// hard code your secrets in your app rather than storing the secrets i
n a separate unsecured file. |
| 59 /// </remarks> |
| 60 /// <param name="scopes"> |
| 61 /// The scopes which indicate the Google API access your application is
requesting. |
| 62 /// </param> |
| 63 /// <param name="user">The user to authenticate.</param> |
| 64 /// <param name="taskCancellationToken">Cancellation token to cancel an
operation.</param> |
| 65 /// <returns>User credential</returns> |
| 66 public static async Task<UserCredential> AuthenticateAsync(Stream client
SecretsStream, |
| 67 IEnumerable<string> scopes, string user, CancellationToken taskCance
llationToken) |
| 68 { |
| 69 var initializer = new GoogleAuthorizationCodeFlow.Initializer |
| 70 { |
| 71 ClientSecretsStream = clientSecretsStream, |
| 72 }; |
| 73 |
| 74 return await AuthenticateAsyncCore(initializer, scopes, user, taskCa
ncellationToken); |
| 75 } |
| 76 |
| 77 /// <summary>The core logic for asynchronously authenticating the specif
ied user.</summary> |
| 78 /// <param name="initializer">The authorization code initializer.</param
> |
| 79 /// <param name="scopes"> |
| 80 /// The scopes which indicate the Google API access your application is
requesting. |
| 81 /// </param> |
| 82 /// <param name="user">The user to authenticate.</param> |
| 83 /// <param name="taskCancellationToken">Cancellation token to cancel an
operation.</param> |
| 84 /// <returns>User credential.</returns> |
| 85 private static async Task<UserCredential> AuthenticateAsyncCore(Authoriz
ationCodeFlow.Initializer initializer, |
| 86 IEnumerable<string> scopes, string user, CancellationToken taskCance
llationToken) |
| 87 { |
| 88 initializer.Scopes = scopes; |
| 89 initializer.DataStore = new StorageDataStore(); |
| 90 |
| 91 var installedApp = new AuthorizationCodeWPInstalledApp(initializer); |
| 92 return await installedApp.AuthorizeAsync(user, taskCancellationToken
); |
| 93 } |
| 94 } |
| 95 } |
OLD | NEW |