mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-15 23:24:52 +09:00
Move user token signing to UserManager
Only the controller has access to the secret needed to sign user tokens so it makes sense for the user signing code to live in the controller. This saves about 20ms on the time ctl takes to start up due to lib no longer needing to import jsonwebtoken.
This commit is contained in:
1 parent
6af25e085c
commit
a5c7eb3a27
7 files changed
+20
-21
No files matched your search
@@ -130,7 +130,7 @@ async function handleBootstrapCommand(args, controllerConfig) {
|
||||
return;
|
||||
}
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(user.createToken(controllerConfig.get("controller.auth_secret")));
|
||||
console.log(userManager.signUserToken(user.name));
|
||||
|
||||
} else if (subCommand === "generate-host-token") {
|
||||
// eslint-disable-next-line no-console
|
||||
@@ -151,7 +151,8 @@ async function handleBootstrapCommand(args, controllerConfig) {
|
||||
|
||||
controlConfig.set("control.controller_url", Controller.calculateControllerUrl(controllerConfig));
|
||||
controlConfig.set(
|
||||
"control.controller_token", admin.createToken(controllerConfig.get("controller.auth_secret"))
|
||||
"control.controller_token",
|
||||
userManager.signUserToken(admin.name),
|
||||
);
|
||||
|
||||
let content = JSON.stringify(controlConfig.serialize(), null, 4);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"use strict";
|
||||
const fs = require("fs-extra");
|
||||
const jwt = require("jsonwebtoken");
|
||||
|
||||
const lib = require("@clusterio/lib");
|
||||
|
||||
@@ -83,6 +84,20 @@ class UserManager {
|
||||
this.users.set(name, user);
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sign access token for the given user name
|
||||
*
|
||||
* @param {string} name - user name to sign token for
|
||||
* @returns {string} JWT access token for the user.
|
||||
*/
|
||||
signUserToken(name) {
|
||||
return jwt.sign(
|
||||
{ aud: "user", user: name },
|
||||
Buffer.from(this._config.get("controller.auth_secret"), "base64")
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = UserManager;
|
||||
@@ -16,7 +16,6 @@
|
||||
"chalk": "^4.1.2",
|
||||
"fast-deep-equal": "^3.1.3",
|
||||
"fs-extra": "^11.1.1",
|
||||
"jsonwebtoken": "^9.0.1",
|
||||
"jszip": "^3.10.1",
|
||||
"klaw": "^4.1.0",
|
||||
"set-blocking": "^2.0.0",
|
||||
|
||||
@@ -7,8 +7,6 @@
|
||||
* @module lib/users
|
||||
*/
|
||||
"use strict";
|
||||
const jwt = require("jsonwebtoken");
|
||||
|
||||
const libErrors = require("./errors");
|
||||
|
||||
const PlayerStats = require("./PlayerStats");
|
||||
@@ -257,16 +255,6 @@ class User {
|
||||
return serialized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate access token for this user
|
||||
*
|
||||
* @param {string} secret - Secret to sign token with.
|
||||
* @returns {string} JWT access token for the user.
|
||||
*/
|
||||
createToken(secret) {
|
||||
return jwt.sign({ aud: "user", user: this.name }, Buffer.from(secret, "base64"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidate current tokens for the user
|
||||
*
|
||||
|
||||
@@ -44,10 +44,6 @@ module.exports = (env = {}) => ({
|
||||
test: /node_modules.fs-extra/,
|
||||
use: require.resolve("null-loader"),
|
||||
},
|
||||
{
|
||||
test: /node_modules.jsonwebtoken/,
|
||||
use: require.resolve("null-loader"),
|
||||
},
|
||||
{
|
||||
test: /node_modules.winston.dist.winston.transports.(http|file)/,
|
||||
use: require.resolve("null-loader"),
|
||||
|
||||
@@ -149,7 +149,7 @@ class ControllerPlugin extends lib.BaseControllerPlugin {
|
||||
return;
|
||||
}
|
||||
|
||||
let token = user.createToken(secret);
|
||||
let token = this.controller.userManager.signUserToken(user.name);
|
||||
res.send({ verified: true, token });
|
||||
return;
|
||||
|
||||
|
||||
+1
-1
@@ -165,7 +165,7 @@ class MockController {
|
||||
},
|
||||
};
|
||||
|
||||
this.userManager = new UserManager();
|
||||
this.userManager = new UserManager(this.config);
|
||||
this.userManager.roles = new Map([
|
||||
[0, new lib.Role({ id: 0, name: "Admin", description: "admin", permissions: ["core.admin"] })],
|
||||
[1, new lib.Role({ id: 1, name: "Player", description: "player", permissions: [] })],
|
||||
|
||||
Reference in new issue
Block a user