| OLD | NEW |
| 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 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 }); | 207 }); |
| 208 }, | 208 }, |
| 209 | 209 |
| 210 /** | 210 /** |
| 211 * Invoked when the background page sends 'onHostedPageLoaded' message. | 211 * Invoked when the background page sends 'onHostedPageLoaded' message. |
| 212 * @param {!Object} msg Details sent with the message. | 212 * @param {!Object} msg Details sent with the message. |
| 213 */ | 213 */ |
| 214 onAuthPageLoaded_: function(msg) { | 214 onAuthPageLoaded_: function(msg) { |
| 215 var isSAMLPage = msg.url.indexOf(this.gaiaUrl_) != 0; | 215 var isSAMLPage = msg.url.indexOf(this.gaiaUrl_) != 0; |
| 216 | 216 |
| 217 // Set isSAMLFlow_ flag when a SAML page is loaded. The flag is sticky. | 217 if (isSAMLPage && !this.isSAMLFlow_) { |
| 218 if (isSAMLPage) | 218 // GAIA redirected to a SAML login page. The credentials provided to this |
| 219 // page will determine what user gets logged in. The credentials obtained |
| 220 // from the GAIA login from are no longer relevant and can be discarded. |
| 219 this.isSAMLFlow_ = true; | 221 this.isSAMLFlow_ = true; |
| 222 this.email_ = null; |
| 223 this.password_ = null; |
| 224 } |
| 220 | 225 |
| 221 window.parent.postMessage({ | 226 window.parent.postMessage({ |
| 222 'method': 'authPageLoaded', | 227 'method': 'authPageLoaded', |
| 223 'isSAML': this.isSAMLFlow_, | 228 'isSAML': this.isSAMLFlow_, |
| 224 'domain': extractDomain(msg.url) | 229 'domain': extractDomain(msg.url) |
| 225 }, this.parentPage_); | 230 }, this.parentPage_); |
| 226 }, | 231 }, |
| 227 | 232 |
| 228 onLoginUILoaded: function() { | 233 onLoginUILoaded: function() { |
| 229 var msg = { | 234 var msg = { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 241 } | 246 } |
| 242 this.loaded_ = true; | 247 this.loaded_ = true; |
| 243 }, | 248 }, |
| 244 | 249 |
| 245 onConfirmLogin_: function() { | 250 onConfirmLogin_: function() { |
| 246 if (!this.isSAMLFlow_) { | 251 if (!this.isSAMLFlow_) { |
| 247 this.completeLogin(this.email_, this.password_); | 252 this.completeLogin(this.email_, this.password_); |
| 248 return; | 253 return; |
| 249 } | 254 } |
| 250 | 255 |
| 256 // Retrieve the e-mail address of the user who just authenticated from GAIA. |
| 257 window.parent.postMessage({method: 'retrieveAuthenticatedUserEmail', |
| 258 attemptToken: this.attemptToken_}, |
| 259 this.parentPage_); |
| 260 |
| 251 this.samlSupportChannel_.sendWithCallback( | 261 this.samlSupportChannel_.sendWithCallback( |
| 252 {name: 'getScrapedPasswords'}, | 262 {name: 'getScrapedPasswords'}, |
| 253 function(passwords) { | 263 function(passwords) { |
| 254 if (passwords.length == 0) { | 264 if (passwords.length == 0) { |
| 255 window.parent.postMessage( | 265 window.parent.postMessage( |
| 256 {method: 'noPassword', email: this.email_}, | 266 {method: 'noPassword', email: this.email_}, |
| 257 this.parentPage_); | 267 this.parentPage_); |
| 258 } else { | 268 } else { |
| 259 window.parent.postMessage( | 269 window.parent.postMessage( |
| 260 {method: 'confirmPassword', email: this.email_}, | 270 {method: 'confirmPassword', email: this.email_}, |
| 261 this.parentPage_); | 271 this.parentPage_); |
| 262 } | 272 } |
| 263 }.bind(this)); | 273 }.bind(this)); |
| 264 }, | 274 }, |
| 265 | 275 |
| 276 maybeCompleteSAMLLogin_: function() { |
| 277 // SAML login is complete when the user's e-mail address has been retrieved |
| 278 // from GAIA and the user has successfully confirmed the password. |
| 279 if (this.email_ !== null && this.password_ !== null) |
| 280 this.completeLogin(this.email_, this.password_); |
| 281 }, |
| 282 |
| 266 onVerifyConfirmedPassword_: function(password) { | 283 onVerifyConfirmedPassword_: function(password) { |
| 267 this.samlSupportChannel_.sendWithCallback( | 284 this.samlSupportChannel_.sendWithCallback( |
| 268 {name: 'getScrapedPasswords'}, | 285 {name: 'getScrapedPasswords'}, |
| 269 function(passwords) { | 286 function(passwords) { |
| 270 for (var i = 0; i < passwords.length; ++i) { | 287 for (var i = 0; i < passwords.length; ++i) { |
| 271 if (passwords[i] == password) { | 288 if (passwords[i] == password) { |
| 272 this.completeLogin(this.email_, passwords[i]); | 289 this.password_ = passwords[i]; |
| 290 this.maybeCompleteSAMLLogin_(); |
| 273 return; | 291 return; |
| 274 } | 292 } |
| 275 } | 293 } |
| 276 window.parent.postMessage( | 294 window.parent.postMessage( |
| 277 {method: 'confirmPassword', email: this.email_}, | 295 {method: 'confirmPassword', email: this.email_}, |
| 278 this.parentPage_); | 296 this.parentPage_); |
| 279 }.bind(this)); | 297 }.bind(this)); |
| 280 }, | 298 }, |
| 281 | 299 |
| 282 onMessage: function(e) { | 300 onMessage: function(e) { |
| 283 var msg = e.data; | 301 var msg = e.data; |
| 284 if (msg.method == 'attemptLogin' && this.isGaiaMessage_(e)) { | 302 if (msg.method == 'attemptLogin' && this.isGaiaMessage_(e)) { |
| 285 this.email_ = msg.email; | 303 this.email_ = msg.email; |
| 286 this.password_ = msg.password; | 304 this.password_ = msg.password; |
| 287 this.attemptToken_ = msg.attemptToken; | 305 this.attemptToken_ = msg.attemptToken; |
| 288 this.isSAMLFlow_ = false; | 306 this.isSAMLFlow_ = false; |
| 289 if (this.samlSupportChannel_) | 307 if (this.samlSupportChannel_) |
| 290 this.samlSupportChannel_.send({name: 'startAuth'}); | 308 this.samlSupportChannel_.send({name: 'startAuth'}); |
| 291 } else if (msg.method == 'clearOldAttempts' && this.isGaiaMessage_(e)) { | 309 } else if (msg.method == 'clearOldAttempts' && this.isGaiaMessage_(e)) { |
| 292 this.email_ = null; | 310 this.email_ = null; |
| 293 this.password_ = null; | 311 this.password_ = null; |
| 294 this.attemptToken_ = null; | 312 this.attemptToken_ = null; |
| 295 this.isSAMLFlow_ = false; | 313 this.isSAMLFlow_ = false; |
| 296 this.onLoginUILoaded(); | 314 this.onLoginUILoaded(); |
| 297 if (this.samlSupportChannel_) | 315 if (this.samlSupportChannel_) |
| 298 this.samlSupportChannel_.send({name: 'resetAuth'}); | 316 this.samlSupportChannel_.send({name: 'resetAuth'}); |
| 317 } else if (msg.method == 'setAuthenticatedUserEmail' && |
| 318 this.isParentMessage_(e)) { |
| 319 if (this.attemptToken_ == msg.attemptToken) { |
| 320 this.email_ = msg.email; |
| 321 this.maybeCompleteSAMLLogin_(); |
| 322 } |
| 299 } else if (msg.method == 'confirmLogin' && this.isInternalMessage_(e)) { | 323 } else if (msg.method == 'confirmLogin' && this.isInternalMessage_(e)) { |
| 300 if (this.attemptToken_ == msg.attemptToken) | 324 if (this.attemptToken_ == msg.attemptToken) |
| 301 this.onConfirmLogin_(); | 325 this.onConfirmLogin_(); |
| 302 else | 326 else |
| 303 console.error('Authenticator.onMessage: unexpected attemptToken!?'); | 327 console.error('Authenticator.onMessage: unexpected attemptToken!?'); |
| 304 } else if (msg.method == 'verifyConfirmedPassword' && | 328 } else if (msg.method == 'verifyConfirmedPassword' && |
| 305 this.isParentMessage_(e)) { | 329 this.isParentMessage_(e)) { |
| 306 this.onVerifyConfirmedPassword_(msg.password); | 330 this.onVerifyConfirmedPassword_(msg.password); |
| 307 } else if (msg.method == 'navigate' && | 331 } else if (msg.method == 'navigate' && |
| 308 this.isParentMessage_(e)) { | 332 this.isParentMessage_(e)) { |
| 309 $('gaia-frame').src = msg.src; | 333 $('gaia-frame').src = msg.src; |
| 310 } else if (msg.method == 'redirectToSignin' && | 334 } else if (msg.method == 'redirectToSignin' && |
| 311 this.isParentMessage_(e)) { | 335 this.isParentMessage_(e)) { |
| 312 $('gaia-frame').src = this.constructInitialFrameUrl_(); | 336 $('gaia-frame').src = this.constructInitialFrameUrl_(); |
| 313 } else { | 337 } else { |
| 314 console.error('Authenticator.onMessage: unknown message + origin!?'); | 338 console.error('Authenticator.onMessage: unknown message + origin!?'); |
| 315 } | 339 } |
| 316 } | 340 } |
| 317 }; | 341 }; |
| 318 | 342 |
| 319 Authenticator.getInstance().initialize(); | 343 Authenticator.getInstance().initialize(); |
| OLD | NEW |