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 * @param {function(string):void} onOk Function to invoke with access token if
42 * an access token was successfully retrieved.
43 * @param {function(remoting.Error):void} onError Function to invoke with an
44 * error code on failure.
45 * @return {void} Nothing.
47 remoting.Identity.prototype.callWithToken = function(onOk, onError) {
48 this.pendingCallbacks_.push(new remoting.Identity.Callbacks(onOk, onError));
49 if (this.pendingCallbacks_.length == 1) {
50 chrome.identity.getAuthToken(
51 { 'interactive': false },
52 this.onAuthComplete_.bind(this, false));
57 * Remove the cached auth token, if any.
59 * @param {function():void} onDone Completion callback.
60 * @return {void} Nothing.
62 remoting.Identity.prototype.removeCachedAuthToken = function(onDone) {
63 /** @param {string} token */
64 var onToken = function(token) {
66 chrome.identity.removeCachedAuthToken({ 'token': token }, onDone);
71 chrome.identity.getAuthToken({ 'interactive': false }, onToken);
75 * Get the user's email address.
77 * @param {function(string):void} onOk Callback invoked when the email
78 * address is available.
79 * @param {function(remoting.Error):void} onError Callback invoked if an
81 * @return {void} Nothing.
83 remoting.Identity.prototype.getEmail = function(onOk, onError) {
84 /** @type {remoting.Identity} */
86 /** @param {string} email */
87 var onResponse = function(email) {
93 remoting.OAuth2Api.getEmail.bind(null, onResponse, onError), onError);
97 * Get the user's email address, or null if no successful call to getEmail
100 * @return {?string} The cached email address, if available.
102 remoting.Identity.prototype.getCachedEmail = function() {
107 * Callback for the getAuthToken API.
109 * @param {boolean} interactive The value of the "interactive" parameter to
111 * @param {?string} token The auth token, or null if the request failed.
114 remoting.Identity.prototype.onAuthComplete_ = function(interactive, token) {
115 // Pass the token to the callback(s) if it was retrieved successfully.
117 while (this.pendingCallbacks_.length > 0) {
118 var callback = /** @type {remoting.Identity.Callbacks} */
119 this.pendingCallbacks_.shift();
120 callback.onOk(token);
125 // If not, pass an error back to the callback(s) if we've already prompted the
126 // user for permission.
129 chrome.runtime.lastError ? chrome.runtime.lastError.message
131 console.error(error_message);
132 while (this.pendingCallbacks_.length > 0) {
133 var callback = /** @type {remoting.Identity.Callbacks} */
134 this.pendingCallbacks_.shift();
135 callback.onError(remoting.Error.NOT_AUTHENTICATED);
140 // If there's no token, but we haven't yet prompted for permission, do so
141 // now. The consent callback is responsible for continuing the auth flow.
142 this.consentCallback_(this.onAuthContinue_.bind(this));
146 * Called in response to the user signing in to the web-app.
150 remoting.Identity.prototype.onAuthContinue_ = function() {
151 chrome.identity.getAuthToken(
152 { 'interactive': true },
153 this.onAuthComplete_.bind(this, true));
157 * Internal representation for pair of callWithToken callbacks.
159 * @param {function(string):void} onOk
160 * @param {function(remoting.Error):void} onError
164 remoting.Identity.Callbacks = function(onOk, onError) {
165 /** @type {function(string):void} */
167 /** @type {function(remoting.Error):void} */
168 this.onError = onError;
172 * Returns whether the web app has authenticated with the Google services.
176 remoting.Identity.prototype.isAuthenticated = function() {
177 return remoting.identity.email_ != null;