-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
195 lines (172 loc) · 5.46 KB
/
app.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
function redirect(params, token) {
var url = params.redirect_uri + (token ? '#access_token=' + encodeURIComponent(token) : '');
document.location = url;
}
var _afterRender = [];
function afterRender(callback) { _afterRender.push(callback); }
function renderTemplate(name, locals) {
var el = document.querySelector('script[data-template-name="' + name + '"]');
if(! el) {
var templateNames = Array.prototype.map.call(
document.querySelectorAll('script[data-template-name]'),
function(template) {
return template.getAttribute('data-template-name');
}
);
throw "Template not found: " + name + " (have: " + templateNames.join(', ') + ")";
}
try {
document.getElementById('wrapper').innerHTML = Mustache.render(el.innerHTML, locals);
} catch(exc) {
console.error("Failed to render template: " + exc.message);
}
setTimeout(function() {
_afterRender.forEach(function(callback) { callback(); });
}, 0);
}
function attachAction(element, handler) {
if(element.tagName === 'FORM') {
element.onsubmit = handler;
} else {
element.onclick = handler;
}
}
function setAction(name, handler) {
var element = document.querySelector('*[data-action="' + name + '"]');
if(! element) {
throw "Action element not found: " + name;
}
attachAction(element, handler);
}
function setEachAction(name, handler) {
var els = document.querySelectorAll('*[data-action="' + name + '"]');
for(var i=0;i<els.length;i++) {
attachAction(els[i], handler);
}
}
function dialog(sessionKey, params) {
console.log("SCOPE", params.scope);
var viewLocals = {
origin: params.origin,
scope: []
};
for(var key in params.scope) {
viewLocals.scope.push({
category: key,
mode: params.scope[key] == 'rw' ? 'read, write' : 'read'
});
}
renderTemplate('dialog', viewLocals);
setAction('allow', function() {
sockethubClient.getBearerToken(sessionKey, params.origin, params.scope).
then(function(token) {
redirect(params, token);
}, function(error) {
displayError('getBearerToken failed: ' + error.message);
});
});
setAction('reject', function() { redirect(params); });
}
function extractOrigin(uri) {
var md = uri.match(/^(https?:\/\/[^\/]+)\//);
if(md) {
return md[1];
} else {
throw "Invalid http(s) URI: " + uri;
}
}
function parseScope(scopeString) {
return scopeString.split(' ').reduce(function(m, part) {
var kv = part.split(':');
m[kv[0]] = kv[1];
return m;
}, {});
}
function renderListing(sessionKey) {
renderTemplate('list', { loading: true });
sockethubClient.listBearerTokens(sessionKey).then(function(response) {
var tokens = response.object.tokens;
renderTemplate('list', { authorizations: tokens, empty: !tokens.length });
setEachAction('revoke', function(evt) { alert('not implemented!'); });
}, function(error) { displayError('listBearerTokens failed: ' + error.message); });
}
function displayError(msg) {
var el = document.getElementById('error');
el.textContent = msg;
el.style.display = 'block';
}
function main(sessionKey) {
var md = document.location.search.match(/\?(.+)/);
afterRender(function() {
setAction('logout', function() {
delete localStorage.unhtAuthSessionKey;
document.location = '/';
});
});
if(md) {
var params = md[1].split('&').reduce(function(m, kvs) {
var kv = kvs.split('=');
m[decodeURIComponent(kv[0])] = decodeURIComponent(kv[1]);
return m;
}, {});
if(params.redirect_uri && params.scope) {
params.origin = extractOrigin(params.redirect_uri);
params.scope = parseScope(params.scope);
dialog(sessionKey, params);
}
} else {
renderListing(sessionKey);
}
}
window.onload = function() {
sockethubClient = SockethubClient.connect({
host: document.body.getAttribute('data-sockethub-host'),
ssl: true,
register: {
secret: document.body.getAttribute('data-sockethub-secret')
}
});
sockethubClient.declareVerb('getSession', ['object.email', 'object.password'], {
platform: 'customer',
object: {}
});
sockethubClient.declareVerb('listBearerTokens', ['object.sessionKey'], {
platform: 'customer',
object: {}
});
sockethubClient.declareVerb('getBearerToken', ['object.sessionKey', 'object.origin', 'object.scope'], {
platform: 'customer',
object: {}
});
sockethubClient.declareVerb('revokeBearerToken', ['object.sessionKey', 'object.origin', 'object.token'], {
platform: 'customer',
object: {}
});
sockethubClient.on('failed', function() {
alert("Failed to connect to sockethub!");
});
sockethubClient.on('registered', function() {
if(localStorage.unhtAuthSessionKey) {
main(localStorage.unhtAuthSessionKey);
} else {
renderTemplate('login', { host: document.body.getAttribute('data-sockethub-host') });
document.querySelector('input[name=email]').focus();
setAction('login', function(evt) {
evt.preventDefault();
sockethubClient.getSession(
evt.target.email.value,
evt.target.password.value
).then(function(response) {
if(response.status) {
localStorage.unhtAuthSessionKey = response.object.sessionKey;
main(localStorage.unhtAuthSessionKey);
} else {
displayError("Login failed: " + response.message);
}
}, function(error) {
displayError('getSession failed: ' + error.message);
});
});
}
});
};