2020-04-14 22:29:11 +02:00
|
|
|
/* eslint-disable import/no-extraneous-dependencies */
|
|
|
|
/* eslint-disable import/no-unresolved */
|
|
|
|
|
2019-05-19 08:34:20 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Keycloak hook for securing an Unleash server
|
|
|
|
*
|
|
|
|
* This example assumes that all users authenticating via
|
|
|
|
* keycloak should have access. You would probably limit access
|
|
|
|
* to users you trust.
|
|
|
|
*
|
2019-05-19 12:52:46 +02:00
|
|
|
* The implementation assumes the following environment variables:
|
2019-05-19 08:34:20 +02:00
|
|
|
*
|
|
|
|
* - AUTH_HOST
|
|
|
|
* - AUTH_REALM
|
|
|
|
* - AUTH_CLIENT_ID
|
|
|
|
*/
|
|
|
|
|
2019-05-19 12:52:46 +02:00
|
|
|
const KeycloakStrategy = require('@exlinc/keycloak-passport');
|
2019-05-19 08:34:20 +02:00
|
|
|
const passport = require('passport');
|
|
|
|
|
2020-04-14 22:29:11 +02:00
|
|
|
// const { User, AuthenticationRequired } = require('unleash-server');
|
|
|
|
const { User, AuthenticationRequired } = require('../lib/server-impl.js');
|
|
|
|
|
2019-08-10 16:06:09 +02:00
|
|
|
const host = process.env.AUTH_HOST;
|
2019-05-19 12:52:46 +02:00
|
|
|
const realm = process.env.AUTH_REALM;
|
2019-08-10 16:06:09 +02:00
|
|
|
const clientID = process.env.AUTH_CLIENT_ID;
|
2019-05-19 12:52:46 +02:00
|
|
|
const contextPath = process.env.CONTEXT_PATH;
|
2019-05-19 08:34:20 +02:00
|
|
|
|
|
|
|
passport.use(
|
2019-05-19 12:52:46 +02:00
|
|
|
'keycloak',
|
2019-05-19 08:34:20 +02:00
|
|
|
new KeycloakStrategy(
|
|
|
|
{
|
2019-05-19 12:52:46 +02:00
|
|
|
host,
|
|
|
|
realm,
|
2019-08-10 16:06:09 +02:00
|
|
|
clientID,
|
2019-05-19 08:34:20 +02:00
|
|
|
clientSecret: "We don't need that, but is required",
|
2019-05-19 12:52:46 +02:00
|
|
|
callbackURL: `${contextPath}/api/auth/callback`,
|
2019-08-10 16:06:09 +02:00
|
|
|
authorizationURL: `${host}/auth/realms/${realm}/protocol/openid-connect/auth`,
|
|
|
|
tokenURL: `${host}/auth/realms/${realm}/protocol/openid-connect/token`,
|
|
|
|
userInfoURL: `${host}/auth/realms/${realm}/protocol/openid-connect/userinfo`,
|
2019-05-19 08:34:20 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
(accessToken, refreshToken, profile, done) => {
|
|
|
|
done(
|
|
|
|
null,
|
|
|
|
new User({
|
|
|
|
name: profile.fullName,
|
|
|
|
email: profile.email,
|
2020-04-14 22:29:11 +02:00
|
|
|
}),
|
2019-05-19 08:34:20 +02:00
|
|
|
);
|
2020-04-14 22:29:11 +02:00
|
|
|
},
|
|
|
|
),
|
2019-05-19 08:34:20 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
function enableKeycloakOauth(app) {
|
|
|
|
app.use(passport.initialize());
|
|
|
|
app.use(passport.session());
|
|
|
|
|
|
|
|
passport.serializeUser((user, done) => done(null, user));
|
|
|
|
passport.deserializeUser((user, done) => done(null, user));
|
2019-05-19 12:52:46 +02:00
|
|
|
|
2019-05-19 08:34:20 +02:00
|
|
|
app.get('/api/admin/login', passport.authenticate('keycloak'));
|
|
|
|
|
2019-05-19 12:52:46 +02:00
|
|
|
app.get(
|
|
|
|
'/api/auth/callback',
|
|
|
|
passport.authenticate('keycloak'),
|
|
|
|
(req, res) => {
|
|
|
|
res.redirect(`${contextPath}/`);
|
2020-04-14 22:29:11 +02:00
|
|
|
},
|
2019-05-19 12:52:46 +02:00
|
|
|
);
|
2019-05-19 08:34:20 +02:00
|
|
|
|
|
|
|
app.use('/api/admin/', (req, res, next) => {
|
|
|
|
if (req.user) {
|
2020-04-14 22:29:11 +02:00
|
|
|
return next();
|
2019-05-19 08:34:20 +02:00
|
|
|
}
|
2020-04-14 22:29:11 +02:00
|
|
|
// Instruct unleash-frontend to pop-up auth dialog
|
|
|
|
return res
|
|
|
|
.status('401')
|
|
|
|
.json(
|
|
|
|
new AuthenticationRequired({
|
|
|
|
path: `${contextPath}/api/admin/login`,
|
|
|
|
type: 'custom',
|
|
|
|
message: `You have to identify yourself in order to use Unleash.
|
|
|
|
Click the button and follow the instructions.`,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.end();
|
2019-05-19 08:34:20 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = enableKeycloakOauth;
|