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 22 matching lines...) Expand all Loading... | |
| 33 /** @private */ | 33 /** @private */ |
| 34 remoting.OAuth2.prototype.KEY_ACCESS_TOKEN_ = 'oauth2-access-token'; | 34 remoting.OAuth2.prototype.KEY_ACCESS_TOKEN_ = 'oauth2-access-token'; |
| 35 /** @private */ | 35 /** @private */ |
| 36 remoting.OAuth2.prototype.KEY_EMAIL_ = 'remoting-email'; | 36 remoting.OAuth2.prototype.KEY_EMAIL_ = 'remoting-email'; |
| 37 | 37 |
| 38 // Constants for parameters used in retrieving the OAuth2 credentials. | 38 // Constants for parameters used in retrieving the OAuth2 credentials. |
| 39 /** @private */ | 39 /** @private */ |
| 40 remoting.OAuth2.prototype.SCOPE_ = | 40 remoting.OAuth2.prototype.SCOPE_ = |
| 41 'https://www.googleapis.com/auth/chromoting ' + | 41 'https://www.googleapis.com/auth/chromoting ' + |
| 42 'https://www.googleapis.com/auth/googletalk ' + | 42 'https://www.googleapis.com/auth/googletalk ' + |
| 43 'https://www.googleapis.com/auth/userinfo#email'; | 43 'https://www.googleapis.com/auth/userinfo#email'; |
|
Jamie
2013/01/09 18:51:09
Does the scope need to be changed as well? And if
rmsousa
2013/01/09 20:34:39
Same scope, no issue with the tokens.
| |
| 44 /** @private */ | 44 /** @private */ |
| 45 remoting.OAuth2.prototype.OAUTH2_TOKEN_ENDPOINT_ = | 45 remoting.OAuth2.prototype.OAUTH2_TOKEN_ENDPOINT_ = |
| 46 'https://accounts.google.com/o/oauth2/token'; | 46 'https://accounts.google.com/o/oauth2/token'; |
| 47 /** @private */ | 47 /** @private */ |
| 48 remoting.OAuth2.prototype.OAUTH2_VALIDATE_TOKEN_ENDPOINT_ = | 48 remoting.OAuth2.prototype.OAUTH2_VALIDATE_TOKEN_ENDPOINT_ = |
| 49 'https://www.googleapis.com/oauth2/v1/tokeninfo'; | 49 'https://www.googleapis.com/oauth2/v1/tokeninfo'; |
| 50 /** @private */ | 50 /** @private */ |
| 51 remoting.OAuth2.prototype.OAUTH2_REVOKE_TOKEN_ENDPOINT_ = | 51 remoting.OAuth2.prototype.OAUTH2_REVOKE_TOKEN_ENDPOINT_ = |
| 52 'https://accounts.google.com/o/oauth2/revoke'; | 52 'https://accounts.google.com/o/oauth2/revoke'; |
| 53 | 53 |
| (...skipping 431 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 = |
| 496 email = xhr.responseText.split('&')[0].split('=')[1]; | 496 /** @type {{email: string}} */ (jsonParseSafe(xhr.responseText)); |
| 497 window.localStorage.setItem(that.KEY_EMAIL_, email); | 497 window.localStorage.setItem(that.KEY_EMAIL_, result.email); |
| 498 onOk(email); | 498 onOk(result.email); |
|
Jamie
2013/01/09 18:51:09
I think we could benefit from some sanity-checking
rmsousa
2013/01/09 20:34:39
Done.
| |
| 499 return; | 499 return; |
| 500 } | 500 } |
| 501 console.error('Unable to get email address:', xhr.status, xhr); | 501 console.error('Unable to get email address:', xhr.status, xhr); |
| 502 if (xhr.status == 401) { | 502 if (xhr.status == 401) { |
| 503 onError(remoting.Error.AUTHENTICATION_FAILED); | 503 onError(remoting.Error.AUTHENTICATION_FAILED); |
| 504 } else { | 504 } else { |
| 505 onError(that.interpretUnexpectedXhrStatus_(xhr.status)); | 505 onError(that.interpretUnexpectedXhrStatus_(xhr.status)); |
| 506 } | 506 } |
| 507 }; | 507 }; |
| 508 | 508 |
| 509 /** @param {string} token The access token. */ | 509 /** @param {string} token The access token. */ |
| 510 var getEmailFromToken = function(token) { | 510 var getEmailFromToken = function(token) { |
| 511 var headers = { 'Authorization': 'OAuth ' + token }; | 511 var headers = { 'Authorization': 'OAuth ' + token }; |
| 512 // TODO(ajwong): Update to new v2 API. | 512 remoting.xhr.get('https://www.googleapis.com/oauth2/v1/userinfo', |
| 513 remoting.xhr.get('https://www.googleapis.com/userinfo/email', | |
| 514 onResponse, '', headers); | 513 onResponse, '', headers); |
| 515 }; | 514 }; |
| 516 | 515 |
| 517 this.callWithToken(getEmailFromToken, onError); | 516 this.callWithToken(getEmailFromToken, onError); |
| 518 }; | 517 }; |
| 519 | 518 |
| 520 /** | 519 /** |
| 521 * If the user's email address is cached, return it, otherwise return null. | 520 * If the user's email address is cached, return it, otherwise return null. |
| 522 * | 521 * |
| 523 * @return {?string} The email address, if it has been cached by a previous call | 522 * @return {?string} The email address, if it has been cached by a previous call |
| 524 * to getEmail, otherwise null. | 523 * to getEmail, otherwise null. |
| 525 */ | 524 */ |
| 526 remoting.OAuth2.prototype.getCachedEmail = function() { | 525 remoting.OAuth2.prototype.getCachedEmail = function() { |
| 527 var value = window.localStorage.getItem(this.KEY_EMAIL_); | 526 var value = window.localStorage.getItem(this.KEY_EMAIL_); |
| 528 if (typeof value == 'string') { | 527 if (typeof value == 'string') { |
| 529 return value; | 528 return value; |
| 530 } | 529 } |
| 531 return null; | 530 return null; |
| 532 }; | 531 }; |
| OLD | NEW |