This repository has been archived by the owner on Aug 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 263
/
issuer.js
376 lines (338 loc) · 12.3 KB
/
issuer.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
var _ = require('underscore');
var request = require('request');
var fs = require('fs');
var url = require('url');
var logger = require('../lib/logging').logger;
var reverse = require('../lib/router').reverse;
var awardBadge = require('../lib/award');
var remote = require('../lib/remote');
var Badge = require('../models/badge.js');
var regex = require('../lib/regex.js');
/**
* Fully qualify a url.
*
* @param {String} pathOrUrl either a path like /what.html or a full url
* @param {String} origin a full quallified url
* @return {String} a fully qualified url, using parts from the origin if
* the original `pathOrUrl` was just a path.
*/
function qualifyUrl(pathOrUrl, origin) {
var parts = url.parse(pathOrUrl);
if (!parts.hostname) {
var originParts = url.parse(origin);
parts.host = originParts.host;
parts.port = originParts.port;
parts.slashes = originParts.slashes;
parts.protocol = originParts.protocol;
parts.hostname = originParts.hostname;
}
return url.format(parts);
}
function validUrl(url) {
//check if the assertion url is malformed
if (!regex.url.test(url)) {
// try one pass of decoding
logger.debug('url did not pass, trying to decodeURIComponent');
url = decodeURIComponent(url);
if (!regex.url.test(url)) {
return false;
}
}
return true;
}
var myFiles = [
"issuer-parts/issuer-script-intro.js",
"jquery.min.js",
"jschannel.js",
"issuer-parts/issuer-core.js",
"issuer-parts/issuer-script-outro.js"
];
myFiles = myFiles.map(function (filename) {
return __dirname + '/../static/js/' + filename;
});
function concatenate(files, cb) {
var completed = 0;
var contents = [];
function startLoading(i) {
fs.readFile(files[i], function (err, data) {
if (err) {
cb(err);
return;
}
contents[i] = data;
completed++;
if (completed == files.length)
cb(null, contents.join('\n'));
});
}
for (var i = 0; i < files.length; i++)
startLoading(i);
}
if (module.parent === null) {
concatenate(myFiles, function (err, data) {
var filename = 'issuer.js';
if (err)
throw err;
fs.writeFileSync(filename, data);
console.log('wrote', filename, '(' + data.length, 'bytes)');
});
}
exports.generateScript = function (req, res) {
concatenate(myFiles, function (err, data) {
if (err) {
res.send(500);
throw err;
} else {
res.header('Content-Type', 'application/javascript');
res.send(data);
}
});
};
/* TODO: issuer-frame.hogan.js should really be accept-badges.hogan.js
or something (it's not always a frame anymore) */
exports.frame = function (req, res) {
res.header('Cache-Control', 'no-cache, must-revalidate');
res.render('issuer-frame', {
layout: null,
framed: true,
csrfToken: req.session._csrf,
email: req.session.emails && req.session.emails[0]
});
};
exports.frameless = function (req, res) {
var assertionUrls = req.body.assertions || [];
assertionUrls = typeof assertionUrls === 'string' ? [assertionUrls] : assertionUrls;
for (var i = 0; i < assertionUrls.length; i++) {
var url = assertionUrls[i];
if (!validUrl(url)) {
logger.error("malformed url " + url + " returning 400");
return res.send('malformed url', 400);
}
}
res.header('Cache-Control', 'no-cache, must-revalidate');
res.render('issuer-frame', {
layout: null,
framed: false,
assertions: JSON.stringify(assertionUrls),
csrfToken: req.session._csrf,
email: req.session.emails && req.session.emails[0]
});
};
exports.issuerBadgeAddFromAssertion = function (req, res, next) {
/* the issuer api, flawed in that it needs to query to badge assertion
* so that we're not making a double request to the issuer, once for the GET
* confirming the badge, and once for the POST awarding the badge. Not
* sure what caching options we have currently, so just going ahead and
* making a double request.
*
* request can either be a GET or a POST, one required param 'url'
* which points to a badge assertion.
*
*/
logger.debug("here's my full url " + req.originalUrl);
var user = req.user;
var error = req.flash('error');
var success = req.flash('success');
// is the user logged in? if not, suggest they redirect to the login page
if (!user) return res.json({ message: "user is not logged in, redirect to " + reverse('backpack.login'),
redirect_to: reverse('backpack.login') }, 403);
// get the url param (lots of debugging statements here)
var assertionUrl = req.query.url; // if it was as a query param in the GET
if (!assertionUrl) {
logger.debug("I'm doing a " + req.method);
logger.debug("tried GET assertionUrl, didn't get anything " + req.param());
logger.debug("full query " + JSON.stringify(req.query));
// if the param was in a POST body
assertionUrl = req.body['url'];
logger.debug("POST attempt got " + assertionUrl);
// more debugging
if (!assertionUrl && req.method == 'GET') {
logger.debug("GET is erroring this was the original url " + req.originalUrl);
logger.debug(JSON.stringify(req.body));
}
}
// no assertionUrl was passed, return error
if (!assertionUrl) {
logger.error("didn't receive an assertionUrl returning 400");
return res.json({message: 'url is a required param'}, 400);
}
if (!validUrl(assertionUrl)) {
logger.error("malformed url " + assertionUrl + " returning 400");
return res.json({ message: 'malformed url' }, 400);
}
/* grabbing the remote assertion, 3 nested steps -
*
* 1) grab the remote assertion
* 2) grab the remote badge image
* if the request is a POST
* 3) award the badge
*/
remote.getHostedAssertion(assertionUrl, function (err, assertion) {
var recipient = user.get('email');
if (err || !assertion) {
var error_msg = "trying to grab url " + assertionUrl + " got error " + err;
logger.error(error_msg);
return res.json({ message: error_msg }, 502);
}
var userOwnsBadge = Badge.confirmRecipient(assertion, recipient);
if (req.method == 'POST' && !userOwnsBadge) {
return res.json({ message: "badge assertion is for a different user" }, 403);
}
// #TODO: write tests for invalid assertions, potentially move this check
// into remote.getHostedAssertion?
// Badge.validateBody is ill named -- it returns null if the badge is
// valid, an error object if the badge is not valid.
if (Badge.validateBody(assertion)) {
return res.json({ message: "badge assertion appears to be invalid" }, 400);
}
// grabbing the remote badge image
var imageUrl = qualifyUrl(assertion.badge.image, assertion.badge.issuer.origin);
remote.badgeImage(imageUrl, function (err, imagedata) {
if (err) {
var error_msg = "trying to grab image at url " + imageUrl + " got error " + err;
logger.error(error_msg);
return res.json({ message: error_msg }, 502);
}
// awarding the badge, only done if this is a POST
if (req.method == 'POST') {
var opts = {
assertion: assertion,
url: assertionUrl,
imagedata: imagedata,
recipient: recipient
};
awardBadge(opts, function (err, badge) {
if (err) {
var error_message = "badge error " + assertionUrl + err;
logger.error(error_message);
// check if this badge is a duplicate, currently in the
// error message
logger.error(err);
var dupe_regex = /Duplicate entry/;
if (dupe_regex.test(err)) {
return res.json({badge: assertion, exists: true, message: "badge already exists"}, 304);
}
// return a general error message
return res.json({badge: assertion, exists: false, 'message': error_message}, 500);
}
logger.debug("badge added " + assertionUrl);
return res.json({exists: false, badge: assertion}, 201);
});
}
// if this is a GET, we still need to return the badge
else {
assertion.badge.image = imageUrl;
var response = {exists: false, badge: assertion, recipient: recipient};
Badge.findOne({endpoint: assertionUrl}, function (err, badge) {
if (err) {
logger.error(err);
return res.json({message: "internal server error"}, 500);
}
if (badge && badge.get("user_id") == req.user.get("id"))
response.exists = true;
if (Badge.confirmRecipient(assertion, req.user.get('email')))
response.owner = true;
return res.json(response, 200);
});
}
});
}); // end of the assertion grabbing badge adding.
};
exports.validator = function (request, response) {
var assertion = request.query.assertion || (request.body && request.body.assertion);
var accept = request.headers['accept'];
var missingMsg = 'error: could not validate, could not find assertion in `data` field';
var status = 200;
var fields = {};
if (!assertion) {
status = 400;
fields = { general: missingMsg };
}
else {
try {
status = 200;
var assertionObject = JSON.parse(assertion);
if (!assertionObject.badge) assertionObject.badge = {};
if (!assertionObject.badge.issuer) assertionObject.badge.issuer = {};
var errors = Badge.validateBody(assertionObject);
if (errors) {
fields = errors.fields;
status = 400;
}
}
catch (err) {
status = 500;
if (err.name === "SyntaxError") {
fields = { general: "Could not parse this JSON blob. Make sure it is well formed and try again!" };
} else {
fields = { general: err.name + ": " + err.message };
}
}
}
function humanize(value, field) {
var url = 'Must either be a fully qualified URL (<code>http://example.com/path/evidence.html</code>) or begin with a forward slash (<code>/path/evidence.html</code>). <br> Non-qualified URLs will be prefixed with the origin specified in <code>badge.issuer.origin</code>';
var length = 'Cannot be longer than 128 characters';
var msgs = {
recipient: 'Must be an email address (<code>someone@example.com</code>) or a hash (<code>sha256$1234567890abcdef</code>)',
evidence: url,
"badge.version": 'Must be in the form of <code>x.y</code> or <code>x.y.z</code>',
"badge.name": length,
"badge.description": length,
"badge.image": url.replace(/evidence.html/g, 'image.png'),
"badge.criteria": url.replace(/evidence.html/g, 'criteria.html'),
"badge.issuer.name": length,
"badge.issuer.org": length,
"badge.issuer.contact": 'Must be an email address',
"badge.issuer.origin": 'Must be a fully qualified origin (<code>http://example.com</code>)'
};
if (value.match(/invalid/)) value = msgs[field] || value;
return {field: field, value: value};
}
var responder = {
'text/plain': function () {
response.contentType('txt');
if (!_.isEmpty(fields)) {
var values = _.values(fields);
var bullets = _.map(values, function (s) { return '* ' + s; }).join('\n');
return response.send(bullets, status);
}
else {
return response.send('everything looks good', 200);
}
},
'application/json': function () {
response.contentType('json');
if (fields) {
return response.json({ status: 'error', errors: _.map(fields, humanize) }, status);
}
else {
return response.json({ status: 'okay' }, 200);
}
},
'default': function () {
var fielderrors = _.map(fields, humanize);
return response.render('validator', {
status: 200,
errors: fielderrors,
csrfToken: request.session._csrf,
submitted: !!assertion,
success: assertion && _.isEmpty(fields),
assertion: assertion
});
}
};
if (!responder[accept]) accept = 'default';
return responder[accept]();
};
exports.welcome = function(request, response, next) {
var user = request.user;
if (!user) return response.redirect(reverse('backpack.login'), 303);
function makeResponse(err, badges) {
if (err) return next(err);
if (badges && badges.length)
return response.redirect(reverse('backpack.manage'), 303);
else
return response.render('issuer-welcome');
}
Badge.find({email: user.get('email')}, makeResponse);
};