-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
372 lines (322 loc) · 10.4 KB
/
server.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
// imports
// nodejs builtins
import semver from 'semver';
import path from 'path';
import fs from 'fs';
import http from 'http';
import https from 'https';
// 3rd party dependencies
import helmet from 'helmet';
import express from 'express';
import cookieParser from 'cookie-parser';
// my own external dependencies
import {config} from 'sirdb';
// internal modules
import './types.js';
import {
APP_ROOT,
SD_MODE,
DEFAULT_PORT,
MESSAGES,
MAX_REQUEST_SIZE,
COOKIE_NAME,
STATIC,
DEBUG,
PORT
} from './common.js';
import {
Log,
route,
} from './helpers.js';
import {
DB_ROOT,
getList,
getListSorted,
getSearchResult,
getStoredQueryResult,
getSelectionData,
displaySelectionData,
runStoredAction,
initialize,
newItem,
setItem,
getItem,
_getTable,
dbCleanup,
} from './db_helpers.js';
import {getSession} from './_middlewares/session.js';
import {attachPermission} from './_middlewares/permission.js';
import {catchError} from './_middlewares/error.js';
import {withView} from './views.js';
// constants
const CANON = 'calendar.dosyago.com'; // the canonical host name
const S_CANON = 'https://' + CANON;
// paths dispatch
const DISPATCH = {
// landing
'GET /': landing,
// forms
'GET /redirected/message/:message/with/:view': withView(showMessage),
'GET /form/table/:table/:id/with/:view': withView(getItem),
'GET /form/list/table/:table/with/:view/': withView(getList),
'GET /form/list/table/:table/with/:view/sort/:prop': withView(getListSorted),
'GET /form/search/table/:table/with/:view': withView(getSearchResult),
'GET /form/query/:query/with/:view': withView(getStoredQueryResult),
'GET /form/selection/:selection/:id': displaySelectionData,
'GET /form/selection/:selection': displaySelectionData,
'POST /form/table/:table/new/with/:view': withView(newItem),
'POST /form/table/:table/:id/with/:view': withView(setItem),
'POST /form/action/:action/with/:view': withView(runStoredAction),
'POST /form/action/:action': runStoredAction,
'POST /form/action/:action/redir/:selection': toSelection(runStoredAction),
// JSON API
'GET /json/table/:table/:id': getItem,
'GET /json/list/table/:table/': getList,
'GET /json/list/table/:table/sort/:prop': getListSorted,
'GET /json/search/table/:table': getSearchResult,
'GET /json/query/:query': getStoredQueryResult,
'GET /json/selection/:selection/:id': getSelectionData,
'GET /json/selection/:selection': getSelectionData,
'POST /json/table/:table': newItem,
'PATCH /json/table/:table/:id': setItem,
'POST /json/action/:action': runStoredAction,
// not implemented
/*
'PUT /json/table/:table/:id': overwriteItem,
*/
};
// semver check
{
const version = JSON.parse(
fs.readFileSync(path.resolve(APP_ROOT, 'package.json')).toString("utf-8")
).engines.node;
if (!semver.satisfies(process.version, version)) {
console.log(
`Required node version ${version} not satisfied with current version ${process.version}.`
);
process.exit(1)
}
}
process.on('unhandledRejection', (...args) => {
console.error(`Unhandled promise rejection`);
console.log(`Details`, args);
setTimeout(() => process.exit(1), 3000);
});
export async function initializeDB() {
await initialize();
}
export function servedata({callConfig: callConfig = false, secure: secure = true} = {}) {
if ( callConfig ) {
DEBUG.WARN && console.warn("Calling config in servedata with default DB_ROOT");
config({root:DB_ROOT});
}
const app = express();
app.use(helmet());
app.set('etag', false);
app.set('trust proxy', true);
app.use((req,res,next) => {
// set cache
res.set('Cache-Control', 'no-store, no-cache, must-revalidate, private');
//set type
if ( req.path.startsWith('/form')) {
res.type('html');
} else if ( req.path.startsWith('/redirect') ) {
res.type('html');
} else if ( req.path.startsWith('/json')) {
res.type('json');
}
next();
});
app.use(cookieParser());
app.use(express.urlencoded({extended:true, limit:MAX_REQUEST_SIZE}));
app.use(express.json({
limit:MAX_REQUEST_SIZE,
verify: (req, res, buf) => {
req.rawBody = buf;
}
}));
app.use(express.static(STATIC, {extensions:['html'], fallthrough:true, index: false}));
app.use(getSession);
// JSON
// getters
app.get('/json/table/:table/:id', X);
app.get('/json/list/table/:table', X);
app.get('/json/list/table/:table/sort/:prop', X);
app.get('/json/search/table/:table', X);
// auto id
app.post('/json/table/:table', X);
// given id ( set/ overwrite (not implemented) )
// app.put('/json/table/:table/:id', X);
// update (setItem, standard implementation, subset of properties)
app.patch('/json/table/:table/:id', X);
// stored procedures
app.get('/json/query/:query', X);
app.post('/json/action/:action', X);
// FORM
// getters
app.get('/form/table/:table/:id/with/:view', X);
app.get('/form/list/table/:table/with/:view', X);
app.get('/form/list/table/:table/sort/:prop/with/:view', X);
app.get('/form/search/table/:table/with/:view', X);
app.get('/form/selection/:selection/:id', X);
app.get('/form/selection/:selection', X);
// create
app.post('/form/table/:table/new/with/:view', X);
// update
app.post('/form/table/:table/:id/with/:view', X);
// stored procedure
app.get('/form/query/:query/with/:view', X);
app.post('/form/action/:action', X);
app.post('/form/action/:action/with/:view', X);
app.post('/form/action/:action/redir/:selection', X);
// other
app.get('/redirected/message/:message/with/:view', X);
app.get('/', X);
app.get('*', (req, res, next) => {
next(new Error(`404 not found: ${req.path}`));
});
app.use(catchError);
const server = (secure ? https : http).createServer(secure ? {
key: fs.readFileSync(path.resolve('sslcerts', 'privkey.pem')),
cert: fs.readFileSync(path.resolve('sslcerts', 'fullchain.pem')),
ca: fs.readFileSync(path.resolve('sslcerts', 'chain.pem')),
} : {}, app);
server.on('close', () => {
console.log("Server closing");
dbCleanup();
});
server.listen(PORT, err => {
if ( err ) {
throw err;
}
Log({serverUp:{port:PORT, up:Date.now()}});
});
return server;
}
async function X(req, res, next) {
const permissionResult = attachPermission(req, res);
switch ( permissionResult ) {
case "permissions-attached":
break;
case "re-auth-required":
return reAuth(req, res);
default:
throw new Error(`Unknown result of attachPermission ${permissionResult}`);
}
if ( ! req.authorization ) {
next({status: 401, error: '401 Not authorized. No valid user identified.'});
}
const grant = await accessGranted(req, res, next);
if ( ! grant ) {
return;
}
const way = `${req.method} ${req.route.path}`;
const data = {...req.params, item: req.body, _search: req.query};
if ( data.table ) {
data.table = _getTable(data.table);
data.userid = req.authorization.userid;
}
let result;
try {
if ( req.path.includes('/action/') || req.path.includes('/selection/') || req.path == '/' ) {
result = await DISPATCH[way](data, req, res);
} else {
result = await DISPATCH[way](data);
}
if ( req.path.includes('/redir/') ) {
const {pathname} = result;
if ( !pathname ) {
throw new TypeError(`Redirect can only be used when task returns a 'pathname' field`);
}
const toUrl = route(req, pathname);
res.redirect(toUrl);
} else {
if ( req.path.startsWith('/json') ) {
result = JSON.stringify(result);
}
res.end(result);
}
} catch(e) {
console.error({way, path:req.path});
console.error(e);
next(e);
}
}
async function accessGranted(req, res, next) {
if ( req.method == 'GET' && !req.params.action && !req.authorization.permissions.view ) {
next({status:401, error: '401 Not authorized. User has no view permission on this scope.'});
return false;
}
if ( req.method == 'POST' ) {
if ( req.params.table ) {
if ( ! req.params.id && !req.authorization.permissions.create ) {
next({status:401, error: '401 Not authorized. User has no create permission on this scope.'});
return false;
} else if ( req.params.id && !req.authorization.permissions.alter ) {
next({status:401, error: '401 Not authorized. User has no alter permission on this scope.'});
return false;
}
} else {
if ( !req.authorization.permissions.create ) {
next({status:401, error: '401 Not authorized. User has no create permission on this scope.'});
return false;
}
}
return true;
}
return true;
}
function showMessage({message}) {
message = MESSAGES[message];
if ( ! message ) {
message = 'Unknown message';
}
return {message};
}
function landing(nothing, req, res) {
res.type('html');
const state = {
authorization: req.authorization
};
res.end(`
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="width=device-width, initial-scale=1">
<title>Dosyago</title>
<link rel=stylesheet href=/static/style.css>
<script type=module>
import {init} from '/${DEBUG.BUILD}/app.js'
init(${JSON.stringify({state})});
</script>
</html>
`);
}
export function toSelection(f) {
return async (...args) => {
const raw = await f(...args);
const [{selection}] = args;
const {id} = raw;
let pathname;
if ( id ) {
pathname = `/form/selection/${selection}/${id}`;
} else {
pathname = `/form/selection/${selection}`;
}
return {pathname}
};
}
function reAuth(req, res) {
console.log(req.path);
if ( req.path.startsWith("/form") ) {
res.clearCookie(COOKIE_NAME);
res.redirect('/login.html?');
} else if ( req.path.startsWith("/json") ) {
res.type("json");
res.end(JSON.stringify({error:'Need to reauthenticate'}));
} else if ( req.path == "/" ) {
res.clearCookie(COOKIE_NAME);
return landing(null, req, res);
} else {
throw new Error(`The session was invalid. Reuathenticaiton required.`);
}
}