Files
supabase/apps/studio/data/api-keys/temp-api-keys-utils.ts
Ivan Vasilov 6437bd2a38 fix: Change the valid time for temp API keys to 30 seconds. (#43390)
This pull request makes a minor adjustment to the temporary API key
validation logic. The key is now considered invalid if it has less than
30 seconds remaining before expiry, instead of the previous 20 seconds.
This change helps avoid edge cases where a key might expire during use.
2026-03-05 09:56:38 +01:00

57 lines
1.6 KiB
TypeScript

import { getTemporaryAPIKey } from './temp-api-keys-query'
type ProjectRef = string
const projectApiKeys = new Map<ProjectRef, Promise<TemporaryApiKey>>()
export interface TemporaryApiKey {
apiKey: string
expiryTimeMs: number
}
export function createTemporaryApiKey(apiKey: string, expiryInSeconds: number): TemporaryApiKey {
return {
apiKey,
expiryTimeMs: Date.now() + expiryInSeconds * 1000,
}
}
export function isTemporaryApiKeyValid(
key: TemporaryApiKey | null | undefined
): key is TemporaryApiKey {
if (!key) return false
const now = Date.now()
const timeRemaining = key.expiryTimeMs - now
// Consider the key invalid if it has less than 30 seconds remaining to avoid edge cases where the key
// expires during use.
return timeRemaining > 30_000
}
const checkOrRefreshTemporaryApiKey = async (
projectRef: ProjectRef,
existingKey: Promise<TemporaryApiKey> | undefined
): Promise<TemporaryApiKey> => {
const resolvedKey = await existingKey
if (isTemporaryApiKeyValid(resolvedKey)) {
return resolvedKey
}
const expiryInSeconds = 60
const fetchedKey = getTemporaryAPIKey({
projectRef,
expiry: expiryInSeconds,
}).then((data) => createTemporaryApiKey(data.api_key, expiryInSeconds))
return fetchedKey
}
// This function should never be marked as async, it should always return a promise.
export function getOrRefreshTemporaryApiKey(projectRef: ProjectRef): Promise<TemporaryApiKey> {
const existingKey = projectApiKeys.get(projectRef)
const data = checkOrRefreshTemporaryApiKey(projectRef, existingKey)
projectApiKeys.set(projectRef, data)
return data
}