-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
75 lines (57 loc) · 2.21 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
'use strict';
// save base folder location
global.__baseFolder = __dirname;
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const logger = require('morgan');
const indexRouter = require('./routes/index');
// Authentication API
const signinRouter = require('./api-auth/signin');
const refreshRouter = require('./api-auth/refresh');
// Registration API
const registerRouter = require('./api-register/register');
const pollRouter = require('./api-register/poll');
const claimRouter = require('./api-register/claim');
// Cloud API
const scannersRouter = require('./api-cloud/scanners');
const userRouter = require('./api-cloud/user');
const blocksRouter = require('./api-cloud/blocks');
// Local API
const localRouter = require('./api-local/direct');
const app = express();
app.use(logger('dev'));
app.use(express.json({ strict: false }));
app.use(bodyParser.raw({ limit: '10mb' }));
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
// API that does not require authentication
app.use('/api/authentication/signin', signinRouter);
app.use('/api/authentication/refresh', refreshRouter);
app.use('/api/register', registerRouter);
app.use('/api/poll', pollRouter);
// Auth middleware
app.use((req, res, next) => {
if (!req.headers.authorization) {
return res.status(403).json({ error: 'No credentials sent!' });
}
// TODO: parse JWT and save principal id
req.twain = {};
req.twain.principalId = req.headers.authorization;
return next();
});
// APIs that do require authentication
app.use('/api/claim', claimRouter);
app.use('/api/user', userRouter);
app.use('/api/scanners', scannersRouter);
scannersRouter.use('/:scannerId/blocks', blocksRouter);
// v1 - original APIs with 'privet' prefix
//scannersRouter.use('/:scannerId/privet/infoex', localRouter);
//scannersRouter.use('/:scannerId/privet/twaindirect/session', localRouter);
// v2 - APIs without 'privet' prefix
scannersRouter.use('/:scannerId/infoex', localRouter);
scannersRouter.use('/:scannerId/twaindirect/session', localRouter);
module.exports = app;