CJVT updates August and September 2026

This commit is contained in:
Luka Romih
2026-10-01 00:49:26 +02:00
parent fd02e25719
commit 83f73aeb58
135 changed files with 6146 additions and 1970 deletions
+122 -1
View File
@@ -1,6 +1,8 @@
const passport = require('passport')
const LocalStrategy = require('passport-local').Strategy
const RememberMeStrategy = require('passport-remember-me-extended').Strategy
const RememberMeStrategy =
require('../lib/passport-remember-me-extended').Strategy
const { Issuer, Strategy: OidcStrategy, custom } = require('openid-client')
const bcrypt = require('bcrypt')
const db = require('../models/db')
const User = require('../models/user')
@@ -8,6 +10,16 @@ const {
rememberMeCookieSettings,
REMEMBER_ME_DURATION_SQL
} = require('../config/settings')
const {
oidcUniLjClientId,
oidcUniLjClientSecret,
oidcUniLjDiscoveryUrl,
oidcUniLjAuthorizationExternalOrigin,
origin
} = require('../config/keys')
// Fail fast rather than hang a request indefinitely if the IdP is unreachable.
custom.setHttpOptionsDefaults({ timeout: 5000 })
passport.serializeUser((user, done) => {
done(null, user.id)
@@ -53,6 +65,14 @@ passport.use(
})
}
if (!user.bcrypt_hash) {
return done(null, false, {
message: req.t(
'Ta uporabniški račun je bil ustvarjen prek prijave z Univerzo v Ljubljani in nima gesla. Prijavite se prek gumba za prijavo z Univerzo v Ljubljani.'
)
})
}
const isCorrectPassword = await bcrypt.compare(
password,
user.bcrypt_hash
@@ -114,4 +134,105 @@ passport.use(
)
)
let cachedUniLjStrategy = null
// Lazily discovers + caches the University of Ljubljana OIDC client/Strategy. Deliberately NOT
// run at module load: `docker compose up` (default profile) never starts the dev Authentik
// stand-in (`--profile develop-idp`), and eager discovery here would hang/throw at express boot
// for every developer not touching SSO. Only ever called from an actual SSO login/callback
// request; failures are not cached, so the very next click retries (no restart needed once the
// IdP comes back up).
async function getUniLjStrategy() {
if (cachedUniLjStrategy) return cachedUniLjStrategy
const internalIssuer = await Issuer.discover(oidcUniLjDiscoveryUrl)
const metadata = internalIssuer.metadata
if (oidcUniLjAuthorizationExternalOrigin) {
const authUrl = new URL(metadata.authorization_endpoint)
const externalOrigin = new URL(oidcUniLjAuthorizationExternalOrigin)
authUrl.protocol = externalOrigin.protocol
authUrl.host = externalOrigin.host // includes port
metadata.authorization_endpoint = authUrl.href
}
const issuer = new Issuer(metadata)
const redirectUri = new URL('/users/oidc/callback', origin).href
const client = new issuer.Client({
client_id: oidcUniLjClientId,
client_secret: oidcUniLjClientSecret,
redirect_uris: [redirectUri],
response_types: ['code']
})
cachedUniLjStrategy = new OidcStrategy(
{
client,
params: { scope: 'openid email profile' },
passReqToCallback: true
},
verifyUniLj
)
passport.use('oidc-uni-lj', cachedUniLjStrategy)
return cachedUniLjStrategy
}
// The ID token's `claims` never carries given_name/family_name on the real University of
// Ljubljana/Entra IdP - only the standard /userinfo response does. The 4-arg verify-callback
// signature is required to get openid-client's Strategy to auto-fetch userinfo (see
// node_modules/openid-client/lib/passport_strategy.js's `loadUserinfo` check, which triggers on a
// 4-arg callback) - `userinfo` is a real data source here, not a debugging aid.
async function verifyUniLj(req, tokenset, userinfo, done) {
try {
const claims = tokenset.claims()
const subject = claims.sub
const linkedUser = await User.fetchByExternalIdentity('uni_lj', subject)
if (linkedUser) {
if (linkedUser.status !== 'active') {
return done(null, false, { action: 'account-inactive' })
}
return done(null, { id: linkedUser.id })
}
const parsedClaims = {
givenName: userinfo.given_name || '',
familyName: userinfo.family_name || '',
email: claims.email || '',
preferredUsername: claims.preferred_username || ''
}
// Both the create and convert flows below need a name to show/store - there's nothing to
// review or overwrite with otherwise. This is checked before the email-match lookup since it
// blocks both outcomes equally.
if (!parsedClaims.givenName || !parsedClaims.familyName) {
return done(null, false, { action: 'missing-required-data' })
}
if (parsedClaims.email) {
const matchedUser = await User.fetchActiveByEmail(parsedClaims.email)
if (matchedUser) {
return done(null, false, {
action: 'convert',
claims: parsedClaims,
subject,
matchedUserId: matchedUser.id
})
}
}
done(null, false, {
action: 'create-profile',
claims: parsedClaims,
subject
})
} catch (error) {
done(error)
}
}
module.exports = passport
module.exports.getUniLjStrategy = getUniLjStrategy