| 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 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 485 if (typeof cached == 'string') { | 485 if (typeof cached == 'string') { |
| 486 onOk(cached); | 486 onOk(cached); |
| 487 return; | 487 return; |
| 488 } | 488 } |
| 489 /** @type {remoting.OAuth2} */ | 489 /** @type {remoting.OAuth2} */ |
| 490 var that = this; | 490 var that = this; |
| 491 /** @param {XMLHttpRequest} xhr The XHR response. */ | 491 /** @param {XMLHttpRequest} xhr The XHR response. */ |
| 492 var onResponse = function(xhr) { | 492 var onResponse = function(xhr) { |
| 493 var email = null; | 493 var email = null; |
| 494 if (xhr.status == 200) { | 494 if (xhr.status == 200) { |
| 495 // TODO(ajwong): See if we can't find a JSON endpoint. | 495 var result = jsonParseSafe(xhr.responseText); |
| 496 email = xhr.responseText.split('&')[0].split('=')[1]; | 496 if (result && 'email' in result) { |
| 497 window.localStorage.setItem(that.KEY_EMAIL_, email); | 497 window.localStorage.setItem(that.KEY_EMAIL_, result['email']); |
| 498 onOk(email); | 498 onOk(result['email']); |
| 499 return; | 499 return; |
| 500 } else { |
| 501 console.error( |
| 502 'Cannot parse userinfo response: ', xhr.responseText, xhr); |
| 503 onError(remoting.Error.UNEXPECTED); |
| 504 return; |
| 505 } |
| 500 } | 506 } |
| 501 console.error('Unable to get email address:', xhr.status, xhr); | 507 console.error('Unable to get email address:', xhr.status, xhr); |
| 502 if (xhr.status == 401) { | 508 if (xhr.status == 401) { |
| 503 onError(remoting.Error.AUTHENTICATION_FAILED); | 509 onError(remoting.Error.AUTHENTICATION_FAILED); |
| 504 } else { | 510 } else { |
| 505 onError(that.interpretUnexpectedXhrStatus_(xhr.status)); | 511 onError(that.interpretUnexpectedXhrStatus_(xhr.status)); |
| 506 } | 512 } |
| 507 }; | 513 }; |
| 508 | 514 |
| 509 /** @param {string} token The access token. */ | 515 /** @param {string} token The access token. */ |
| 510 var getEmailFromToken = function(token) { | 516 var getEmailFromToken = function(token) { |
| 511 var headers = { 'Authorization': 'OAuth ' + token }; | 517 var headers = { 'Authorization': 'OAuth ' + token }; |
| 512 // TODO(ajwong): Update to new v2 API. | 518 remoting.xhr.get('https://www.googleapis.com/oauth2/v1/userinfo', |
| 513 remoting.xhr.get('https://www.googleapis.com/userinfo/email', | |
| 514 onResponse, '', headers); | 519 onResponse, '', headers); |
| 515 }; | 520 }; |
| 516 | 521 |
| 517 this.callWithToken(getEmailFromToken, onError); | 522 this.callWithToken(getEmailFromToken, onError); |
| 518 }; | 523 }; |
| 519 | 524 |
| 520 /** | 525 /** |
| 521 * If the user's email address is cached, return it, otherwise return null. | 526 * If the user's email address is cached, return it, otherwise return null. |
| 522 * | 527 * |
| 523 * @return {?string} The email address, if it has been cached by a previous call | 528 * @return {?string} The email address, if it has been cached by a previous call |
| 524 * to getEmail, otherwise null. | 529 * to getEmail, otherwise null. |
| 525 */ | 530 */ |
| 526 remoting.OAuth2.prototype.getCachedEmail = function() { | 531 remoting.OAuth2.prototype.getCachedEmail = function() { |
| 527 var value = window.localStorage.getItem(this.KEY_EMAIL_); | 532 var value = window.localStorage.getItem(this.KEY_EMAIL_); |
| 528 if (typeof value == 'string') { | 533 if (typeof value == 'string') { |
| 529 return value; | 534 return value; |
| 530 } | 535 } |
| 531 return null; | 536 return null; |
| 532 }; | 537 }; |
| OLD | NEW |