25 lines
664 B
TypeScript
25 lines
664 B
TypeScript
export const PASCAL_HOST_USER_HEADER = 'x-pascal-user-id'
|
|
|
|
export interface PascalHostIdentity {
|
|
allowed: boolean
|
|
required: boolean
|
|
userId: string | null
|
|
}
|
|
|
|
export function getPascalHostIdentity(
|
|
headers: Headers,
|
|
env: NodeJS.ProcessEnv = process.env,
|
|
): PascalHostIdentity {
|
|
const required =
|
|
env.PASCAL_REQUIRE_HOST_USER === 'true' ||
|
|
(env.NODE_ENV === 'production' && env.PASCAL_REQUIRE_HOST_USER !== 'false')
|
|
const supplied = headers.get(PASCAL_HOST_USER_HEADER)?.trim() ?? ''
|
|
const userId = /^[a-zA-Z0-9_-]{1,128}$/.test(supplied) ? supplied : null
|
|
|
|
return {
|
|
allowed: Boolean(userId) || !required,
|
|
required,
|
|
userId,
|
|
}
|
|
}
|