video-optimizer-bot / worker /src /security.ts
Hugging Face Deployer
Deploy bot to Hugging Face Spaces
03b405b
Raw History Blame Contribute Delete
796 Bytes
export interface Env {
WORKER_FORWARD_URL: string;
WORKER_SHARED_SECRET: string;
TELEGRAM_WEBHOOK_SECRET: string;
}
export async function timingSafeEqualText(left: string, right: string): Promise<boolean> {
const encoder = new TextEncoder();
const a = encoder.encode(left || "");
const b = encoder.encode(right || "");
if (a.length !== b.length) return false;
let diff = 0;
for (let index = 0; index < a.length; index += 1) {
diff |= a[index] ^ b[index];
}
return diff === 0;
}
export async function validateTelegramSecret(request: Request, env: Env): Promise<boolean> {
if (!env.TELEGRAM_WEBHOOK_SECRET) return true;
const header = request.headers.get("x-telegram-bot-api-secret-token") || "";
return timingSafeEqualText(header, env.TELEGRAM_WEBHOOK_SECRET);
}