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/gamma/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/gamma/js/Clipperz/PM/DataModel/OneTimePassword.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/gamma/js/Clipperz/PM/DataModel/OneTimePassword.js | 357 |
1 files changed, 357 insertions, 0 deletions
diff --git a/frontend/gamma/js/Clipperz/PM/DataModel/OneTimePassword.js b/frontend/gamma/js/Clipperz/PM/DataModel/OneTimePassword.js new file mode 100644 index 0000000..9f1c197 --- a/dev/null +++ b/frontend/gamma/js/Clipperz/PM/DataModel/OneTimePassword.js | |||
@@ -0,0 +1,357 @@ | |||
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 | //this._user = args['user']; | ||
40 | this._reference = args['reference']|| Clipperz.PM.Crypto.randomKey(); | ||
41 | this._password = args['password']; | ||
42 | this._passwordValue = Clipperz.PM.DataModel.OneTimePassword.normalizedOneTimePassword(args['password']); | ||
43 | this._creationDate = args['created'] ? Clipperz.PM.Date.parseDateWithUTCFormat(args['created']) : new Date(); | ||
44 | this._usageDate = args['used'] ? Clipperz.PM.Date.parseDateWithUTCFormat(args['used']) : null; | ||
45 | |||
46 | this._status = args['status'] || 'ACTIVE'; //'REQUESTED', 'USED', 'DISABLED' | ||
47 | this._connectionInfo= null; | ||
48 | |||
49 | this._key = null; | ||
50 | this._keyChecksum= null; | ||
51 | |||
52 | return this; | ||
53 | } | ||
54 | |||
55 | Clipperz.PM.DataModel.OneTimePassword.prototype = MochiKit.Base.update(null, { | ||
56 | |||
57 | 'toString': function() { | ||
58 | return "Clipperz.PM.DataModel.OneTimePassword"; | ||
59 | }, | ||
60 | /* | ||
61 | //------------------------------------------------------------------------- | ||
62 | |||
63 | 'user': function() { | ||
64 | return this._user; | ||
65 | }, | ||
66 | |||
67 | //------------------------------------------------------------------------- | ||
68 | |||
69 | 'password': function() { | ||
70 | return this._password; | ||
71 | }, | ||
72 | |||
73 | //------------------------------------------------------------------------- | ||
74 | |||
75 | 'passwordValue': function() { | ||
76 | return this._passwordValue; | ||
77 | }, | ||
78 | |||
79 | //------------------------------------------------------------------------- | ||
80 | |||
81 | 'creationDate': function() { | ||
82 | return this._creationDate; | ||
83 | }, | ||
84 | |||
85 | //------------------------------------------------------------------------- | ||
86 | |||
87 | 'reference': function() { | ||
88 | return this._reference; | ||
89 | }, | ||
90 | |||
91 | //------------------------------------------------------------------------- | ||
92 | |||
93 | 'key': function() { | ||
94 | if (this._key == null) { | ||
95 | this._key = Clipperz.PM.DataModel.OneTimePassword.computeKeyWithUsernameAndPassword(this.user().username(), this.passwordValue()); | ||
96 | } | ||
97 | |||
98 | return this._key; | ||
99 | }, | ||
100 | |||
101 | //------------------------------------------------------------------------- | ||
102 | |||
103 | 'keyChecksum': function() { | ||
104 | if (this._keyChecksum == null) { | ||
105 | this._keyChecksum = Clipperz.PM.DataModel.OneTimePassword.computeKeyChecksumWithUsernameAndPassword(this.user().username(), this.passwordValue()); | ||
106 | } | ||
107 | |||
108 | return this._keyChecksum; | ||
109 | }, | ||
110 | */ | ||
111 | //------------------------------------------------------------------------- | ||
112 | |||
113 | 'status': function() { | ||
114 | return this._status; | ||
115 | }, | ||
116 | |||
117 | 'setStatus': function(aValue) { | ||
118 | this._status = aValue; | ||
119 | }, | ||
120 | |||
121 | //------------------------------------------------------------------------- | ||
122 | /* | ||
123 | 'serializedData': function() { | ||
124 | var result; | ||
125 | |||
126 | result = { | ||
127 | 'password': this.password(), | ||
128 | 'created': this.creationDate() ? Clipperz.PM.Date.formatDateWithUTCFormat(this.creationDate()) : null, | ||
129 | 'used': this.usageDate() ? Clipperz.PM.Date.formatDateWithUTCFormat(this.usageDate()) : null, | ||
130 | 'status': this.status() | ||
131 | }; | ||
132 | |||
133 | return result; | ||
134 | }, | ||
135 | |||
136 | //------------------------------------------------------------------------- | ||
137 | |||
138 | 'packedPassphrase': function() { | ||
139 | var result; | ||
140 | var packedPassphrase; | ||
141 | var encodedPassphrase; | ||
142 | varprefixPadding; | ||
143 | var suffixPadding; | ||
144 | var getRandomBytes; | ||
145 | |||
146 | getRandomBytes = MochiKit.Base.method(Clipperz.Crypto.PRNG.defaultRandomGenerator(), 'getRandomBytes'); | ||
147 | |||
148 | encodedPassphrase = new Clipperz.ByteArray(this.user().passphrase()).toBase64String(); | ||
149 | //MochiKit.Logging.logDebug("--- encodedPassphrase.length: " + encodedPassphrase.length); | ||
150 | prefixPadding = getRandomBytes(getRandomBytes(1).byteAtIndex(0)).toBase64String(); | ||
151 | //MochiKit.Logging.logDebug("--- prefixPadding.length: " + prefixPadding.length); | ||
152 | suffixPadding = getRandomBytes((500 - prefixPadding.length - encodedPassphrase.length) * 6 / 8).toBase64String(); | ||
153 | //MochiKit.Logging.logDebug("--- suffixPadding.length: " + suffixPadding.length); | ||
154 | //MochiKit.Logging.logDebug("--- total.length: " + (prefixPadding.length + encodedPassphrase.length + suffixPadding.length)); | ||
155 | |||
156 | packedPassphrase = { | ||
157 | 'prefix': prefixPadding, | ||
158 | 'passphrase': encodedPassphrase, | ||
159 | 'suffix': suffixPadding | ||
160 | }; | ||
161 | |||
162 | // result = Clipperz.Base.serializeJSON(packedPassphrase); | ||
163 | result = packedPassphrase; | ||
164 | //MochiKit.Logging.logDebug("===== OTP packedPassprase: [" + result.length + "]" + result); | ||
165 | //MochiKit.Logging.logDebug("<<< OneTimePassword.packedPassphrase"); | ||
166 | |||
167 | return result; | ||
168 | }, | ||
169 | |||
170 | //------------------------------------------------------------------------- | ||
171 | |||
172 | 'encryptedPackedPassphrase': function() { | ||
173 | return Clipperz.PM.Crypto.deferredEncryptWithCurrentVersion(this.passwordValue(), this.packedPassphrase()) | ||
174 | }, | ||
175 | |||
176 | //------------------------------------------------------------------------- | ||
177 | |||
178 | 'encryptedData': function() { | ||
179 | var deferredResult; | ||
180 | varresult; | ||
181 | |||
182 | //MochiKit.Logging.logDebug(">>> OneTimePassword.encryptedData"); | ||
183 | //MochiKit.Logging.logDebug("--- OneTimePassword.encryptedData - id: " + this.reference()); | ||
184 | result = { | ||
185 | 'reference': this.reference(), | ||
186 | 'key': this.key(), | ||
187 | 'keyChecksum': this.keyChecksum(), | ||
188 | 'data': "", | ||
189 | 'version': Clipperz.PM.Crypto.encryptingFunctions.currentVersion | ||
190 | } | ||
191 | //MochiKit.Logging.logDebug("--- OneTimePassword.encryptedData - 2: " + Clipperz.Base.serializeJSON(result)); | ||
192 | deferredResult = new MochiKit.Async.Deferred(); | ||
193 | //MochiKit.Logging.logDebug("--- OneTimePassword.encryptedData - 3"); | ||
194 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("OneTimePassword.encryptedData - 1: " + res); return res;}); | ||
195 | //# deferredResult.addCallback(Clipperz.PM.Crypto.deferredEncryptWithCurrentVersion, this.passwordValue(), this.packedPassphrase()); | ||
196 | deferredResult.addCallback(MochiKit.Base.method(this, 'encryptedPackedPassphrase')); | ||
197 | //MochiKit.Logging.logDebug("--- OneTimePassword.encryptedData - 4"); | ||
198 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("OneTimePassword.encryptedData - 2: [" + res.length + "]" + res); return res;}); | ||
199 | deferredResult.addCallback(function(aResult, res) { | ||
200 | aResult['data'] = res; | ||
201 | return aResult; | ||
202 | }, result); | ||
203 | //MochiKit.Logging.logDebug("--- OneTimePassword.encryptedData - 5"); | ||
204 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("OneTimePassword.encryptedData - 3: " + Clipperz.Base.serializeJSON(res)); return res;}); | ||
205 | deferredResult.callback(); | ||
206 | //MochiKit.Logging.logDebug("--- OneTimePassword.encryptedData - 6"); | ||
207 | |||
208 | return deferredResult; | ||
209 | }, | ||
210 | |||
211 | //------------------------------------------------------------------------- | ||
212 | |||
213 | 'saveChanges': function() { | ||
214 | var deferredResult; | ||
215 | varresult; | ||
216 | |||
217 | //MochiKit.Logging.logDebug(">>> OneTimePassword.saveChanges"); | ||
218 | result = {}; | ||
219 | deferredResult = new MochiKit.Async.Deferred(); | ||
220 | |||
221 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'updatedProgressState', 'saveOTP_encryptUserData'); | ||
222 | deferredResult.addCallback(MochiKit.Base.method(this.user(), 'encryptedData')); | ||
223 | deferredResult.addCallback(function(aResult, res) { | ||
224 | aResult['user'] = res; | ||
225 | return aResult; | ||
226 | }, result); | ||
227 | |||
228 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'updatedProgressState', 'saveOTP_encryptOTPData'); | ||
229 | deferredResult.addCallback(MochiKit.Base.method(this, 'encryptedData')); | ||
230 | deferredResult.addCallback(function(aResult, res) { | ||
231 | aResult['oneTimePassword'] = res; | ||
232 | return aResult; | ||
233 | }, result); | ||
234 | |||
235 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'updatedProgressState', 'saveOTP_sendingData'); | ||
236 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("OneTimePassword.saveChanges - 1: " + Clipperz.Base.serializeJSON(res)); return res;}); | ||
237 | deferredResult.addCallback(MochiKit.Base.method(this.user().connection(), 'message'), 'addNewOneTimePassword'); | ||
238 | |||
239 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'updatedProgressState', 'saveOTP_updatingInterface'); | ||
240 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("OneTimePassword.saveChanges - 2: " + res); return res;}); | ||
241 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'notify', 'OTPUpdated'); | ||
242 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'oneTimePassword_saveChanges_done', null); | ||
243 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("OneTimePassword.saveChanges - 2: " + res); return res;}); | ||
244 | deferredResult.callback(); | ||
245 | //MochiKit.Logging.logDebug("<<< OneTimePassword.saveChanges"); | ||
246 | |||
247 | return deferredResult; | ||
248 | }, | ||
249 | |||
250 | //------------------------------------------------------------------------- | ||
251 | |||
252 | 'usageDate': function() { | ||
253 | return this._usageDate; | ||
254 | }, | ||
255 | |||
256 | 'setUsageDate': function(aValue) { | ||
257 | this._usageDate = aValue; | ||
258 | }, | ||
259 | |||
260 | //------------------------------------------------------------------------- | ||
261 | |||
262 | 'connectionInfo': function() { | ||
263 | return this._connectionInfo; | ||
264 | }, | ||
265 | |||
266 | 'setConnectionInfo': function(aValue) { | ||
267 | this._connectionInfo = aValue; | ||
268 | }, | ||
269 | |||
270 | //------------------------------------------------------------------------- | ||
271 | |||
272 | 'isExpired': function() { | ||
273 | return (this.usageDate() != null); | ||
274 | }, | ||
275 | |||
276 | //------------------------------------------------------------------------- | ||
277 | |||
278 | 'updateStatusWithValues': function(someValues) { | ||
279 | var result; | ||
280 | |||
281 | result = false; | ||
282 | |||
283 | if (someValues['status'] != this.status()) { | ||
284 | result = true; | ||
285 | } | ||
286 | |||
287 | this.setStatus(someValues['status']); | ||
288 | this.setUsageDate(Clipperz.PM.Date.parseDateWithUTCFormat(someValues['requestDate'])); | ||
289 | this.setConnectionInfo(someValues['connection']); | ||
290 | |||
291 | return result; | ||
292 | }, | ||
293 | */ | ||
294 | //------------------------------------------------------------------------- | ||
295 | __syntaxFix__: "syntax fix" | ||
296 | }); | ||
297 | |||
298 | //############################################################################# | ||
299 | |||
300 | Clipperz.PM.DataModel.OneTimePassword.computeKeyWithUsernameAndPassword = function(anUsername, aPassword) { | ||
301 | return Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aPassword)).toHexString().substring(2); | ||
302 | } | ||
303 | |||
304 | Clipperz.PM.DataModel.OneTimePassword.computeKeyChecksumWithUsernameAndPassword = function(anUsername, aPassword) { | ||
305 | return Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(anUsername + aPassword)).toHexString().substring(2); | ||
306 | } | ||
307 | |||
308 | //============================================================================= | ||
309 | |||
310 | Clipperz.PM.DataModel.OneTimePassword.isValidOneTimePasswordValue = function(aPassword) { | ||
311 | var result; | ||
312 | |||
313 | //"yaxx k7ww - f8y6 tqz5 - 58b6 th44 - 9cwv q0fg" | ||
314 | //console.log("Clipperz.PM.DataModel.OneTimePassword.isValidOneTimePasswordValue", aPassword); | ||
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 = true; | ||
323 | } catch(exception) { | ||
324 | result = false; | ||
325 | } | ||
326 | } else { | ||
327 | result = false; | ||
328 | } | ||
329 | |||
330 | return result; | ||
331 | } | ||
332 | |||
333 | //============================================================================= | ||
334 | |||
335 | Clipperz.PM.DataModel.OneTimePassword.normalizedOneTimePassword = function(aPassword) { | ||
336 | varresult; | ||
337 | |||
338 | if (aPassword.replace(/[\s\-]/g, '').length == 32) { | ||
339 | try { | ||
340 | var passwordByteArray; | ||
341 | |||
342 | passwordByteArray = new Clipperz.ByteArray(); | ||
343 | passwordByteArray.appendBase32String(aPassword); | ||
344 | |||
345 | result = passwordByteArray.toBase64String(); | ||
346 | } catch(exception) { | ||
347 | result = aPassword; | ||
348 | } | ||
349 | } else { | ||
350 | result = aPassword; | ||
351 | } | ||
352 | |||
353 | //console.log("Clipperz.PM.DataModel.OneTimePassword.normalizedOneTimePassword", aPassword, result); | ||
354 | return result; | ||
355 | } | ||
356 | |||
357 | //############################################################################# | ||