summaryrefslogtreecommitdiff
path: root/frontend/gamma/js/Clipperz/PM/PIN.js
Unidiff
Diffstat (limited to 'frontend/gamma/js/Clipperz/PM/PIN.js') (more/less context) (ignore whitespace changes)
-rw-r--r--frontend/gamma/js/Clipperz/PM/PIN.js134
1 files changed, 134 insertions, 0 deletions
diff --git a/frontend/gamma/js/Clipperz/PM/PIN.js b/frontend/gamma/js/Clipperz/PM/PIN.js
new file mode 100644
index 0000000..bc932b2
--- a/dev/null
+++ b/frontend/gamma/js/Clipperz/PM/PIN.js
@@ -0,0 +1,134 @@
1/*
2
3Copyright 2008-2011 Clipperz Srl
4
5This file is part of Clipperz Community Edition.
6Clipperz Community Edition is an online password manager.
7For further information about its features and functionalities please
8refer to http://www.clipperz.com.
9
10* Clipperz Community Edition is free software: you can redistribute
11 it and/or modify it under the terms of the GNU Affero General Public
12 License as published by the Free Software Foundation, either version
13 3 of the License, or (at your option) any later version.
14
15* Clipperz Community Edition is distributed in the hope that it will
16 be useful, but WITHOUT ANY WARRANTY; without even the implied
17 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU Affero General Public License for more details.
19
20* You should have received a copy of the GNU Affero General Public
21 License along with Clipperz Community Edition. If not, see
22 <http://www.gnu.org/licenses/>.
23
24*/
25
26if (typeof(Clipperz) == 'undefined') { Clipperz = {}; }
27if (typeof(Clipperz.PM) == 'undefined') { Clipperz.PM = {}; }
28if (typeof(Clipperz.PM.PIN) == 'undefined') { Clipperz.PM.PIN = {}; }
29
30MochiKit.Base.update(Clipperz.PM.PIN, {
31
32 //-------------------------------------------------------------------------
33
34 '__repr__': function () {
35 return "[" + this.NAME + " " + this.VERSION + "]";
36 },
37
38 //-------------------------------------------------------------------------
39
40 'toString': function () {
41 return this.__repr__();
42 },
43
44 'CREDENTIALS': 'CLIPPERZ.CREDENTIALS',
45 'FAILURE_COUNT': 'CLIPPERZ.FAILED_LOGIN_COUNT',
46 'ALLOWED_RETRY': 3,
47
48 //-------------------------------------------------------------------------
49
50 'isSet': function () {
51 return (this.storedCredentials() != null);
52 },
53
54 'storedCredentials': function () {
55 return localStorage[this.CREDENTIALS];
56 },
57
58 //-------------------------------------------------------------------------
59
60 'recordFailedAttempt': function () {
61 varfailureCount;
62 varresult;
63
64 failureCount = localStorage[this.FAILURE_COUNT];
65
66 if (failureCount == null) {
67 failureCount = 0
68 }
69
70 failureCount ++;
71
72 if (failureCount < this.ALLOWED_RETRY) {
73 localStorage[this.FAILURE_COUNT] = failureCount;
74 result = failureCount;
75 } else {
76 this.removeLocalCredentials();
77 result = -1;
78 }
79
80 return result;
81 },
82
83 'resetFailedAttemptCount': function () {
84 localStorage.removeItem(this.FAILURE_COUNT);
85 },
86
87 'failureCount': function () {
88 return localStorage[this.FAILURE_COUNT];
89 },
90
91 //-------------------------------------------------------------------------
92
93 'deriveKeyFromPin': function (aPIN) {
94 return Clipperz.Crypto.SHA.sha256(new Clipperz.ByteArray(aPIN));
95 },
96
97 'credentialsWithPIN': function (aPIN) {
98 varbyteArrayValue;
99 var decryptedValue;
100 varresult;
101
102 byteArrayValue = (new Clipperz.ByteArray()).appendBase64String(localStorage[this.CREDENTIALS]);
103 decryptedValue = Clipperz.Crypto.AES.decrypt(this.deriveKeyFromPin(aPIN), byteArrayValue).asString();
104 try {
105 result = Clipperz.Base.evalJSON(decryptedValue);
106 } catch (error) {
107 result = {'username':'fakeusername', 'passphrase':'fakepassphrase'};
108 }
109
110 return result;
111 },
112
113 'setCredentialsWithPIN': function (aPIN, someCredentials) {
114 varencodedValue;
115 varbyteArrayValue;
116 var encryptedValue;
117
118 encodedValue = Clipperz.Base.serializeJSON(someCredentials);
119 byteArrayValue = new Clipperz.ByteArray(encodedValue);
120 encryptedValue = Clipperz.Crypto.AES.encrypt(this.deriveKeyFromPin(aPIN), byteArrayValue).toBase64String();
121
122 localStorage[this.CREDENTIALS] = encryptedValue;
123 },
124
125 'removeLocalCredentials': function () {
126 localStorage.removeItem(this.CREDENTIALS);
127 localStorage.removeItem(this.FAILURE_COUNT);
128 },
129
130 //-------------------------------------------------------------------------
131 __syntaxFix__: "syntax fix"
132
133});
134