author | Giulio Cesare Solaroli <giulio.cesare@clipperz.com> | 2011-10-02 23:56:18 (UTC) |
---|---|---|
committer | Giulio Cesare Solaroli <giulio.cesare@clipperz.com> | 2011-10-02 23:56:18 (UTC) |
commit | ef68436ac04da078ffdcacd7e1f785473a303d45 (patch) (unidiff) | |
tree | c403752d66a2c4775f00affd4fa8431b29c5b68c /frontend/beta/js/Clipperz/PM/DataModel/OneTimePassword.js | |
parent | 597ecfbc0249d83e1b856cbd558340c01237a360 (diff) | |
download | clipperz-ef68436ac04da078ffdcacd7e1f785473a303d45.zip clipperz-ef68436ac04da078ffdcacd7e1f785473a303d45.tar.gz clipperz-ef68436ac04da078ffdcacd7e1f785473a303d45.tar.bz2 |
First version of the newly restructured repository
Diffstat (limited to 'frontend/beta/js/Clipperz/PM/DataModel/OneTimePassword.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/beta/js/Clipperz/PM/DataModel/OneTimePassword.js | 333 |
1 files changed, 333 insertions, 0 deletions
diff --git a/frontend/beta/js/Clipperz/PM/DataModel/OneTimePassword.js b/frontend/beta/js/Clipperz/PM/DataModel/OneTimePassword.js new file mode 100644 index 0000000..dd8d5c9 --- a/dev/null +++ b/frontend/beta/js/Clipperz/PM/DataModel/OneTimePassword.js | |||
@@ -0,0 +1,333 @@ | |||
1 | /* | ||
2 | |||
3 | Copyright 2008-2011 Clipperz Srl | ||
4 | |||
5 | This file is part of Clipperz's Javascript Crypto Library. | ||
6 | Javascript Crypto Library provides web developers with an extensive | ||
7 | and efficient set of cryptographic functions. The library aims to | ||
8 | obtain maximum execution speed while preserving modularity and | ||
9 | reusability. | ||
10 | For further information about its features and functionalities please | ||
11 | refer to http://www.clipperz.com | ||
12 | |||
13 | * Javascript Crypto Library is free software: you can redistribute | ||
14 | it and/or modify it under the terms of the GNU Affero General Public | ||
15 | License as published by the Free Software Foundation, either version | ||
16 | 3 of the License, or (at your option) any later version. | ||
17 | |||
18 | * Javascript Crypto Library is distributed in the hope that it will | ||
19 | be useful, but WITHOUT ANY WARRANTY; without even the implied | ||
20 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
21 | See the GNU Affero General Public License for more details. | ||
22 | |||
23 | * You should have received a copy of the GNU Affero General Public | ||
24 | License along with Javascript Crypto Library. If not, see | ||
25 | <http://www.gnu.org/licenses/>. | ||
26 | |||
27 | */ | ||
28 | |||
29 | if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } | ||
30 | if (typeof(Clipperz.PM) == 'undefined') { Clipperz.PM = {}; } | ||
31 | if (typeof(Clipperz.PM.DataModel) == 'undefined') { Clipperz.PM.DataModel = {}; } | ||
32 | |||
33 | |||
34 | //############################################################################# | ||
35 | |||
36 | Clipperz.PM.DataModel.OneTimePassword = function(args) { | ||
37 | args = args || {}; | ||
38 | |||
39 | //console.log("new OneTimePassword", args); | ||
40 | //MochiKit.Logging.logDebug("---"); | ||
41 | this._user = args['user']; | ||
42 | this._password = args['password']; | ||
43 | this._passwordValue = Clipperz.PM.DataModel.OneTimePassword.normalizedOneTimePassword(args['password']); | ||
44 | this._reference = args['reference'] || Clipperz.PM.Crypto.randomKey(); | ||
45 | this._creationDate = args['created'] ? Clipperz.PM.Date.parseDateWithUTCFormat(args['created']) : new Date(); | ||
46 | this._usageDate = args['used'] ? Clipperz.PM.Date.parseDateWithUTCFormat(args['used']) : null; | ||
47 | |||
48 | this._status = args['status'] || 'ACTIVE'; | ||
49 | this._connectionInfo = null; | ||
50 | |||
51 | this._key = null; | ||
52 | this._keyChecksum = null; | ||
53 | |||
54 | return this; | ||
55 | } | ||
56 | |||
57 | Clipperz.PM.DataModel.OneTimePassword.prototype = MochiKit.Base.update(null, { | ||
58 | |||
59 | 'toString': function() { | ||
60 | return "Clipperz.PM.DataModel.OneTimePassword"; | ||
61 | }, | ||
62 | |||
63 | //------------------------------------------------------------------------- | ||
64 | |||
65 | 'user': function() { | ||
66 | return this._user; | ||
67 | }, | ||
68 | |||
69 | //------------------------------------------------------------------------- | ||
70 | |||
71 | 'password': function() { | ||
72 | return this._password; | ||
73 | }, | ||
74 | |||
75 | //------------------------------------------------------------------------- | ||
76 | |||
77 | 'passwordValue': function() { | ||
78 | return this._passwordValue; | ||
79 | }, | ||
80 | |||
81 | //------------------------------------------------------------------------- | ||
82 | |||
83 | 'creationDate': function() { | ||
84 | return this._creationDate; | ||
85 | }, | ||
86 | |||
87 | //------------------------------------------------------------------------- | ||
88 | |||
89 | 'reference': function() { | ||
90 | return this._reference; | ||
91 | }, | ||
92 | |||
93 | //------------------------------------------------------------------------- | ||
94 | |||
95 | 'key': function() { | ||
96 | if (this._key == null) { | ||
97 | this._key = Clipperz.PM.DataModel.OneTimePassword.computeKeyWithUsernameAndPassword(this.user().username(), this.passwordValue()); | ||
98 | } | ||
99 | |||
100 | return this._key; | ||
101 | }, | ||
102 | |||
103 | //------------------------------------------------------------------------- | ||
104 | |||
105 | 'keyChecksum': function() { | ||
106 | if (this._keyChecksum == null) { | ||
107 | this._keyChecksum = Clipperz.PM.DataModel.OneTimePassword.computeKeyChecksumWithUsernameAndPassword(this.user().username(), this.passwordValue()); | ||
108 | } | ||
109 | |||
110 | return this._keyChecksum; | ||
111 | }, | ||
112 | |||
113 | //------------------------------------------------------------------------- | ||
114 | |||
115 | 'status': function() { | ||
116 | return this._status; | ||
117 | }, | ||
118 | |||
119 | 'setStatus': function(aValue) { | ||
120 | this._status = aValue; | ||
121 | }, | ||
122 | |||
123 | //------------------------------------------------------------------------- | ||
124 | |||
125 | 'serializedData': function() { | ||
126 | var result; | ||
127 | |||
128 | result = { | ||
129 | 'password': this.password(), | ||
130 | 'created': this.creationDate() ? Clipperz.PM.Date.formatDateWithUTCFormat(this.creationDate()) : null, | ||
131 | 'used': this.usageDate() ? Clipperz.PM.Date.formatDateWithUTCFormat(this.usageDate()) : null, | ||
132 | 'status': this.status() | ||
133 | }; | ||
134 | |||
135 | return result; | ||
136 | }, | ||
137 | |||
138 | //------------------------------------------------------------------------- | ||
139 | |||
140 | 'packedPassphrase': function() { | ||
141 | var result; | ||
142 | var packedPassphrase; | ||
143 | var encodedPassphrase; | ||
144 | varprefixPadding; | ||
145 | var suffixPadding; | ||
146 | var getRandomBytes; | ||
147 | |||
148 | getRandomBytes = MochiKit.Base.method(Clipperz.Crypto.PRNG.defaultRandomGenerator(), 'getRandomBytes'); | ||
149 | |||
150 | encodedPassphrase = new Clipperz.ByteArray(this.user().passphrase()).toBase64String(); | ||
151 | //MochiKit.Logging.logDebug("--- encodedPassphrase.length: " + encodedPassphrase.length); | ||
152 | prefixPadding = getRandomBytes(getRandomBytes(1).byteAtIndex(0)).toBase64String(); | ||
153 | //MochiKit.Logging.logDebug("--- prefixPadding.length: " + prefixPadding.length); | ||
154 | suffixPadding = getRandomBytes((500 - prefixPadding.length - encodedPassphrase.length) * 6 / 8).toBase64String(); | ||
155 | //MochiKit.Logging.logDebug("--- suffixPadding.length: " + suffixPadding.length); | ||
156 | //MochiKit.Logging.logDebug("--- total.length: " + (prefixPadding.length + encodedPassphrase.length + suffixPadding.length)); | ||
157 | |||
158 | packedPassphrase = { | ||
159 | 'prefix': prefixPadding, | ||
160 | 'passphrase': encodedPassphrase, | ||
161 | 'suffix': suffixPadding | ||
162 | }; | ||
163 | |||
164 | // result = Clipperz.Base.serializeJSON(packedPassphrase); | ||
165 | result = packedPassphrase; | ||
166 | //MochiKit.Logging.logDebug("===== OTP packedPassprase: [" + result.length + "]" + result); | ||
167 | //MochiKit.Logging.logDebug("<<< OneTimePassword.packedPassphrase"); | ||
168 | |||
169 | return result; | ||
170 | }, | ||
171 | |||
172 | //------------------------------------------------------------------------- | ||
173 | |||
174 | 'encryptedPackedPassphrase': function() { | ||
175 | return Clipperz.PM.Crypto.deferredEncryptWithCurrentVersion(this.passwordValue(), this.packedPassphrase()) | ||
176 | }, | ||
177 | |||
178 | //------------------------------------------------------------------------- | ||
179 | |||
180 | 'encryptedData': function() { | ||
181 | var deferredResult; | ||
182 | varresult; | ||
183 | |||
184 | //MochiKit.Logging.logDebug(">>> OneTimePassword.encryptedData"); | ||
185 | //MochiKit.Logging.logDebug("--- OneTimePassword.encryptedData - id: " + this.reference()); | ||
186 | result = { | ||
187 | 'reference': this.reference(), | ||
188 | 'key': this.key(), | ||
189 | 'keyChecksum': this.keyChecksum(), | ||
190 | 'data': "", | ||
191 | 'version': Clipperz.PM.Crypto.encryptingFunctions.currentVersion | ||
192 | } | ||
193 | //MochiKit.Logging.logDebug("--- OneTimePassword.encryptedData - 2: " + Clipperz.Base.serializeJSON(result)); | ||
194 | deferredResult = new MochiKit.Async.Deferred(); | ||
195 | //MochiKit.Logging.logDebug("--- OneTimePassword.encryptedData - 3"); | ||
196 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("OneTimePassword.encryptedData - 1: " + res); return res;}); | ||
197 | //# deferredResult.addCallback(Clipperz.PM.Crypto.deferredEncryptWithCurrentVersion, this.passwordValue(), this.packedPassphrase()); | ||
198 | deferredResult.addCallback(MochiKit.Base.method(this, 'encryptedPackedPassphrase')); | ||
199 | //MochiKit.Logging.logDebug("--- OneTimePassword.encryptedData - 4"); | ||
200 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("OneTimePassword.encryptedData - 2: [" + res.length + "]" + res); return res;}); | ||
201 | deferredResult.addCallback(function(aResult, res) { | ||
202 | aResult['data'] = res; | ||
203 | return aResult; | ||
204 | }, result); | ||
205 | //MochiKit.Logging.logDebug("--- OneTimePassword.encryptedData - 5"); | ||
206 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("OneTimePassword.encryptedData - 3: " + Clipperz.Base.serializeJSON(res)); return res;}); | ||
207 | deferredResult.callback(); | ||
208 | //MochiKit.Logging.logDebug("--- OneTimePassword.encryptedData - 6"); | ||
209 | |||
210 | return deferredResult; | ||
211 | }, | ||
212 | |||
213 | //------------------------------------------------------------------------- | ||
214 | |||
215 | 'saveChanges': function() { | ||
216 | var deferredResult; | ||
217 | varresult; | ||
218 | |||
219 | //MochiKit.Logging.logDebug(">>> OneTimePassword.saveChanges"); | ||
220 | result = {}; | ||
221 | deferredResult = new MochiKit.Async.Deferred(); | ||
222 | |||
223 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'updatedProgressState', 'saveOTP_encryptUserData'); | ||
224 | deferredResult.addCallback(MochiKit.Base.method(this.user(), 'encryptedData')); | ||
225 | deferredResult.addCallback(function(aResult, res) { | ||
226 | aResult['user'] = res; | ||
227 | return aResult; | ||
228 | }, result); | ||
229 | |||
230 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'updatedProgressState', 'saveOTP_encryptOTPData'); | ||
231 | deferredResult.addCallback(MochiKit.Base.method(this, 'encryptedData')); | ||
232 | deferredResult.addCallback(function(aResult, res) { | ||
233 | aResult['oneTimePassword'] = res; | ||
234 | return aResult; | ||
235 | }, result); | ||
236 | |||
237 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'updatedProgressState', 'saveOTP_sendingData'); | ||
238 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("OneTimePassword.saveChanges - 1: " + Clipperz.Base.serializeJSON(res)); return res;}); | ||
239 | deferredResult.addCallback(MochiKit.Base.method(this.user().connection(), 'message'), 'addNewOneTimePassword'); | ||
240 | |||
241 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'updatedProgressState', 'saveOTP_updatingInterface'); | ||
242 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("OneTimePassword.saveChanges - 2: " + res); return res;}); | ||
243 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'notify', 'OTPUpdated'); | ||
244 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'oneTimePassword_saveChanges_done', null); | ||
245 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("OneTimePassword.saveChanges - 2: " + res); return res;}); | ||
246 | deferredResult.callback(); | ||
247 | //MochiKit.Logging.logDebug("<<< OneTimePassword.saveChanges"); | ||
248 | |||
249 | return deferredResult; | ||
250 | }, | ||
251 | |||
252 | //------------------------------------------------------------------------- | ||
253 | |||
254 | 'usageDate': function() { | ||
255 | return this._usageDate; | ||
256 | }, | ||
257 | |||
258 | 'setUsageDate': function(aValue) { | ||
259 | this._usageDate = aValue; | ||
260 | }, | ||
261 | |||
262 | //------------------------------------------------------------------------- | ||
263 | |||
264 | 'connectionInfo': function() { | ||
265 | return this._connectionInfo; | ||
266 | }, | ||
267 | |||
268 | 'setConnectionInfo': function(aValue) { | ||
269 | this._connectionInfo = aValue; | ||
270 | }, | ||
271 | |||
272 | //------------------------------------------------------------------------- | ||
273 | |||
274 | 'isExpired': function() { | ||
275 | return (this.usageDate() != null); | ||
276 | }, | ||
277 | |||
278 | //------------------------------------------------------------------------- | ||
279 | |||
280 | 'updateStatusWithValues': function(someValues) { | ||
281 | var result; | ||
282 | |||
283 | result = false; | ||
284 | |||
285 | if (someValues['status'] != this.status()) { | ||
286 | result = true; | ||
287 | } | ||
288 | |||
289 | this.setStatus(someValues['status']); | ||
290 | this.setUsageDate(Clipperz.PM.Date.parseDateWithUTCFormat(someValues['requestDate'])); | ||
291 | this.setConnectionInfo(someValues['connection']); | ||
292 | |||
293 | return result; | ||
294 | }, | ||
295 | |||
296 | //------------------------------------------------------------------------- | ||
297 | __syntaxFix__: "syntax fix" | ||
298 | }); | ||
299 | |||
300 | //============================================================================= | ||
301 | |||
302 | Clipperz.PM.DataModel.OneTimePassword.computeKeyWithUsernameAndPassword = function(anUsername, aPassword) { | ||
303 | return Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aPassword)).toHexString().substring(2); | ||
304 | } | ||
305 | |||
306 | Clipperz.PM.DataModel.OneTimePassword.computeKeyChecksumWithUsernameAndPassword = function(anUsername, aPassword) { | ||
307 | return Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(anUsername + aPassword)).toHexString().substring(2); | ||
308 | } | ||
309 | |||
310 | //============================================================================= | ||
311 | |||
312 | Clipperz.PM.DataModel.OneTimePassword.normalizedOneTimePassword = function(aPassword) { | ||
313 | varresult; | ||
314 | |||
315 | if (aPassword.replace(/[\s\-]/g, '').length == 32) { | ||
316 | try { | ||
317 | var passwordByteArray; | ||
318 | |||
319 | passwordByteArray = new Clipperz.ByteArray(); | ||
320 | passwordByteArray.appendBase32String(aPassword); | ||
321 | |||
322 | result = passwordByteArray.toBase64String(); | ||
323 | } catch(exception) { | ||
324 | result = aPassword; | ||
325 | } | ||
326 | } else { | ||
327 | result = aPassword; | ||
328 | } | ||
329 | |||
330 | return result; | ||
331 | } | ||
332 | |||
333 | //============================================================================= | ||