libves 2.1.1 → 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,7 +27,7 @@
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
  *
@@ -3540,4 +3540,103 @@ libVES.Flow.prototype.logout = function() {
3540
3540
  return libVES.Flow.logout();
3541
3541
  };
3542
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
+
3543
3642
  module.exports = libVES;
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libves",
3
- "version": "2.1.1",
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": {
package/libves-2.1.1.tgz DELETED
Binary file