/** * Authentication utilities for postgres.do packages * * This module provides shared authentication utilities including: * - Bearer token extraction from HTTP headers * - User-scoped database ID generation * - OAuth token validation * - Type definitions for authenticated users * * @module @dotdo/postgres-shared/auth */ /** * Authenticated user information */ export interface AuthenticatedUser { /** Unique user identifier */ id: string; /** User's email address */ email: string; /** User's display name */ name?: string; /** Additional user metadata */ metadata?: Record; } /** * Token validation result */ export interface AuthTokenValidationResult { valid: boolean; user?: AuthenticatedUser; error?: string; expiresAt?: Date; } /** * Options for token validation */ export interface ValidateTokenOptions { /** OAuth provider URL (default: https://oauth.do) */ oauthUrl?: string; } /** * Extract bearer token from Authorization header * * Parses the Authorization header and extracts the token if it uses * the Bearer scheme. * * @param request - The HTTP request to extract the token from * @returns The bearer token string or null if not found * * @example * ```typescript * import { extractBearerToken } from '@dotdo/postgres-shared' * * const token = extractBearerToken(request) * if (token) { * // Validate the token * const result = await validateToken(token) * } * ``` */ export declare function extractBearerToken(request: Request): string | null; /** * Generate a database ID from user ID * * Creates a deterministic, URL-safe identifier for user-scoped databases. * The generated ID is prefixed with 'user_' and has non-alphanumeric * characters replaced with underscores. * * @param userId - The user's unique identifier * @returns A URL-safe database identifier * * @example * ```typescript * import { generateDatabaseId } from '@dotdo/postgres-shared' * * const dbId = generateDatabaseId('user-123') * // Returns: 'user_user_123' * * const dbId2 = generateDatabaseId('abc@def.com') * // Returns: 'user_abc_def_com' * ``` */ export declare function generateDatabaseId(userId: string): string; /** * Validate a token against oauth.do * * Makes an HTTP request to the OAuth provider to validate the token * and retrieve user information. * * @param token - The bearer token to validate * @param options - Validation options * @returns The validation result with user info if valid * * @example * ```typescript * import { validateToken } from '@dotdo/postgres-shared' * * const result = await validateToken(token) * if (result.valid && result.user) { * console.log(`Authenticated as: ${result.user.email}`) * } else { * console.error(`Validation failed: ${result.error}`) * } * ``` */ export declare function validateToken(token: string, options?: ValidateTokenOptions): Promise; /** * Default OAuth URL */ export declare const DEFAULT_OAUTH_URL = "https://oauth.do"; /** * Default token cache TTL in milliseconds (1 minute) */ export declare const DEFAULT_TOKEN_CACHE_TTL = 60000; //# sourceMappingURL=auth.d.ts.map