pediatric-ai-scribe-v3/src/utils/policy.js

35 lines
1.3 KiB
JavaScript

var db = require('../db/database');
async function isSSOOnly() {
return await db.getSetting('oidc.enabled') === 'true' && await db.getSetting('oidc.disable_local_auth') === 'true';
}
async function requireLocalAuth(req, res, next) {
try {
if (await isSSOOnly()) return res.status(403).json({ error: 'Local authentication is disabled. Use SSO.', code: 'sso_only' });
next();
} catch (e) { res.status(503).json({ error: 'Authentication policy unavailable' }); }
}
// Missing settings retain the seeded defaults; malformed values deny access.
async function isFeatureEnabled(name) {
var value = await db.getSetting('feature.' + name);
return value == null || value === 'true';
}
function requireFeature(name) {
return async function(req, res, next) {
try {
if (!await isFeatureEnabled(name)) return res.status(403).json({ error: 'Feature disabled', code: 'feature_disabled' });
next();
} catch (e) { res.status(503).json({ error: 'Feature policy unavailable' }); }
};
}
async function getUserFeatures() {
var features = {};
for (var name of ['read_aloud', 'nextcloud', 'memories']) features[name] = await isFeatureEnabled(name);
return features;
}
module.exports = { isSSOOnly, requireLocalAuth, isFeatureEnabled, requireFeature, getUserFeatures };