Add HostManager for host records and host tokens

controller.hosts is now a HostManager wrapping the hosts datastore, the
same way controller.users wraps the users datastore. It owns creating
hosts (moved from HostPairingManager), revoking them (moved from
ControlConnection) and signing and verifying host tokens (moved from
Controller, generateHostToken is renamed signHostToken to match
signUserToken).

UserManager gains verifyUserToken, replacing the copies of the user
token check in WsServer and routes. The WsServer copy accepted a token
without iat when the user had no tokenValidAfter; the shared check
always requires iat, which jsonwebtoken adds by default.

Follows Cooldude2606's comment on #1161.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bbassieandClaude Opus 5.5 committed 2026-11-04 00:37:37 +00:00
1 parent 5d1e98dafd
commit e6554e09c7
20 files changed
+404 -198

No files matched your search

+6 -23
View File
@@ -6,7 +6,6 @@ import busboy from "busboy";
import crypto from "crypto";
import events from "events";
import fs from "node:fs/promises";
import jwt from "jsonwebtoken";
import path from "path";
import nodeStream from "stream";
import util from "util";
@@ -189,7 +188,7 @@ function validateHostToken(req: Request, res: Response, next: any) {
}
try {
req.app.locals.controller.verifyHostToken(token);
req.app.locals.controller.hosts.verifyHostToken(token);
} catch (err) {
res.sendStatus(401);
return;
@@ -206,7 +205,7 @@ function validateUserToken(req: Request, res: Response, next: any) {
}
try {
res.locals.user = userFromToken(req.app.locals.controller, token);
res.locals.user = req.app.locals.controller.users.verifyUserToken(token);
} catch (err) {
res.sendStatus(401);
return;
@@ -215,21 +214,6 @@ function validateUserToken(req: Request, res: Response, next: any) {
next();
}
function userFromToken(controller: Controller, token: string) {
const tokenPayload = jwt.verify(token, controller.authSecret, { audience: "user" });
if (typeof tokenPayload === "string") {
throw new Error("unexpected JsonWebToken type");
}
let user = controller.users.getByName(tokenPayload.user);
if (!user) {
throw new Error("invalid user");
}
if (!tokenPayload.iat || tokenPayload.iat < user.tokenValidAfter) {
throw new Error("invalid token");
}
return user;
}
// Responds with the status of a pairing error, returns false for other errors.
function sendHostPairingError(res: Response, err: unknown) {
if (!(err instanceof HostPairingError)) {
@@ -271,7 +255,7 @@ function createHostPairing(req: Request, res: Response) {
if (token !== undefined) {
let user;
try {
user = userFromToken(controller, token);
user = controller.users.verifyUserToken(token);
} catch (err) {
res.status(401).json({ error: "Invalid user token" });
return;
@@ -285,12 +269,11 @@ function createHostPairing(req: Request, res: Response) {
res.status(403).json({ error: `User ${user.name} is not allowed to approve hosts` });
return;
}
const result = controller.hostPairing.createHost(name);
const host = controller.hosts.createHost(name);
logger.audit(
`User ${user.name} approved host ${result.name} (${result.hostId}) from ${remoteAddress} ` +
"with their token"
`User ${user.name} approved host ${host.name} (${host.id}) from ${remoteAddress} with their token`
);
res.status(200).json(result);
res.status(200).json({ hostId: host.id, token: controller.hosts.signHostToken(host.id), name: host.name });
return;
}