Add revoke user token action

Implement revoking of user tokens to allow lost credentials to be
invalidated.
This commit is contained in:
Hornwitser committed 2023-02-21 22:39:23 +01:00
1 parent 5426d9b2bd
commit 2960db2cca
7 files changed
+107 -6

No files matched your search

+9
View File
@@ -1480,6 +1480,15 @@ userCommands.add(new libCommand.Command({
},
}));
userCommands.add(new libCommand.Command({
definition: ["revoke-token <name>", "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 <user>", "Promote or demote a user to admin", (yargs) => {
yargs.positional("user", { describe: "Name of user set admin status for", type: "string" });
+9
View File
@@ -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"],
+11
View File
@@ -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",
+18
View File
@@ -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) {
+14
View File
@@ -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
*
@@ -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"] && <Tag color="red">Banned</Tag>}
</span>
</Space>}
extra={
account.hasPermission("core.user.delete") && <Popconfirm
extra={<>
{account.hasPermission("core.user.revoke_token") && (
account.name === userName || account.hasPermission("core.user.revoke_other_token")
) && <Button
danger
onClick={() => {
libLink.messages.revokeUserToken.send(
control, { name: userName }
).then(() => {
notify("User token revoked");
}, err => {
if (err instanceof libErrors.SessionLost && userName === account.name) {
// Got kicked out after revoking our own token
notify("User token revoked");
return;
}
throw err;
}).catch(notifyErrorHandler("Error revoking token"));
}}
>Revoke token</Button>}
{account.hasPermission("core.user.delete") && <Popconfirm
title={<>
Delete user account and all data associated with it?
{(user["is_banned"] && <><br/>This will remove the user ban!</>)}
@@ -141,8 +160,8 @@ export default function UserViewPage() {
<Button danger >
<DeleteOutlined />
</Button>
</Popconfirm>
}
</Popconfirm>}
</>}
/>
<Form
form={form}
+21
View File
@@ -1,6 +1,7 @@
"use strict";
const assert = require("assert").strict;
const fs = require("fs-extra");
const jwt = require("jsonwebtoken");
const path = require("path");
const events = require("events");
const phin = require("phin");
@@ -1087,6 +1088,26 @@ describe("Integration of Clusterio", function() {
});
});
describe("user revoke-token", function() {
it("should kick existing sessions for the user", async function() {
slowTest(this);
await libLink.messages.createUser.send(getControl(), { name: "revokee" });
let tlsCa = await fs.readFile("test/file/tls/cert.pem");
let connector = new TestControlConnector(url, 2, tlsCa);
connector.token = jwt.sign(
{ aud: "user", user: "revokee" }, Buffer.from("TestSecretDoNotUse", "base64")
);
let revokeeControl = new TestControl(connector);
await connector.connect();
connector.setClosing();
let closed = new Promise(resolve => 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 = [];