libves 2.0.3 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/libVES.node.js CHANGED
@@ -27,20 +27,24 @@
27
27
  *
28
28
  * @title libVES
29
29
  * @dev A JavaScript end-to-end encryption interface to VESvault REST API
30
- * @version 2.00
30
+ * @version 2.01
31
31
  *
32
32
  * @dev Official source code: https://github.com/vesvault/libVES
33
33
  *
34
34
  * @author Jim Zubov <jz@vesvault.com> (VESvault Corp)
35
35
  *
36
36
  ***************************************************************************/
37
+ crypto = require('crypto');
38
+ crypto.subtle = require('subtle');
39
+ XMLHttpRequest = require('xhr2');
37
40
 
38
- if (!window.libVES) window.libVES = function(optns) {
41
+
42
+ if (typeof(libVES) != 'function') function libVES(optns) {
39
43
  try {
40
- if (!window.crypto.subtle.digest) throw new libVES.Error('Init','crypto.subtle is improperly implemented?');
44
+ if (!crypto.subtle.digest) throw new libVES.Error('Init', 'crypto.subtle is unavailable or improperly implemented');
41
45
  } catch (e) {
42
46
  if (e instanceof libVES.Error) throw e;
43
- throw new libVES.Error('Init','crypto.subtle is not usable' + (document.location.protocol.match(/https/) ? '' : ' (try https?)'));
47
+ throw new libVES.Error('Init', 'crypto.subtle is not available');
44
48
  }
45
49
  for (var k in optns) this[k] = optns[k];
46
50
  if (this.domain) this.type = 'secondary';
@@ -586,7 +590,7 @@ libVES.prototype = {
586
590
  }
587
591
  if (!optns || !optns.n) return Promise.reject(new libVES.Error('InvalidValue','optns.n must be an integer'));
588
592
  var rkey = new Uint8Array(32);
589
- window.crypto.getRandomValues(rkey);
593
+ crypto.getRandomValues(rkey);
590
594
  var algo = optns.v ? libVES.Scramble.algo[optns.v] : libVES.Scramble.RDX;
591
595
  if (!algo) return Promise.reject(new libVES.Error('InvalidValue','Unknown scramble algorithm: ' + optns.v));
592
596
  var s = new algo(optns.n);
@@ -1587,7 +1591,7 @@ libVES.Math.Shifter.prototype = {
1587
1591
 
1588
1592
  libVES.Object = function(data) {
1589
1593
  for (var k in data) this[k] = data[k];
1590
- if (window.Trigger) this.trigger = Trigger.resolve(this);
1594
+ if (typeof(Trigger) != 'undefined') this.trigger = Trigger.resolve(this);
1591
1595
  };
1592
1596
 
1593
1597
  libVES.Object.prototype = {
@@ -2426,7 +2430,6 @@ libVES.Algo = {
2426
2430
  case 'public':
2427
2431
  return crypto.subtle.exportKey('spki', data).then(function(der) {
2428
2432
  var asn = libVES.Util.ASN1.decode(new Uint8Array(der))[0];
2429
- console.log(asn);
2430
2433
  switch (String(asn[0][0])) {
2431
2434
  case '1.2.840.10045.2.1':
2432
2435
  return der;
@@ -2546,7 +2549,7 @@ libVES.Algo = {
2546
2549
  },
2547
2550
  acquire: function(key,optns) {
2548
2551
  return Promise.resolve(key).then(function(k) {
2549
- if (k instanceof window.CryptoKey) return k;
2552
+ if (typeof(CryptoKey) != 'undefined' && (k instanceof CryptoKey)) return k;
2550
2553
  else if (typeof(k) == 'object') {
2551
2554
  if (k.privateKey) return k.privateKey;
2552
2555
  else if (k.publicKey) return k.publicKey;
@@ -2838,7 +2841,7 @@ libVES.Scramble.RDX.prototype = {
2838
2841
  var u = [sc];
2839
2842
  for (var i = 1; i < this.size; i++) {
2840
2843
  u[i] = new Uint8Array(sc.length);
2841
- window.crypto.getRandomValues(u[i]);
2844
+ crypto.getRandomValues(u[i]);
2842
2845
  }
2843
2846
  var v = [];
2844
2847
  for (var i = 0; i < this.size - 1; i++) v[i] = (function(ui,ui1) {
@@ -3537,4 +3540,103 @@ libVES.Flow.prototype.logout = function() {
3537
3540
  return libVES.Flow.logout();
3538
3541
  };
3539
3542
 
3543
+
3544
+ libVES.Auth = function(optns) {
3545
+ for (var k in optns) this[k] = optns[k];
3546
+ return this;
3547
+ };
3548
+
3549
+ libVES.Auth.prototype.getToken = function() {
3550
+ if (this.vaultItem) return Promise.all([this.vaultItem.getId(), this.vaultItem.getField('verifyToken')]).then(function(flds) {
3551
+ return 'vaultItem.' + flds[0] + '.' + flds[1];
3552
+ });
3553
+ else if (this.vaultKey) return Promise.all([this.vaultKey.getId(), this.vaultItem.getSessionToken()]).then(function(flds) {
3554
+ return 'vaultKey.' + flds[0] + '.' + flds[1];
3555
+ });
3556
+ else if ((VES = this.VES) && VES.token) return this.VES.getVaultKey().then(function(vkey) {
3557
+ return vkey.getId().then(function(id) {
3558
+ return 'vaultKey.' + id + '.' + VES.token;
3559
+ });
3560
+ });
3561
+ return Promise.reject({code: 'VESauth', message: 'No object to generate a VESauth token for'});
3562
+ };
3563
+
3564
+ libVES.Auth.prototype.getData = function(url, type) {
3565
+ return this.getToken().then(function(tkn) {
3566
+ return new Promise(function(resolve, reject) {
3567
+ var xhr = new XMLHttpRequest();
3568
+ xhr.open('GET', url);
3569
+ if (type) xhr.responseType = type;
3570
+ xhr.onreadystatechange = function() {
3571
+ switch(xhr.readyState) {
3572
+ case 4:
3573
+ if (xhr.status == 200) resolve(xhr.response);
3574
+ else reject({code: xhr.status, message: xhr.statusText});
3575
+ }
3576
+ };
3577
+ xhr.setRequestHeader('X-VES-Authorization', tkn);
3578
+ xhr.send();
3579
+ });
3580
+ });
3581
+ };
3582
+
3583
+ libVES.Auth.prototype.getJSON = function(url) {
3584
+ return this.getData(url, 'json').then(function(json) {
3585
+ var a = document.createElement('a');
3586
+ a.href = url;
3587
+ if (!a.hash) return json;
3588
+ var path = a.hash.split(/\//);
3589
+ for (var i = 1; i < path.length; i++) if (path[i]) {
3590
+ if (json instanceof Array) json = json[Number(path[i])];
3591
+ else if (json instanceof Object) json = json[path[i]];
3592
+ else return null;
3593
+ }
3594
+ return json;
3595
+ });
3596
+ };
3597
+
3598
+ libVES.Auth.prototype.auth = function(token) {
3599
+ return this.verify(token, true);
3600
+ };
3601
+
3602
+ libVES.Auth.prototype.verify = function(token, auth) {
3603
+ var tk = token.match(/^(\w*)\.(\d*)\.(.*)$/);
3604
+ if (!tk) return Promise.reject(new libVES.Error('VESauth', 'Invalid VESauth token'));
3605
+ var ves = new libVES({domain: (this.domain || '*'), token: tk[3]});
3606
+ if (tk[1] == 'vaultItem') {
3607
+ if (auth) return Promise.reject(new libVES.Error('VESauth', 'Verify token cannot be used for authentication'));
3608
+ return Promise.resolve(this.vaultItem ? this.vaultItem.getId() : tk[2]).then(function(id) {
3609
+ var vi = new libVES.VaultItem({id: id}, ves);
3610
+ return vi.getFile().then(function(file) {
3611
+ return vi.getField('deleted').then(function(f) {
3612
+ if (f) throw new libVES.Error('VESauth', 'Verify token expired');
3613
+ return file.getCreator();
3614
+ });
3615
+ });
3616
+ });
3617
+ }
3618
+ if (tk[1] != 'vaultKey') return Promise.reject(new libVES.Error('VESauth', 'Unknown token type'));
3619
+ if (this.vaultItem) return this.vaultItem.getId().then(function(id) {
3620
+ var vi = new libVES.VaultItem({id: id}, ves);
3621
+ return vi.getVaultEntries().then(function(vents) {
3622
+ if (!auth && vents && vents.length) return ves.me();
3623
+ if (vents && tk[2]) for (var i = 0; i < vents.length; i++) if (vents[i].vaultKey && Number(vents[i].vaultKey.id) == Number(tk[2])) return ves.me();
3624
+ throw new libVES.Error('VESauth', 'ACL not authorized');
3625
+ });
3626
+ });
3627
+ if (!auth) return ves.me();
3628
+ return Promise.resolve(this.vaultKey ? this.vaultKey.getId() : tk[2]).then(function(id) {
3629
+ return (new libVES.VaultKey({id: id}, ves)).getExternals().then(function(ext) {
3630
+ if (!ext || !ext[0]) throw new libVES.Error('VESauth', 'Bad vaultKey for VESauth');
3631
+ return ext[0].getDomain().then(function(dom) {
3632
+ return ext[0].getExternalId().then(function(extid) {
3633
+ if (!extid || !extid.match(/^[^\!\@]+\@[^\!\@]+$/)) throw new libVES.Error('VESauth', 'Bad vaultKey for VESauth');
3634
+ if (dom && dom.toLowerCase() == ves.domain.toLowerCase()) return ves.me();
3635
+ throw new libVES.Error('VESuath', 'VES domain does not match');
3636
+ });
3637
+ });
3638
+ });
3639
+ });
3640
+ };
3641
+
3540
3642
  module.exports = libVES;
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libves",
3
- "version": "2.0.3",
3
+ "version": "2.1.2",
4
4
  "description": "Simple, safe and secure end-to-end encryption for data at-rest",
5
5
  "main": "libVES.node.js",
6
6
  "scripts": {
@@ -17,9 +17,15 @@
17
17
  "e2e",
18
18
  "at-rest",
19
19
  "end-to-end",
20
- "browser encryption",
20
+ "browser",
21
+ "browser-encryption",
21
22
  "pin"
22
23
  ],
24
+ "dependencies": {
25
+ "crypto": "*",
26
+ "subtle": "*",
27
+ "xh2r": "*"
28
+ },
23
29
  "author": "jz@vesvault.com",
24
30
  "license": "GPL-3.0",
25
31
  "bugs": {
package/libves-2.0.3.tgz DELETED
Binary file