Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(299)

Side by Side Diff: chrome/browser/resources/gaia_auth/main.js

Issue 130963006: Reimplement inline signin with iframe (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** 5 /**
6 * Authenticator class wraps the communications between Gaia and its host. 6 * Authenticator class wraps the communications between Gaia and its host.
7 */ 7 */
8 function Authenticator() { 8 function Authenticator() {
9 } 9 }
10 10
(...skipping 18 matching lines...) Expand all
29 Authenticator.prototype = { 29 Authenticator.prototype = {
30 email_: null, 30 email_: null,
31 password_: null, 31 password_: null,
32 attemptToken_: null, 32 attemptToken_: null,
33 33
34 // Input params from extension initialization URL. 34 // Input params from extension initialization URL.
35 inputLang_: undefined, 35 inputLang_: undefined,
36 intputEmail_: undefined, 36 intputEmail_: undefined,
37 37
38 isSAMLFlow_: false, 38 isSAMLFlow_: false,
39 samlSupportChannel_: null, 39 isSAMLEnabled_: false,
40 supportChannel_: null,
41
42 ui_initialized_: false,
xiyuan 2014/01/26 01:59:57 nit: ui_initialized_ -> uiInitialized_
guohui 2014/01/29 12:50:51 Removed tha variable since it is not really needed
40 43
41 GAIA_URL: 'https://accounts.google.com/', 44 GAIA_URL: 'https://accounts.google.com/',
42 GAIA_PAGE_PATH: 'ServiceLogin?skipvpage=true&sarp=1&rm=hide', 45 GAIA_PAGE_PATH: 'ServiceLogin?skipvpage=true&sarp=1&rm=hide',
43 PARENT_PAGE: 'chrome://oobe/', 46 PARENT_PAGE: 'chrome://oobe/',
44 SERVICE_ID: 'chromeoslogin', 47 SERVICE_ID: 'chromeoslogin',
45 CONTINUE_URL: Authenticator.THIS_EXTENSION_ORIGIN + '/success.html', 48 CONTINUE_URL: Authenticator.THIS_EXTENSION_ORIGIN + '/success.html',
46 CONSTRAINED_FLOW_SOURCE: 'chrome', 49 CONSTRAINED_FLOW_SOURCE: 'chrome',
47 50
48 initialize: function() { 51 initialize: function() {
49 var params = getUrlSearchParams(location.search); 52 var params = getUrlSearchParams(location.search);
50 this.parentPage_ = params.parentPage || this.PARENT_PAGE; 53 this.parentPage_ = params.parentPage || this.PARENT_PAGE;
51 this.gaiaUrl_ = params.gaiaUrl || this.GAIA_URL; 54 this.gaiaUrl_ = params.gaiaUrl || this.GAIA_URL;
52 this.gaiaPath_ = params.gaiaPath || this.GAIA_PAGE_PATH; 55 this.gaiaPath_ = params.gaiaPath || this.GAIA_PAGE_PATH;
53 this.inputLang_ = params.hl; 56 this.inputLang_ = params.hl;
54 this.inputEmail_ = params.email; 57 this.inputEmail_ = params.email;
55 this.service_ = params.service || this.SERVICE_ID; 58 this.service_ = params.service || this.SERVICE_ID;
56 this.continueUrl_ = params.continueUrl || this.CONTINUE_URL; 59 this.continueUrl_ = params.continueUrl || this.CONTINUE_URL;
57 this.continueUrlWithoutParams_ = stripParams(this.continueUrl_); 60 this.desktopMode_ = params.desktopMode == '1';
58 this.inlineMode_ = params.inlineMode == '1'; 61 this.isConstrainedWindow_ = params.constrained == '1';
59 this.constrained_ = params.constrained == '1';
60 this.partitionId_ = params.partitionId || '';
61 this.initialFrameUrl_ = params.frameUrl || this.constructInitialFrameUrl_(); 62 this.initialFrameUrl_ = params.frameUrl || this.constructInitialFrameUrl_();
62 this.initialFrameUrlWithoutParams_ = stripParams(this.initialFrameUrl_); 63 this.initialFrameUrlWithoutParams_ = stripParams(this.initialFrameUrl_);
63 this.loaded_ = false;
64 64
65 document.addEventListener('DOMContentLoaded', this.onPageLoad.bind(this)); 65 if (this.desktopMode_) {
66 this.supportChannel_ = new Channel();
67 this.supportChannel_.connect('authMain');
68
69 this.supportChannel_.send({
70 name: 'initDesktopFlow',
71 gaiaUrl: this.gaiaUrl_,
72 continueUrl: stripParams(this.continueUrl_),
73 isConstrainedWindow: this.isConstrainedWindow_
74 });
75
76 this.supportChannel_.registerMessage(
77 'switchToFullTab', this.switchToFullTab_.bind(this));
78 this.supportChannel_.registerMessage(
79 'completeLogin', this.completeLogin_.bind(this));
80 }
81
82 document.addEventListener('DOMContentLoaded', this.onPageLoad_.bind(this));
66 document.addEventListener('enableSAML', this.onEnableSAML_.bind(this)); 83 document.addEventListener('enableSAML', this.onEnableSAML_.bind(this));
67 }, 84 },
68 85
69 isGaiaMessage_: function(msg) { 86 isGaiaMessage_: function(msg) {
70 // Not quite right, but good enough. 87 // Not quite right, but good enough.
71 return this.gaiaUrl_.indexOf(msg.origin) == 0 || 88 return this.gaiaUrl_.indexOf(msg.origin) == 0 ||
72 this.GAIA_URL.indexOf(msg.origin) == 0; 89 this.GAIA_URL.indexOf(msg.origin) == 0;
73 }, 90 },
74 91
75 isInternalMessage_: function(msg) { 92 isInternalMessage_: function(msg) {
76 return msg.origin == Authenticator.THIS_EXTENSION_ORIGIN; 93 return msg.origin == Authenticator.THIS_EXTENSION_ORIGIN;
77 }, 94 },
78 95
79 isParentMessage_: function(msg) { 96 isParentMessage_: function(msg) {
80 return msg.origin == this.parentPage_; 97 return msg.origin == this.parentPage_;
81 }, 98 },
82 99
83 constructInitialFrameUrl_: function() { 100 constructInitialFrameUrl_: function() {
84 var url = this.gaiaUrl_ + this.gaiaPath_; 101 var url = this.gaiaUrl_ + this.gaiaPath_;
85 102
86 url = appendParam(url, 'service', this.service_); 103 url = appendParam(url, 'service', this.service_);
87 url = appendParam(url, 'continue', this.continueUrl_); 104 url = appendParam(url, 'continue', this.continueUrl_);
88 if (this.inputLang_) 105 if (this.inputLang_)
89 url = appendParam(url, 'hl', this.inputLang_); 106 url = appendParam(url, 'hl', this.inputLang_);
90 if (this.inputEmail_) 107 if (this.inputEmail_)
91 url = appendParam(url, 'Email', this.inputEmail_); 108 url = appendParam(url, 'Email', this.inputEmail_);
92 if (this.constrained_) 109 if (this.isConstrainedWindow_)
93 url = appendParam(url, 'source', this.CONSTRAINED_FLOW_SOURCE); 110 url = appendParam(url, 'source', this.CONSTRAINED_FLOW_SOURCE);
94 return url; 111 return url;
95 }, 112 },
96 113
97 /** Callback when all loads in the gaia webview is complete. */ 114 onPageLoad_: function() {
98 onWebviewLoadstop_: function(gaiaFrame) { 115 window.addEventListener('message', this.onMessage.bind(this), false);
99 if (gaiaFrame.src.lastIndexOf(this.continueUrlWithoutParams_, 0) == 0) { 116 this.loadFrame_();
100 // Detect when login is finished by the load stop event of the continue 117 },
101 // URL. Cannot reuse the login complete flow in success.html, because 118
102 // webview does not support extension pages yet. 119 loadFrame_: function() {
103 var skipForNow = false; 120 var gaiaFrame = $('gaia-frame');
104 if (this.inlineMode_ && gaiaFrame.src.indexOf('ntp=1') >= 0) { 121 gaiaFrame.src = this.initialFrameUrl_;
105 skipForNow = true; 122 if (this.desktopMode_) {
106 } 123 gaiaFrame.addEventListener('load', function() {
xiyuan 2014/01/26 01:59:57 'load' event is not a reliable. Think it could fir
guohui 2014/01/29 12:50:51 the sole purpose of this listener now is to remove
107 msg = { 124 this.ui_initialized_ || this.onLoginUILoaded_();
108 'method': 'completeLogin', 125 }.bind(this));
109 'skipForNow': skipForNow
110 };
111 window.parent.postMessage(msg, this.parentPage_);
112 // Do no report state to the parent for the continue URL, since it is a
113 // blank page.
114 return;
115 } 126 }
116
117 // Report the current state to the parent which will then update the
118 // browser history so that later it could respond properly to back/forward.
119 var msg = {
120 'method': 'reportState',
121 'src': gaiaFrame.src
122 };
123 window.parent.postMessage(msg, this.parentPage_);
124
125 if (gaiaFrame.src.lastIndexOf(this.gaiaUrl_, 0) == 0) {
126 gaiaFrame.executeScript({file: 'inline_injected.js'}, function() {
127 // Send an initial message to gaia so that it has an JavaScript
128 // reference to the embedder.
129 gaiaFrame.contentWindow.postMessage('', gaiaFrame.src);
130 });
131 if (this.constrained_) {
132 var preventContextMenu = 'document.addEventListener("contextmenu", ' +
133 'function(e) {e.preventDefault();})';
134 gaiaFrame.executeScript({code: preventContextMenu});
135 }
136 }
137
138 this.loaded_ || this.onLoginUILoaded();
139 }, 127 },
140 128
141 /** 129 /**
142 * Callback when the gaia webview attempts to open a new window. 130 * Invoked when the login UI is initialized or reset.
143 */ 131 */
144 onWebviewNewWindow_: function(gaiaFrame, e) { 132 onLoginUILoaded_: function() {
145 window.open(e.targetUrl, '_blank'); 133 var msg = {
146 e.window.discard(); 134 'method': 'loginUILoaded'
135 };
136 window.parent.postMessage(msg, this.parentPage_);
137 this.ui_initialized_ = true;
147 }, 138 },
148 139
149 onWebviewRequestCompleted_: function(details) { 140 /**
150 if (details.url.lastIndexOf(this.continueUrlWithoutParams_, 0) == 0) { 141 * Invoked when the background script sends a message to indicate that the
151 return; 142 * current content does not fit in a constrained window.
152 } 143 * @param {Object=} opt_extraMsg Optional extra info to send.
153 144 */
154 var headers = details.responseHeaders; 145 switchToFullTab_: function(msg) {
155 for (var i = 0; headers && i < headers.length; ++i) {
156 if (headers[i].name.toLowerCase() == 'google-accounts-embedded') {
157 return;
158 }
159 }
160 var msg = { 146 var msg = {
xiyuan 2014/01/26 01:59:57 |msg| is used for both function argument and local
guohui 2014/01/29 12:50:51 Done.
161 'method': 'switchToFullTab', 147 'method': 'switchToFullTab',
162 'url': details.url 148 'url': msg.url
163 }; 149 };
164 window.parent.postMessage(msg, this.parentPage_); 150 window.parent.postMessage(msg, this.parentPage_);
165 }, 151 },
166 152
167 loadFrame_: function() { 153 /**
168 var gaiaFrame = $('gaia-frame'); 154 * Invoked when the signin flow is complete.
169 gaiaFrame.partition = this.partitionId_; 155 * @param {Object=} opt_extraMsg Optional extra info to send.
170 gaiaFrame.src = this.initialFrameUrl_; 156 */
171 if (this.inlineMode_) { 157 completeLogin_: function(opt_extraMsg) {
172 gaiaFrame.addEventListener(
173 'loadstop', this.onWebviewLoadstop_.bind(this, gaiaFrame));
174 gaiaFrame.addEventListener(
175 'newwindow', this.onWebviewNewWindow_.bind(this, gaiaFrame));
176 }
177 if (this.constrained_) {
178 gaiaFrame.request.onCompleted.addListener(
179 this.onWebviewRequestCompleted_.bind(this),
180 {urls: ['<all_urls>'], types: ['main_frame']},
181 ['responseHeaders']);
182 }
183 },
184
185 completeLogin: function() {
186 var msg = { 158 var msg = {
187 'method': 'completeLogin', 159 'method': 'completeLogin',
188 'email': this.email_, 160 'email': opt_extraMsg.email || this.email_,
189 'password': this.password_, 161 'password': this.password_,
190 'usingSAML': this.isSAMLFlow_ 162 'usingSAML': this.isSAMLFlow_
163 'skipForNow': opt_extraMsg && opt_extraMsg.skipForNow,
164 'sessionIndex': opt_extraMsg && opt_extraMsg.sessionIndex
191 }; 165 };
192 window.parent.postMessage(msg, this.parentPage_); 166 window.parent.postMessage(msg, this.parentPage_);
193 if (this.samlSupportChannel_) 167 if (this.isSAMLEnabled_)
194 this.samlSupportChannel_.send({name: 'resetAuth'}); 168 this.supportChannel_.send({name: 'resetAuth'});
195 },
196
197 onPageLoad: function(e) {
198 window.addEventListener('message', this.onMessage.bind(this), false);
199 this.loadFrame_();
200 }, 169 },
201 170
202 /** 171 /**
203 * Invoked when 'enableSAML' event is received to initialize SAML support. 172 * Invoked when 'enableSAML' event is received to initialize SAML support.
204 */ 173 */
205 onEnableSAML_: function() { 174 onEnableSAML_: function() {
175 this.isSAMLEnabled_ = true;
206 this.isSAMLFlow_ = false; 176 this.isSAMLFlow_ = false;
207 177
208 this.samlSupportChannel_ = new Channel(); 178 if (!this.supportChannel_) {
209 this.samlSupportChannel_.connect('authMain'); 179 this.supportChannel_ = new Channel();
210 this.samlSupportChannel_.registerMessage( 180 this.supportChannel_.connect('authMain');
181 }
182
183 this.supportChannel_.registerMessage(
211 'onAuthPageLoaded', this.onAuthPageLoaded_.bind(this)); 184 'onAuthPageLoaded', this.onAuthPageLoaded_.bind(this));
212 this.samlSupportChannel_.registerMessage( 185 this.supportChannel_.registerMessage(
213 'apiCall', this.onAPICall_.bind(this)); 186 'apiCall', this.onAPICall_.bind(this));
214 this.samlSupportChannel_.send({ 187 this.supportChannel_.send({
215 name: 'setGaiaUrl', 188 name: 'setGaiaUrl',
216 gaiaUrl: this.gaiaUrl_ 189 gaiaUrl: this.gaiaUrl_
217 }); 190 });
218 }, 191 },
219 192
220 /** 193 /**
221 * Invoked when the background page sends 'onHostedPageLoaded' message. 194 * Invoked when the background page sends 'onHostedPageLoaded' message.
222 * @param {!Object} msg Details sent with the message. 195 * @param {!Object} msg Details sent with the message.
223 */ 196 */
224 onAuthPageLoaded_: function(msg) { 197 onAuthPageLoaded_: function(msg) {
(...skipping 27 matching lines...) Expand all
252 this.email_ = call.user; 225 this.email_ = call.user;
253 this.password_ = call.password; 226 this.password_ = call.password;
254 } else if (call.method == 'confirm') { 227 } else if (call.method == 'confirm') {
255 if (call.token != this.apiToken_) 228 if (call.token != this.apiToken_)
256 console.error('Authenticator.onAPICall_: token mismatch'); 229 console.error('Authenticator.onAPICall_: token mismatch');
257 } else { 230 } else {
258 console.error('Authenticator.onAPICall_: unknown message'); 231 console.error('Authenticator.onAPICall_: unknown message');
259 } 232 }
260 }, 233 },
261 234
262 onLoginUILoaded: function() {
263 var msg = {
264 'method': 'loginUILoaded'
265 };
266 window.parent.postMessage(msg, this.parentPage_);
267 if (this.inlineMode_) {
268 // TODO(guohui): temporary workaround until webview team fixes the focus
269 // on their side.
270 var gaiaFrame = $('gaia-frame');
271 gaiaFrame.focus();
272 gaiaFrame.onblur = function() {
273 gaiaFrame.focus();
274 };
275 }
276 this.loaded_ = true;
277 },
278
279 onConfirmLogin_: function() { 235 onConfirmLogin_: function() {
280 if (!this.isSAMLFlow_) { 236 if (!this.isSAMLFlow_) {
281 this.completeLogin(); 237 this.completeLogin_();
282 return; 238 return;
283 } 239 }
284 240
285 // Retrieve the e-mail address of the user who just authenticated from GAIA. 241 // Retrieve the e-mail address of the user who just authenticated from GAIA.
286 window.parent.postMessage({method: 'retrieveAuthenticatedUserEmail', 242 window.parent.postMessage({method: 'retrieveAuthenticatedUserEmail',
287 attemptToken: this.attemptToken_}, 243 attemptToken: this.attemptToken_},
288 this.parentPage_); 244 this.parentPage_);
289 245
290 if (!this.password_) { 246 if (!this.password_) {
291 this.samlSupportChannel_.sendWithCallback( 247 this.supportChannel_.sendWithCallback(
292 {name: 'getScrapedPasswords'}, 248 {name: 'getScrapedPasswords'},
293 function(passwords) { 249 function(passwords) {
294 if (passwords.length == 0) { 250 if (passwords.length == 0) {
295 window.parent.postMessage( 251 window.parent.postMessage(
296 {method: 'noPassword', email: this.email_}, 252 {method: 'noPassword', email: this.email_},
297 this.parentPage_); 253 this.parentPage_);
298 } else { 254 } else {
299 window.parent.postMessage( 255 window.parent.postMessage(
300 {method: 'confirmPassword', email: this.email_}, 256 {method: 'confirmPassword', email: this.email_},
301 this.parentPage_); 257 this.parentPage_);
302 } 258 }
303 }.bind(this)); 259 }.bind(this));
304 } 260 }
305 }, 261 },
306 262
307 maybeCompleteSAMLLogin_: function() { 263 maybeCompleteSAMLLogin_: function() {
308 // SAML login is complete when the user's e-mail address has been retrieved 264 // SAML login is complete when the user's e-mail address has been retrieved
309 // from GAIA and the user has successfully confirmed the password. 265 // from GAIA and the user has successfully confirmed the password.
310 if (this.email_ !== null && this.password_ !== null) 266 if (this.email_ !== null && this.password_ !== null)
311 this.completeLogin(); 267 this.completeLogin_();
312 }, 268 },
313 269
314 onVerifyConfirmedPassword_: function(password) { 270 onVerifyConfirmedPassword_: function(password) {
315 this.samlSupportChannel_.sendWithCallback( 271 this.supportChannel_.sendWithCallback(
316 {name: 'getScrapedPasswords'}, 272 {name: 'getScrapedPasswords'},
317 function(passwords) { 273 function(passwords) {
318 for (var i = 0; i < passwords.length; ++i) { 274 for (var i = 0; i < passwords.length; ++i) {
319 if (passwords[i] == password) { 275 if (passwords[i] == password) {
320 this.password_ = passwords[i]; 276 this.password_ = passwords[i];
321 this.maybeCompleteSAMLLogin_(); 277 this.maybeCompleteSAMLLogin_();
322 return; 278 return;
323 } 279 }
324 } 280 }
325 window.parent.postMessage( 281 window.parent.postMessage(
326 {method: 'confirmPassword', email: this.email_}, 282 {method: 'confirmPassword', email: this.email_},
327 this.parentPage_); 283 this.parentPage_);
328 }.bind(this)); 284 }.bind(this));
329 }, 285 },
330 286
331 onMessage: function(e) { 287 onMessage: function(e) {
332 var msg = e.data; 288 var msg = e.data;
333 if (msg.method == 'attemptLogin' && this.isGaiaMessage_(e)) { 289 if (msg.method == 'attemptLogin' && this.isGaiaMessage_(e)) {
334 this.email_ = msg.email; 290 this.email_ = msg.email;
335 this.password_ = msg.password; 291 this.password_ = msg.password;
336 this.attemptToken_ = msg.attemptToken; 292 this.attemptToken_ = msg.attemptToken;
293 this.chooseWhatToSync_ = msg.chooseWhatToSync;
337 this.isSAMLFlow_ = false; 294 this.isSAMLFlow_ = false;
338 if (this.samlSupportChannel_) 295 if (this.isSAMLEnabled_)
339 this.samlSupportChannel_.send({name: 'startAuth'}); 296 this.supportChannel_.send({name: 'startAuth'});
340 } else if (msg.method == 'clearOldAttempts' && this.isGaiaMessage_(e)) { 297 } else if (msg.method == 'clearOldAttempts' && this.isGaiaMessage_(e)) {
341 this.email_ = null; 298 this.email_ = null;
342 this.password_ = null; 299 this.password_ = null;
343 this.attemptToken_ = null; 300 this.attemptToken_ = null;
344 this.isSAMLFlow_ = false; 301 this.isSAMLFlow_ = false;
345 this.onLoginUILoaded(); 302 this.onLoginUILoaded_();
346 if (this.samlSupportChannel_) 303 if (this.isSAMLEnabled_)
347 this.samlSupportChannel_.send({name: 'resetAuth'}); 304 this.supportChannel_.send({name: 'resetAuth'});
348 } else if (msg.method == 'setAuthenticatedUserEmail' && 305 } else if (msg.method == 'setAuthenticatedUserEmail' &&
349 this.isParentMessage_(e)) { 306 this.isParentMessage_(e)) {
350 if (this.attemptToken_ == msg.attemptToken) { 307 if (this.attemptToken_ == msg.attemptToken) {
351 this.email_ = msg.email; 308 this.email_ = msg.email;
352 this.maybeCompleteSAMLLogin_(); 309 this.maybeCompleteSAMLLogin_();
353 } 310 }
354 } else if (msg.method == 'confirmLogin' && this.isInternalMessage_(e)) { 311 } else if (msg.method == 'confirmLogin' && this.isInternalMessage_(e)) {
355 if (this.attemptToken_ == msg.attemptToken) 312 if (this.attemptToken_ == msg.attemptToken)
356 this.onConfirmLogin_(); 313 this.onConfirmLogin_();
357 else 314 else
358 console.error('Authenticator.onMessage: unexpected attemptToken!?'); 315 console.error('Authenticator.onMessage: unexpected attemptToken!?');
359 } else if (msg.method == 'verifyConfirmedPassword' && 316 } else if (msg.method == 'verifyConfirmedPassword' &&
360 this.isParentMessage_(e)) { 317 this.isParentMessage_(e)) {
361 this.onVerifyConfirmedPassword_(msg.password); 318 this.onVerifyConfirmedPassword_(msg.password);
362 } else if (msg.method == 'navigate' && 319 } else if (msg.method == 'navigate' &&
363 this.isParentMessage_(e)) { 320 this.isParentMessage_(e)) {
364 $('gaia-frame').src = msg.src; 321 $('gaia-frame').src = msg.src;
365 } else if (msg.method == 'redirectToSignin' && 322 } else if (msg.method == 'redirectToSignin' &&
366 this.isParentMessage_(e)) { 323 this.isParentMessage_(e)) {
367 $('gaia-frame').src = this.constructInitialFrameUrl_(); 324 $('gaia-frame').src = this.constructInitialFrameUrl_();
368 } else { 325 } else {
369 console.error('Authenticator.onMessage: unknown message + origin!?'); 326 console.error('Authenticator.onMessage: unknown message + origin!?');
370 } 327 }
371 } 328 }
372 }; 329 };
373 330
374 Authenticator.getInstance().initialize(); 331 Authenticator.getInstance().initialize();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698