OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * OAuth2 class that handles retrieval/storage of an OAuth2 token. | 7 * OAuth2 class that handles retrieval/storage of an OAuth2 token. |
8 * | 8 * |
9 * Uses a content script to trampoline the OAuth redirect page back into the | 9 * Uses a content script to trampoline the OAuth redirect page back into the |
10 * extension context. This works around the lack of native support for | 10 * extension context. This works around the lack of native support for |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 | 199 |
200 /** | 200 /** |
201 * Update state based on token response from the OAuth2 /token endpoint. | 201 * Update state based on token response from the OAuth2 /token endpoint. |
202 * | 202 * |
203 * @private | 203 * @private |
204 * @param {XMLHttpRequest} xhr The XHR object for this request. | 204 * @param {XMLHttpRequest} xhr The XHR object for this request. |
205 * @return {void} Nothing. | 205 * @return {void} Nothing. |
206 */ | 206 */ |
207 remoting.OAuth2.prototype.processTokenResponse_ = function(xhr) { | 207 remoting.OAuth2.prototype.processTokenResponse_ = function(xhr) { |
208 if (xhr.status == 200) { | 208 if (xhr.status == 200) { |
209 var tokens = jsonParseSafe(xhr.responseText); | 209 try { |
210 if (tokens) { | 210 // Don't use jsonParseSafe here unless you move the definition out of |
| 211 // remoting.js, otherwise this won't work from the OAuth trampoline. |
| 212 // TODO(jamiewalch): Fix this once we're no longer using the trampoline. |
| 213 var tokens = JSON.parse(xhr.responseText); |
211 if ('refresh_token' in tokens) { | 214 if ('refresh_token' in tokens) { |
212 this.setRefreshToken(tokens['refresh_token']); | 215 this.setRefreshToken(tokens['refresh_token']); |
213 } | 216 } |
214 | 217 |
215 // Offset by 120 seconds so that we can guarantee that the token | 218 // Offset by 120 seconds so that we can guarantee that the token |
216 // we return will be valid for at least 2 minutes. | 219 // we return will be valid for at least 2 minutes. |
217 // If the access token is to be useful, this object must make some | 220 // If the access token is to be useful, this object must make some |
218 // guarantee as to how long the token will be valid for. | 221 // guarantee as to how long the token will be valid for. |
219 // The choice of 2 minutes is arbitrary, but that length of time | 222 // The choice of 2 minutes is arbitrary, but that length of time |
220 // is part of the contract satisfied by callWithToken(). | 223 // is part of the contract satisfied by callWithToken(). |
221 // Offset by a further 30 seconds to account for RTT issues. | 224 // Offset by a further 30 seconds to account for RTT issues. |
222 this.setAccessToken(tokens['access_token'], | 225 this.setAccessToken(tokens['access_token'], |
223 (tokens['expires_in'] - (120 + 30)) * 1000 + Date.now()); | 226 (tokens['expires_in'] - (120 + 30)) * 1000 + Date.now()); |
224 } else { | 227 } catch (err) { |
225 console.error('Invalid "token" response from server.'); | 228 console.error('Invalid "token" response from server:', |
| 229 /** @type {*} */ err); |
226 } | 230 } |
227 } else { | 231 } else { |
228 console.error('Failed to get tokens. Status: ' + xhr.status + | 232 console.error('Failed to get tokens. Status: ' + xhr.status + |
229 ' response: ' + xhr.responseText); | 233 ' response: ' + xhr.responseText); |
230 } | 234 } |
231 }; | 235 }; |
232 | 236 |
233 /** | 237 /** |
234 * Asynchronously retrieves a new access token from the server. | 238 * Asynchronously retrieves a new access token from the server. |
235 * | 239 * |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 * @return {?string} The email address, if it has been cached by a previous call | 409 * @return {?string} The email address, if it has been cached by a previous call |
406 * to getEmail, otherwise null. | 410 * to getEmail, otherwise null. |
407 */ | 411 */ |
408 remoting.OAuth2.prototype.getCachedEmail = function() { | 412 remoting.OAuth2.prototype.getCachedEmail = function() { |
409 var value = window.localStorage.getItem(this.KEY_EMAIL_); | 413 var value = window.localStorage.getItem(this.KEY_EMAIL_); |
410 if (typeof value == 'string') { | 414 if (typeof value == 'string') { |
411 return value; | 415 return value; |
412 } | 416 } |
413 return null; | 417 return null; |
414 }; | 418 }; |
OLD | NEW |