forked from duerig/xml-signer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
geni-auth.js
executable file
·140 lines (123 loc) · 4.08 KB
/
geni-auth.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
var genilib = {};
genilib.trustedHost = 'https://www.emulab.net';
genilib.trustedPath = '/protogeni/speaks-for/index.html';
genilib.authorize = function(idOrObject, cert, callback, defaultMA, userBundle)
{
var id;
var authenticate;
// If the user invokes with just a single object, it is a dictionary of options
if (typeof(idOrObject) === 'object')
{
id = idOrObject.id;
cert = idOrObject.toolCertificate;
callback = idOrObject.complete;
defaultMA = idOrObject.defaultMA;
userBundle = idOrObject.userBundle;
authenticate = idOrObject.authenticate;
console.log('set args based on dictionary');
}
else
{
id = idOrObject;
}
shouldAuthenticate = (authenticate !== undefined)
var wrapper = {};
wrapper.other = window.open(genilib.trustedHost + genilib.trustedPath +
'?id=' +
encodeURIComponent(id),
'GENI Tool Authorization',
'height=600,width=800');
wrapper.listener = function (event) {
var data;
if (event.source === wrapper.other &&
event.origin === genilib.trustedHost &&
event.data.ready)
{
data = {
certificate: cert,
tool: true,
auth: shouldAuthenticate
};
if (userBundle)
{
data.userBundle = userBundle;
}
/* Include the default MA if specified. */
if (defaultMA && defaultMA.url && defaultMA.name) {
data.ma = {};
data.ma.url = defaultMA.url;
data.ma.name = defaultMA.name;
}
console.log('Sending cert to ' + genilib.trustedHost);
wrapper.other.postMessage(data, genilib.trustedHost);
}
else if (event.source === wrapper.other &&
event.origin === genilib.trustedHost &&
event.data.id && event.data.id === id && event.data.credential)
{
wrapper.credential = event.data.credential;
wrapper.credentialId = event.data.id;
wrapper.authenticationToken = event.data.userToken;
wrapper.encryptedCredential = event.data.encryptedCredential;
if (shouldAuthenticate && event.data.userCertificate)
{
console.log('authenticating');
authenticate(event.data.userCertificate, wrapper.authenticationToken, wrapper.toolAuthSuccess, wrapper.toolAuthFailure);
}
else
{
console.log('sendAck');
wrapper.sendAck();
}
}
};
wrapper.sendAck = function () {
window.removeEventListener('message', wrapper.listener, false);
// wrapper.other.removeEventListener('close', wrapper.close, false);
data = {
id: wrapper.credentialId,
ack: true
};
console.log('Sending ack to ' + genilib.trustedHost);
wrapper.other.postMessage(data, genilib.trustedHost);
callback(wrapper.credential, wrapper.encryptedCredential);
};
wrapper.toolAuthSuccess = function (toolToken) {
data = {
id: wrapper.credentialId,
toolToken: toolToken
};
console.log('Sending tool token to ' + genilib.trustedHost);
wrapper.other.postMessage(data, genilib.trustedHost);
};
wrapper.toolAuthFailure = function () {
wrapper.sendAck();
};
wrapper.close = function (event) {
window.removeEventListener('message', wrapper.message, false);
// wrapper.other.removeEventListener('close', wrapper.close, false);
};
window.addEventListener('message', wrapper.listener, false);
// wrapper.other.addEventListener('close', wrapper.close, false);
// Return the other window so the caller can notice when it's gone
// or put up UI directing the user's attention to the other window
return wrapper.other;
};
genilib.sendCertificate = function (cert)
{
var options = {
certificate: cert,
authority: true
};
window.opener.postMessage(options, '*'/*genilib.trustedHost*/);
window.close();
};
genilib.sendCredential = function (cred)
{
var options = {
sfcred: cred,
authority: true
};
window.opener.postMessage(options, '*'/*genilib.trustedHost*/);
window.close();
};