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. 4 5 function Authenticator() { 6 } 7 8 /** 9 * Singleton getter of Authenticator. 10 * @return {Object} The singleton instance of Authenticator. 11 */ 12 Authenticator.getInstance = function() { 13 if (!Authenticator.instance_) { 14 Authenticator.instance_ = new Authenticator(); 15 } 16 return Authenticator.instance_; 17 }; 18 19 Authenticator.prototype = { 20 email_: null, 21 password_: null, 22 attemptToken_: null, 23 24 // Input params from extension initialization URL. 25 inputLang_: undefined, 26 intputEmail_: undefined, 27 28 GAIA_URL: 'https://accounts.google.com/', 29 GAIA_PAGE_PATH: 'ServiceLogin?service=chromeoslogin' + 30 '&skipvpage=true&sarp=1&rm=hide' + 31 '&continue=chrome-extension://mfffpogegjflfpflabcdkioaeobkgjik/' + 32 'success.html', 33 THIS_EXTENSION_ORIGIN: 'chrome-extension://mfffpogegjflfpflabcdkioaeobkgjik', 34 PARENT_PAGE: 'chrome://oobe/', 35 36 initialize: function() { 37 var params = getUrlSearchParams(location.search); 38 this.parentPage_ = params['parentPage'] || this.PARENT_PAGE; 39 this.gaiaUrl_ = params['gaiaUrl'] || this.GAIA_URL; 40 this.inputLang_ = params['hl']; 41 this.inputEmail_ = params['email']; 42 43 document.addEventListener('DOMContentLoaded', this.onPageLoad.bind(this)); 44 }, 45 46 isGaiaMessage_: function(msg) { 47 // Not quite right, but good enough. 48 return this.gaiaUrl_.indexOf(msg.origin) == 0 || 49 this.GAIA_URL.indexOf(msg.origin) == 0; 50 }, 51 52 isInternalMessage_: function(msg) { 53 return msg.origin == this.THIS_EXTENSION_ORIGIN; 54 }, 55 56 getFrameUrl_: function() { 57 var url = this.gaiaUrl_; 58 59 url += this.GAIA_PAGE_PATH; 60 61 if (this.inputLang_) 62 url += '&hl=' + encodeURIComponent(this.inputLang_); 63 if (this.inputEmail_) 64 url += '&Email=' + encodeURIComponent(this.inputEmail_); 65 return url; 66 }, 67 68 loadFrame_: function() { 69 $('gaia-frame').src = this.getFrameUrl_(); 70 }, 71 72 onPageLoad: function(e) { 73 window.addEventListener('message', this.onMessage.bind(this), false); 74 this.loadFrame_(); 75 }, 76 77 onLoginUILoaded: function() { 78 var msg = { 79 'method': 'loginUILoaded' 80 }; 81 window.parent.postMessage(msg, this.parentPage_); 82 }, 83 84 onMessage: function(e) { 85 var msg = e.data; 86 if (msg.method == 'attemptLogin' && this.isGaiaMessage_(e)) { 87 this.email_ = msg.email; 88 this.password_ = msg.password; 89 this.attemptToken_ = msg.attemptToken; 90 } else if (msg.method == 'clearOldAttempts' && this.isGaiaMessage_(e)) { 91 this.email_ = null; 92 this.password_ = null; 93 this.attemptToken_ = null; 94 this.onLoginUILoaded(); 95 } else if (msg.method == 'confirmLogin' && this.isInternalMessage_(e)) { 96 if (this.attemptToken_ == msg.attemptToken) { 97 var msg = { 98 'method': 'completeLogin', 99 'email': this.email_, 100 'password': this.password_ 101 }; 102 window.parent.postMessage(msg, this.parentPage_); 103 } else { 104 console.log('#### Authenticator.onMessage: unexpected attemptToken!?'); 105 } 106 } else { 107 console.log('#### Authenticator.onMessage: unknown message + origin!?'); 108 } 109 } 110 }; 111 112 Authenticator.getInstance().initialize(); 113