summaryrefslogtreecommitdiffstats
path: root/services/sync/tps/extensions/tps/resource/auth/sync.jsm
blob: 676b17a9189f61586145b2550d719aed1f48dbd5 (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
/* 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;
  }
};