Skip to content

Commit

Permalink
Encrypt/Decrypt pass in JWT value for verification in single-user pas…
Browse files Browse the repository at this point in the history
…sword mode
  • Loading branch information
timothycarambat committed Aug 14, 2024
1 parent 87526bf commit b30b4f6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dev-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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/*'
Expand Down
5 changes: 4 additions & 1 deletion server/endpoints/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,10 @@ function systemEndpoints(app) {
});
response.status(200).json({
valid: true,
token: makeJWT({ p: new EncryptionManager().encrypt(password) }, "30d"),
token: makeJWT(
{ p: new EncryptionManager().encrypt(password) },
"30d"
),
message: null,
});
}
Expand Down
28 changes: 19 additions & 9 deletions server/utils/middleware/validatedRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ async function validatedRequest(request, response, next) {

// When in development passthrough auth token for ease of development.
// Or if the user simply did not set an Auth token or JWT Secret
// if (
// process.env.NODE_ENV === "development" ||
// !process.env.AUTH_TOKEN ||
// !process.env.JWT_SECRET
// ) {
// next();
// return;
// }
if (
process.env.NODE_ENV === "development" ||
!process.env.AUTH_TOKEN ||
!process.env.JWT_SECRET
) {
next();
return;
}

if (!process.env.AUTH_TOKEN) {
response.status(401).json({
Expand Down Expand Up @@ -48,7 +48,17 @@ async function validatedRequest(request, response, next) {
return;
}

if (!bcrypt.compareSync(EncryptionMgr.decrypt(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.",
});
Expand Down

0 comments on commit b30b4f6

Please sign in to comment.