Diffstat (limited to 'frontend/beta/js/Clipperz/PM/DataModel/Header.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/beta/js/Clipperz/PM/DataModel/Header.js | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/frontend/beta/js/Clipperz/PM/DataModel/Header.js b/frontend/beta/js/Clipperz/PM/DataModel/Header.js index c780855..908d9f4 100644 --- a/frontend/beta/js/Clipperz/PM/DataModel/Header.js +++ b/frontend/beta/js/Clipperz/PM/DataModel/Header.js | |||
@@ -1,214 +1,212 @@ | |||
1 | /* | 1 | /* |
2 | 2 | ||
3 | Copyright 2008-2011 Clipperz Srl | 3 | Copyright 2008-2013 Clipperz Srl |
4 | 4 | ||
5 | This file is part of Clipperz Community Edition. | 5 | This file is part of Clipperz, the online password manager. |
6 | Clipperz Community Edition is an online password manager. | ||
7 | For further information about its features and functionalities please | 6 | For further information about its features and functionalities please |
8 | refer to http://www.clipperz.com. | 7 | refer to http://www.clipperz.com. |
9 | 8 | ||
10 | * Clipperz Community Edition is free software: you can redistribute | 9 | * Clipperz is free software: you can redistribute it and/or modify it |
11 | it and/or modify it under the terms of the GNU Affero General Public | 10 | under the terms of the GNU Affero General Public License as published |
12 | License as published by the Free Software Foundation, either version | 11 | by the Free Software Foundation, either version 3 of the License, or |
13 | 3 of the License, or (at your option) any later version. | 12 | (at your option) any later version. |
14 | 13 | ||
15 | * Clipperz Community Edition is distributed in the hope that it will | 14 | * Clipperz is distributed in the hope that it will be useful, but |
16 | be useful, but WITHOUT ANY WARRANTY; without even the implied | 15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. | 17 | See the GNU Affero General Public License for more details. |
19 | 18 | ||
20 | * You should have received a copy of the GNU Affero General Public | 19 | * You should have received a copy of the GNU Affero General Public |
21 | License along with Clipperz Community Edition. If not, see | 20 | License along with Clipperz. If not, see http://www.gnu.org/licenses/. |
22 | <http://www.gnu.org/licenses/>. | ||
23 | 21 | ||
24 | */ | 22 | */ |
25 | 23 | ||
26 | if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } | 24 | if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } |
27 | if (typeof(Clipperz.PM) == 'undefined') { Clipperz.PM = {}; } | 25 | if (typeof(Clipperz.PM) == 'undefined') { Clipperz.PM = {}; } |
28 | if (typeof(Clipperz.PM.DataModel) == 'undefined') { Clipperz.PM.DataModel = {}; } | 26 | if (typeof(Clipperz.PM.DataModel) == 'undefined') { Clipperz.PM.DataModel = {}; } |
29 | 27 | ||
30 | 28 | ||
31 | //############################################################################# | 29 | //############################################################################# |
32 | 30 | ||
33 | Clipperz.PM.DataModel.Header = function(args) { | 31 | Clipperz.PM.DataModel.Header = function(args) { |
34 | args = args || {}; | 32 | args = args || {}; |
35 | 33 | ||
36 | this._user = args.user; | 34 | this._user = args.user; |
37 | 35 | ||
38 | this._serverData = null; | 36 | this._serverData = null; |
39 | this._serverDataVersion = null; | 37 | this._serverDataVersion = null; |
40 | this._jsonEvaledServerData = null; | 38 | this._jsonEvaledServerData = null; |
41 | 39 | ||
42 | this._decryptedLegacyServerData = null; | 40 | this._decryptedLegacyServerData = null; |
43 | this._isDecryptingLegacyServerData = false; | 41 | this._isDecryptingLegacyServerData = false; |
44 | this._decryptingLegacyServerDataPendingQueue = []; | 42 | this._decryptingLegacyServerDataPendingQueue = []; |
45 | 43 | ||
46 | this.resetUpdatedSections(); | 44 | this.resetUpdatedSections(); |
47 | 45 | ||
48 | this._shouldLoadSections = {}; | 46 | this._shouldLoadSections = {}; |
49 | 47 | ||
50 | Clipperz.NotificationCenter.register(this.user(), 'updatedSection', this, 'updatedSectionHandler'); | 48 | Clipperz.NotificationCenter.register(this.user(), 'updatedSection', this, 'updatedSectionHandler'); |
51 | 49 | ||
52 | return this; | 50 | return this; |
53 | } | 51 | } |
54 | 52 | ||
55 | Clipperz.PM.DataModel.Header.prototype = MochiKit.Base.update(null, { | 53 | Clipperz.PM.DataModel.Header.prototype = MochiKit.Base.update(null, { |
56 | 54 | ||
57 | //------------------------------------------------------------------------- | 55 | //------------------------------------------------------------------------- |
58 | 56 | ||
59 | 'user': function() { | 57 | 'user': function() { |
60 | return this._user; | 58 | return this._user; |
61 | }, | 59 | }, |
62 | 60 | ||
63 | //------------------------------------------------------------------------- | 61 | //------------------------------------------------------------------------- |
64 | //------------------------------------------------------------------------- | 62 | //------------------------------------------------------------------------- |
65 | //------------------------------------------------------------------------- | 63 | //------------------------------------------------------------------------- |
66 | //------------------------------------------------------------------------- | 64 | //------------------------------------------------------------------------- |
67 | //------------------------------------------------------------------------- | 65 | //------------------------------------------------------------------------- |
68 | //------------------------------------------------------------------------- | 66 | //------------------------------------------------------------------------- |
69 | //------------------------------------------------------------------------- | 67 | //------------------------------------------------------------------------- |
70 | //------------------------------------------------------------------------- | 68 | //------------------------------------------------------------------------- |
71 | //------------------------------------------------------------------------- | 69 | //------------------------------------------------------------------------- |
72 | 70 | ||
73 | 'updatedSections': function() { | 71 | 'updatedSections': function() { |
74 | return this._updatedSections; | 72 | return this._updatedSections; |
75 | }, | 73 | }, |
76 | 74 | ||
77 | 'markSectionAsUpdated': function(aSectionName) { | 75 | 'markSectionAsUpdated': function(aSectionName) { |
78 | this.updatedSections().push(aSectionName); | 76 | this.updatedSections().push(aSectionName); |
79 | }, | 77 | }, |
80 | 78 | ||
81 | 'resetUpdatedSections': function() { | 79 | 'resetUpdatedSections': function() { |
82 | this._updatedSections = [] | 80 | this._updatedSections = [] |
83 | }, | 81 | }, |
84 | 82 | ||
85 | 'hasSectionBeenUpdated': function(aSectionName) { | 83 | 'hasSectionBeenUpdated': function(aSectionName) { |
86 | return (this.updatedSections().join().indexOf(aSectionName) != -1); | 84 | return (this.updatedSections().join().indexOf(aSectionName) != -1); |
87 | }, | 85 | }, |
88 | 86 | ||
89 | 'cachedServerDataSection': function(aSectionName) { | 87 | 'cachedServerDataSection': function(aSectionName) { |
90 | return (this.hasSectionBeenUpdated(aSectionName)) ? {} : this.jsonEvaledServerData()[aSectionName]; | 88 | return (this.hasSectionBeenUpdated(aSectionName)) ? {} : this.jsonEvaledServerData()[aSectionName]; |
91 | }, | 89 | }, |
92 | 90 | ||
93 | 'updateAllSections': function() { | 91 | 'updateAllSections': function() { |
94 | this.resetUpdatedSections(); | 92 | this.resetUpdatedSections(); |
95 | this.markSectionAsUpdated('records'); | 93 | this.markSectionAsUpdated('records'); |
96 | this.markSectionAsUpdated('directLogins'); | 94 | this.markSectionAsUpdated('directLogins'); |
97 | this.markSectionAsUpdated('preferences'); | 95 | this.markSectionAsUpdated('preferences'); |
98 | this.markSectionAsUpdated('oneTimePasswords'); | 96 | this.markSectionAsUpdated('oneTimePasswords'); |
99 | 97 | ||
100 | return MochiKit.Async.succeed(this); | 98 | return MochiKit.Async.succeed(this); |
101 | }, | 99 | }, |
102 | 100 | ||
103 | 'updatedSectionHandler': function(anEvent) { | 101 | 'updatedSectionHandler': function(anEvent) { |
104 | this.markSectionAsUpdated(anEvent.parameters()); | 102 | this.markSectionAsUpdated(anEvent.parameters()); |
105 | }, | 103 | }, |
106 | 104 | ||
107 | //------------------------------------------------------------------------- | 105 | //------------------------------------------------------------------------- |
108 | 106 | ||
109 | 'getObjectKeyIndex': function(anObject) { | 107 | 'getObjectKeyIndex': function(anObject) { |
110 | var result; | 108 | var result; |
111 | varitemReference; | 109 | varitemReference; |
112 | var index; | 110 | var index; |
113 | 111 | ||
114 | result = {}; | 112 | result = {}; |
115 | index = 0; | 113 | index = 0; |
116 | 114 | ||
117 | for (itemReference in anObject) { | 115 | for (itemReference in anObject) { |
118 | result[itemReference] = index.toString(); | 116 | result[itemReference] = index.toString(); |
119 | index ++; | 117 | index ++; |
120 | } | 118 | } |
121 | 119 | ||
122 | return result; | 120 | return result; |
123 | }, | 121 | }, |
124 | 122 | ||
125 | //------------------------------------------------------------------------- | 123 | //------------------------------------------------------------------------- |
126 | 124 | ||
127 | 'serializedDataWithRecordAndDirectLoginIndexes': function(aRecordIndexes, aDirectLoginIndexs) { | 125 | 'serializedDataWithRecordAndDirectLoginIndexes': function(aRecordIndexes, aDirectLoginIndexs) { |
128 | var result; | 126 | var result; |
129 | var records; | 127 | var records; |
130 | var recordReference; | 128 | var recordReference; |
131 | 129 | ||
132 | //MochiKit.Logging.logDebug(">>> Header.serializedData"); | 130 | //MochiKit.Logging.logDebug(">>> Header.serializedData"); |
133 | result = { | 131 | result = { |
134 | 'records': {}, | 132 | 'records': {}, |
135 | 'directLogins': {} | 133 | 'directLogins': {} |
136 | }; | 134 | }; |
137 | 135 | ||
138 | records = this.user().records(); | 136 | records = this.user().records(); |
139 | for (recordReference in records) { | 137 | for (recordReference in records) { |
140 | result['records'][aRecordIndexes[recordReference]] = this.user().records()[recordReference].headerData(); | 138 | result['records'][aRecordIndexes[recordReference]] = this.user().records()[recordReference].headerData(); |
141 | } | 139 | } |
142 | 140 | ||
143 | for (directLoginReference in this.user().directLoginReferences()) { | 141 | for (directLoginReference in this.user().directLoginReferences()) { |
144 | var currentDirectLogin; | 142 | var currentDirectLogin; |
145 | vardirectLoginData; | 143 | vardirectLoginData; |
146 | 144 | ||
147 | currentDirectLogin = this.user().directLoginReferences()[directLoginReference]; | 145 | currentDirectLogin = this.user().directLoginReferences()[directLoginReference]; |
148 | if (aRecordIndexes[currentDirectLogin.recordReference()] != null) { | 146 | if (aRecordIndexes[currentDirectLogin.recordReference()] != null) { |
149 | directLoginData = { | 147 | directLoginData = { |
150 | // reference: currentDirectLogin.reference(), | 148 | // reference: currentDirectLogin.reference(), |
151 | record: aRecordIndexes[currentDirectLogin.recordReference()].toString(), | 149 | record: aRecordIndexes[currentDirectLogin.recordReference()].toString(), |
152 | label: currentDirectLogin.label(), | 150 | label: currentDirectLogin.label(), |
153 | favicon:currentDirectLogin.favicon() || "" | 151 | favicon:currentDirectLogin.favicon() || "" |
154 | } | 152 | } |
155 | 153 | ||
156 | result['directLogins'][aDirectLoginIndexs[directLoginReference]] = directLoginData; | 154 | result['directLogins'][aDirectLoginIndexs[directLoginReference]] = directLoginData; |
157 | } | 155 | } |
158 | 156 | ||
159 | } | 157 | } |
160 | //MochiKit.Logging.logDebug("<<< Header.serializedData - result: " + Clipperz.Base.serializeJSON(result)); | 158 | //MochiKit.Logging.logDebug("<<< Header.serializedData - result: " + Clipperz.Base.serializeJSON(result)); |
161 | //MochiKit.Logging.logDebug("<<< Header.serializedData"); | 159 | //MochiKit.Logging.logDebug("<<< Header.serializedData"); |
162 | 160 | ||
163 | return result; | 161 | return result; |
164 | }, | 162 | }, |
165 | 163 | ||
166 | //------------------------------------------------------------------------- | 164 | //------------------------------------------------------------------------- |
167 | 165 | ||
168 | 'encryptedData': function() { | 166 | 'encryptedData': function() { |
169 | var deferredResult; | 167 | var deferredResult; |
170 | var recordIndex; | 168 | var recordIndex; |
171 | var directLoginIndex; | 169 | var directLoginIndex; |
172 | varserializedData; | 170 | varserializedData; |
173 | var result; | 171 | var result; |
174 | 172 | ||
175 | //MochiKit.Logging.logDebug(">>> [" + (new Date()).valueOf() + "] Header.encryptedData"); | 173 | //MochiKit.Logging.logDebug(">>> [" + (new Date()).valueOf() + "] Header.encryptedData"); |
176 | //MochiKit.Logging.logDebug("### Header.encryptedData - " + Clipperz.Base.serializeJSON(this.updatedSections())); | 174 | //MochiKit.Logging.logDebug("### Header.encryptedData - " + Clipperz.Base.serializeJSON(this.updatedSections())); |
177 | result = { | 175 | result = { |
178 | 'records': this.cachedServerDataSection('records'), | 176 | 'records': this.cachedServerDataSection('records'), |
179 | 'directLogins': this.cachedServerDataSection('directLogins'), | 177 | 'directLogins': this.cachedServerDataSection('directLogins'), |
180 | 'preferences': this.cachedServerDataSection('preferences'), | 178 | 'preferences': this.cachedServerDataSection('preferences'), |
181 | 'oneTimePasswords': this.cachedServerDataSection('oneTimePasswords'), | 179 | 'oneTimePasswords': this.cachedServerDataSection('oneTimePasswords'), |
182 | 'version': '0.1' | 180 | 'version': '0.1' |
183 | }; | 181 | }; |
184 | 182 | ||
185 | if (this.hasSectionBeenUpdated('records')) { | 183 | if (this.hasSectionBeenUpdated('records')) { |
186 | recordIndex = this.getObjectKeyIndex(this.user().records()); | 184 | recordIndex = this.getObjectKeyIndex(this.user().records()); |
187 | result['records']['index'] = recordIndex; | 185 | result['records']['index'] = recordIndex; |
188 | } else { | 186 | } else { |
189 | recordIndex = result['records']['index']; | 187 | recordIndex = result['records']['index']; |
190 | } | 188 | } |
191 | 189 | ||
192 | if (this.hasSectionBeenUpdated('directLogins')) { | 190 | if (this.hasSectionBeenUpdated('directLogins')) { |
193 | directLoginIndex = this.getObjectKeyIndex(this.user().directLoginReferences()); | 191 | directLoginIndex = this.getObjectKeyIndex(this.user().directLoginReferences()); |
194 | result['directLogins']['index'] = directLoginIndex; | 192 | result['directLogins']['index'] = directLoginIndex; |
195 | } else { | 193 | } else { |
196 | directLoginIndex = result['directLogins']['index']; | 194 | directLoginIndex = result['directLogins']['index']; |
197 | } | 195 | } |
198 | 196 | ||
199 | if (this.hasSectionBeenUpdated('records') || this.hasSectionBeenUpdated('directLogins')) { | 197 | if (this.hasSectionBeenUpdated('records') || this.hasSectionBeenUpdated('directLogins')) { |
200 | serializedData = this.serializedDataWithRecordAndDirectLoginIndexes(recordIndex, directLoginIndex); | 198 | serializedData = this.serializedDataWithRecordAndDirectLoginIndexes(recordIndex, directLoginIndex); |
201 | } | 199 | } |
202 | 200 | ||
203 | deferredResult = new MochiKit.Async.Deferred(); | 201 | deferredResult = new MochiKit.Async.Deferred(); |
204 | 202 | ||
205 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("Header.encryptedData - 1: " + res); return res;}); | 203 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("Header.encryptedData - 1: " + res); return res;}); |
206 | if (this.hasSectionBeenUpdated('records')) { | 204 | if (this.hasSectionBeenUpdated('records')) { |
207 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("Header.encryptedData - 1.1: " + res); return res;}); | 205 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("Header.encryptedData - 1.1: " + res); return res;}); |
208 | deferredResult.addCallback(function(anHeader, aResult, aSerializedData, aValue) { | 206 | deferredResult.addCallback(function(anHeader, aResult, aSerializedData, aValue) { |
209 | return Clipperz.PM.Crypto.deferredEncryptWithCurrentVersion(anHeader.user().passphrase(), aSerializedData['records']); | 207 | return Clipperz.PM.Crypto.deferredEncryptWithCurrentVersion(anHeader.user().passphrase(), aSerializedData['records']); |
210 | }, this, result, serializedData); | 208 | }, this, result, serializedData); |
211 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("Header.encryptedData - 1.2: " + res); return res;}); | 209 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("Header.encryptedData - 1.2: " + res); return res;}); |
212 | deferredResult.addCallback(function(anHeader, aResult, aValue) { | 210 | deferredResult.addCallback(function(anHeader, aResult, aValue) { |
213 | aResult['records']['data'] = aValue; | 211 | aResult['records']['data'] = aValue; |
214 | }, this, result); | 212 | }, this, result); |