Moved GAIA auth extension to public repo

BUG=chromium-os:17470
TEST=none
TBR=mnissler

Change-Id: I79d6bc3d374e7c68bf702b9b550b0c3adad2a2a3
Reviewed-on: http://gerrit.chromium.org/gerrit/4595
Reviewed-by: Zelidrag Hornung <zelidrag@chromium.org>
Tested-by: Zelidrag Hornung <zelidrag@chromium.org>
diff --git a/gaia_auth/main.html b/gaia_auth/main.html
new file mode 100644
index 0000000..9a4427b
--- /dev/null
+++ b/gaia_auth/main.html
@@ -0,0 +1,128 @@
+<!DOCTYPE html>
+<html>
+<style>
+iframe {
+  overflow-x: hidden; 
+  overflow-y: hidden;
+  height: 294px;
+  width: 100%;
+  margin: 0px;
+}
+</style>
+<script>
+function $(id) {
+  return document.getElementById(id);
+}
+
+function Authenticator() {
+};
+
+Authenticator.getInstance = function() {
+  if (!Authenticator.instance_) {
+    Authenticator.instance_ = new Authenticator();
+  }
+  return Authenticator.instance_;
+}
+
+Authenticator.prototype = {
+  email_: null,
+  password_: null,
+  attemptToken_: null,
+  frameLoaded_: false,
+
+  //GAIA_PAGE_ORIGIN: 'https://gaiastaging.corp.google.com',
+  GAIA_PAGE_ORIGIN: 'https://www.google.com',
+  GAIA_PAGE_PATH: '/accounts/ServiceLogin?nui=1&ifr=0&skipvpage=true&sarp=1' +
+      '&continue=chrome-extension://mfffpogegjflfpflabcdkioaeobkgjik/' +
+      'success.html',
+  THIS_EXTENSION_ORIGIN: 'chrome-extension://mfffpogegjflfpflabcdkioaeobkgjik',
+  PARENT_PAGE: 'chrome://oobe/',
+
+  initialize: function() {
+    this.frameLoaded_ = false;
+    console.log('### Authenticator.initialize');
+    window.addEventListener('offline', this.onOffline.bind(this), false);
+    window.addEventListener('online', this.onOnline.bind(this), false);
+    window.addEventListener('message', this.onMessage.bind(this), false);
+    document.addEventListener('DOMContentLoaded', this.onPageLoad.bind(this));
+  },
+
+  isGaiaMessage_: function(msg) {
+    return msg.origin == this.GAIA_PAGE_ORIGIN;
+  },
+
+  isInternalMessage_: function(msg) {
+    return msg.origin == this.THIS_EXTENSION_ORIGIN;
+  },
+
+  getFrameUrl_: function() {
+    return this.GAIA_PAGE_ORIGIN + this.GAIA_PAGE_PATH
+  },
+
+  loadFrame_: function() {
+    console.log('Loading GAIA frame');
+    var frame = $('gaia-frame');
+    var self = this;
+    frame.addEventListener('load', function(e) {
+      console.log('### Authenticator.loadFrame_: Frame loaded.');
+      self.frameLoaded_ = true;
+    });
+    frame.contentWindow.location.href = this.getFrameUrl_();
+  },
+
+  onPageLoad: function(e) {
+    console.log('#### Authenticator.onPageLoad: navigator.onLine = ' + navigator.onLine);
+    if (navigator.onLine || true) {
+      console.log('#### Authenticator.onPageLoad: loading frame...');
+      this.loadFrame_();
+    }
+  },
+
+  onOffline: function(e) {
+    if (frameLoaded_) {
+      console.log('#### Authenticator.onOffline: went offline, cancel auth...');
+      // TODO(zelidrag): Signal parent page that we can't complete auth process.
+    }
+  },
+
+  onOnline: function(e) {
+    if (!frameLoaded_) {
+      // console.log('#### Authenticator.onOnline: loading frame...');
+      this.loadFrame_();
+    }
+  },
+
+  onMessage: function(e) {
+    var msg = e.data;
+    // console.log('#### Authenticator.onMessage: ' + JSON.stringify(msg));
+    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;
+    } 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!?');
+    }
+  }
+};
+
+console.log('#### main.html start');
+Authenticator.getInstance().initialize();
+</script>
+<body><iframe id="gaia-frame" src="about:blank"
+  frameborder="0"></iframe></body></html>
+
diff --git a/gaia_auth/success.html b/gaia_auth/success.html
new file mode 100644
index 0000000..c319797
--- /dev/null
+++ b/gaia_auth/success.html
@@ -0,0 +1,43 @@
+<!DOCTYPE html>
+<html>
+<script>
+function getUrlSearchParams(search) {
+  var params = {};
+
+  if (search) {
+    // Strips leading '?'
+    search = search.substring(1);
+    var pairs = search.split('&');
+    for (var i = 0; i < pairs.length; ++i) {
+      var pair =  pairs[i].split('=');
+      if (pair.length == 2) {
+        params[pair[0]] = pair[1];
+      } else {
+        params[pair] = true;
+      }
+    }
+  }
+
+  return params;
+}
+
+function load() {
+  var params = getUrlSearchParams(location.search);
+
+  console.log('### success url=' + location.href);
+  console.log('### success params=' + JSON.stringify(params));
+
+  var msg = {
+    'method': 'confirmLogin',
+    'attemptToken': params['attemptToken']
+  };
+  window.parent.postMessage(msg,
+          'chrome-extension://mfffpogegjflfpflabcdkioaeobkgjik/main.html');
+}
+
+document.addEventListener('DOMContentLoaded', load);
+</script>
+
+<body><div id='test-result' hidden style='color:white'></div></div>
+</body>
+</html>