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
3 // found in the LICENSE file.
7 * Wrapper class for Chrome's identity API.
12 /** @suppress {duplicate} */
13 var remoting
= remoting
|| {};
16 * TODO(jamiewalch): Remove remoting.OAuth2 from this type annotation when
17 * the Apps v2 work is complete.
19 * @type {remoting.Identity|remoting.OAuth2}
21 remoting
.identity
= null;
24 * @param {function(function():void):void} consentCallback Callback invoked if
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").
29 remoting
.Identity = function(consentCallback
) {
31 this.consentCallback_
= consentCallback
;
32 /** @type {?string} @private */
34 /** @type {Array.<remoting.Identity.Callbacks>} */
35 this.pendingCallbacks_
= [];
39 * Call a function with an access token.
41 * TODO(jamiewalch): Currently, this results in a new GAIA token being minted
42 * each time the function is called. Implement caching functionality unless
43 * getAuthToken starts doing so itself.
45 * @param {function(string):void} onOk Function to invoke with access token if
46 * an access token was successfully retrieved.
47 * @param {function(remoting.Error):void} onError Function to invoke with an
48 * error code on failure.
49 * @return {void} Nothing.
51 remoting
.Identity
.prototype.callWithToken = function(onOk
, onError
) {
52 this.pendingCallbacks_
.push(new remoting
.Identity
.Callbacks(onOk
, onError
));
53 if (this.pendingCallbacks_
.length
== 1) {
54 chrome
.identity
.getAuthToken(
55 { 'interactive': false },
56 this.onAuthComplete_
.bind(this, false));
61 * Get the user's email address.
63 * @param {function(string):void} onOk Callback invoked when the email
64 * address is available.
65 * @param {function(remoting.Error):void} onError Callback invoked if an
67 * @return {void} Nothing.
69 remoting
.Identity
.prototype.getEmail = function(onOk
, onError
) {
70 /** @type {remoting.Identity} */
72 /** @param {string} email */
73 var onResponse = function(email
) {
79 remoting
.OAuth2Api
.getEmail
.bind(null, onResponse
, onError
), onError
);
83 * Get the user's email address, or null if no successful call to getEmail
86 * @return {?string} The cached email address, if available.
88 remoting
.Identity
.prototype.getCachedEmail = function() {
93 * Callback for the getAuthToken API.
95 * @param {boolean} interactive The value of the "interactive" parameter to
97 * @param {?string} token The auth token, or null if the request failed.
100 remoting
.Identity
.prototype.onAuthComplete_ = function(interactive
, token
) {
101 // Pass the token to the callback(s) if it was retrieved successfully.
103 while (this.pendingCallbacks_
.length
> 0) {
104 var callback
= /** @type {remoting.Identity.Callbacks} */
105 this.pendingCallbacks_
.shift();
106 callback
.onOk(token
);
111 // If not, pass an error back to the callback(s) if we've already prompted the
112 // user for permission.
113 // TODO(jamiewalch): Figure out what to do with the error in this case.
115 console
.error(chrome
.runtime
.lastError
);
116 while (this.pendingCallbacks_
.length
> 0) {
117 var callback
= /** @type {remoting.Identity.Callbacks} */
118 this.pendingCallbacks_
.shift();
119 callback
.onError(remoting
.Error
.UNEXPECTED
);
124 // If there's no token, but we haven't yet prompted for permission, do so
125 // now. The consent callback is responsible for continuing the auth flow.
126 this.consentCallback_(this.onAuthContinue_
.bind(this));
130 * Called in response to the user signing in to the web-app.
134 remoting
.Identity
.prototype.onAuthContinue_ = function() {
135 chrome
.identity
.getAuthToken(
136 { 'interactive': true },
137 this.onAuthComplete_
.bind(this, true));
141 * Internal representation for pair of callWithToken callbacks.
143 * @param {function(string):void} onOk
144 * @param {function(remoting.Error):void} onError
148 remoting
.Identity
.Callbacks = function(onOk
, onError
) {
149 /** @type {function(string):void} */
151 /** @type {function(remoting.Error):void} */
152 this.onError
= onError
;
156 * Returns whether the web app has authenticated with the Google services.
160 remoting
.Identity
.prototype.isAuthenticated = function() {
161 return remoting
.identity
.email_
!= null;