mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-13 07:04:53 +09:00
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:
1 parent
5d1e98dafd
commit
e6554e09c7
20 files changed
+404
-198
No files matched your search
@@ -135,6 +135,31 @@ describe("controller/UserManager", function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe(".verifyUserToken()", function () {
|
||||
const secret = () => Buffer.from(config.get("controller.auth_secret"), "base64");
|
||||
|
||||
it("should return the user the token was signed for", function () {
|
||||
const user = userManager.createUser("Judy");
|
||||
assert.equal(userManager.verifyUserToken(userManager.signUserToken(user)).id, user.id);
|
||||
});
|
||||
it("should reject a token for an unknown user", function () {
|
||||
const token = userManager.signUserToken({ id: "nobody" });
|
||||
assert.throws(() => userManager.verifyUserToken(token), new Error("invalid user"));
|
||||
});
|
||||
it("should reject a token issued before tokenValidAfter", function () {
|
||||
const user = userManager.createUser("Mallory");
|
||||
const token = jwt.sign({ aud: "user", user: user.id, iat: user.tokenValidAfter - 1 }, secret());
|
||||
assert.throws(() => userManager.verifyUserToken(token), new Error("invalid token"));
|
||||
});
|
||||
it("should reject host tokens and bad signatures", function () {
|
||||
const user = userManager.createUser("Niaj");
|
||||
const hostToken = jwt.sign({ aud: "host", host: 5 }, secret());
|
||||
assert.throws(() => userManager.verifyUserToken(hostToken), /audience invalid/);
|
||||
const forged = jwt.sign({ aud: "user", user: user.id }, "wrong secret");
|
||||
assert.throws(() => userManager.verifyUserToken(forged), /invalid signature/);
|
||||
});
|
||||
});
|
||||
|
||||
describe(".clearStatsOfInstance()", function () {
|
||||
it("should clear instance stats for all users", function () {
|
||||
const instanceId = 42;
|
||||
|
||||
Reference in new issue
Block a user