Skip to content

Commit a52a78f

Browse files
committed
Avoid reading env variables directly in the token utils
1 parent b315656 commit a52a78f

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

apps/webapp/app/services/organizationAccessToken.server.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { z } from "zod";
44
import { prisma } from "~/db.server";
55
import { logger } from "./logger.server";
66
import { decryptToken, encryptToken, hashToken } from "~/utils/tokens";
7+
import { env } from "~/env.server";
78

89
const tokenValueLength = 40;
910
//lowercase only, removed 0 and l to avoid confusion
@@ -147,7 +148,7 @@ export async function createOrganizationAccessToken({
147148
expiresAt,
148149
}: CreateOrganizationAccessTokenOptions) {
149150
const token = createToken();
150-
const encryptedToken = encryptToken(token);
151+
const encryptedToken = encryptToken(token, env.ENCRYPTION_KEY);
151152

152153
const organizationAccessToken = await prisma.organizationAccessToken.create({
153154
data: {
@@ -199,7 +200,8 @@ function decryptOrganizationAccessToken(organizationAccessToken: OrganizationAcc
199200
const decryptedToken = decryptToken(
200201
encryptedData.data.nonce,
201202
encryptedData.data.ciphertext,
202-
encryptedData.data.tag
203+
encryptedData.data.tag,
204+
env.ENCRYPTION_KEY
203205
);
204206
return decryptedToken;
205207
}

apps/webapp/app/services/personalAccessToken.server.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { z } from "zod";
44
import { prisma } from "~/db.server";
55
import { logger } from "./logger.server";
66
import { decryptToken, encryptToken, hashToken } from "~/utils/tokens";
7+
import { env } from "~/env.server";
78

89
const tokenValueLength = 40;
910
//lowercase only, removed 0 and l to avoid confusion
@@ -265,7 +266,7 @@ export async function createPersonalAccessToken({
265266
userId,
266267
}: CreatePersonalAccessTokenOptions) {
267268
const token = createToken();
268-
const encryptedToken = encryptToken(token);
269+
const encryptedToken = encryptToken(token, env.ENCRYPTION_KEY);
269270

270271
const personalAccessToken = await prisma.personalAccessToken.create({
271272
data: {
@@ -313,7 +314,8 @@ function decryptPersonalAccessToken(personalAccessToken: PersonalAccessToken) {
313314
const decryptedToken = decryptToken(
314315
encryptedData.data.nonce,
315316
encryptedData.data.ciphertext,
316-
encryptedData.data.tag
317+
encryptedData.data.tag,
318+
env.ENCRYPTION_KEY
317319
);
318320
return decryptedToken;
319321
}

apps/webapp/app/utils/tokens.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import nodeCrypto from "node:crypto";
2-
import { env } from "~/env.server";
32

4-
export function encryptToken(value: string) {
3+
export function encryptToken(value: string, key: string) {
54
const nonce = nodeCrypto.randomBytes(12);
6-
const cipher = nodeCrypto.createCipheriv("aes-256-gcm", env.ENCRYPTION_KEY, nonce);
5+
const cipher = nodeCrypto.createCipheriv("aes-256-gcm", key, nonce);
76

87
let encrypted = cipher.update(value, "utf8", "hex");
98
encrypted += cipher.final("hex");
@@ -17,12 +16,8 @@ export function encryptToken(value: string) {
1716
};
1817
}
1918

20-
export function decryptToken(nonce: string, ciphertext: string, tag: string): string {
21-
const decipher = nodeCrypto.createDecipheriv(
22-
"aes-256-gcm",
23-
env.ENCRYPTION_KEY,
24-
Buffer.from(nonce, "hex")
25-
);
19+
export function decryptToken(nonce: string, ciphertext: string, tag: string, key: string): string {
20+
const decipher = nodeCrypto.createDecipheriv("aes-256-gcm", key, Buffer.from(nonce, "hex"));
2621

2722
decipher.setAuthTag(Buffer.from(tag, "hex"));
2823

0 commit comments

Comments
 (0)