-
Notifications
You must be signed in to change notification settings - Fork 3
/
isf-cim.js
86 lines (77 loc) · 3.64 KB
/
isf-cim.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
'use strict';
const path = require('path'),
configs = require('../configurations'),
helpers = require('../helpers'),
request = require('superagent'),
express = require('express'),
router = express.Router();
module.exports = function (appContext, directory) {
/* GET /isfselection
* Returns the isf-cim html page for presentation to the user within Brightspace.
*/
router.get('/isfselection', function(req, res) {
res.sendFile(path.join(directory+'/html/isf-cim.html'));
});
/* POST /lti/isfcontent
* The LTI endpoint for a Insert Stuff (CIM) remote plugin.
*/
router.post('/lti/isfcontent', function (req, res) {
const url = req.protocol + '://' + req.get('host') + '/lti/isfcontent';
if (!helpers.verifyLtiRequest(url, req.body, configs.ltiSecret)) {
console.log('Could not verify the LTI Request. OAuth 1.0 Validation Failed');
res.status(500).send({error: 'Could not verify the LTI Request. OAuth 1.0 Validation Failed'});
} else {
res.cookie('lti-request', {
contentItemReturnUrl: req.body.content_item_return_url,
oauth_version: req.body.oauth_version,
oauth_nonce: req.body.oauth_nonce,
oauth_consumer_key: req.body.oauth_consumer_key,
oauth_callback: req.body.oauth_callback,
oauth_signature_method: req.body.oauth_signature_method
}, configs.cookieOptions);
res.redirect('/isfselection');
}
});
/* GET /getisfdetails
* Returns the details for the request that needs to be submitted through the form back
* to Brightspace in order to insert the stuff into Brightspace.
*/
router.get('/getisfdetails', function (req, res) {
const imageUrl = req.protocol + '://' + req.get('host') + '/content/isf/' + req.query.image;
const contentItemReturnUrl = req.cookies['lti-request'].contentItemReturnUrl;
const contentItems = {
'@context' : 'http://purl.imsglobal.org/ctx/lti/v1/ContentItem',
'@graph': [
{
'@type' : 'ContentItem',
mediaType: 'image/png',
title: 'Brightspace Logo',
text: '',
url: imageUrl,
placementAdvice : {
displayWidth : 100,
displayHeight : 30,
presentationDocumentTarget : 'embed',
windowTarget : '_blank'
}
}
]
};
const responseObject = {
lti_message_type: 'ContentItemSelection',
lti_version: 'LTI-1p0',
content_items: JSON.stringify(contentItems),
oauth_version: req.cookies['lti-request'].oauth_version,
oauth_nonce: req.cookies['lti-request'].oauth_nonce,
oauth_timestamp: helpers.getUnixTimestamp(),
oauth_consumer_key: req.cookies['lti-request'].oauth_consumer_key,
oauth_callback: req.cookies['lti-request'].oauth_callback,
oauth_signature_method: req.cookies['lti-request'].oauth_signature_method
};
responseObject.oauth_signature = helpers.generateAuthSignature(contentItemReturnUrl, responseObject, configs.ltiSecret);
responseObject.lti_return_url = contentItemReturnUrl;
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(responseObject));
});
return router;
};