52 lines
1.7 KiB
JavaScript
52 lines
1.7 KiB
JavaScript
import OpenIDConnectStrategy from "passport-openidconnect";
|
|
import { checkUser } from "../../http/auth.js";
|
|
import { getConfig } from "../../common/config.js";
|
|
|
|
const key = 'oidc';
|
|
|
|
export function getModuleConfig() {
|
|
const config = getConfig();
|
|
return {
|
|
title: process.env.OIDC_BUTTON_LABEL || 'FedID',
|
|
icon: 'login',
|
|
issuer: process.env.OIDC_ISSUER,
|
|
authorizationURL: process.env.OIDC_AUTHORIZATION_URL,
|
|
tokenURL: process.env.OIDC_TOKEN_URL,
|
|
userInfoURL: process.env.OIDC_USERINFO_URL,
|
|
logoutURL: process.env.OIDC_LOGOUT_URL,
|
|
clientID: process.env.OIDC_CLIENT_ID,
|
|
clientSecret: process.env.OIDC_CLIENT_SECRET,
|
|
callbackURL: `${config.publicCallbackUrl}/callback/${key}`,
|
|
}
|
|
}
|
|
|
|
export async function initModule(app, passport) {
|
|
const config = getConfig();
|
|
|
|
passport.use(new OpenIDConnectStrategy(
|
|
config.authModules[key],
|
|
function verify(issuer, profile, cb) {
|
|
(async () => {
|
|
try {
|
|
const user = await checkUser(key, issuer, profile.id, profile.username);
|
|
return cb(null, user);
|
|
} catch (e) {
|
|
if (config.debug)
|
|
console.error(e);
|
|
return cb(null, null);
|
|
}
|
|
})();
|
|
}
|
|
));
|
|
app.get(`/login/${key}`,passport.authenticate('openidconnect'));
|
|
app.get(`/callback/${key}`,
|
|
passport.authenticate('openidconnect', {
|
|
failureRedirect: '/',
|
|
failureMessage: true
|
|
}),
|
|
function (req, res) {
|
|
req.session.authStrategy = 'oidc';
|
|
res.redirect('/home');
|
|
}
|
|
);
|
|
} |