Chromium Code Reviews| 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('Cannot parse userinfo response: ', xhr.responseText); | |
|
Jamie
2013/01/09 20:47:13
For xhr errors, I usually try to log both the resp
| |
| 502 onError(remoting.Error.UNEXPECTED); | |
| 503 return; | |
| 504 } | |
| 500 } | 505 } |
| 501 console.error('Unable to get email address:', xhr.status, xhr); | 506 console.error('Unable to get email address:', xhr.status, xhr); |
| 502 if (xhr.status == 401) { | 507 if (xhr.status == 401) { |
| 503 onError(remoting.Error.AUTHENTICATION_FAILED); | 508 onError(remoting.Error.AUTHENTICATION_FAILED); |
| 504 } else { | 509 } else { |
| 505 onError(that.interpretUnexpectedXhrStatus_(xhr.status)); | 510 onError(that.interpretUnexpectedXhrStatus_(xhr.status)); |
| 506 } | 511 } |
| 507 }; | 512 }; |
| 508 | 513 |
| 509 /** @param {string} token The access token. */ | 514 /** @param {string} token The access token. */ |
| 510 var getEmailFromToken = function(token) { | 515 var getEmailFromToken = function(token) { |
| 511 var headers = { 'Authorization': 'OAuth ' + token }; | 516 var headers = { 'Authorization': 'OAuth ' + token }; |
| 512 // TODO(ajwong): Update to new v2 API. | 517 remoting.xhr.get('https://www.googleapis.com/oauth2/v1/userinfo', |
| 513 remoting.xhr.get('https://www.googleapis.com/userinfo/email', | |
| 514 onResponse, '', headers); | 518 onResponse, '', headers); |
| 515 }; | 519 }; |
| 516 | 520 |
| 517 this.callWithToken(getEmailFromToken, onError); | 521 this.callWithToken(getEmailFromToken, onError); |
| 518 }; | 522 }; |
| 519 | 523 |
| 520 /** | 524 /** |
| 521 * If the user's email address is cached, return it, otherwise return null. | 525 * If the user's email address is cached, return it, otherwise return null. |
| 522 * | 526 * |
| 523 * @return {?string} The email address, if it has been cached by a previous call | 527 * @return {?string} The email address, if it has been cached by a previous call |
| 524 * to getEmail, otherwise null. | 528 * to getEmail, otherwise null. |
| 525 */ | 529 */ |
| 526 remoting.OAuth2.prototype.getCachedEmail = function() { | 530 remoting.OAuth2.prototype.getCachedEmail = function() { |
| 527 var value = window.localStorage.getItem(this.KEY_EMAIL_); | 531 var value = window.localStorage.getItem(this.KEY_EMAIL_); |
| 528 if (typeof value == 'string') { | 532 if (typeof value == 'string') { |
| 529 return value; | 533 return value; |
| 530 } | 534 } |
| 531 return null; | 535 return null; |
| 532 }; | 536 }; |
| OLD | NEW |