LEFT | RIGHT |
(no file at all) | |
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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 /// </returns> | 97 /// </returns> |
98 public async Task<AuthResult> AuthorizeAsync(string userId, Cancellation
Token taskCancellationToken) | 98 public async Task<AuthResult> AuthorizeAsync(string userId, Cancellation
Token taskCancellationToken) |
99 { | 99 { |
100 // Try to load a token from the data store. | 100 // Try to load a token from the data store. |
101 var token = await Flow.LoadTokenAsync(userId, taskCancellationToken)
.ConfigureAwait(false); | 101 var token = await Flow.LoadTokenAsync(userId, taskCancellationToken)
.ConfigureAwait(false); |
102 | 102 |
103 // If the stored token is null or it doesn't have a refresh token an
d the access token is expired, we need· | 103 // If the stored token is null or it doesn't have a refresh token an
d the access token is expired, we need· |
104 // to retrieve a new access token. | 104 // to retrieve a new access token. |
105 if (token == null || (token.RefreshToken == null && token.IsExpired(
flow.Clock))) | 105 if (token == null || (token.RefreshToken == null && token.IsExpired(
flow.Clock))) |
106 { | 106 { |
107 // Create a authorization code request. | 107 // Create an authorization code request. |
108 AuthorizationCodeRequestUrl codeRequest = Flow.CreateAuthorizati
onCodeRequest(redirectUri); | 108 AuthorizationCodeRequestUrl codeRequest = Flow.CreateAuthorizati
onCodeRequest(redirectUri); |
109 | 109 |
110 // Add a random number to the end of the state so we can indicat
e the original request was made by this | 110 // Add a random number to the end of the state so we can indicat
e the original request was made by this |
111 // call. | 111 // call. |
112 var oauthState = state; | 112 var oauthState = state; |
113 if (Flow.DataStore != null) | 113 if (Flow.DataStore != null) |
114 { | 114 { |
115 var rndString = new string('9', StateRandomLength); | 115 var rndString = new string('9', StateRandomLength); |
116 var random = new Random().Next(int.Parse(rndString)).ToStrin
g("D" + StateRandomLength); | 116 var random = new Random().Next(int.Parse(rndString)).ToStrin
g("D" + StateRandomLength); |
117 oauthState += random; | 117 oauthState += random; |
118 await Flow.DataStore.StoreAsync(StateKey + userId, oauthStat
e).ConfigureAwait(false); | 118 await Flow.DataStore.StoreAsync(StateKey + userId, oauthStat
e).ConfigureAwait(false); |
119 } | 119 } |
120 codeRequest.State = oauthState; | 120 codeRequest.State = oauthState; |
121 | 121 |
122 return new AuthResult { RedirectUri = codeRequest.Build().ToStri
ng() }; | 122 return new AuthResult { RedirectUri = codeRequest.Build().ToStri
ng() }; |
123 } | 123 } |
124 | 124 |
125 return new AuthResult { Credential = new UserCredential(flow, userId
, token) }; | 125 return new AuthResult { Credential = new UserCredential(flow, userId
, token) }; |
126 } | 126 } |
127 } | 127 } |
128 } | 128 } |
LEFT | RIGHT |