Encryption in JWT for single-user password mode (#2111)

* wip encrypting jwt value

* Encrypt/Decrypt pass in JWT value for verification in single-user password mode
This commit is contained in:
Timothy Carambat 2024-08-13 17:54:12 -07:00 committed by GitHub
parent b541623c9e
commit 4430ddb059
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 4 deletions

View File

@ -6,7 +6,7 @@ concurrency:
on:
push:
branches: ['pipertts-support'] # put your current branch to create a build. Core team only.
branches: ['encrypt-jwt-value'] # put your current branch to create a build. Core team only.
paths-ignore:
- '**.md'
- 'cloud-deployments/*'

View File

@ -51,6 +51,7 @@ const {
generateRecoveryCodes,
} = require("../utils/PasswordRecovery");
const { SlashCommandPresets } = require("../models/slashCommandsPresets");
const { EncryptionManager } = require("../utils/EncryptionManager");
function systemEndpoints(app) {
if (!app) return;
@ -236,7 +237,10 @@ function systemEndpoints(app) {
});
response.status(200).json({
valid: true,
token: makeJWT({ p: password }, "30d"),
token: makeJWT(
{ p: new EncryptionManager().encrypt(password) },
"30d"
),
message: null,
});
}

View File

@ -1,6 +1,8 @@
const { SystemSettings } = require("../../models/systemSettings");
const { User } = require("../../models/user");
const { EncryptionManager } = require("../EncryptionManager");
const { decodeJWT } = require("../http");
const EncryptionMgr = new EncryptionManager();
async function validatedRequest(request, response, next) {
const multiUserMode = await SystemSettings.isMultiUserMode();
@ -39,14 +41,24 @@ async function validatedRequest(request, response, next) {
const bcrypt = require("bcrypt");
const { p } = decodeJWT(token);
if (p === null) {
if (p === null || !/\w{32}:\w{32}/.test(p)) {
response.status(401).json({
error: "Token expired or failed validation.",
});
return;
}
if (!bcrypt.compareSync(p, bcrypt.hashSync(process.env.AUTH_TOKEN, 10))) {
// Since the blame of this comment we have been encrypting the `p` property of JWTs with the persistent
// encryptionManager PEM's. This prevents us from storing the `p` unencrypted in the JWT itself, which could
// be unsafe. As a consequence, existing JWTs with invalid `p` values that do not match the regex
// in ln:44 will be marked invalid so they can be logged out and forced to log back in and obtain an encrypted token.
// This kind of methodology only applies to single-user password mode.
if (
!bcrypt.compareSync(
EncryptionMgr.decrypt(p),
bcrypt.hashSync(process.env.AUTH_TOKEN, 10)
)
) {
response.status(401).json({
error: "Invalid auth credentials.",
});