|
1 | 1 | import { create, decode, verify } from "djwt"; |
2 | | -import { getJwtSecret } from "./env.ts"; |
| 2 | +import { getJwtSecret, getAdminCredentials } from "./env.ts"; |
3 | 3 |
|
4 | | -async function getKey(): Promise<CryptoKey> { |
5 | | - const secretKey = getJwtSecret(); |
6 | | - const secretBytes = new TextEncoder().encode(secretKey); |
7 | | - if (secretBytes.length === 0) { |
| 4 | +function validateSecret(secretKey: string) { |
| 5 | + if (!secretKey || secretKey.trim().length === 0) { |
8 | 6 | throw new Error("JWT_SECRET must not be empty"); |
9 | 7 | } |
| 8 | + |
| 9 | + const trimmed = secretKey.trim(); |
| 10 | + if (trimmed.length < 16) { |
| 11 | + console.warn("Warning: JWT_SECRET is shorter than 16 characters. Consider using a longer secret for better security."); |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +function validateAdminCredentials() { |
| 16 | + const { username, password } = getAdminCredentials(); |
| 17 | + if (!username || username.trim().length === 0) { |
| 18 | + throw new Error("ADMIN_USER must not be empty"); |
| 19 | + } |
| 20 | + if (!password || password.trim().length === 0) { |
| 21 | + throw new Error("ADMIN_PASS must not be empty"); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +async function getKey(): Promise<CryptoKey> { |
| 26 | + validateAdminCredentials(); |
| 27 | + const secretKey = getJwtSecret(); |
| 28 | + validateSecret(secretKey); |
| 29 | + const secretBytes = new TextEncoder().encode(secretKey.trim()); |
10 | 30 | const key = await crypto.subtle.importKey( |
11 | 31 | "raw", |
12 | 32 | secretBytes, |
|
0 commit comments