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 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 chrome.extension.onMessage.removeListener(oauth2MessageListener); | 285 chrome.extension.onMessage.removeListener(oauth2MessageListener); |
286 sendResponse(null); | 286 sendResponse(null); |
287 } | 287 } |
288 chrome.extension.onMessage.addListener(oauth2MessageListener); | 288 chrome.extension.onMessage.addListener(oauth2MessageListener); |
289 window.open(GET_CODE_URL, '_blank', 'location=yes,toolbar=no,menubar=no'); | 289 window.open(GET_CODE_URL, '_blank', 'location=yes,toolbar=no,menubar=no'); |
290 }; | 290 }; |
291 | 291 |
292 /** | 292 /** |
293 * Redirect page to get a new OAuth Refresh Token. | 293 * Redirect page to get a new OAuth Refresh Token. |
294 * | 294 * |
295 * @param {function():void} onDone Completion callback. | 295 * @return {Promise} A promise that resolves when the token is received. |
296 * @return {void} Nothing. | |
297 */ | 296 */ |
298 remoting.OAuth2.prototype.doAuthRedirect = function(onDone) { | 297 remoting.OAuth2.prototype.handleAuthFailure = function() { |
| 298 var deferred = new base.Deferred(); |
| 299 |
299 /** @type {remoting.OAuth2} */ | 300 /** @type {remoting.OAuth2} */ |
300 var that = this; | 301 var that = this; |
| 302 |
301 /** @param {?string} code */ | 303 /** @param {?string} code */ |
302 var onAuthorizationCode = function(code) { | 304 var onAuthorizationCode = function(code) { |
303 if (code) { | 305 if (code) { |
304 that.exchangeCodeForToken(code, onDone); | 306 that.exchangeCodeForToken(code, deferred.resolve.bind(deferred)); |
305 } else { | 307 } else { |
306 onDone(); | 308 deferred.reject(remoting.Error.NOT_AUTHENTICATED); |
307 } | 309 } |
308 }; | 310 }; |
309 this.getAuthorizationCode(onAuthorizationCode); | 311 this.getAuthorizationCode(onAuthorizationCode); |
| 312 |
| 313 return deferred.promise(); |
310 }; | 314 }; |
311 | 315 |
312 /** | 316 /** |
| 317 * Removes the cached token, prompts the user to get the new token and |
| 318 * relaunches the current window. |
| 319 * @return {void} Nothing. |
| 320 */ |
| 321 remoting.OAuth2.prototype.handleAuthFailureAndRelaunch = function() { |
| 322 this.handleAuthFailure().then(function() { |
| 323 window.location.reload(); |
| 324 }); |
| 325 }; |
| 326 |
| 327 /** |
313 * Asynchronously exchanges an authorization code for a refresh token. | 328 * Asynchronously exchanges an authorization code for a refresh token. |
314 * | 329 * |
315 * @param {string} code The OAuth2 authorization code. | 330 * @param {string} code The OAuth2 authorization code. |
316 * @param {function():void} onDone Callback to invoke on completion. | 331 * @param {function():void} onDone Callback to invoke on completion. |
317 * @return {void} Nothing. | 332 * @return {void} Nothing. |
318 */ | 333 */ |
319 remoting.OAuth2.prototype.exchangeCodeForToken = function(code, onDone) { | 334 remoting.OAuth2.prototype.exchangeCodeForToken = function(code, onDone) { |
320 /** @param {remoting.Error} error */ | 335 /** @param {remoting.Error} error */ |
321 var onError = function(error) { | 336 var onError = function(error) { |
322 console.error('Unable to exchange code for token: ', error); | 337 console.error('Unable to exchange code for token: ', error); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 * @return {?string} The user's full name, if it has been cached by a previous | 472 * @return {?string} The user's full name, if it has been cached by a previous |
458 * call to getUserInfo, otherwise null. | 473 * call to getUserInfo, otherwise null. |
459 */ | 474 */ |
460 remoting.OAuth2.prototype.getCachedUserFullName = function() { | 475 remoting.OAuth2.prototype.getCachedUserFullName = function() { |
461 var value = window.localStorage.getItem(this.KEY_FULLNAME_); | 476 var value = window.localStorage.getItem(this.KEY_FULLNAME_); |
462 if (typeof value == 'string') { | 477 if (typeof value == 'string') { |
463 return value; | 478 return value; |
464 } | 479 } |
465 return null; | 480 return null; |
466 }; | 481 }; |
OLD | NEW |