-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticationConnection.js
79 lines (69 loc) · 2.25 KB
/
authenticationConnection.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
76
77
78
79
const fs = require('fs');
const Keycloak = require('keycloak-connect');
const session = require('express-session');
const passport = require('passport');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const express = require('express');
const { ensureLoggedIn } = require('connect-ensure-login');
const passportLocal = require('passport-local');
let keycloak = null;
function connectAuthentication(server, readConfig) {
if (fs.existsSync('./config/keycloak.json') || fs.existsSync('/opt/config/keycloak.json')) {
const memoryStore = new session.MemoryStore();
server.use(session({
secret: 'secret',
resave: false,
saveUninitialized: true,
store: memoryStore,
}));
keycloak = fs.existsSync('./config/keycloak.json')?
new Keycloak({ store: memoryStore }, './config/keycloak.json'):
new Keycloak({ store: memoryStore }, '/opt/config/keycloak.json');
server.use(keycloak.middleware());
return 'keycloak';
}
server.use(cookieParser());
server.use(bodyParser.urlencoded({ extended: true }));
server.use(session({
secret: 'superSecret',
resave: true,
saveUninitialized: true,
}));
server.use(passport.initialize());
server.use(passport.session());
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((user, done) => {
done(null, user);
});
const LocalStrategy = passportLocal.Strategy;
passport.use(new LocalStrategy(
((username, password, done) => {
const { users } = readConfig();
if (users) {
const user = users.find((u) => u.username === username && u.password === password);
if (user) {
return done(null, { username: user.username });
}
}
return done(null, false, { message: 'Incorrect password.' });
}),
));
server.use('/login', express.static(`${__dirname}/login`));
server.post('/login',
passport.authenticate('local', { failureRedirect: '/login' }),
(req, res) => {
res.redirect('/');
});
return 'local';
}
function protect() {
if (keycloak) {
return keycloak.protect();
}
return ensureLoggedIn('/login');
}
module.exports.connectAuthentication = connectAuthentication;
module.exports.protect = protect;