-
Notifications
You must be signed in to change notification settings - Fork 1
/
libVES.Flow.js
288 lines (261 loc) · 9.11 KB
/
libVES.Flow.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/***************************************************************************
* ___ ___
* / \ / \ VESvault
* \__ / \ __/ Encrypt Everything without fear of losing the Key
* \\ // https://vesvault.com https://ves.host
* \\ //
* ___ \\_//
* / \ / \ libVES.Flow: Cross-origin private context manager
* \__ / \ __/
* \\ //
* \\ //
* \\_//
* / \
* \___/
*
*
* (c) 2022 VESvault Corp
* Jim Zubov <[email protected]>
*
* GNU General Public License v3
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* @title libVES.Flow
* @ Securely maintain private session data across multiple https origins
* @version 0.1a
*
* @author Jim Zubov <[email protected]> (VESvault Corp)
*
***************************************************************************/
libVES.Flow = function(name, optns) {
this.name = name;
this.origin = document.location.origin;
this.url = this.origin + '/VESflow';
if (optns) for (var k in optns) this[k] = optns[k];
return this;
};
libVES.Flow.algo = {name: 'ECDH', namedCurve: 'P-256'};
libVES.Flow.keyAlgo = function(jwk) {
if (jwk && jwk.kty == 'EC') return {name: 'ECDH', namedCurve: jwk.crv};
return null;
};
libVES.Flow.toOrigin = function(url) {
var a = document.createElement('a');
a.href = url;
return a.origin;
};
libVES.Flow.jwkPub = function(jwk) {
delete(jwk.d);
delete(jwk.key_ops);
delete(jwk.ext);
return jwk;
};
libVES.Flow.parseToken = function(src, callbk) {
src = src.replace(/\#VESflow\.(\w+)=([^\#\&\=]+)/g, function(s, k, v) {
var tk;
try {
tk = JSON.parse(decodeURIComponent(v));
if (callbk(decodeURIComponent(k), tk)) return '';
} catch(e) {
console.log(e);
}
return s;
});
return src;
};
libVES.Flow.logout = function() {
for (var k in sessionStorage) if (k.match(/^VESflow\b/)) delete(sessionStorage[k]);
};
libVES.Flow.prototype.source = function(src) {
if (src != null) history.replaceState(null, '', src);
return document.location.href;
};
libVES.Flow.prototype.privKey = function(algo) {
if (!algo) algo = libVES.Flow.algo;
var a = JSON.stringify(algo);
return Promise.resolve(sessionStorage['VESflow' + a]).then(function(k) {
if (!k) throw null;
return crypto.subtle.importKey('jwk', JSON.parse(k), algo, true, ['deriveBits', 'deriveKey']);
}).catch(function(e) {
return crypto.subtle.generateKey(algo, true, ['deriveBits', 'deriveKey']).then(function(pair) {
return crypto.subtle.exportKey('jwk', pair.privateKey).then(function(jwk) {
sessionStorage['VESflow' + a] = JSON.stringify(jwk);
return pair.privateKey;
});
});
});
};
libVES.Flow.prototype.pubJWK = function(org) {
return ((!org || org == this.origin) ? this.privKey().then(function(priv) {
return crypto.subtle.exportKey('jwk', priv).then(function(jwk) {
return libVES.Flow.jwkPub(jwk);
});
}) : Promise.resolve(sessionStorage['VESflow|' + org]).then(function(k) {
if (!k) throw new libVES.Error('NotFound', 'No public key stored for ' + org);
return JSON.parse(k);
}));
};
libVES.Flow.prototype.pubKey = function(org) {
return this.pubJWK(org).then(function(jwk) {
return crypto.subtle.importKey('jwk', jwk, libVES.Flow.keyAlgo(jwk), true, []);
});
};
libVES.Flow.prototype.cipher = function(url) {
var self = this;
return self.pubKey(libVES.Flow.toOrigin(url)).then(function(pub) {
return self.privKey(pub.algorithm).then(function(priv) {
return crypto.subtle.deriveBits({name: 'ECDH', public: pub}, priv, 8 * libVES.Algo.ECDH.curveBytes[pub.algorithm.namedCurve]);
});
}).then(function(raw) {
return crypto.subtle.digest({name: 'SHA-256'}, raw).then(function(buf) {
return crypto.subtle.importKey('raw', buf, 'AES-GCM', false, ['encrypt', 'decrypt']);
});
});
};
libVES.Flow.prototype.encrypt = function(url, val) {
var self = this;
return Promise.resolve(val || this.value()).then(function(val) {
if (val == null) throw new libVES.Error('NoData', 'Empty value');
return self.cipher(url).then(function(ci) {
var iv = new Uint8Array(12);
crypto.getRandomValues(iv);
return crypto.subtle.encrypt({name: 'AES-GCM', iv: iv.buffer}, ci, libVES.Util.StringToByteArray(JSON.stringify(val))).then(function(ctext) {
return libVES.Util.ByteArrayToB64W(iv) + '.' + libVES.Util.ByteArrayToB64W(ctext);
});
});
});
};
libVES.Flow.prototype.token = function(url) {
var self = this;
return this.pubJWK().then(function(jwk) {
return {url: self.url, key: jwk};
}).then(function(tk) {
return self.encrypt(url).then(function(ctext) {
tk.enc = ctext;
self.erase(url);
self.sent = true;
return tk;
}).catch(function(e) {
self.store(url).catch(function(e) {});
return tk;
});
});
};
libVES.Flow.prototype.addToken = function(url) {
var self = this;
return self.token(url).then(function(tk) {
return url + '#VESflow.' + self.name + '=' + encodeURIComponent(JSON.stringify(tk));
});
};
libVES.Flow.prototype.decrypt = function(ctext, url) {
var self = this;
var c = ctext.split('.');
return self.cipher(url).then(function(ci) {
return crypto.subtle.decrypt({name: 'AES-GCM', iv: libVES.Util.B64ToByteArray(c[0])}, ci, libVES.Util.B64ToByteArray(c[1])).then(function(ptext) {
return JSON.parse(libVES.Util.ByteArrayToString(ptext));
});
});
};
libVES.Flow.prototype.value = function() {
var self = this;
return Promise.resolve(history.state && history.state['VESflow.' + self.name]).then(function(ctext) {
return self.decrypt(ctext);
});
};
libVES.Flow.prototype.setValue = function(val) {
var self = this;
return Promise.resolve(val? self.encrypt(null, val) : null).then(function(ctext) {
var s = {};
if (history.state) for (var k in history.state) s[k] = history.state[k];
if (ctext) s['VESflow.' + self.name] = ctext;
else delete(s['VESflow.' + self.name]);
history.replaceState(s, '');
return val;
});
};
libVES.Flow.prototype.click = function(a) {
var self = this;
(a.origin == this.origin ? self.store() : self.addToken(a.href).then(function(url) {
a.href = url;
})).catch(function(e) {
if (!e || e.code != 'NotFound') throw e;
}).then(function() {
document.location.href = a.href;
}).catch(function(e) {
console.log(e);
});
return false;
};
libVES.Flow.prototype.reload = function(e) {
if (!this.sent) this.store().catch(function(e) {});
};
libVES.Flow.prototype.recv = function(dval) {
var self = this;
return Promise.resolve(self.source()).then(function(src) {
var tk = null;
src = libVES.Flow.parseToken(src, function(k, t) {
if (k != self.name) return false;
tk = t;
return true;
});
if (tk == null) throw new libVES.Error('NotFound', 'No valid token in the source url');
return Promise.resolve(self.source(src)).then(function() {
if (dval) for (var k in dval) if (tk[k] === undefined) tk[k] = dval[k];
return self.recvToken(tk);
});
});
};
libVES.Flow.prototype.recvToken = function(tk) {
var org = libVES.Flow.toOrigin(tk.url);
if (org != this.origin && tk.key) sessionStorage['VESflow|' + org] = JSON.stringify(tk.key);
if (!tk.enc) return Promise.reject(new libVES.Error('Incomplete', 'Missing encrypted data', {url: tk.url}));
return this.decrypt(tk.enc, org);
};
libVES.Flow.prototype.get = function() {
var self = this;
return self.fetch().catch(function(e) {
if (!e || e.code != 'NotFound') console.log(e);
return null;
}).then(function(data) {
return self.recv().catch(function(e) {
if (e && e.code == 'Incomplete') {
self.url = document.location.href;
return self.addToken(e.url).then(function(url) {
document.location.replace(url);
throw new libVES.Error('Redirect', 'Completing the key exchange');
});
}
if (!data) throw e;
return data;
});
});
};
libVES.Flow.prototype.store = function(url) {
if (history.state && history.state['VESflow.' + this.name]) {
this.sent = 1;
return Promise.resolve(sessionStorage['VESflow.' + this.name + '>' + libVES.Flow.toOrigin(url)] = history.state['VESflow.' + this.name]);
}
return Promise.reject(new libVES.Error('NotFound', 'Value is not set'));
};
libVES.Flow.prototype.fetch = function(url) {
var self = this;
return Promise.resolve(sessionStorage['VESflow.' + self.name + '>' + libVES.Flow.toOrigin(url)]).then(function(ctext) {
if (!ctext) throw new libVES.Error('NotFound', 'Not in the session storage');
return self.decrypt(ctext).then(function(data) {
self.erase(url);
return data;
});
});
};
libVES.Flow.prototype.erase = function(url) {
delete(sessionStorage['VESflow.' + this.name + '>' + libVES.Flow.toOrigin(url)]);
};
libVES.Flow.prototype.logout = function() {
this.setValue(undefined);
return libVES.Flow.logout();
};