|
| 1 | +const passport = require('passport'); |
| 2 | +const db = require('../../db'); |
| 3 | + |
| 4 | +const configure = async () => { |
| 5 | + // Temp fix for ERR_REQUIRE_ESM, will be changed when we refactor to ESM |
| 6 | + const { discovery, fetchUserInfo } = await import('openid-client'); |
| 7 | + const { Strategy } = await import('openid-client/passport'); |
| 8 | + const config = require('../../config').getAuthentication(); |
| 9 | + const { oidcConfig } = config; |
| 10 | + const { issuer, clientID, clientSecret, callbackURL, scope } = oidcConfig; |
| 11 | + |
| 12 | + if (!oidcConfig || !oidcConfig.issuer) { |
| 13 | + throw new Error('Missing OIDC issuer in configuration') |
| 14 | + } |
| 15 | + |
| 16 | + const server = new URL(issuer); |
| 17 | + |
| 18 | + try { |
| 19 | + const config = await discovery(server, clientID, clientSecret); |
| 20 | + |
| 21 | + const strategy = new Strategy({ callbackURL, config, scope }, async (tokenSet, done) => { |
| 22 | + // Validate token sub for added security |
| 23 | + const idTokenClaims = tokenSet.claims(); |
| 24 | + const expectedSub = idTokenClaims.sub; |
| 25 | + const userInfo = await fetchUserInfo(config, tokenSet.access_token, expectedSub); |
| 26 | + handleUserAuthentication(userInfo, done); |
| 27 | + }); |
| 28 | + |
| 29 | + // currentUrl must be overridden to match the callback URL |
| 30 | + strategy.currentUrl = function (request) { |
| 31 | + const callbackUrl = new URL(callbackURL); |
| 32 | + const currentUrl = Strategy.prototype.currentUrl.call(this, request); |
| 33 | + currentUrl.host = callbackUrl.host; |
| 34 | + currentUrl.protocol = callbackUrl.protocol; |
| 35 | + return currentUrl; |
| 36 | + }; |
| 37 | + |
| 38 | + passport.use(strategy); |
| 39 | + |
| 40 | + passport.serializeUser((user, done) => { |
| 41 | + done(null, user.oidcId || user.username); |
| 42 | + }) |
| 43 | + |
| 44 | + passport.deserializeUser(async (id, done) => { |
| 45 | + try { |
| 46 | + const user = await db.findUserByOIDC(id); |
| 47 | + done(null, user); |
| 48 | + } catch (err) { |
| 49 | + done(err); |
| 50 | + } |
| 51 | + }) |
| 52 | + passport.type = server.host; |
| 53 | + |
| 54 | + return passport; |
| 55 | + } catch (error) { |
| 56 | + console.error('OIDC configuration failed:', error); |
| 57 | + throw error; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +module.exports.configure = configure; |
| 63 | + |
| 64 | +/** |
| 65 | + * Handles user authentication with OIDC. |
| 66 | + * @param {Object} userInfo the OIDC user info object |
| 67 | + * @param {Function} done the callback function |
| 68 | + * @return {Promise} a promise with the authenticated user or an error |
| 69 | + */ |
| 70 | +const handleUserAuthentication = async (userInfo, done) => { |
| 71 | + try { |
| 72 | + const user = await db.findUserByOIDC(userInfo.sub); |
| 73 | + |
| 74 | + if (!user) { |
| 75 | + const email = safelyExtractEmail(userInfo); |
| 76 | + if (!email) return done(new Error('No email found in OIDC profile')); |
| 77 | + |
| 78 | + const newUser = { |
| 79 | + username: getUsername(email), |
| 80 | + email, |
| 81 | + oidcId: userInfo.sub, |
| 82 | + }; |
| 83 | + |
| 84 | + await db.createUser(newUser.username, null, newUser.email, 'Edit me', false, newUser.oidcId); |
| 85 | + return done(null, newUser); |
| 86 | + } |
| 87 | + |
| 88 | + return done(null, user); |
| 89 | + } catch (err) { |
| 90 | + return done(err); |
| 91 | + } |
| 92 | +}; |
| 93 | + |
| 94 | +/** |
| 95 | + * Extracts email from OIDC profile. |
| 96 | + * This function is necessary because OIDC providers have different ways of storing emails. |
| 97 | + * @param {object} profile the profile object from OIDC provider |
| 98 | + * @return {string | null} the email address |
| 99 | + */ |
| 100 | +const safelyExtractEmail = (profile) => { |
| 101 | + return profile.email || (profile.emails && profile.emails.length > 0 ? profile.emails[0].value : null); |
| 102 | +}; |
| 103 | + |
| 104 | +/** |
| 105 | + * Generates a username from email address. |
| 106 | + * This helps differentiate users within the specific OIDC provider. |
| 107 | + * Note: This is incompatible with multiple providers. Ideally, users are identified by |
| 108 | + * OIDC ID (requires refactoring the database). |
| 109 | + * @param {string} email the email address |
| 110 | + * @return {string} the username |
| 111 | + */ |
| 112 | +const getUsername = (email) => { |
| 113 | + return email ? email.split('@')[0] : ''; |
| 114 | +}; |
0 commit comments