OLD | NEW |
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 |
(...skipping 14 matching lines...) Expand all Loading... |
25 using Google.Apis.Auth.OAuth2.Flows; | 25 using Google.Apis.Auth.OAuth2.Flows; |
26 using Google.Apis.Util.Store; | 26 using Google.Apis.Util.Store; |
27 | 27 |
28 namespace Google.Apis.Auth.OAuth2 | 28 namespace Google.Apis.Auth.OAuth2 |
29 { | 29 { |
30 /// <summary>A helper utility to manage the authorization code flow.</summar
y> | 30 /// <summary>A helper utility to manage the authorization code flow.</summar
y> |
31 public class GoogleWebAuthorizationBroker | 31 public class GoogleWebAuthorizationBroker |
32 { | 32 { |
33 /// <summary>Asynchronously authorizes the specified user.</summary> | 33 /// <summary>Asynchronously authorizes the specified user.</summary> |
34 /// <remarks> | 34 /// <remarks> |
35 /// It uses <see cref="Google.Apis.Util.Store.StroageDataStore"/> as the
flow's data store by default. | 35 /// It uses <see cref="Google.Apis.Util.Store.StorageDataStore"/> as the
flow's data store by default. |
36 /// </remarks> | 36 /// </remarks> |
37 /// <param name="clientSecretsUri">The client secrets URI.</param> | 37 /// <param name="clientSecretsUri">The client secrets URI.</param> |
38 /// <param name="scopes"> | 38 /// <param name="scopes"> |
39 /// The scopes which indicate the Google API access your application is
requesting. | 39 /// The scopes which indicate the Google API access your application is
requesting. |
40 /// </param> | 40 /// </param> |
41 /// <param name="user">The user to authorize.</param> | 41 /// <param name="user">The user to authorize.</param> |
42 /// <param name="taskCancellationToken">Cancellation token to cancel an
operation.</param> | 42 /// <param name="taskCancellationToken">Cancellation token to cancel an
operation.</param> |
43 /// <returns>User credential.</returns> | 43 /// <returns>User credential.</returns> |
44 public static async Task<UserCredential> AuthorizeAsync(Uri clientSecret
sUri, IEnumerable<string> scopes, | 44 public static async Task<UserCredential> AuthorizeAsync(Uri clientSecret
sUri, IEnumerable<string> scopes, |
45 string user, CancellationToken taskCancellationToken) | 45 string user, CancellationToken taskCancellationToken) |
46 { | 46 { |
47 var clientSecrets = await LoadClientSecrets(clientSecretsUri).Config
ureAwait(false); | 47 var clientSecrets = await LoadClientSecrets(clientSecretsUri).Config
ureAwait(false); |
48 return await AuthorizeAsync(clientSecrets, scopes, user, taskCancell
ationToken).ConfigureAwait(false); | 48 return await AuthorizeAsync(clientSecrets, scopes, user, taskCancell
ationToken).ConfigureAwait(false); |
49 } | 49 } |
50 | 50 |
51 /// <summary>Asynchronously authorizes the specified user.</summary> | 51 /// <summary>Asynchronously authorizes the specified user.</summary> |
52 /// <remarks> | 52 /// <remarks> |
53 /// It uses <see cref="Google.Apis.Util.Store.StroageDataStore"/> as the
flow's data store by default. | 53 /// It uses <see cref="Google.Apis.Util.Store.StorageDataStore"/> as the
flow's data store by default. |
54 /// </remarks> | 54 /// </remarks> |
55 /// <param name="clientSecrets">The client secrets URI.</param> | 55 /// <param name="clientSecrets">The client secrets URI.</param> |
56 /// <param name="scopes"> | 56 /// <param name="scopes"> |
57 /// The scopes which indicate the Google API access your application is
requesting. | 57 /// The scopes which indicate the Google API access your application is
requesting. |
58 /// </param> | 58 /// </param> |
59 /// <param name="user">The user to authorize.</param> | 59 /// <param name="user">The user to authorize.</param> |
60 /// <param name="taskCancellationToken">Cancellation token to cancel an
operation.</param> | 60 /// <param name="taskCancellationToken">Cancellation token to cancel an
operation.</param> |
61 /// <returns>User credential.</returns> | 61 /// <returns>User credential.</returns> |
62 private static async Task<UserCredential> AuthorizeAsync(ClientSecrets c
lientSecrets, | 62 private static async Task<UserCredential> AuthorizeAsync(ClientSecrets c
lientSecrets, |
63 IEnumerable<string> scopes, string user, CancellationToken taskCance
llationToken) | 63 IEnumerable<string> scopes, string user, CancellationToken taskCance
llationToken) |
64 { | 64 { |
65 var initializer = new GoogleAuthorizationCodeFlow.Initializer | 65 var initializer = new GoogleAuthorizationCodeFlow.Initializer |
66 { | 66 { |
67 ClientSecrets = clientSecrets, | 67 ClientSecrets = clientSecrets, |
68 Scopes = scopes, | 68 Scopes = scopes, |
69 DataStore = new StroageDataStore() | 69 DataStore = new StorageDataStore() |
70 }; | 70 }; |
71 | 71 |
72 var installedApp = new AuthorizationCodeWindowsInstalledApp(new Goog
leAuthorizationCodeFlow(initializer)); | 72 var installedApp = new AuthorizationCodeWindowsInstalledApp(new Goog
leAuthorizationCodeFlow(initializer)); |
73 return await installedApp.AuthorizeAsync(user, taskCancellationToken
).ConfigureAwait(false); | 73 return await installedApp.AuthorizeAsync(user, taskCancellationToken
).ConfigureAwait(false); |
74 } | 74 } |
75 | 75 |
76 /// <summary> | 76 /// <summary> |
77 /// Asynchronously reauthorizes the user. This method should be called i
f the users want to authorize after· | 77 /// Asynchronously reauthorizes the user. This method should be called i
f the users want to authorize after· |
78 /// they revoked the token. | 78 /// they revoked the token. |
79 /// </summary> | 79 /// </summary> |
80 /// <param name="userCredential">The current user credential. Its <see c
ref="UserCredential.Token"/> will be | 80 /// <param name="userCredential">The current user credential. Its <see c
ref="UserCredential.Token"/> will be |
81 /// updated. </param> | 81 /// updated. </param> |
82 /// <param name="taskCancellationToken">Cancellation token to cancel an
operation.</param> | 82 /// <param name="taskCancellationToken">Cancellation token to cancel an
operation.</param> |
83 public static async Task ReauthorizeAsync(UserCredential userCredential, | 83 public static async Task ReauthorizeAsync(UserCredential userCredential, |
84 CancellationToken taskCancellationToken) | 84 CancellationToken taskCancellationToken) |
85 { | 85 { |
86 var installedApp = new AuthorizationCodeWindowsInstalledApp(userCred
ential.Flow); | 86 var installedApp = new AuthorizationCodeWindowsInstalledApp(userCred
ential.Flow); |
87 // Create an authorization code installed app instance and authorize
the user. | 87 // Create an authorization code installed app instance and authorize
the user. |
88 UserCredential newUserCredential = await installedApp.AuthorizeAsync
( | 88 UserCredential newUserCredential = await installedApp.AuthorizeAsync
( |
89 userCredential.UderId, taskCancellationToken).ConfigureAwait(fal
se); | 89 userCredential.UserId, taskCancellationToken).ConfigureAwait(fal
se); |
90 userCredential.Token = newUserCredential.Token; | 90 userCredential.Token = newUserCredential.Token; |
91 } | 91 } |
92 | 92 |
93 /// <summary>Loads the client secrets from the given URI.</summary> | 93 /// <summary>Loads the client secrets from the given URI.</summary> |
94 /// <param name="clientSecretsUri">The client secrets URI.</param> | 94 /// <param name="clientSecretsUri">The client secrets URI.</param> |
95 /// <returns>Client secrets.</returns> | 95 /// <returns>Client secrets.</returns> |
96 private static async Task<ClientSecrets> LoadClientSecrets(Uri clientSec
retsUri) | 96 private static async Task<ClientSecrets> LoadClientSecrets(Uri clientSec
retsUri) |
97 { | 97 { |
98 var file = await StorageFile.GetFileFromApplicationUriAsync(clientSe
cretsUri); | 98 var file = await StorageFile.GetFileFromApplicationUriAsync(clientSe
cretsUri); |
99 var content = await FileIO.ReadTextAsync(file); | 99 var content = await FileIO.ReadTextAsync(file); |
100 | 100 |
101 using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(content)
)) | 101 using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(content)
)) |
102 { | 102 { |
103 return GoogleClientSecrets.Load(stream).Secrets; | 103 return GoogleClientSecrets.Load(stream).Secrets; |
104 } | 104 } |
105 } | 105 } |
106 } | 106 } |
107 } | 107 } |
OLD | NEW |