From 2960db2cca373893212985569956378870c61b6c Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Tue, 21 Feb 2023 19:38:21 +0100 Subject: [PATCH] Add revoke user token action Implement revoking of user tokens to allow lost credentials to be invalidated. --- packages/ctl/src/commands.js | 9 ++++++ packages/lib/link/messages.js | 9 ++++++ packages/lib/users.js | 11 +++++++ packages/master/src/ControlConnection.js | 18 +++++++++++ packages/master/src/WsServerConnector.js | 14 +++++++++ .../web_ui/src/components/UserViewPage.jsx | 31 +++++++++++++++---- test/integration/clusterio.js | 21 +++++++++++++ 7 files changed, 107 insertions(+), 6 deletions(-) diff --git a/packages/ctl/src/commands.js b/packages/ctl/src/commands.js index 4f1c03ce..9e0cc35d 100644 --- a/packages/ctl/src/commands.js +++ b/packages/ctl/src/commands.js @@ -1480,6 +1480,15 @@ userCommands.add(new libCommand.Command({ }, })); +userCommands.add(new libCommand.Command({ + definition: ["revoke-token ", "Revoke token for user", (yargs) => { + yargs.positional("name", { describe: "Name of user to revoke token for", type: "string" }); + }], + handler: async function(args, control) { + await libLink.messages.revokeUserToken.send(control, { name: args.name }); + }, +})); + userCommands.add(new libCommand.Command({ definition: ["set-admin ", "Promote or demote a user to admin", (yargs) => { yargs.positional("user", { describe: "Name of user set admin status for", type: "string" }); diff --git a/packages/lib/link/messages.js b/packages/lib/link/messages.js index 21a063db..5fa4707b 100644 --- a/packages/lib/link/messages.js +++ b/packages/lib/link/messages.js @@ -1036,6 +1036,15 @@ messages.createUser = new Request({ }, }); +messages.revokeUserToken = new Request({ + type: "revoke_user_token", + links: ["control-master"], + permission: "core.user.revoke_token", + requestProperties: { + "name": { type: "string" }, + }, +}); + messages.updateUserRoles = new Request({ type: "update_user_roles", links: ["control-master"], diff --git a/packages/lib/users.js b/packages/lib/users.js index 00b90de9..1e0156cb 100644 --- a/packages/lib/users.js +++ b/packages/lib/users.js @@ -637,6 +637,17 @@ definePermission({ title: "Create users", description: "Create user acounts with a given name.", }); +definePermission({ + name: "core.user.revoke_token", + title: "Revoke user token", + description: "Revoke access token and terminate all existing control connections for oneself.", + grantByDefault: true, +}); +definePermission({ + name: "core.user.revoke_other_token", + title: "Revoke other user's token", + description: "Allow revoking tokens for other users than oneself. Requires Revoke user token.", +}); definePermission({ name: "core.user.update_roles", title: "Update user roles", diff --git a/packages/master/src/ControlConnection.js b/packages/master/src/ControlConnection.js index f695156e..2a7fbd3e 100644 --- a/packages/master/src/ControlConnection.js +++ b/packages/master/src/ControlConnection.js @@ -833,6 +833,24 @@ class ControlConnection extends BaseConnection { this._master.userUpdated(user); } + async revokeUserTokenRequestHandler(message) { + let user = this._master.userManager.users.get(message.data.name); + if (!user) { + throw new libErrors.RequestError(`User '${message.data.name}' does not exist`); + } + if (user.name !== this.user.name) { + this.user.checkPermission("core.user.revoke_other_token"); + } + + user.invalidateToken(); + for (let controlConnection of this._master.wsServer.controlConnections) { + if (controlConnection.user.name === user.name) { + controlConnection.connector.terminate(); + } + } + this._master.userUpdated(user); + } + async updateUserRolesRequestHandler(message) { let user = this._master.userManager.users.get(message.data.name); if (!user) { diff --git a/packages/master/src/WsServerConnector.js b/packages/master/src/WsServerConnector.js index 1db846cb..84311284 100644 --- a/packages/master/src/WsServerConnector.js +++ b/packages/master/src/WsServerConnector.js @@ -191,6 +191,20 @@ class WsServerConnector extends libLink.WebSocketBaseConnector { await events.once(this, "close"); } + /** + * Forcefully close the connection immediately + * + * This should only be used if there's a security implication from + * letting the connection carry on with a normal close proceedure. + */ + terminate() { + this.setClosing(); + + if (this._socket) { + this._socket.terminate(); + } + } + /** * Notify the connection is expected to close * diff --git a/packages/web_ui/src/components/UserViewPage.jsx b/packages/web_ui/src/components/UserViewPage.jsx index 7015f6e9..084a7a60 100644 --- a/packages/web_ui/src/components/UserViewPage.jsx +++ b/packages/web_ui/src/components/UserViewPage.jsx @@ -6,7 +6,7 @@ import { } from "antd"; import DeleteOutlined from "@ant-design/icons/DeleteOutlined"; -import { libLink } from "@clusterio/lib"; +import { libErrors, libLink } from "@clusterio/lib"; import { useAccount } from "../model/account"; import { useInstanceList } from "../model/instance"; @@ -14,7 +14,7 @@ import ControlContext from "./ControlContext"; import PageLayout from "./PageLayout"; import PluginExtra from "./PluginExtra"; import SectionHeader from "./SectionHeader"; -import { notifyErrorHandler } from "../util/notify"; +import notify, { notifyErrorHandler } from "../util/notify"; import { formatDuration } from "../util/time_format"; import { formatLastSeen, sortLastSeen, useUser } from "../model/user"; @@ -121,8 +121,27 @@ export default function UserViewPage() { {user["is_banned"] && Banned} } - extra={ - account.hasPermission("core.user.delete") && + {account.hasPermission("core.user.revoke_token") && ( + account.name === userName || account.hasPermission("core.user.revoke_other_token") + ) && } + {account.hasPermission("core.user.delete") && Delete user account and all data associated with it? {(user["is_banned"] && <>
This will remove the user ban!)} @@ -141,8 +160,8 @@ export default function UserViewPage() { -
- } +
} + } />
connector.once("close", resolve)); + + await execCtl("user revoke-token revokee"); + + await closed; + }); + }); + describe("user set-roles", function() { it("should set the roles on the user", async function() { getControl().userUpdates = [];