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 * Wrapper class for Chrome's identity API. | 7 * Wrapper class for Chrome's identity API. |
8 */ | 8 */ |
9 | 9 |
10 'use strict'; | 10 'use strict'; |
11 | 11 |
12 /** @suppress {duplicate} */ | 12 /** @suppress {duplicate} */ |
13 var remoting = remoting || {}; | 13 var remoting = remoting || {}; |
14 | 14 |
15 /** | 15 /** |
16 * TODO(jamiewalch): Remove remoting.OAuth2 from this type annotation when | 16 * TODO(jamiewalch): Remove remoting.OAuth2 from this type annotation when |
17 * the Apps v2 work is complete. | 17 * the Apps v2 work is complete. |
18 * | 18 * |
19 * @type {remoting.Identity|remoting.OAuth2} | 19 * @type {remoting.Identity|remoting.OAuth2} |
20 */ | 20 */ |
21 remoting.identity = null; | 21 remoting.identity = null; |
22 | 22 |
23 /** | 23 /** |
24 * @param {function(function():void):void} consentCallback Callback invoked if | 24 * @param {function(function():void):void} consentCallback Callback invoked if |
25 * user consent is required. The callback is passed a continuation function | 25 * user consent is required. The callback is passed a continuation function |
26 * which must be called from an interactive event handler (e.g. "click"). | 26 * which must be called from an interactive event handler (e.g. "click"). |
27 * @constructor | 27 * @constructor |
28 */ | 28 */ |
29 remoting.Identity = function(consentCallback) { | 29 remoting.Identity = function(consentCallback) { |
Jamie
2015/01/28 20:52:09
As discussed, I don't think we need any changes to
kelvinp
2015/01/29 01:05:34
Done.
| |
30 /** @private */ | 30 /** @private */ |
31 this.consentCallback_ = consentCallback; | 31 this.consentCallback_ = consentCallback; |
32 /** @type {string} @private */ | 32 /** @type {string} @private */ |
33 this.email_ = ''; | 33 this.email_ = ''; |
34 /** @type {string} @private */ | 34 /** @type {string} @private */ |
35 this.fullName_ = ''; | 35 this.fullName_ = ''; |
36 /** @type {Array.<remoting.Identity.Callbacks>} */ | 36 /** @type {Array.<remoting.Identity.Callbacks>} */ |
37 this.pendingCallbacks_ = []; | 37 this.pendingCallbacks_ = []; |
38 /** @type {Promise} */ | |
39 this.handleAuthFailurePromise_ = null; | |
38 }; | 40 }; |
39 | 41 |
40 /** | 42 /** |
41 * Call a function with an access token. | 43 * Call a function with an access token. |
42 * | 44 * |
43 * @param {function(string):void} onOk Function to invoke with access token if | 45 * @param {function(string):void} onOk Function to invoke with access token if |
44 * an access token was successfully retrieved. | 46 * an access token was successfully retrieved. |
45 * @param {function(remoting.Error):void} onError Function to invoke with an | 47 * @param {function(remoting.Error):void} onError Function to invoke with an |
46 * error code on failure. | 48 * error code on failure. |
47 * @return {void} Nothing. | 49 * @return {void} Nothing. |
(...skipping 20 matching lines...) Expand all Loading... | |
68 /** @type {remoting.Identity} */ | 70 /** @type {remoting.Identity} */ |
69 var that = this; | 71 var that = this; |
70 | 72 |
71 /** | 73 /** |
72 * @param {string} token | 74 * @param {string} token |
73 */ | 75 */ |
74 function revokeToken(token) { | 76 function revokeToken(token) { |
75 chrome.identity.removeCachedAuthToken( | 77 chrome.identity.removeCachedAuthToken( |
76 {'token': token }, | 78 {'token': token }, |
77 that.callWithToken.bind(that, onOk, onError)); | 79 that.callWithToken.bind(that, onOk, onError)); |
78 }; | 80 } |
79 | 81 |
80 this.callWithToken(revokeToken, onError); | 82 this.callWithToken(revokeToken, onError); |
81 }; | 83 }; |
82 | 84 |
83 /** | 85 /** |
86 * Removes the cached token and prompts the user to get a new one. | |
87 * @return {Promise} A promise that resolves when a new token is fetched. | |
88 */ | |
89 remoting.Identity.prototype.handleAuthFailure = function() { | |
90 if (!this.handleAuthFailurePromise_) { | |
91 var that = this; | |
92 | |
93 var revokeToken = base.Promise.as(this.removeCachedAuthToken_, []); | |
Jamie
2015/01/23 23:33:27
Using Promise.as to Promisify an API we own is a l
kelvinp
2015/01/28 00:26:46
Agreed. I have converted the private API (removeCa
| |
94 | |
95 this.handleAuthFailurePromise_= revokeToken.then(function() { | |
96 return base.Promise.as(that.consentCallback_, []); | |
97 }).then(function(){ | |
98 return base.Promise.as( | |
99 chrome.identity.getAuthToken, [{ 'interactive': true }]); | |
100 }).then( | |
101 /** @param {string} token */ | |
102 function(token) { | |
103 that.handleAuthFailurePromise_ = null; | |
104 if (token) { | |
105 return Promise.resolve(token); | |
106 } | |
107 return Promise.reject(remoting.Error.NOT_AUTHENTICATED); | |
108 } | |
109 ); | |
110 } | |
111 return this.handleAuthFailurePromise_; | |
112 }; | |
113 | |
114 /** | |
115 * Removes the cached token, prompts the user to get a new one and | |
116 * relaunches the current window. | |
117 * @return {void} Nothing. | |
118 */ | |
119 remoting.Identity.prototype.handleAuthFailureAndRelaunch = function() { | |
120 this.handleAuthFailure().then(function() { | |
121 base.RemoteMethods.invoke(remoting.ActivationHandler.Request.RELAUNCH, | |
122 [chrome.app.window.current().id]); | |
123 }); | |
124 }; | |
125 | |
126 /** | |
84 * Remove the cached auth token, if any. | 127 * Remove the cached auth token, if any. |
85 * | 128 * |
86 * @param {function():void} onDone Completion callback. | 129 * @param {function():void} onDone Completion callback. |
87 * @return {void} Nothing. | 130 * @return {void} Nothing. |
88 */ | 131 */ |
89 remoting.Identity.prototype.removeCachedAuthToken = function(onDone) { | 132 remoting.Identity.prototype.removeCachedAuthToken_ = function(onDone) { |
Jamie
2015/01/23 23:33:27
Underscore and @private should go hand-in-hand. Pl
kelvinp
2015/01/28 00:26:46
Done.
| |
90 /** @param {string} token */ | 133 /** @param {string} token */ |
91 var onToken = function(token) { | 134 var onToken = function(token) { |
92 if (token) { | 135 if (token) { |
93 chrome.identity.removeCachedAuthToken({ 'token': token }, onDone); | 136 chrome.identity.removeCachedAuthToken({ 'token': token }, onDone); |
94 } else { | 137 } else { |
95 onDone(); | 138 onDone(); |
96 } | 139 } |
97 }; | 140 }; |
98 chrome.identity.getAuthToken({ 'interactive': false }, onToken); | 141 chrome.identity.getAuthToken({ 'interactive': false }, onToken); |
99 }; | 142 }; |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
237 }; | 280 }; |
238 | 281 |
239 /** | 282 /** |
240 * Returns whether the web app has authenticated with the Google services. | 283 * Returns whether the web app has authenticated with the Google services. |
241 * | 284 * |
242 * @return {boolean} | 285 * @return {boolean} |
243 */ | 286 */ |
244 remoting.Identity.prototype.isAuthenticated = function() { | 287 remoting.Identity.prototype.isAuthenticated = function() { |
245 return remoting.identity.email_ !== ''; | 288 return remoting.identity.email_ !== ''; |
246 }; | 289 }; |
OLD | NEW |