/
example.ts
86 lines (75 loc) · 2.46 KB
/
example.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
// eslint-disable-next-line import/no-extraneous-dependencies
import express, { Request, Response } from 'express';
import {
askBasicAuth,
verifyBasicAuth,
verifyTokenAuth,
setTokenAuth,
allowRoles,
nextRouteIfUnauthorized,
nextRouteIfForbidden,
sendUser,
AuthenticationFunction,
BasicAuthenticationOptions,
TokenAuthenticationOptions,
AuthorizationFunction,
AuthorizationOptions,
} from '.';
type User = {
username: string;
password: string;
role: 'admin' | 'user';
};
const users: User[] = [
{ username: 'admin', password: '1234', role: 'admin' },
{ username: 'user', password: '5678', role: 'user' },
];
const authenticateUser: AuthenticationFunction = async (username: string, password?: string) =>
users.find(user => user.username === username && (password === undefined || user.password === password));
const authorizeUser: AuthorizationFunction<User, User['role']> = (user, ...roles) => roles.some(role => user.role === role);
const options: BasicAuthenticationOptions & TokenAuthenticationOptions & AuthorizationOptions<User, User['role']> = {
authenticationFunction: authenticateUser,
authorizationFunction: authorizeUser,
jwtSigningSecret: '1d2c468e-3b6b-4309-9b4c-04a890da7e76',
headerName: 'x-access-token',
expiresIn: 24 * 60 * 60,
};
const auth = {
askBasicAuth: askBasicAuth(),
verifyBasicAuth: verifyBasicAuth(options),
verifyTokenAuth: verifyTokenAuth(options),
setTokenAuth: setTokenAuth(options),
allowRoles: allowRoles<User, User['role']>(options),
nextRouteIfUnauthorized: nextRouteIfUnauthorized(),
nextRouteIfForbidden: nextRouteIfForbidden(),
sendUser: sendUser(),
};
const app = express();
// login with basic auth, send back user and JWT in header for future requests
app.get('/login', auth.askBasicAuth, auth.verifyBasicAuth, auth.setTokenAuth, auth.sendUser);
// private route for admins
app.get(
'/data',
auth.verifyTokenAuth,
auth.nextRouteIfUnauthorized,
auth.allowRoles('admin'),
auth.nextRouteIfForbidden,
(request: Request, response: Response) => {
response.json({ data: 'admin private data' });
},
);
// private route for users
app.get(
'/data',
auth.verifyTokenAuth,
auth.nextRouteIfUnauthorized,
auth.allowRoles('user'),
auth.nextRouteIfForbidden,
(request: Request, response: Response) => {
response.json({ data: 'user private data' });
},
);
// public route
app.get('/data', (request: Request, response: Response) => {
response.json({ data: 'public data' });
});