ad2cacf094
- Auth tab is now a single choice (Off / Basic auth / SSO) instead of two independent toggles that could both be on at once, which made it ambiguous which gate actually protected a request. Enforced both in the UI and server-side (POST/PUT), accounting for partial PUT updates against the existing record. - Add per-user basic-auth management (change password, delete) so an admin no longer has to blow away and retype the whole user list to remove or rotate one account. - Fix: `Model.errors.ObjectValidateError(...)` is a constructor and was being called without `new` everywhere in this codebase. Without `new`, `this` inside it was the module's shared `errors` object (mutated in place) and the call evaluated to `undefined` — so every `throw Model.errors.ObjectValidateError(...)` actually threw `undefined`, which Express's `next(undefined)` treats as "no error" and silently falls through to the catch-all 404 handler. Every host/user/group/ permission/dns-provider validation error (bad hostname, bad IP, etc.) was showing a confusing "Page not found" instead of the real message. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
240 lines
7.7 KiB
JavaScript
Executable File
240 lines
7.7 KiB
JavaScript
Executable File
'use strict';
|
|
|
|
const router = require('express').Router();
|
|
const conf = require('@simpleworkjs/conf');
|
|
const {Host, Domain, User} = require('../models').models;
|
|
const {LocalGroup} = require('../models/local_group');
|
|
const {Permission} = require('../models/permission');
|
|
const authz = require('../middleware/authz');
|
|
const {normalizeHostFeatures, sanitizeBasicAuthObject} = require('../utils/host_features');
|
|
const {collectHostFieldErrors} = require('../utils/hostname_validate');
|
|
const {hashBasicAuthUsers} = require('../utils/basicauth');
|
|
|
|
const Model = Host;
|
|
|
|
// Reject a malformed host/target before it reaches the model. Throws a 422
|
|
// ObjectValidateError (per-field keys) that the frontend surfaces inline.
|
|
function validateHostFields(body){
|
|
let errors = collectHostFieldErrors(body);
|
|
if(errors.length) throw new Model.errors.ObjectValidateError(errors);
|
|
}
|
|
|
|
// Basic auth and SSO are mutually exclusive per host (having both enabled
|
|
// invites confusion about which gate actually protected a request). `existing`
|
|
// is the current record (undefined on create), so a partial PUT that only
|
|
// touches one of the two fields is still checked against the other's current
|
|
// value.
|
|
function validateAuthExclusivity(body, existing){
|
|
let basic = 'basicauth_enabled' in body ? body.basicauth_enabled : (existing ? existing.basicauth_enabled : false);
|
|
let sso = 'sso_enabled' in body ? body.sso_enabled : (existing ? existing.sso_enabled : false);
|
|
if(basic && sso){
|
|
throw new Model.errors.ObjectValidateError([
|
|
{key: 'sso_enabled', message: 'Basic auth and SSO cannot both be enabled for the same host — pick one.'},
|
|
]);
|
|
}
|
|
}
|
|
|
|
// After normalizeHostFeatures has parsed basic-auth creds to {user: plaintext},
|
|
// hash them so plaintext never reaches Redis. Runs at the route layer only, so
|
|
// internally-copied records (cache/wildcard children) keep their existing hashes.
|
|
function hashHostSecrets(body){
|
|
if(body.basicauth_users && typeof body.basicauth_users === 'object'){
|
|
body.basicauth_users = hashBasicAuthUsers(body.basicauth_users);
|
|
}
|
|
}
|
|
|
|
// Autocomplete source for the per-host auth allow-lists (SSO users/groups).
|
|
// Available to any authenticated host editor (not just global admins). Groups
|
|
// are derived from local groups, existing permission group-subjects, and the
|
|
// conf.auth admin/role-map groups.
|
|
router.get('/auth-suggestions', async function(req, res, next){
|
|
try{
|
|
let users = [];
|
|
try{ users = (await User.list()) || []; }catch(error){ /* none */ }
|
|
|
|
let groups = new Set();
|
|
try{ for(let g of await LocalGroup.list()) groups.add(g); }catch(error){ /* none */ }
|
|
try{
|
|
for(let p of await Permission.listDetail()){
|
|
if(p.subjectType === 'group' && p.subject) groups.add(p.subject);
|
|
}
|
|
}catch(error){ /* none */ }
|
|
for(let g of (conf.auth && conf.auth.adminGroups) || []) groups.add(g);
|
|
for(let g of Object.keys((conf.auth && conf.auth.groupRoleMap) || {})) groups.add(g);
|
|
|
|
return res.json({users, groups: [...groups].sort()});
|
|
}catch(error){
|
|
return next(error);
|
|
}
|
|
});
|
|
|
|
router.get('/', async function(req, res, next){
|
|
try{
|
|
let results = await Model[req.query.detail ? "listDetail" : "list"]();
|
|
|
|
// Restrict to hosts whose domain the caller may view. list() yields host
|
|
// strings; listDetail() yields instances with a .host.
|
|
results = await authz.filterViewable(req, results,
|
|
item => (typeof item === 'string' ? item : item.host));
|
|
|
|
return res.json({results});
|
|
}catch(error){
|
|
return next(error);
|
|
}
|
|
});
|
|
|
|
router.post('/', authz.requireDomainRole('manager', authz.resolve.hostBody), async function(req, res, next){
|
|
try{
|
|
req.body.created_by = authz.reqUsername(req);
|
|
validateHostFields(req.body);
|
|
normalizeHostFeatures(req.body);
|
|
validateAuthExclusivity(req.body);
|
|
hashHostSecrets(req.body);
|
|
let item = await Model.create(req.body);
|
|
|
|
return res.json({
|
|
message: `"${item[Model._key]}" added.`,
|
|
...item,
|
|
});
|
|
} catch (error){
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
router.get('/lookup/:item', authz.requireDomainRole('viewer', authz.resolve.hostParam), async function(req, res, next){
|
|
try{
|
|
return res.json({
|
|
string: req.params.item,
|
|
results: await Model.lookUp(req.params.item),
|
|
});
|
|
|
|
}catch(error){
|
|
return next(error);
|
|
}
|
|
});
|
|
|
|
// The full lookup tree exposes every host, so restrict it to admins.
|
|
router.get('/lookupobj', authz.requireAdmin, async function(req, res, next){
|
|
try{
|
|
return res.json({
|
|
results: Model.lookUpObj,
|
|
});
|
|
|
|
}catch(error){
|
|
return next(error);
|
|
}
|
|
});
|
|
|
|
router.delete('/cache', authz.requireAdmin, async function(req, res, next){
|
|
try{
|
|
let count = await Model.clearCache();
|
|
|
|
return res.json({
|
|
message: `Cleared ${count} cached host${count === 1 ? '' : 's'}.`,
|
|
count,
|
|
});
|
|
}catch(error){
|
|
return next(error);
|
|
}
|
|
});
|
|
|
|
router.get('/:item', authz.requireDomainRole('viewer', authz.resolve.hostParam), async function(req, res, next){
|
|
try{
|
|
|
|
return res.json({
|
|
item: req.params.item,
|
|
results: await Model.get(req.params.item)
|
|
});
|
|
}catch(error){
|
|
return next(error);
|
|
}
|
|
});
|
|
|
|
router.put('/:item', authz.requireDomainRole('manager', authz.resolve.hostParam), async function(req, res, next){
|
|
try{
|
|
req.body.updated_by = authz.reqUsername(req);
|
|
validateHostFields(req.body);
|
|
normalizeHostFeatures(req.body);
|
|
let existing = await Model.get(req.params.item);
|
|
validateAuthExclusivity(req.body, existing);
|
|
hashHostSecrets(req.body);
|
|
let item = await existing.update(req.body);
|
|
|
|
return res.json({
|
|
message: `"${req.params.item}" updated.`,
|
|
__requestedHost: req.params.item,
|
|
...item,
|
|
});
|
|
|
|
}catch(error){
|
|
return next(error);
|
|
|
|
}
|
|
});
|
|
|
|
router.delete('/:item', authz.requireDomainRole('manager', authz.resolve.hostParam), async function(req, res, next){
|
|
try{
|
|
let item = await Model.get(req.params.item);
|
|
let count = await item.remove();
|
|
|
|
return res.json({
|
|
message: `${req.params.item} deleted`,
|
|
...item,
|
|
});
|
|
|
|
}catch(error){
|
|
return next(error);
|
|
}
|
|
});
|
|
|
|
// Manage a single basic-auth user without replacing the whole list — the bulk
|
|
// PUT /:item endpoint always replaces basicauth_users wholesale (an empty
|
|
// textarea there means "leave existing users untouched", see
|
|
// normalizeHostFeatures), which makes deleting or rotating one user's
|
|
// password error-prone from that form. These two routes touch exactly one key.
|
|
router.put('/:item/basicauth-user/:username', authz.requireDomainRole('manager', authz.resolve.hostParam), async function(req, res, next){
|
|
try{
|
|
let item = await Model.get(req.params.item);
|
|
let sanitized = sanitizeBasicAuthObject({[req.params.username]: req.body.password});
|
|
let username = Object.keys(sanitized)[0];
|
|
if(!username){
|
|
throw new Model.errors.ObjectValidateError([{key: 'password', message: 'Invalid username or empty password.'}]);
|
|
}
|
|
|
|
let users = Object.assign({}, item.basicauth_users, hashBasicAuthUsers(sanitized));
|
|
item = await item.update({basicauth_users: users, updated_by: authz.reqUsername(req)});
|
|
|
|
return res.json({message: `User "${username}" saved.`, ...item});
|
|
}catch(error){
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
router.delete('/:item/basicauth-user/:username', authz.requireDomainRole('manager', authz.resolve.hostParam), async function(req, res, next){
|
|
try{
|
|
let item = await Model.get(req.params.item);
|
|
let users = Object.assign({}, item.basicauth_users);
|
|
delete users[req.params.username];
|
|
item = await item.update({basicauth_users: users, updated_by: authz.reqUsername(req)});
|
|
|
|
return res.json({message: `User "${req.params.username}" removed.`, ...item});
|
|
}catch(error){
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
router.put('/:item/renew', authz.requireDomainRole('manager', authz.resolve.hostParam), async function(req, res, next){
|
|
try{
|
|
let item = await Model.get(req.params.item);
|
|
item.createWildcardCert();
|
|
|
|
return res.json({
|
|
message: `Requesting wildcard cert for ${req.params.item}`,
|
|
})
|
|
}catch(error){
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|