Revert 264226 "Reduce dependency of TiclInvalidationService on P..."
[chromium-blink-merge.git] / remoting / webapp / identity.js
blob6aeab57716742d1b23a451fd738da15f06eee719
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.
5 /**
6 * @fileoverview
7 * Wrapper class for Chrome's identity API.
8 */
10 'use strict';
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
15 /**
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;
23 /**
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").
27 * @constructor
29 remoting.Identity = function(consentCallback) {
30 /** @private */
31 this.consentCallback_ = consentCallback;
32 /** @type {?string} @private */
33 this.email_ = null;
34 /** @type {Array.<remoting.Identity.Callbacks>} */
35 this.pendingCallbacks_ = [];
38 /**
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));
60 /**
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
66 * error occurs.
67 * @return {void} Nothing.
69 remoting.Identity.prototype.getEmail = function(onOk, onError) {
70 /** @type {remoting.Identity} */
71 var that = this;
72 /** @param {string} email */
73 var onResponse = function(email) {
74 that.email_ = email;
75 onOk(email);
78 this.callWithToken(
79 remoting.OAuth2Api.getEmail.bind(null, onResponse, onError), onError);
82 /**
83 * Get the user's email address, or null if no successful call to getEmail
84 * has been made.
86 * @return {?string} The cached email address, if available.
88 remoting.Identity.prototype.getCachedEmail = function() {
89 return this.email_;
92 /**
93 * Callback for the getAuthToken API.
95 * @param {boolean} interactive The value of the "interactive" parameter to
96 * getAuthToken.
97 * @param {?string} token The auth token, or null if the request failed.
98 * @private
100 remoting.Identity.prototype.onAuthComplete_ = function(interactive, token) {
101 // Pass the token to the callback(s) if it was retrieved successfully.
102 if (token) {
103 while (this.pendingCallbacks_.length > 0) {
104 var callback = /** @type {remoting.Identity.Callbacks} */
105 this.pendingCallbacks_.shift();
106 callback.onOk(token);
108 return;
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.
114 if (interactive) {
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);
121 return;
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.
132 * @private
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
145 * @constructor
146 * @private
148 remoting.Identity.Callbacks = function(onOk, onError) {
149 /** @type {function(string):void} */
150 this.onOk = onOk;
151 /** @type {function(remoting.Error):void} */
152 this.onError = onError;
156 * Returns whether the web app has authenticated with the Google services.
158 * @return {boolean}
160 remoting.Identity.prototype.isAuthenticated = function() {
161 return remoting.identity.email_ != null;