summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/gaia_auth/main.js
blob: 8a40128f0cc1003c18e32aed7ff8917f7151a619 (plain)
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
// 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.

function Authenticator() {
}

Authenticator.getInstance = function() {
  if (!Authenticator.instance_) {
    Authenticator.instance_ = new Authenticator();
  }
  return Authenticator.instance_;
};

Authenticator.prototype = {
  email_: null,
  password_: null,
  attemptToken_: null,

  // Input params from extension initialization URL.
  inputLang_: undefined,
  intputEmail_: undefined,

  GAIA_PAGE_ORIGIN: 'https://accounts.google.com',
  GAIA_PAGE_PATH: '/ServiceLogin?service=chromeoslogin' +
      '&skipvpage=true&sarp=1&rm=hide' +
      '&continue=chrome-extension://mfffpogegjflfpflabcdkioaeobkgjik/' +
      'success.html',
  THIS_EXTENSION_ORIGIN: 'chrome-extension://mfffpogegjflfpflabcdkioaeobkgjik',
  PARENT_PAGE: 'chrome://oobe/',

  initialize: function() {
    var params = getUrlSearchParams(location.search);
    this.gaiaOrigin_ = params['gaiaOrigin'] || this.GAIA_PAGE_ORIGIN;
    this.gaiaUrlPath_ = params['gaiaUrlPath'] || '';
    this.inputLang_ = params['hl'];
    this.inputEmail_ = params['email'];
    this.testEmail_ = params['test_email'];
    this.testPassword_ = params['test_password'];

    document.addEventListener('DOMContentLoaded', this.onPageLoad.bind(this));
  },

  isGaiaMessage_: function(msg) {
    return msg.origin == this.gaiaOrigin_ ||
           msg.origin == this.GAIA_PAGE_ORIGIN;
  },

  isInternalMessage_: function(msg) {
    return msg.origin == this.THIS_EXTENSION_ORIGIN;
  },

  getFrameUrl_: function() {
    var url = this.gaiaOrigin_;

    if (this.gaiaOrigin_ == 'https://www.google.com')
      url += '/accounts';

    if (this.gaiaUrlPath_ && this.gaiaUrlPath_ != '')
      url += this.gaiaUrlPath_;

    url += this.GAIA_PAGE_PATH;

    if (this.inputLang_)
      url += '&hl=' + encodeURIComponent(this.inputLang_);
    if (this.inputEmail_)
      url += '&Email=' + encodeURIComponent(this.inputEmail_);
    if (this.testEmail_)
      url += '&test_email=' + encodeURIComponent(this.testEmail_);
    if (this.testPassword_)
      url += '&test_pwd=' + encodeURIComponent(this.testPassword_);
    return url;
  },

  loadFrame_: function() {
    $('gaia-frame').src = this.getFrameUrl_();
  },

  onPageLoad: function(e) {
    window.addEventListener('message', this.onMessage.bind(this), false);
    this.loadFrame_();
  },

  onLoginUILoaded: function() {
    var msg = {
      'method': 'loginUILoaded'
    };
    window.parent.postMessage(msg, this.PARENT_PAGE);
  },

  onMessage: function(e) {
    var msg = e.data;
    if (msg.method == 'attemptLogin' && this.isGaiaMessage_(e)) {
      this.email_ = msg.email;
      this.password_ = msg.password;
      this.attemptToken_ = msg.attemptToken;
    } else if (msg.method == 'clearOldAttempts' && this.isGaiaMessage_(e)) {
      this.email_ = null;
      this.password_ = null;
      this.attemptToken_ = null;
      this.onLoginUILoaded();
    } else if (msg.method == 'confirmLogin' && this.isInternalMessage_(e)) {
      if (this.attemptToken_ == msg.attemptToken) {
        var msg = {
          'method': 'completeLogin',
          'email': this.email_,
          'password': this.password_
        };
        window.parent.postMessage(msg, this.PARENT_PAGE);
      } else {
        console.log('#### Authenticator.onMessage: unexpected attemptToken!?');
      }
    } else {
      console.log('#### Authenticator.onMessage: unknown message + origin!?');
    }
  }
};

Authenticator.getInstance().initialize();