OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 11 matching lines...) Expand all Loading... | |
22 | 22 |
23 /** @constructor */ | 23 /** @constructor */ |
24 remoting.OAuth2 = function() { | 24 remoting.OAuth2 = function() { |
25 }; | 25 }; |
26 | 26 |
27 // Constants representing keys used for storing persistent state. | 27 // Constants representing keys used for storing persistent state. |
28 /** @private */ | 28 /** @private */ |
29 remoting.OAuth2.prototype.KEY_REFRESH_TOKEN_ = 'oauth2-refresh-token'; | 29 remoting.OAuth2.prototype.KEY_REFRESH_TOKEN_ = 'oauth2-refresh-token'; |
30 /** @private */ | 30 /** @private */ |
31 remoting.OAuth2.prototype.KEY_ACCESS_TOKEN_ = 'oauth2-access-token'; | 31 remoting.OAuth2.prototype.KEY_ACCESS_TOKEN_ = 'oauth2-access-token'; |
32 /** @private */ | |
33 remoting.OAuth2.prototype.KEY_EMAIL_ = 'remoting-email'; | |
Jamie
2011/10/27 20:41:36
This class now handles retrieving and saving the e
garykac
2011/10/27 22:32:39
OK
| |
32 | 34 |
33 // Constants for parameters used in retrieving the OAuth2 credentials. | 35 // Constants for parameters used in retrieving the OAuth2 credentials. |
34 /** @private */ remoting.OAuth2.prototype.CLIENT_ID_ = | 36 /** @private */ |
37 remoting.OAuth2.prototype.CLIENT_ID_ = | |
35 '440925447803-2pi3v45bff6tp1rde2f7q6lgbor3o5uj.' + | 38 '440925447803-2pi3v45bff6tp1rde2f7q6lgbor3o5uj.' + |
36 'apps.googleusercontent.com'; | 39 'apps.googleusercontent.com'; |
37 /** @private */ | 40 /** @private */ |
38 remoting.OAuth2.prototype.CLIENT_SECRET_ = 'W2ieEsG-R1gIA4MMurGrgMc_'; | 41 remoting.OAuth2.prototype.CLIENT_SECRET_ = 'W2ieEsG-R1gIA4MMurGrgMc_'; |
39 /** @private */ remoting.OAuth2.prototype.SCOPE_ = | 42 /** @private */ |
43 remoting.OAuth2.prototype.SCOPE_ = | |
40 'https://www.googleapis.com/auth/chromoting ' + | 44 'https://www.googleapis.com/auth/chromoting ' + |
41 'https://www.googleapis.com/auth/googletalk ' + | 45 'https://www.googleapis.com/auth/googletalk ' + |
42 'https://www.googleapis.com/auth/userinfo#email'; | 46 'https://www.googleapis.com/auth/userinfo#email'; |
43 /** @private */ remoting.OAuth2.prototype.REDIRECT_URI_ = | 47 /** @private */ |
48 remoting.OAuth2.prototype.REDIRECT_URI_ = | |
44 'https://talkgadget.google.com/talkgadget/blank'; | 49 'https://talkgadget.google.com/talkgadget/blank'; |
45 /** @private */ remoting.OAuth2.prototype.OAUTH2_TOKEN_ENDPOINT_ = | 50 /** @private */ |
51 remoting.OAuth2.prototype.OAUTH2_TOKEN_ENDPOINT_ = | |
46 'https://accounts.google.com/o/oauth2/token'; | 52 'https://accounts.google.com/o/oauth2/token'; |
47 | 53 |
48 /** @return {boolean} True if the app is already authenticated. */ | 54 /** @return {boolean} True if the app is already authenticated. */ |
49 remoting.OAuth2.prototype.isAuthenticated = function() { | 55 remoting.OAuth2.prototype.isAuthenticated = function() { |
50 if (this.getRefreshToken()) { | 56 if (this.getRefreshToken()) { |
51 return true; | 57 return true; |
52 } | 58 } |
53 return false; | 59 return false; |
54 }; | 60 }; |
55 | 61 |
56 /** | 62 /** |
57 * Removes all storage, and effectively unauthenticates the user. | 63 * Removes all storage, and effectively unauthenticates the user. |
58 * | 64 * |
59 * @return {void} Nothing. | 65 * @return {void} Nothing. |
60 */ | 66 */ |
61 remoting.OAuth2.prototype.clear = function() { | 67 remoting.OAuth2.prototype.clear = function() { |
62 window.localStorage.removeItem(this.KEY_REFRESH_TOKEN_); | 68 window.localStorage.removeItem(this.KEY_REFRESH_TOKEN_); |
69 window.localStorage.removeItem(this.KEY_EMAIL_); | |
63 this.clearAccessToken(); | 70 this.clearAccessToken(); |
64 }; | 71 }; |
65 | 72 |
66 /** | 73 /** |
67 * @param {string} token The new refresh token. | 74 * @param {string} token The new refresh token. |
68 * @return {void} Nothing. | 75 * @return {void} Nothing. |
69 */ | 76 */ |
70 remoting.OAuth2.prototype.setRefreshToken = function(token) { | 77 remoting.OAuth2.prototype.setRefreshToken = function(token) { |
71 window.localStorage.setItem(this.KEY_REFRESH_TOKEN_, escape(token)); | 78 window.localStorage.setItem(this.KEY_REFRESH_TOKEN_, escape(token)); |
72 this.clearAccessToken(); | 79 this.clearAccessToken(); |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
284 // If we still need it, we're going to infinite loop. | 291 // If we still need it, we're going to infinite loop. |
285 throw 'Unable to get access token.'; | 292 throw 'Unable to get access token.'; |
286 } | 293 } |
287 myfunc(that.getAccessToken()); | 294 myfunc(that.getAccessToken()); |
288 }); | 295 }); |
289 return; | 296 return; |
290 } | 297 } |
291 | 298 |
292 myfunc(this.getAccessToken()); | 299 myfunc(this.getAccessToken()); |
293 }; | 300 }; |
301 | |
302 /** | |
303 * Get the user's email address. | |
304 * | |
305 * @param {function(?string):void} setEmail Callback invoked when the email | |
306 * address is available, or on error. | |
307 * @return {void} Nothing. | |
308 */ | |
309 remoting.OAuth2.prototype.getEmail = function(setEmail) { | |
310 /** @type {remoting.OAuth2} */ | |
311 var that = this; | |
312 /** @param {XMLHttpRequest} xhr The XHR response. */ | |
313 var onResponse = function(xhr) { | |
314 that.email = null; | |
315 if (xhr.status == 200) { | |
316 // TODO(ajwong): See if we can't find a JSON endpoint. | |
317 that.email = xhr.responseText.split('&')[0].split('=')[1]; | |
318 } | |
319 window.localStorage.setItem(that.KEY_EMAIL_, that.email); | |
320 setEmail(that.email); | |
321 }; | |
322 | |
323 /** @param {string} token The access token. */ | |
324 var getEmailFromToken = function(token) { | |
325 var headers = { 'Authorization': 'OAuth ' + token }; | |
326 // TODO(ajwong): Update to new v2 API. | |
327 remoting.xhr.get('https://www.googleapis.com/userinfo/email', | |
328 onResponse, '', headers); | |
329 }; | |
330 | |
331 this.callWithToken(getEmailFromToken); | |
332 }; | |
333 | |
334 /** | |
335 * If the user's email address is cached, return it, otherwise return null. | |
336 * | |
337 * @return {?string} The email address, if it has been cached by a previous call | |
338 * to getEmail, otherwise null. | |
339 */ | |
340 remoting.OAuth2.prototype.getCachedEmail = function() { | |
341 var value = window.localStorage.getItem(this.KEY_EMAIL_); | |
342 if (typeof value == 'string') { | |
343 return value; | |
344 } | |
345 return null; | |
346 }; | |
OLD | NEW |