-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-server.js
52 lines (43 loc) · 1.18 KB
/
api-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
const express = require('express');
const morgan = require('morgan');
const helmet = require('helmet');
const cors = require('cors');
const jwt = require('express-jwt');
const jwksRsa = require('jwks-rsa');
const authConfig = require('./auth_config.json');
const app = express();
if (
!authConfig.domain ||
!authConfig.audience ||
authConfig.audience === "YOUR_API_IDENTIFIER"
) {
console.log(
"Exiting: Please make sure that auth_config.json is in place and populated with valid domain and audience values"
);
process.exit();
}
app.use(morgan('dev'));
app.use(helmet());
app.use(
cors({
origin: authConfig.appUri,
})
);
const checkJwt = jwt({
secret: jwksRsa.expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: `https://${authConfig.domain}/.well-known/jwks.json`,
}),
audience: authConfig.audience,
issuer: `https://${authConfig.domain}/`,
algorithms: ['RS256'],
});
app.get('/api/external', checkJwt, (req, res) => {
res.send({
msg: 'Your access token was successfully validated!',
});
});
const port = process.env.API_SERVER_PORT || 3001;
app.listen(port, () => console.log(`Api started on port ${port}`));