forked from vesvault/libVES
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibVES.Delegate.js
More file actions
144 lines (144 loc) · 5.96 KB
/
libVES.Delegate.js
File metadata and controls
144 lines (144 loc) · 5.96 KB
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
/***************************************************************************
* ___ ___
* / \ / \ VESvault
* \__ / \ __/ Encrypt Everything without fear of losing the Key
* \\ // https://vesvault.com https://ves.host
* \\ //
* ___ \\_//
* / \ / \ libVES: VESvault API library
* \__ / \ __/
* \\ //
* \\ //
* \\_// - Key Management and Exchange
* / \ - Item Encryption and Sharing
* \___/ - VESrecovery (TM)
*
*
* (c) 2018 VESvault Corp
* Jim Zubov <jz@vesvault.com>
*
* 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.
*
* libVES.Delegate.js libVES: Delegate login via VESvault
*
***************************************************************************/
libVES.Delegate = {
html: '<div style="position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.5);display:table;z-index:200000;">'
+ '<div style="display:table-row;"><div style="display:table-cell;vertical-align:middle;text-align:center;">'
+ '<div style="min-width:320px;max-width:640px;;background-color:white;margin: auto;padding: 30px;">'
+ '<p>Use the VESvault popup window to grant access to the App Vault</p>'
+ '<p class="VESvaultDelegateBlockerMsg" style="color: #bf7f00; font-style:italic;"> </p>'
+ '<p><a class="VESvaultDelegateRetryLnk" href="{$url}" target="VESvaultDelegate" onclick="return !libVES.Delegate.retryPopup(this.href,this)">Click here</a> if you can\'t see VESvault popup window</p>'
+ '<p><a class="VESvaultDelegateCancelLnk" href="#" onclick="libVES.Delegate.cancel(); return false;">Cancel</a></p>'
+ '</div></div></div></div>',
htmlBlockerMsg: 'Looks like your browser is using a popup blocker...',
name: 'VESvaultDelegate',
login: function(VES,challenge,optns) {
if (this.popup) return this.response || Promise.reject(new libVES.Error('InvalidValue','The delegate popup is already open'));
if (!challenge) {
} else try {
var info = document.location.search.match(/[\?\&]VESvaultDelegate=([^\&]+)/)[0];
} catch(e) {}
this.VES = VES;
var self = this;
return this.response = new Promise(function(resolve,reject) {
self.reject = reject;
self.resolve = resolve;
var url = VES.wwwUrl + 'session/delegate/' + escape(VES.app) + '/' + escape(VES.domain);
self.matchOrigin = (function(m) { return m ? m[0] : document.location.protocol + '//' + document.location.host; })(url.match(/^(https\:\/\/[^\/\?\#]+)/));
self.popup = document.createElement('DIV');
self.popup.innerHTML = self.html.replace('{$url}',url);
document.getElementsByTagName('BODY')[0].appendChild(self.popup);
self.retryPopupCalled = 0;
try {
document.getElementsByClassName('VESvaultDelegateRetryLnk')[0].onclick = function() {
return !libVES.Delegate.retryPopup(this.href,this);
};
document.getElementsByClassName('VESvaultDelegateCancelLnk')[0].onclick = function() {
libVES.Delegate.cancel();
return false;
};
} catch(e) {}
if (!self.openPopup(url)) try {
document.getElementsByClassName('VESvaultDelegateBlockerMsg')[0].innerHTML = self.htmlBlockerMsg;
} catch(e) {
window.alert(self.htmlBlockerMsg);
}
window.addEventListener('message',self.listener.bind(self));
window.addEventListener('focus',self.chkCancel.bind(self));
window.addEventListener('beforeunload',self.cancel.bind(self));
window.clearInterval(self.popupInterval);
self.popupInterval = window.setInterval(self.chkCancel.bind(self),1000);
});
},
openPopup: function(url) {
return this.popupWindow = window.open(url,this.name,"width=600,height=600,top=100,left=100");
},
retryPopup: function(url,href) {
var f = this.retryPopupCalled;
this.retryPopupCalled++;
if (href && f > 1) href.target = '_blank';
else if (this.popupWindow) try {
this.popupWindow.focus();
} catch(e) {}
return !f && this.openPopup(url);
},
listener: function(evnt) {
if (this.popupWindow && evnt.origin == this.matchOrigin) try {
var msg = JSON.parse(evnt.data);
var VES = this.VES;
if (msg.externalId) {
VES.externalId = msg.externalId;
this.resolve(VES.unlock(msg.VESkey).then(function() {
return VES;
}));
} else if (msg.token) {
VES.token = msg.token;
this.resolve(VES.getSecondaryKey({domain:VES.domain},true).then(function(vaultKey) {
return vaultKey.getExternals().then(function(externals) {
return Promise.all(externals.map(function(ext,i) {
return ext.getDomain();
})).then(function(domains) {
for (var i = 0; i < domains.length; i++) if (domains[i] == VES.domain) return externals[i].getExternalId();
throw new libVES.Error('Internal','No external id found for newly created secondary key');
}).then(function(extId) {
VES.externalId = extId;
return VES;
});
});
}));
} else return;
this.close();
if (!evnt.source.closed) evnt.source.close();
} catch(e) {}
},
close: function() {
if (this.popup) {
if (this.popupWindow) {
try {
this.popupWindow.close();
} catch (e) {}
this.popupWindow = null;
}
window.clearInterval(this.popupInterval);
this.popupInterval = null;
this.popup.parentNode.removeChild(this.popup);
this.popup = null;
return self.response;
}
},
cancel: function() {
var rs = this.close();
if (this.response && this.reject) this.reject(new libVES.Error('Aborted','VESvault login is cancelled'));
return rs;
},
chkCancel: function() {
if (this.popupWindow && this.popupWindow.closed) this.cancel();
}
};