Files
jump-host/nodejs/middleware/auth.js
T
wmantly 67e2fc54c2 Release 1.2.0: adopt shared @simpleworkjs/* packages; fix directory envelope drift
Rewire onto the shared @simpleworkjs/oidc-client, /directory-schema, /ldap, and
/app-stack packages (deleting the byte-identical local forks). utils/access.js
now fetches reachable hosts through the shared directory client, which
validates the {results} envelope and treats envelope drift as a failed group
rather than silently returning []. models/user_ldap.js is a thin wrapper over
createLdapClient (loose TLS default preserved). build_info moves to utils/ with
the shared {buildVersion,buildHash,buildYear} shape. Align ldapts ^8.1.8 and
redis ^6.1.0. Lockfile regenerated from the registry (no file:/link:).

Co-Authored-By: Claude <noreply@anthropic.com>
2026-07-25 15:55:03 -04:00

55 lines
1.7 KiB
JavaScript

'use strict';
// Web UI/API auth, mirroring the sibling apps: a browser session token
// (`auth-token: <AuthToken uuid>`) established via local login or the OIDC
// callback. The token carries the group snapshot captured at login.
const conf = require('@simpleworkjs/conf');
const { Auth } = require('../models');
async function auth(req, res, next){
try{
req.token = await Auth.checkToken(req.header('auth-token'));
req.user = req.token.user;
req.groups = typeof req.token.groupsArray === 'function' ? req.token.groupsArray() : [];
return next();
}catch(error){
next(error);
}
}
// Is the authenticated request an admin? Admin = a session whose OIDC groups
// intersect conf.auth.adminGroups, OR the local anti-lockout admin
// (conf.auth.adminUsers). The whole web UI is admin-only (audit + metrics).
function isAdmin(req){
const adminGroups = (conf.auth && conf.auth.adminGroups) || [];
const adminUsers = (conf.auth && conf.auth.adminUsers) || [];
const username = req.user && req.user.username;
if(username && adminUsers.includes(username)) return true;
return (req.groups || []).some(g => adminGroups.includes(g));
}
async function requireAdmin(req, res, next){
if(isAdmin(req)) return next();
const error = new Error('Forbidden');
error.name = 'Forbidden';
error.status = 403;
error.message = 'Admin access required.';
next(error);
}
// Socket.IO handshake auth (app-base.js connects with the session token).
async function authIO(socket, next){
try{
const tok = socket.handshake.auth && socket.handshake.auth.token;
if(!tok) return next(Auth.errors.login());
const token = await Auth.checkToken(tok);
socket.user = token.user;
next();
}catch(error){
next(error);
}
}
module.exports = { auth, requireAdmin, authIO, isAdmin };