-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
227 lines (206 loc) · 6.98 KB
/
index.ts
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
const express = require('express');
const lti = require('ims-lti')
const q = require('q');
const ejs = require('ejs');
const http = require('http');
const fs = require('fs');
const https = require('https');
const privateKey = fs.readFileSync('sslcert/server.key', 'utf8');
const certificate = fs.readFileSync('sslcert/server.crt', 'utf8');
const credentials = {key: privateKey, cert: certificate};
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE');
res.setHeader(
'Access-Control-Allow-Headers',
'accept, Authorization, Context-Id, Content-Type, Content-Length, X-Requested-With'
);
res.setHeader('Access-Control-Expose-Headers', 'uuid, content-disposition');
if ('OPTIONS' == req.method) {
res.sendStatus(200);
}
next();
});
import configurationManager from './configurationManager';
import DatabaseClient from './DatabaseClient';
import FileStore from './FileStore';
import RequestHandler from './requestHandler';
let utils = require('./utils').default;
configurationManager.initialize();
const config = configurationManager.loadConfigFile();
configurationManager.watch();
const requestHandler = new RequestHandler(config);
const databaseClient = new DatabaseClient(config);
const fileStore = new FileStore(config);
const getAuthToken = req => {
const authorization = req.get('authorization');
if (!authorization || authorization.length < 7)
throw 'Authorization header missing';
return authorization.length > 7 && authorization.substr(7);
};
const authorizationError = {
code: 401,
msg: 'Unauthorized request'
};
const handleError = (res, err) => {
const errType = Object.prototype.toString.call(err).slice(8, -1);
if (errType === 'String' || errType === 'Error' || !err.code) {
if (errType === 'Error') {
console.error('[ERROR] ' + err + '\n' + err.stack);
err = err.msg;
} else console.error('[ERROR] ' + err);
res.status(500).send(err);
} else {
const errMsg =
err.code === 403
? 'Authentication error'
: err.msg ? err.msg : `code: ${err.code}`;
if (err.code !== 204) {
// 204= file not found
console.error(`[ERROR] ${errMsg}`);
}
res.status(err.code).send(errMsg);
}
};
app.post('/submission', async (req, res) => {
let userInfo;
try {
userInfo = await requestHandler.getUserInfo(
'me',
getAuthToken(req)
);
const dataBaseRequest = await databaseClient.createSubmissionEntry({
userInfo,
...req.body
})
.catch(err => { throw(err) });
const fileStoreRequest = await fileStore.store({
userInfo,
...req.body
})
.catch(err => { throw(err) });
res.send({
database: dataBaseRequest,
fileStore: fileStoreRequest
})
} catch (err) {
handleError(res, err);
}
});
const createEdxGradeEntry = async (req, token) => {
// We store part of the req object in the Firebase database
// We need req's body for later submission to edX, actually two of its LTI parameters:
// 'lis_outcome_service_url', the url where to submit the edX grade, and
// lis_result_sourcedid', a unique identifier of the pair (edX LTI unit, edX user)
// Some req properties are filtered out as they cannot be serialized
// (none of them, except body, are useful for the edX send-and-replace call).
const INCLUDE = ['body', 'raw', 'originalUrl', 'protocol', 'method', 'headers'];
let filteredRequest = {};
Object.keys(req).forEach(key => {
if (INCLUDE.indexOf(key) !== -1) filteredRequest[key] = req[key];
});
filteredRequest['connection'] = { encrypted: req.connection.encrypted };
return databaseClient.createEdxGradeEntry({
request: filteredRequest,
token
});
}
app.post('/edx-launch', async (req, res) => {
try {
const ltiKey = config.ltiConsumerKey;
const ltiSecret = config.ltiConsumerSecret;
let provider = new lti.Provider(ltiKey, ltiSecret);
provider.valid_request(req, (err, isValid) => {
if (!isValid) {
const msg = 'Invalid LTI launch request';
console.error(msg);
throw(msg);
}
if (err) {
console.error('LTI validation error');
throw(err);
}
const token = utils.generateToken(
config.tokenEncryptionKey,
req.body['lis_result_sourcedid']
);
createEdxGradeEntry(req, token)
.then( () => q.denodeify(ejs.renderFile)('views/launch_exercise_show_token.ejs',
{
token,
exercise: req.body['custom_subheader'],
redirect: 'https://collab.humanbrainproject.eu/#/collab'
}))
.then(result => { res.send(result); })
.catch(err => { throw(err) });
});
} catch (err) {
handleError(res, err);
}
});
app.post('/edx-submission/:userId/:header', async (req, res) => {
try {
if (getAuthToken(req) !== config.adminToken) throw(authorizationError);
const dataBaseRequest = await databaseClient.submitUserGradesToEdx(
req.params.userId,
req.params.header
)
.catch(err => { throw(err) });
res.send(dataBaseRequest);
} catch (err) {
console.log(err);
handleError(res, err);
}
});
app.post('/edx-submission/:header', async (req, res) => {
try {
if (getAuthToken(req) !== config.adminToken) throw(authorizationError);
const dataBaseRequest = await databaseClient.submitGradesToEdx(req.params.header)
.catch(err => { throw(err) });
res.send(dataBaseRequest);
} catch (err) {
console.log(err);
handleError(res, err);
}
});
app.post('/final-grades/:userId', async (req, res) => {
try {
if (getAuthToken(req) !== config.adminToken) throw(authorizationError);
const dataBaseRequest = await databaseClient.appendUserFinalGrades(req.params.userId, req.body)
.catch(err => { throw(err) });
res.send(dataBaseRequest);
} catch (err) {
handleError(res, err);
}
});
app.post('/final-grades', async (req, res) => {
try {
if (getAuthToken(req) !== config.adminToken) throw(authorizationError);
const dataBaseRequest = await databaseClient.appendFinalGrades(req.body)
.catch(err => { throw(err) });
res.send(dataBaseRequest);
} catch (err) {
handleError(res, err);
}
});
app.get('/check-token', async (req, res) => {
try {
const dataBaseRequest = await databaseClient.getEdxGradeIdentifiers(req.query.token)
.catch(err => { throw(err) });
const launchRequestBody = dataBaseRequest['request']['body'];
res.send({
custom_header: launchRequestBody['custom_header'],
custom_subheader: launchRequestBody['custom_subheader']
})
} catch (err) {
handleError(res, err);
}
});
// Express configuration
const httpServer = http.createServer(app);
httpServer.listen(8080, () => console.log('Server listening on port 8080!'));
const httpsServer = https.createServer(credentials, app);
httpsServer.listen(8443, () => console.log('Server listening on port 8443!'));