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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
|
// Copyright (c) 2012 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.
/**
* Authenticator class wraps the communications between Gaia and its host.
*/
function Authenticator() {
}
/**
* Gaia auth extension url origin.
* @type {string}
*/
Authenticator.THIS_EXTENSION_ORIGIN =
'chrome-extension://mfffpogegjflfpflabcdkioaeobkgjik';
/**
* The lowest version of the credentials passing API supported.
* @type {number}
*/
Authenticator.MIN_API_VERSION_VERSION = 1;
/**
* The highest version of the credentials passing API supported.
* @type {number}
*/
Authenticator.MAX_API_VERSION_VERSION = 1;
/**
* The key types supported by the credentials passing API.
* @type {Array} Array of strings.
*/
Authenticator.API_KEY_TYPES = [
'KEY_TYPE_PASSWORD_PLAIN',
];
/**
* Allowed origins of the hosting page.
* @type {Array<string>}
*/
Authenticator.ALLOWED_PARENT_ORIGINS = [
'chrome://oobe',
'chrome://chrome-signin'
];
/**
* Singleton getter of Authenticator.
* @return {Object} The singleton instance of Authenticator.
*/
Authenticator.getInstance = function() {
if (!Authenticator.instance_) {
Authenticator.instance_ = new Authenticator();
}
return Authenticator.instance_;
};
Authenticator.prototype = {
email_: null,
gaiaId_: null,
// Depending on the key type chosen, this will contain the plain text password
// or a credential derived from it along with the information required to
// repeat the derivation, such as a salt. The information will be encoded so
// that it contains printable ASCII characters only. The exact encoding is TBD
// when support for key types other than plain text password is added.
passwordBytes_: null,
needPassword_: false,
chooseWhatToSync_: false,
skipForNow_: false,
sessionIndex_: null,
attemptToken_: null,
// Input params from extension initialization URL.
inputLang_: undefined,
intputEmail_: undefined,
isSAMLFlow_: false,
gaiaLoaded_: false,
supportChannel_: null,
GAIA_URL: 'https://accounts.google.com/',
GAIA_PAGE_PATH: 'ServiceLogin?skipvpage=true&sarp=1&rm=hide',
SERVICE_ID: 'chromeoslogin',
CONTINUE_URL: Authenticator.THIS_EXTENSION_ORIGIN + '/success.html',
CONSTRAINED_FLOW_SOURCE: 'chrome',
initialize: function() {
var handleInitializeMessage = function(e) {
if (Authenticator.ALLOWED_PARENT_ORIGINS.indexOf(e.origin) == -1) {
console.error('Unexpected parent message, origin=' + e.origin);
return;
}
window.removeEventListener('message', handleInitializeMessage);
var params = e.data;
params.parentPage = e.origin;
this.initializeFromParent_(params);
this.onPageLoad_();
}.bind(this);
document.addEventListener('DOMContentLoaded', function() {
window.addEventListener('message', handleInitializeMessage);
});
},
initializeFromParent_: function(params) {
this.parentPage_ = params.parentPage;
this.gaiaUrl_ = params.gaiaUrl || this.GAIA_URL;
this.gaiaPath_ = params.gaiaPath || this.GAIA_PAGE_PATH;
this.inputLang_ = params.hl;
this.inputEmail_ = params.email;
this.service_ = params.service || this.SERVICE_ID;
this.continueUrl_ = params.continueUrl || this.CONTINUE_URL;
this.desktopMode_ = params.desktopMode == '1';
this.isConstrainedWindow_ = params.constrained == '1';
this.initialFrameUrl_ = params.frameUrl || this.constructInitialFrameUrl_();
this.initialFrameUrlWithoutParams_ = stripParams(this.initialFrameUrl_);
this.needPassword_ = params.needPassword == '1';
// For CrOS 'ServiceLogin' we assume that Gaia is loaded if we recieved
// 'clearOldAttempts' message. For other scenarios Gaia doesn't send this
// message so we have to rely on 'load' event.
// TODO(dzhioev): Do not rely on 'load' event after b/16313327 is fixed.
this.assumeLoadedOnLoadEvent_ =
this.gaiaPath_.indexOf('ServiceLogin') !== 0 ||
this.service_ !== 'chromeoslogin';
},
isGaiaMessage_: function(msg) {
// Not quite right, but good enough.
return this.gaiaUrl_.indexOf(msg.origin) == 0 ||
this.GAIA_URL.indexOf(msg.origin) == 0;
},
isParentMessage_: function(msg) {
return msg.origin == this.parentPage_;
},
constructInitialFrameUrl_: function() {
var url = this.gaiaUrl_ + this.gaiaPath_;
url = appendParam(url, 'service', this.service_);
url = appendParam(url, 'continue', this.continueUrl_);
if (this.inputLang_)
url = appendParam(url, 'hl', this.inputLang_);
if (this.inputEmail_)
url = appendParam(url, 'Email', this.inputEmail_);
if (this.isConstrainedWindow_)
url = appendParam(url, 'source', this.CONSTRAINED_FLOW_SOURCE);
return url;
},
onPageLoad_: function() {
window.addEventListener('message', this.onMessage.bind(this), false);
this.initSupportChannel_();
if (this.assumeLoadedOnLoadEvent_) {
var gaiaFrame = $('gaia-frame');
var handler = function() {
gaiaFrame.removeEventListener('load', handler);
if (!this.gaiaLoaded_) {
this.gaiaLoaded_ = true;
this.maybeInitialized_();
}
}.bind(this);
gaiaFrame.addEventListener('load', handler);
}
},
initSupportChannel_: function() {
var supportChannel = new Channel();
supportChannel.connect('authMain');
supportChannel.registerMessage('channelConnected', function() {
// Load the gaia frame after the background page indicates that it is
// ready, so that the webRequest handlers are all setup first.
var gaiaFrame = $('gaia-frame');
gaiaFrame.src = this.initialFrameUrl_;
if (this.supportChannel_) {
console.error('Support channel is already initialized.');
return;
}
this.supportChannel_ = supportChannel;
if (this.desktopMode_) {
this.supportChannel_.send({
name: 'initDesktopFlow',
gaiaUrl: this.gaiaUrl_,
continueUrl: stripParams(this.continueUrl_),
isConstrainedWindow: this.isConstrainedWindow_,
initialFrameUrlWithoutParams: this.initialFrameUrlWithoutParams_
});
this.supportChannel_.registerMessage(
'switchToFullTab', this.switchToFullTab_.bind(this));
}
this.supportChannel_.registerMessage(
'completeLogin', this.onCompleteLogin_.bind(this));
this.initSAML_();
this.supportChannel_.send({name: 'resetAuth'});
this.maybeInitialized_();
}.bind(this));
window.setTimeout(function() {
if (!this.supportChannel_) {
// Re-initialize the channel if it is not connected properly, e.g.
// connect may be called before background script started running.
this.initSupportChannel_();
}
}.bind(this), 200);
},
/**
* Called when one of the initialization stages has finished. If all the
* needed parts are initialized, notifies parent about successfull
* initialization.
*/
maybeInitialized_: function() {
if (!this.gaiaLoaded_ || !this.supportChannel_)
return;
var msg = {
'method': 'loginUILoaded'
};
window.parent.postMessage(msg, this.parentPage_);
},
/**
* Invoked when the background script sends a message to indicate that the
* current content does not fit in a constrained window.
* @param {Object=} msg Extra info to send.
*/
switchToFullTab_: function(msg) {
var parentMsg = {
'method': 'switchToFullTab',
'url': msg.url
};
window.parent.postMessage(parentMsg, this.parentPage_);
},
/**
* Invoked when the signin flow is complete.
* @param {Object=} opt_extraMsg Optional extra info to send.
*/
completeLogin_: function(opt_extraMsg) {
var msg = {
'method': 'completeLogin',
'email': (opt_extraMsg && opt_extraMsg.email) || this.email_,
'password': (opt_extraMsg && opt_extraMsg.password) ||
this.passwordBytes_,
'usingSAML': this.isSAMLFlow_,
'chooseWhatToSync': this.chooseWhatToSync_ || false,
'skipForNow': (opt_extraMsg && opt_extraMsg.skipForNow) ||
this.skipForNow_,
'sessionIndex': (opt_extraMsg && opt_extraMsg.sessionIndex) ||
this.sessionIndex_,
'gaiaId': (opt_extraMsg && opt_extraMsg.gaiaId) || this.gaiaId_
};
window.parent.postMessage(msg, this.parentPage_);
this.supportChannel_.send({name: 'resetAuth'});
},
/**
* Invoked when support channel is connected.
*/
initSAML_: function() {
this.isSAMLFlow_ = false;
this.supportChannel_.registerMessage(
'onAuthPageLoaded', this.onAuthPageLoaded_.bind(this));
this.supportChannel_.registerMessage(
'onInsecureContentBlocked', this.onInsecureContentBlocked_.bind(this));
this.supportChannel_.registerMessage(
'apiCall', this.onAPICall_.bind(this));
this.supportChannel_.send({
name: 'setGaiaUrl',
gaiaUrl: this.gaiaUrl_
});
if (!this.desktopMode_ && this.gaiaUrl_.indexOf('https://') == 0) {
// Abort the login flow when content served over an unencrypted connection
// is detected on Chrome OS. This does not apply to tests that explicitly
// set a non-https GAIA URL and want to perform all authentication over
// http.
this.supportChannel_.send({
name: 'setBlockInsecureContent',
blockInsecureContent: true
});
}
},
/**
* Invoked when the background page sends 'onHostedPageLoaded' message.
* @param {!Object} msg Details sent with the message.
*/
onAuthPageLoaded_: function(msg) {
if (msg.isSAMLPage && !this.isSAMLFlow_) {
// GAIA redirected to a SAML login page. The credentials provided to this
// page will determine what user gets logged in. The credentials obtained
// from the GAIA login form are no longer relevant and can be discarded.
this.isSAMLFlow_ = true;
this.email_ = null;
this.gaiaId_ = null;
this.passwordBytes_ = null;
}
window.parent.postMessage({
'method': 'authPageLoaded',
'isSAML': this.isSAMLFlow_,
'domain': extractDomain(msg.url)
}, this.parentPage_);
},
/**
* Invoked when the background page sends an 'onInsecureContentBlocked'
* message.
* @param {!Object} msg Details sent with the message.
*/
onInsecureContentBlocked_: function(msg) {
window.parent.postMessage({
'method': 'insecureContentBlocked',
'url': stripParams(msg.url)
}, this.parentPage_);
},
/**
* Invoked when one of the credential passing API methods is called by a SAML
* provider.
* @param {!Object} msg Details of the API call.
*/
onAPICall_: function(msg) {
var call = msg.call;
if (call.method == 'initialize') {
if (!Number.isInteger(call.requestedVersion) ||
call.requestedVersion < Authenticator.MIN_API_VERSION_VERSION) {
this.sendInitializationFailure_();
return;
}
this.apiVersion_ = Math.min(call.requestedVersion,
Authenticator.MAX_API_VERSION_VERSION);
this.initialized_ = true;
this.sendInitializationSuccess_();
return;
}
if (call.method == 'add') {
if (Authenticator.API_KEY_TYPES.indexOf(call.keyType) == -1) {
console.error('Authenticator.onAPICall_: unsupported key type');
return;
}
// Not setting |email_| and |gaiaId_| because this API call will
// eventually be followed by onCompleteLogin_() which does set it.
this.apiToken_ = call.token;
this.passwordBytes_ = call.passwordBytes;
} else if (call.method == 'confirm') {
if (call.token != this.apiToken_)
console.error('Authenticator.onAPICall_: token mismatch');
} else {
console.error('Authenticator.onAPICall_: unknown message');
}
},
sendInitializationSuccess_: function() {
this.supportChannel_.send({name: 'apiResponse', response: {
result: 'initialized',
version: this.apiVersion_,
keyTypes: Authenticator.API_KEY_TYPES
}});
},
sendInitializationFailure_: function() {
this.supportChannel_.send({
name: 'apiResponse',
response: {result: 'initialization_failed'}
});
},
/**
* Callback invoked for 'completeLogin' message.
* @param {Object=} msg Message sent from background page.
*/
onCompleteLogin_: function(msg) {
if (!msg.email || !msg.gaiaId || !msg.sessionIndex) {
// On desktop, if the skipForNow message field is set, send it to handler.
// This does not require the email, gaiaid or session to be valid.
if (this.desktopMode_ && msg.skipForNow) {
this.completeLogin_(msg);
} else {
console.error('Missing fields to complete login.');
window.parent.postMessage({method: 'missingGaiaInfo'},
this.parentPage_);
return;
}
}
// Skip SAML extra steps for desktop flow and non-SAML flow.
if (!this.isSAMLFlow_ || this.desktopMode_) {
this.completeLogin_(msg);
return;
}
this.email_ = msg.email;
this.gaiaId_ = msg.gaiaId;
// Password from |msg| is not used because ChromeOS SAML flow
// gets password by asking user to confirm.
this.skipForNow_ = msg.skipForNow;
this.sessionIndex_ = msg.sessionIndex;
if (this.passwordBytes_) {
// If the credentials passing API was used, login is complete.
window.parent.postMessage({method: 'samlApiUsed'}, this.parentPage_);
this.completeLogin_(msg);
} else if (!this.needPassword_) {
// If the credentials passing API was not used, the password was obtained
// by scraping. It must be verified before use. However, the host may not
// be interested in the password at all. In that case, verification is
// unnecessary and login is complete.
this.completeLogin_(msg);
} else {
this.supportChannel_.sendWithCallback(
{name: 'getScrapedPasswords'},
function(passwords) {
if (passwords.length == 0) {
window.parent.postMessage(
{method: 'noPassword', email: this.email_},
this.parentPage_);
} else {
window.parent.postMessage({method: 'confirmPassword',
email: this.email_,
passwordCount: passwords.length},
this.parentPage_);
}
}.bind(this));
}
},
onVerifyConfirmedPassword_: function(password) {
this.supportChannel_.sendWithCallback(
{name: 'getScrapedPasswords'},
function(passwords) {
for (var i = 0; i < passwords.length; ++i) {
if (passwords[i] == password) {
this.passwordBytes_ = passwords[i];
// SAML login is complete when the user has successfully
// confirmed the password.
if (this.passwordBytes_ !== null)
this.completeLogin_();
return;
}
}
window.parent.postMessage(
{method: 'confirmPassword', email: this.email_},
this.parentPage_);
}.bind(this));
},
onMessage: function(e) {
var msg = e.data;
if (msg.method == 'attemptLogin' && this.isGaiaMessage_(e)) {
// At this point GAIA does not yet know the gaiaId, so its not set here.
this.email_ = msg.email;
this.passwordBytes_ = msg.password;
this.attemptToken_ = msg.attemptToken;
this.chooseWhatToSync_ = msg.chooseWhatToSync;
this.isSAMLFlow_ = false;
if (this.supportChannel_)
this.supportChannel_.send({name: 'startAuth'});
else
console.error('Support channel is not initialized.');
} else if (msg.method == 'clearOldAttempts' && this.isGaiaMessage_(e)) {
if (!this.gaiaLoaded_) {
this.gaiaLoaded_ = true;
this.maybeInitialized_();
}
this.email_ = null;
this.gaiaId_ = null;
this.sessionIndex_ = false;
this.passwordBytes_ = null;
this.attemptToken_ = null;
this.isSAMLFlow_ = false;
this.skipForNow_ = false;
this.chooseWhatToSync_ = false;
if (this.supportChannel_) {
this.supportChannel_.send({name: 'resetAuth'});
// This message is for clearing saml properties in gaia_auth_host and
// oobe_screen_oauth_enrollment.
window.parent.postMessage({
'method': 'resetAuthFlow',
}, this.parentPage_);
}
} else if (msg.method == 'verifyConfirmedPassword' &&
this.isParentMessage_(e)) {
this.onVerifyConfirmedPassword_(msg.password);
} else if (msg.method == 'redirectToSignin' &&
this.isParentMessage_(e)) {
$('gaia-frame').src = this.constructInitialFrameUrl_();
} else {
console.error('Authenticator.onMessage: unknown message + origin!?');
}
}
};
Authenticator.getInstance().initialize();
|