|
1 | 1 | import dotenv from 'dotenv'
|
2 |
| -import jwt from 'jsonwebtoken' |
3 | 2 | import type { DBMigration } from '@internal/database/migrations'
|
| 3 | +import { SignJWT } from 'jose' |
4 | 4 |
|
5 | 5 | export type StorageBackendType = 'file' | 's3'
|
6 | 6 | export enum MultitenantMigrationStrategy {
|
@@ -93,8 +93,8 @@ type StorageConfigType = {
|
93 | 93 | requestTraceHeader?: string
|
94 | 94 | requestEtagHeaders: string[]
|
95 | 95 | responseSMaxAge: number
|
96 |
| - anonKey: string |
97 |
| - serviceKey: string |
| 96 | + anonKeyAsync: Promise<string> |
| 97 | + serviceKeyAsync: Promise<string> |
98 | 98 | storageBackendType: StorageBackendType
|
99 | 99 | tenantId: string
|
100 | 100 | requestUrlLengthLimit: number
|
@@ -259,10 +259,6 @@ export function getConfig(options?: { reload?: boolean }): StorageConfigType {
|
259 | 259 | 'REQUEST_ADMIN_TRACE_HEADER'
|
260 | 260 | ),
|
261 | 261 |
|
262 |
| - // Auth |
263 |
| - serviceKey: getOptionalConfigFromEnv('SERVICE_KEY') || '', |
264 |
| - anonKey: getOptionalConfigFromEnv('ANON_KEY') || '', |
265 |
| - |
266 | 262 | encryptionKey: getOptionalConfigFromEnv('AUTH_ENCRYPTION_KEY', 'ENCRYPTION_KEY') || '',
|
267 | 263 | jwtSecret: getOptionalIfMultitenantConfigFromEnv('AUTH_JWT_SECRET', 'PGRST_JWT_SECRET') || '',
|
268 | 264 | jwtAlgorithm: getOptionalConfigFromEnv('AUTH_JWT_ALGORITHM', 'PGRST_JWT_ALGORITHM') || 'HS256',
|
@@ -484,18 +480,26 @@ export function getConfig(options?: { reload?: boolean }): StorageConfigType {
|
484 | 480 | ),
|
485 | 481 | } as StorageConfigType
|
486 | 482 |
|
487 |
| - if (!config.isMultitenant && !config.serviceKey) { |
488 |
| - config.serviceKey = jwt.sign({ role: config.dbServiceRole }, config.jwtSecret, { |
489 |
| - expiresIn: '10y', |
490 |
| - algorithm: config.jwtAlgorithm as jwt.Algorithm, |
491 |
| - }) |
| 483 | + const serviceKey = getOptionalConfigFromEnv('SERVICE_KEY') || '' |
| 484 | + if (!config.isMultitenant && !serviceKey) { |
| 485 | + config.serviceKeyAsync = new SignJWT({ role: config.dbServiceRole }) |
| 486 | + .setIssuedAt() |
| 487 | + .setExpirationTime('10y') |
| 488 | + .setProtectedHeader({ alg: 'HS256' }) |
| 489 | + .sign(new TextEncoder().encode(config.jwtSecret)) |
| 490 | + } else { |
| 491 | + config.serviceKeyAsync = Promise.resolve(serviceKey) |
492 | 492 | }
|
493 | 493 |
|
494 |
| - if (!config.isMultitenant && !config.anonKey) { |
495 |
| - config.anonKey = jwt.sign({ role: config.dbAnonRole }, config.jwtSecret, { |
496 |
| - expiresIn: '10y', |
497 |
| - algorithm: config.jwtAlgorithm as jwt.Algorithm, |
498 |
| - }) |
| 494 | + const anonKey = getOptionalConfigFromEnv('ANON_KEY') || '' |
| 495 | + if (!config.isMultitenant && !anonKey) { |
| 496 | + config.anonKeyAsync = new SignJWT({ role: config.dbAnonRole }) |
| 497 | + .setIssuedAt() |
| 498 | + .setExpirationTime('10y') |
| 499 | + .setProtectedHeader({ alg: 'HS256' }) |
| 500 | + .sign(new TextEncoder().encode(config.jwtSecret)) |
| 501 | + } else { |
| 502 | + config.anonKeyAsync = Promise.resolve(anonKey) |
499 | 503 | }
|
500 | 504 |
|
501 | 505 | const jwtJWKS = getOptionalConfigFromEnv('JWT_JWKS') || null
|
|
0 commit comments