|
1 /* -*- Mode: js2; js2-basic-offset: 2; indent-tabs-mode: nil; -*- */ |
|
2 /* vim: set ft=javascript ts=2 et sw=2 tw=80: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 "use strict"; |
|
8 |
|
9 const Cu = Components.utils; |
|
10 const Ci = Components.interfaces; |
|
11 const Cc = Components.classes; |
|
12 const Cr = Components.results; |
|
13 |
|
14 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
15 Cu.import("resource://gre/modules/Services.jsm"); |
|
16 |
|
17 this.EXPORTED_SYMBOLS = ["IdentityStore"]; |
|
18 |
|
19 // the data store for IDService |
|
20 // written as a separate thing so it can easily be mocked |
|
21 function IDServiceStore() { |
|
22 this.reset(); |
|
23 } |
|
24 |
|
25 // Note: eventually these methods may be async, but we haven no need for this |
|
26 // for now, since we're not storing to disk. |
|
27 IDServiceStore.prototype = { |
|
28 addIdentity: function addIdentity(aEmail, aKeyPair, aCert) { |
|
29 this._identities[aEmail] = {keyPair: aKeyPair, cert: aCert}; |
|
30 }, |
|
31 fetchIdentity: function fetchIdentity(aEmail) { |
|
32 return aEmail in this._identities ? this._identities[aEmail] : null; |
|
33 }, |
|
34 removeIdentity: function removeIdentity(aEmail) { |
|
35 let data = this._identities[aEmail]; |
|
36 delete this._identities[aEmail]; |
|
37 return data; |
|
38 }, |
|
39 getIdentities: function getIdentities() { |
|
40 // XXX - should clone? |
|
41 return this._identities; |
|
42 }, |
|
43 clearCert: function clearCert(aEmail) { |
|
44 // XXX - should remove key from store? |
|
45 this._identities[aEmail].cert = null; |
|
46 this._identities[aEmail].keyPair = null; |
|
47 }, |
|
48 |
|
49 /** |
|
50 * set the login state for a given origin |
|
51 * |
|
52 * @param aOrigin |
|
53 * (string) a web origin |
|
54 * |
|
55 * @param aState |
|
56 * (boolean) whether or not the user is logged in |
|
57 * |
|
58 * @param aEmail |
|
59 * (email) the email address the user is logged in with, |
|
60 * or, if not logged in, the default email for that origin. |
|
61 */ |
|
62 setLoginState: function setLoginState(aOrigin, aState, aEmail) { |
|
63 if (aState && !aEmail) { |
|
64 throw "isLoggedIn cannot be set to true without an email"; |
|
65 } |
|
66 return this._loginStates[aOrigin] = {isLoggedIn: aState, email: aEmail}; |
|
67 }, |
|
68 getLoginState: function getLoginState(aOrigin) { |
|
69 return aOrigin in this._loginStates ? this._loginStates[aOrigin] : null; |
|
70 }, |
|
71 clearLoginState: function clearLoginState(aOrigin) { |
|
72 delete this._loginStates[aOrigin]; |
|
73 }, |
|
74 |
|
75 reset: function Store_reset() { |
|
76 // _identities associates emails with keypairs and certificates |
|
77 this._identities = {}; |
|
78 |
|
79 // _loginStates associates. remote origins with a login status and |
|
80 // the email the user has chosen as his or her identity when logging |
|
81 // into that origin. |
|
82 this._loginStates = {}; |
|
83 }, |
|
84 |
|
85 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, Ci.nsIObserver]), |
|
86 |
|
87 observe: function observe(aSubject, aTopic, aData) { |
|
88 switch (aTopic) { |
|
89 case "quit-application-granted": |
|
90 Services.obs.removeObserver(this, "quit-application-granted"); |
|
91 this.reset(); |
|
92 break; |
|
93 } |
|
94 }, |
|
95 }; |
|
96 |
|
97 this.IdentityStore = new IDServiceStore(); |