-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.js
62 lines (58 loc) · 1.7 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
// @flow
import 'babel-polyfill';
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import morgan from 'morgan';
import bunyanWinstonAdapter from 'bunyan-winston-adapter';
import ldap from 'ldapjs';
import {config} from './config';
import logger from './logger';
import {Client, Authenticator, Mapping} from './ldap';
import {Healthz, UserAuthentication, TokenAuthentication} from './api';
// setup basic dependencies
let ldapClient = new Client(
ldap.createClient({
url: config.ldap.uri,
timeout: config.ldap.timeout * 1000,
connectTimeout: config.ldap.timeout * 1000,
reconnect: {
initialDelay: config.ldap.reconnectInitialDelay,
maxDelay: config.ldap.reconnectMaxDelay,
failAfter: config.ldap.reconnectFailAfter,
},
log: bunyanWinstonAdapter.createAdapter(logger),
}),
config.ldap.baseDn,
config.ldap.bindDn,
config.ldap.bindPw
);
let authenticator = new Authenticator(ldapClient, config.ldap.filter, logger);
// setup api dependencies
let healthz = new Healthz();
let userAuthentication = new UserAuthentication(
authenticator,
config.jwt.tokenLifetime,
config.jwt.key,
new Mapping(
config.mapping.username,
config.mapping.uid,
config.mapping.groups,
config.mapping.extraFields,
),
logger);
let tokenAuthentication = new TokenAuthentication(config.jwt.key, logger);
// setup express
const app = express();
app.use(cors());
app.use(morgan('combined', {
stream: {
write: (message, encoding) => {
logger.info(message);
},
},
}));
app.get('/healthz', healthz.run);
app.get('/auth', userAuthentication.run);
app.post('/token', bodyParser.json(), tokenAuthentication.run);
export default app;