-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-handler.js
More file actions
38 lines (32 loc) · 1.27 KB
/
Copy pathcreate-handler.js
File metadata and controls
38 lines (32 loc) · 1.27 KB
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
import { sign } from 'jsonwebtoken';
import fetchAuth from 'fetch-auth-node';
import HAS_VALID_AUTHORISATION_HEADER from './has-valid-authorisation-header';
import NEEDS_TO_REFRESH_ROLES from './needs-to-refresh-roles';
const POST = 'POST';
export default function createHandler({endpoint, options, secret}={}) {
return async function handler(req, res) {
if (req.method === POST && HAS_VALID_AUTHORISATION_HEADER.test(req.headers.authorization)) {
let name;
let roles;
try {
const { userCtx } = await fetchAuth(`${endpoint}/_session`, req.headers.authorization);
name = userCtx.name;
roles = userCtx.roles;
if (NEEDS_TO_REFRESH_ROLES.test(req.url)) {
const nextUserCtx = await fetchAuth(`${endpoint}/_users/org.couchdb.user:${name}`, req.headers.authorization);
roles = nextUserCtx.roles;
}
const token = sign({name, roles}, secret, options);
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(token));
} catch(err) {
console.error(`${new Date().toISOString().replace('T', ' ').substr(0, 19)} 401 ${err.message}`);
res.writeHead(401);
res.end();
}
} else {
res.writeHead(404);
res.end();
}
};
}