| 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 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 'refresh_token': this.getRefreshToken_(), | 301 'refresh_token': this.getRefreshToken_(), |
| 302 'grant_type': 'refresh_token' | 302 'grant_type': 'refresh_token' |
| 303 }; | 303 }; |
| 304 | 304 |
| 305 remoting.xhr.post(this.getOAuth2TokenEndpoint_(), | 305 remoting.xhr.post(this.getOAuth2TokenEndpoint_(), |
| 306 this.processTokenResponse_.bind(this, onDone), | 306 this.processTokenResponse_.bind(this, onDone), |
| 307 parameters); | 307 parameters); |
| 308 }; | 308 }; |
| 309 | 309 |
| 310 /** | 310 /** |
| 311 * @private | |
| 312 * @return {string} A URL-Safe Base64-encoded 128-bit random value. */ | |
| 313 remoting.OAuth2.prototype.generateXsrfToken_ = function() { | |
| 314 var random = new Uint8Array(16); | |
| 315 window.crypto.getRandomValues(random); | |
| 316 var base64Token = window.btoa(String.fromCharCode.apply(null, random)); | |
| 317 return base64Token.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, ''); | |
| 318 }; | |
| 319 | |
| 320 /** | |
| 321 * Redirect page to get a new OAuth2 Refresh Token. | 311 * Redirect page to get a new OAuth2 Refresh Token. |
| 322 * | 312 * |
| 323 * @return {void} Nothing. | 313 * @return {void} Nothing. |
| 324 */ | 314 */ |
| 325 remoting.OAuth2.prototype.doAuthRedirect = function() { | 315 remoting.OAuth2.prototype.doAuthRedirect = function() { |
| 326 var xsrf_token = this.generateXsrfToken_(); | 316 var xsrf_token = remoting.generateXsrfToken(); |
| 327 window.localStorage.setItem(this.KEY_XSRF_TOKEN_, xsrf_token); | 317 window.localStorage.setItem(this.KEY_XSRF_TOKEN_, xsrf_token); |
| 328 var GET_CODE_URL = this.getOAuth2AuthEndpoint_() + '?' + | 318 var GET_CODE_URL = this.getOAuth2AuthEndpoint_() + '?' + |
| 329 remoting.xhr.urlencodeParamHash({ | 319 remoting.xhr.urlencodeParamHash({ |
| 330 'client_id': this.getClientId_(), | 320 'client_id': this.getClientId_(), |
| 331 'redirect_uri': this.getRedirectUri_(), | 321 'redirect_uri': this.getRedirectUri_(), |
| 332 'scope': this.SCOPE_, | 322 'scope': this.SCOPE_, |
| 333 'state': xsrf_token, | 323 'state': xsrf_token, |
| 334 'response_type': 'code', | 324 'response_type': 'code', |
| 335 'access_type': 'offline', | 325 'access_type': 'offline', |
| 336 'approval_prompt': 'force' | 326 'approval_prompt': 'force' |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 * @return {?string} The email address, if it has been cached by a previous call | 513 * @return {?string} The email address, if it has been cached by a previous call |
| 524 * to getEmail, otherwise null. | 514 * to getEmail, otherwise null. |
| 525 */ | 515 */ |
| 526 remoting.OAuth2.prototype.getCachedEmail = function() { | 516 remoting.OAuth2.prototype.getCachedEmail = function() { |
| 527 var value = window.localStorage.getItem(this.KEY_EMAIL_); | 517 var value = window.localStorage.getItem(this.KEY_EMAIL_); |
| 528 if (typeof value == 'string') { | 518 if (typeof value == 'string') { |
| 529 return value; | 519 return value; |
| 530 } | 520 } |
| 531 return null; | 521 return null; |
| 532 }; | 522 }; |
| OLD | NEW |