services/sync/tps/extensions/tps/resource/auth/sync.jsm
author Henrik Skupin <mail@hskupin.info>
Mon, 14 Apr 2014 18:22:23 +0200
changeset 227308 945e92a1d40941d5ba43f3c4b5ca0da3d5eab7fc
parent 226715 59cc6bd58ed99a6eeb58cd52b02094b8441768eb
permissions -rw-r--r--
Bug 996027 - Ensure that TPS always fakes login into Weave. r=rnewman, a=bkerensa DONTBUILD

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

this.EXPORTED_SYMBOLS = [
  "Authentication",
];

const {classes: Cc, interfaces: Ci, utils: Cu} = Components;

Cu.import("resource://services-sync/main.js");
Cu.import("resource://tps/logger.jsm");


/**
 * Helper object for deprecated Firefox Sync authentication
 */
var Authentication = {

  /**
   * Check if an user has been logged in
   */
  get isLoggedIn() {
    return !!this.getSignedInUser();
  },

  /**
   * Wrapper to retrieve the currently signed in user
   *
   * @returns Information about the currently signed in user
   */
  getSignedInUser: function getSignedInUser() {
    let user = null;

    if (Weave.Service.isLoggedIn) {
      user = {
        email: Weave.Service.identity.account,
        password: Weave.Service.identity.basicPassword,
        passphrase: Weave.Service.identity.syncKey
      };
    }

    return user;
  },

  /**
   * Wrapper to synchronize the login of a user
   *
   * @param account
   *        Account information of the user to login
   * @param account.username
   *        The username for the account (utf8)
   * @param account.password
   *        The user's password
   * @param account.passphrase
   *        The users's passphrase
   */
  signIn: function signIn(account) {
    Logger.AssertTrue(account["username"], "Username has been found");
    Logger.AssertTrue(account["password"], "Password has been found");
    Logger.AssertTrue(account["passphrase"], "Passphrase has been found");

    Logger.logInfo("Logging in user: " + account["username"]);

    Weave.Service.identity.account = account["username"];
    Weave.Service.identity.basicPassword = account["password"];
    Weave.Service.identity.syncKey = account["passphrase"];

    if (Weave.Status.login !== Weave.LOGIN_SUCCEEDED) {
      Logger.logInfo("Logging into Weave.");
      Weave.Service.login();
      Logger.AssertEqual(Weave.Status.login, Weave.LOGIN_SUCCEEDED,
                         "Weave logged in");

      // Bug 997279: Temporary workaround until we can ensure that Sync itself
      // sends this notification for the first login attempt by TPS
      Weave.Svc.Obs.notify("weave:service:setup-complete");
    }

    return true;
  }
};