mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-20 23:53:13 +09:00
Return the host record from verifyHostToken
Matches verifyUserToken returning the user. HostConnection._saveInfo is renamed saveRecord to match User.saveRecord. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e6554e09c7
commit
4c6aed3cff
5 files changed
+13
-12
No files matched your search
@@ -97,6 +97,7 @@ The host manager also signs and checks host tokens and creates and deletes hosts
|
|||||||
|
|
||||||
- `controller.generateHostToken(hostId)` is now `controller.hosts.signHostToken(hostId)`.
|
- `controller.generateHostToken(hostId)` is now `controller.hosts.signHostToken(hostId)`.
|
||||||
- `controller.verifyHostToken(token)` is now `controller.hosts.verifyHostToken(token)`.
|
- `controller.verifyHostToken(token)` is now `controller.hosts.verifyHostToken(token)`.
|
||||||
|
It returns the `HostRecord` instead of the host id.
|
||||||
- `controller.hostPairing.createHost(name)` is now `controller.hosts.createHost(name)`.
|
- `controller.hostPairing.createHost(name)` is now `controller.hosts.createHost(name)`.
|
||||||
It returns the new `HostRecord` instead of `{ hostId, token, name }`, sign a token for it with `signHostToken`.
|
It returns the new `HostRecord` instead of `{ hostId, token, name }`, sign a token for it with `signHostToken`.
|
||||||
- `controller.hosts.revokeHost(hostId, revokedBy)` deletes a host the same way `HostRevokeRequest` does.
|
- `controller.hosts.revokeHost(hostId, revokedBy)` deletes a host the same way `HostRevokeRequest` does.
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ export default class HostConnection extends BaseConnection {
|
|||||||
this._controller.instances.records.setMany(instances);
|
this._controller.instances.records.setMany(instances);
|
||||||
|
|
||||||
this.info.connected = false;
|
this.info.connected = false;
|
||||||
this._saveInfo();
|
this.saveRecord();
|
||||||
});
|
});
|
||||||
|
|
||||||
this.handle(lib.GetFactorioCredentialsRequest, this.handleGetFactorioCredentialsRequest.bind(this));
|
this.handle(lib.GetFactorioCredentialsRequest, this.handleGetFactorioCredentialsRequest.bind(this));
|
||||||
@@ -173,7 +173,7 @@ export default class HostConnection extends BaseConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Store changes to the host record, unless the host was revoked */
|
/** Store changes to the host record, unless the host was revoked */
|
||||||
private _saveInfo() {
|
saveRecord() {
|
||||||
if (this._controller.hosts.getMutable(this.id) === this.info) {
|
if (this._controller.hosts.getMutable(this.id) === this.info) {
|
||||||
this._controller.hosts.records.set(this.info);
|
this._controller.hosts.records.set(this.info);
|
||||||
}
|
}
|
||||||
@@ -182,7 +182,7 @@ export default class HostConnection extends BaseConnection {
|
|||||||
async handleHostInfoUpdateEvent(event: lib.HostInfoUpdateEvent) {
|
async handleHostInfoUpdateEvent(event: lib.HostInfoUpdateEvent) {
|
||||||
this.info.name = event.update.name;
|
this.info.name = event.update.name;
|
||||||
this.info.publicAddress = event.update.publicAddress;
|
this.info.publicAddress = event.update.publicAddress;
|
||||||
this._saveInfo();
|
this.saveRecord();
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleInstanceStatusChangedEvent(request: lib.InstanceStatusChangedEvent) {
|
async handleInstanceStatusChangedEvent(request: lib.InstanceStatusChangedEvent) {
|
||||||
|
|||||||
@@ -127,23 +127,23 @@ export default class HostManager {
|
|||||||
* Check a host token
|
* Check a host token
|
||||||
*
|
*
|
||||||
* @param token - Host token to check.
|
* @param token - Host token to check.
|
||||||
* @returns ID of the host the token was issued to.
|
* @returns the host the token was issued to.
|
||||||
* @throws if the token is invalid, its host is not registered or it was issued before tokenValidAfter.
|
* @throws if the token is invalid, its host is not registered or it was issued before tokenValidAfter.
|
||||||
*/
|
*/
|
||||||
verifyHostToken(token: string): number {
|
verifyHostToken(token: string): HostRecord {
|
||||||
// migrate: allow pre-rename tokens issued to hosts before alpha-14
|
// migrate: allow pre-rename tokens issued to hosts before alpha-14
|
||||||
const payload = jwt.verify(token, this._controller.authSecret, { audience: ["host", "slave"] });
|
const payload = jwt.verify(token, this._controller.authSecret, { audience: ["host", "slave"] });
|
||||||
if (typeof payload === "string") {
|
if (typeof payload === "string") {
|
||||||
throw new Error("unexpected JsonWebToken type");
|
throw new Error("unexpected JsonWebToken type");
|
||||||
}
|
}
|
||||||
const hostId = payload.host !== undefined ? payload.host : payload.slave;
|
const hostId = payload.host !== undefined ? payload.host : payload.slave;
|
||||||
const host = this.records.get(hostId);
|
const host = this.records.getMutable(hostId);
|
||||||
if (!host) {
|
if (!host) {
|
||||||
throw new Error("host is not registered, start the host again to request to join the cluster");
|
throw new Error("host is not registered, start the host again to request to join the cluster");
|
||||||
}
|
}
|
||||||
if (!payload.iat || payload.iat < host.tokenValidAfter) {
|
if (!payload.iat || payload.iat < host.tokenValidAfter) {
|
||||||
throw new Error("invalid token");
|
throw new Error("invalid token");
|
||||||
}
|
}
|
||||||
return hostId;
|
return host;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -252,7 +252,7 @@ ${err.stack}`
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (this.controller.hosts.verifyHostToken(data.token) !== data.id) {
|
if (this.controller.hosts.verifyHostToken(data.token).id !== data.id) {
|
||||||
throw new Error("missmatched host id");
|
throw new Error("missmatched host id");
|
||||||
}
|
}
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ describe("controller/src/HostManager", function() {
|
|||||||
assert.equal(host.connected, false);
|
assert.equal(host.connected, false);
|
||||||
assert(host.tokenValidAfter >= before, "tokenValidAfter predates the call");
|
assert(host.tokenValidAfter >= before, "tokenValidAfter predates the call");
|
||||||
assert(host.tokenValidAfter <= Math.floor(Date.now() / 1000), "tokenValidAfter is in the future");
|
assert(host.tokenValidAfter <= Math.floor(Date.now() / 1000), "tokenValidAfter is in the future");
|
||||||
assert.equal(hosts.verifyHostToken(hosts.signHostToken(host.id)), host.id);
|
assert.equal(hosts.verifyHostToken(hosts.signHostToken(host.id)), host);
|
||||||
});
|
});
|
||||||
it("should not reuse the id of an existing host", function() {
|
it("should not reuse the id of an existing host", function() {
|
||||||
hosts.records.set(new HostRecord(5, "existing", "", new Map()));
|
hosts.records.set(new HostRecord(5, "existing", "", new Map()));
|
||||||
@@ -91,12 +91,12 @@ describe("controller/src/HostManager", function() {
|
|||||||
hosts.records.set(new HostRecord(5, "five", "", new Map()));
|
hosts.records.set(new HostRecord(5, "five", "", new Map()));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return the id of the host", function() {
|
it("should return the mutable host record", function() {
|
||||||
assert.equal(hosts.verifyHostToken(hosts.signHostToken(5)), 5);
|
assert.equal(hosts.verifyHostToken(hosts.signHostToken(5)), hosts.getMutable(5));
|
||||||
});
|
});
|
||||||
it("should accept a token from before the slave rename", function() {
|
it("should accept a token from before the slave rename", function() {
|
||||||
const token = jwt.sign({ aud: "slave", slave: 5 }, controller.authSecret);
|
const token = jwt.sign({ aud: "slave", slave: 5 }, controller.authSecret);
|
||||||
assert.equal(hosts.verifyHostToken(token), 5);
|
assert.equal(hosts.verifyHostToken(token).id, 5);
|
||||||
});
|
});
|
||||||
it("should reject a token for an unknown host", function() {
|
it("should reject a token for an unknown host", function() {
|
||||||
assert.throws(() => hosts.verifyHostToken(hosts.signHostToken(6)), /host is not registered/);
|
assert.throws(() => hosts.verifyHostToken(hosts.signHostToken(6)), /host is not registered/);
|
||||||
|
|||||||
Reference in new issue
Block a user