Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1290)

Side by Side Diff: remoting/webapp/crd/js/oauth2.js

Issue 687873003: Allow the background page to get an OAuth token for apps v1. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move jsonParseSafe into base namespace. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « remoting/webapp/crd/js/it2me_helpee_channel.js ('k') | remoting/webapp/crd/js/oauth2_api.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 * @return {{token: string, expiration: number}} The current access token, or 156 * @return {{token: string, expiration: number}} The current access token, or
157 * an invalid token if not authenticated. 157 * an invalid token if not authenticated.
158 */ 158 */
159 remoting.OAuth2.prototype.getAccessTokenInternal_ = function() { 159 remoting.OAuth2.prototype.getAccessTokenInternal_ = function() {
160 if (!window.localStorage.getItem(this.KEY_ACCESS_TOKEN_)) { 160 if (!window.localStorage.getItem(this.KEY_ACCESS_TOKEN_)) {
161 // Always be able to return structured data. 161 // Always be able to return structured data.
162 this.setAccessToken_('', 0); 162 this.setAccessToken_('', 0);
163 } 163 }
164 var accessToken = window.localStorage.getItem(this.KEY_ACCESS_TOKEN_); 164 var accessToken = window.localStorage.getItem(this.KEY_ACCESS_TOKEN_);
165 if (typeof accessToken == 'string') { 165 if (typeof accessToken == 'string') {
166 var result = jsonParseSafe(accessToken); 166 var result = base.jsonParseSafe(accessToken);
167 if (result && 'token' in result && 'expiration' in result) { 167 if (result && 'token' in result && 'expiration' in result) {
168 return /** @type {{token: string, expiration: number}} */ result; 168 return /** @type {{token: string, expiration: number}} */ result;
169 } 169 }
170 } 170 }
171 console.log('Invalid access token stored.'); 171 console.log('Invalid access token stored.');
172 return {'token': '', 'expiration': 0}; 172 return {'token': '', 'expiration': 0};
173 }; 173 };
174 174
175 /** 175 /**
176 * Returns true if the access token is expired, or otherwise invalid. 176 * Returns true if the access token is expired, or otherwise invalid.
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 remoting.OAuth2.prototype.onTokens_ = 230 remoting.OAuth2.prototype.onTokens_ =
231 function(onOk, refreshToken, accessToken, expiresIn) { 231 function(onOk, refreshToken, accessToken, expiresIn) {
232 this.setAccessToken_(accessToken, expiresIn); 232 this.setAccessToken_(accessToken, expiresIn);
233 this.setRefreshToken_(refreshToken); 233 this.setRefreshToken_(refreshToken);
234 onOk(); 234 onOk();
235 }; 235 };
236 236
237 /** 237 /**
238 * Redirect page to get a new OAuth2 Refresh Token. 238 * Redirect page to get a new OAuth2 Refresh Token.
239 * 239 *
240 * @param {function():void} onDone Completion callback.
240 * @return {void} Nothing. 241 * @return {void} Nothing.
241 */ 242 */
242 remoting.OAuth2.prototype.doAuthRedirect = function() { 243 remoting.OAuth2.prototype.doAuthRedirect = function(onDone) {
243 /** @type {remoting.OAuth2} */ 244 /** @type {remoting.OAuth2} */
244 var that = this; 245 var that = this;
245 var xsrf_token = remoting.generateXsrfToken(); 246 var xsrf_token = base.generateXsrfToken();
246 window.localStorage.setItem(this.KEY_XSRF_TOKEN_, xsrf_token); 247 window.localStorage.setItem(this.KEY_XSRF_TOKEN_, xsrf_token);
247 var GET_CODE_URL = this.getOAuth2AuthEndpoint_() + '?' + 248 var GET_CODE_URL = this.getOAuth2AuthEndpoint_() + '?' +
248 remoting.xhr.urlencodeParamHash({ 249 remoting.xhr.urlencodeParamHash({
249 'client_id': this.getClientId_(), 250 'client_id': this.getClientId_(),
250 'redirect_uri': this.getRedirectUri_(), 251 'redirect_uri': this.getRedirectUri_(),
251 'scope': this.SCOPE_, 252 'scope': this.SCOPE_,
252 'state': xsrf_token, 253 'state': xsrf_token,
253 'response_type': 'code', 254 'response_type': 'code',
254 'access_type': 'offline', 255 'access_type': 'offline',
255 'approval_prompt': 'force' 256 'approval_prompt': 'force'
256 }); 257 });
257 258
258 /** 259 /**
259 * Processes the results of the oauth flow. 260 * Processes the results of the oauth flow.
260 * 261 *
261 * @param {Object.<string, string>} message Dictionary containing the parsed 262 * @param {Object.<string, string>} message Dictionary containing the parsed
262 * OAuth redirect URL parameters. 263 * OAuth redirect URL parameters.
263 */ 264 */
264 function oauth2MessageListener(message) { 265 function oauth2MessageListener(message) {
265 if ('code' in message && 'state' in message) { 266 if ('code' in message && 'state' in message) {
266 var onDone = function() {
267 window.location.reload();
268 };
269 that.exchangeCodeForToken( 267 that.exchangeCodeForToken(
270 message['code'], message['state'], onDone); 268 message['code'], message['state'], onDone);
271 } else { 269 } else {
272 if ('error' in message) { 270 if ('error' in message) {
273 console.error( 271 console.error(
274 'Could not obtain authorization code: ' + message['error']); 272 'Could not obtain authorization code: ' + message['error']);
275 } else { 273 } else {
276 // We intentionally don't log the response - since we don't understand 274 // We intentionally don't log the response - since we don't understand
277 // it, we can't tell if it has sensitive data. 275 // it, we can't tell if it has sensitive data.
278 console.error('Invalid oauth2 response.'); 276 console.error('Invalid oauth2 response.');
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 * @return {?string} The email address, if it has been cached by a previous call 367 * @return {?string} The email address, if it has been cached by a previous call
370 * to getEmail, otherwise null. 368 * to getEmail, otherwise null.
371 */ 369 */
372 remoting.OAuth2.prototype.getCachedEmail = function() { 370 remoting.OAuth2.prototype.getCachedEmail = function() {
373 var value = window.localStorage.getItem(this.KEY_EMAIL_); 371 var value = window.localStorage.getItem(this.KEY_EMAIL_);
374 if (typeof value == 'string') { 372 if (typeof value == 'string') {
375 return value; 373 return value;
376 } 374 }
377 return null; 375 return null;
378 }; 376 };
OLDNEW
« no previous file with comments | « remoting/webapp/crd/js/it2me_helpee_channel.js ('k') | remoting/webapp/crd/js/oauth2_api.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698