1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview An UI component to host gaia auth extension in an iframe.
* After the component binds with an iframe, call its {@code load} to start the
* authentication flow. There are two events would be raised after this point:
* a 'ready' event when the authentication UI is ready to use and a 'completed'
* event when the authentication is completed successfully. If caller is
* interested in the user credentials, he may supply a success callback with
* {@code load} call. The callback will be invoked when the authentication is
* completed successfully and with the available credential data.
*/
cr.define('cr.login', function() {
'use strict';
/**
* Base URL of gaia auth extension.
* @const
*/
var AUTH_URL_BASE = 'chrome-extension://mfffpogegjflfpflabcdkioaeobkgjik';
/**
* Auth URL to use for online flow.
* @const
*/
var AUTH_URL = AUTH_URL_BASE + '/main.html';
/**
* Auth URL to use for offline flow.
* @const
*/
var OFFLINE_AUTH_URL = AUTH_URL_BASE + '/offline.html';
/**
* Origin of the gaia sign in page.
* @const
*/
var GAIA_ORIGIN = 'https://accounts.google.com';
/**
* Supported params of auth extension. For a complete list, check out the
* auth extension's main.js.
* @type {!Array<string>}
* @const
*/
var SUPPORTED_PARAMS = [
'gaiaUrl', // Gaia url to use;
'gaiaPath', // Gaia path to use without a leading slash;
'hl', // Language code for the user interface;
'email', // Pre-fill the email field in Gaia UI;
'service', // Name of Gaia service;
'continueUrl', // Continue url to use;
'frameUrl', // Initial frame URL to use. If empty defaults to gaiaUrl.
'useEafe', // Whether to use EAFE.
'clientId', // Chrome's client id.
'constrained' // Whether the extension is loaded in a constrained window;
];
/**
* Supported localized strings. For a complete list, check out the auth
* extension's offline.js
* @type {!Array<string>}
* @const
*/
var LOCALIZED_STRING_PARAMS = [
'stringSignIn',
'stringEmail',
'stringPassword',
'stringEmptyEmail',
'stringEmptyPassword',
'stringError'
];
/**
* Enum for the authorization mode, must match AuthMode defined in
* chrome/browser/ui/webui/inline_login_ui.cc.
* @enum {number}
*/
var AuthMode = {
DEFAULT: 0,
OFFLINE: 1,
DESKTOP: 2
};
/**
* Enum for the auth flow.
* @enum {number}
*/
var AuthFlow = {
GAIA: 0,
SAML: 1
};
/**
* Creates a new gaia auth extension host.
* @param {HTMLIFrameElement|string} container The iframe element or its id
* to host the auth extension.
* @constructor
* @extends {cr.EventTarget}
*/
function GaiaAuthHost(container) {
this.frame_ = typeof container == 'string' ? $(container) : container;
assert(this.frame_);
window.addEventListener('message',
this.onMessage_.bind(this), false);
}
GaiaAuthHost.prototype = {
__proto__: cr.EventTarget.prototype,
/**
* Auth extension params
* @type {Object}
*/
authParams_: {},
/**
* An url to use with {@code reload}.
* @type {?string}
* @private
*/
reloadUrl_: null,
/**
* Invoked when authentication is completed successfully with credential
* data. A credential data object looks like this:
* <pre>
* {@code
* {
* email: 'xx@gmail.com',
* password: 'xxxx', // May not present
* authCode: 'x/xx', // May not present
* authMode: 'x', // Authorization mode, default/offline/desktop.
* }
* }
* </pre>
* @type {function(Object)}
* @private
*/
successCallback_: null,
/**
* Invoked when the auth flow needs a user to confirm his/her passwords.
* This could happen when there are more than one passwords scraped during
* SAML flow. The embedder of GaiaAuthHost should show an UI to collect a
* password from user then call GaiaAuthHost.verifyConfirmedPassword to
* verify. If the password is good, the auth flow continues with success
* path. Otherwise, confirmPasswordCallback_ is invoked again.
* @type {function()}
*/
confirmPasswordCallback_: null,
/**
* Similar to confirmPasswordCallback_ but is used when there is no
* password scraped after a success authentication. The authenticated user
* account is passed to the callback. The embedder should take over the
* flow and decide what to do next.
* @type {function(string)}
*/
noPasswordCallback_: null,
/**
* Invoked when the authentication flow had to be aborted because content
* served over an unencrypted connection was detected.
*/
insecureContentBlockedCallback_: null,
/**
* Invoked to display an error message to the user when a GAIA error occurs
* during authentication.
* @type {function()}
*/
missingGaiaInfoCallback_: null,
/**
* Invoked to record that the credentials passing API was used.
* @type {function()}
*/
samlApiUsedCallback_: null,
/**
* The iframe container.
* @type {HTMLIFrameElement}
*/
get frame() {
return this.frame_;
},
/**
* Sets confirmPasswordCallback_.
* @type {function()}
*/
set confirmPasswordCallback(callback) {
this.confirmPasswordCallback_ = callback;
},
/**
* Sets noPasswordCallback_.
* @type {function()}
*/
set noPasswordCallback(callback) {
this.noPasswordCallback_ = callback;
},
/**
* Sets insecureContentBlockedCallback_.
* @type {function(string)}
*/
set insecureContentBlockedCallback(callback) {
this.insecureContentBlockedCallback_ = callback;
},
/**
* Sets missingGaiaInfoCallback_.
* @type {function()}
*/
set missingGaiaInfoCallback(callback) {
this.missingGaiaInfoCallback_ = callback;
},
/**
* Sets samlApiUsedCallback_.
* @type {function()}
*/
set samlApiUsedCallback(callback) {
this.samlApiUsedCallback_ = callback;
},
/**
* Loads the auth extension.
* @param {AuthMode} authMode Authorization mode.
* @param {Object} data Parameters for the auth extension. See the auth
* extension's main.js for all supported params and their defaults.
* @param {function(Object)} successCallback A function to be called when
* the authentication is completed successfully. The callback is
* invoked with a credential object.
*/
load: function(authMode, data, successCallback) {
var params = {};
var populateParams = function(nameList, values) {
if (!values)
return;
for (var i in nameList) {
var name = nameList[i];
if (values[name])
params[name] = values[name];
}
};
populateParams(SUPPORTED_PARAMS, data);
populateParams(LOCALIZED_STRING_PARAMS, data.localizedStrings);
params['needPassword'] = true;
var url;
switch (authMode) {
case AuthMode.OFFLINE:
url = OFFLINE_AUTH_URL;
break;
case AuthMode.DESKTOP:
url = AUTH_URL;
params['desktopMode'] = true;
break;
default:
url = AUTH_URL;
}
this.authParams_ = params;
this.reloadUrl_ = url;
this.successCallback_ = successCallback;
this.reload();
},
/**
* Reloads the auth extension.
*/
reload: function() {
this.frame_.src = this.reloadUrl_;
this.authFlow = AuthFlow.GAIA;
},
/**
* Verifies the supplied password by sending it to the auth extension,
* which will then check if it matches the scraped passwords.
* @param {string} password The confirmed password that needs verification.
*/
verifyConfirmedPassword: function(password) {
var msg = {
method: 'verifyConfirmedPassword',
password: password
};
this.frame_.contentWindow.postMessage(msg, AUTH_URL_BASE);
},
/**
* Invoked to process authentication success.
* @param {Object} credentials Credential object to pass to success
* callback.
* @private
*/
onAuthSuccess_: function(credentials) {
if (this.successCallback_)
this.successCallback_(credentials);
cr.dispatchSimpleEvent(this, 'completed');
},
/**
* Checks if message comes from the loaded authentication extension.
* @param {Object} e Payload of the received HTML5 message.
* @type {boolean}
*/
isAuthExtMessage_: function(e) {
return this.frame_.src &&
this.frame_.src.indexOf(e.origin) == 0 &&
e.source == this.frame_.contentWindow;
},
/**
* Event handler that is invoked when HTML5 message is received.
* @param {object} e Payload of the received HTML5 message.
*/
onMessage_: function(e) {
var msg = e.data;
if (!this.isAuthExtMessage_(e))
return;
if (msg.method == 'loginUIDOMContentLoaded') {
this.frame_.contentWindow.postMessage(this.authParams_, AUTH_URL_BASE);
return;
}
if (msg.method == 'loginUILoaded') {
cr.dispatchSimpleEvent(this, 'ready');
return;
}
if (/^complete(Login|Authentication)$|^offlineLogin$/.test(msg.method)) {
if (!msg.email && !this.email_ && !msg.skipForNow) {
var msg = {method: 'redirectToSignin'};
this.frame_.contentWindow.postMessage(msg, AUTH_URL_BASE);
return;
}
this.onAuthSuccess_({email: msg.email,
password: msg.password,
gaiaId: msg.gaiaId,
useOffline: msg.method == 'offlineLogin',
usingSAML: msg.usingSAML || false,
chooseWhatToSync: msg.chooseWhatToSync,
skipForNow: msg.skipForNow || false,
sessionIndex: msg.sessionIndex || ''});
return;
}
if (msg.method == 'completeAuthenticationAuthCodeOnly') {
if (!msg.authCode) {
console.error(
'GaiaAuthHost: completeAuthentication without auth code.');
var msg = {method: 'redirectToSignin'};
this.frame_.contentWindow.postMessage(msg, AUTH_URL_BASE);
return;
}
this.onAuthSuccess_({authCodeOnly: true, authCode: msg.authCode});
return;
}
if (msg.method == 'confirmPassword') {
if (this.confirmPasswordCallback_)
this.confirmPasswordCallback_(msg.email, msg.passwordCount);
else
console.error('GaiaAuthHost: Invalid confirmPasswordCallback_.');
return;
}
if (msg.method == 'noPassword') {
if (this.noPasswordCallback_)
this.noPasswordCallback_(msg.email);
else
console.error('GaiaAuthHost: Invalid noPasswordCallback_.');
return;
}
if (msg.method == 'authPageLoaded') {
this.authDomain = msg.domain;
this.authFlow = msg.isSAML ? AuthFlow.SAML : AuthFlow.GAIA;
return;
}
if (msg.method == 'resetAuthFlow') {
this.authFlow = AuthFlow.GAIA;
return;
}
if (msg.method == 'insecureContentBlocked') {
if (this.insecureContentBlockedCallback_) {
this.insecureContentBlockedCallback_(msg.url);
} else {
console.error(
'GaiaAuthHost: Invalid insecureContentBlockedCallback_.');
}
return;
}
if (msg.method == 'switchToFullTab') {
chrome.send('switchToFullTab', [msg.url]);
return;
}
if (msg.method == 'missingGaiaInfo') {
if (this.missingGaiaInfoCallback_) {
this.missingGaiaInfoCallback_();
} else {
console.error('GaiaAuthHost: Invalid missingGaiaInfoCallback_.');
}
return;
}
if (msg.method == 'samlApiUsed') {
if (this.samlApiUsedCallback_) {
this.samlApiUsedCallback_();
} else {
console.error('GaiaAuthHost: Invalid samlApiUsedCallback_.');
}
return;
}
console.error('Unknown message method=' + msg.method);
}
};
/**
* The domain name of the current auth page.
* @type {string}
*/
cr.defineProperty(GaiaAuthHost, 'authDomain');
/**
* The current auth flow of the hosted gaia_auth extension.
* @type {AuthFlow}
*/
cr.defineProperty(GaiaAuthHost, 'authFlow');
GaiaAuthHost.SUPPORTED_PARAMS = SUPPORTED_PARAMS;
GaiaAuthHost.LOCALIZED_STRING_PARAMS = LOCALIZED_STRING_PARAMS;
GaiaAuthHost.AuthMode = AuthMode;
GaiaAuthHost.AuthFlow = AuthFlow;
return {
GaiaAuthHost: GaiaAuthHost
};
});
|