51 lines
2.3 KiB
TypeScript
51 lines
2.3 KiB
TypeScript
import 'server-only';
|
|
import type { BasePayload } from 'payload';
|
|
type VerifyTurnstileArgs = {
|
|
/** Token produced by the client-side widget. */
|
|
token: string;
|
|
/** Payload instance — used to read the secret from SiteIntegrations. */
|
|
payload: BasePayload;
|
|
/** Client IP. Sent to Cloudflare only with `sendRemoteIp: true`. */
|
|
ip?: string;
|
|
/**
|
|
* Send `remoteip` to Cloudflare. Default false: behind Cloudflare / Traefik /
|
|
* Docker the IP the app sees can differ from the one that solved the challenge
|
|
* (IPv6 on mobile vs IPv4 at the proxy), and Cloudflare's check doesn't need it.
|
|
*/
|
|
sendRemoteIp?: boolean;
|
|
/**
|
|
* What to do when verification can't run because of OUR side (missing/wrong
|
|
* secret, Cloudflare unreachable). 'allow' (default): accept the submission —
|
|
* rate limit, honeypot and validation still apply — and show the problem in
|
|
* the panel. 'reject': refuse (fail closed).
|
|
*/
|
|
onConfigError?: 'allow' | 'reject';
|
|
};
|
|
/**
|
|
* - `pass` — token valid.
|
|
* - `reject` — the VISITOR's token is bad (forged, reused, expired, malformed) → refuse.
|
|
* - `degraded` — OUR side is broken (no/wrong secret, Cloudflare unreachable or
|
|
* erroring) → the visitor can't fix it; by default the submission
|
|
* is accepted and the panel shows the problem.
|
|
*/
|
|
export type TurnstileOutcome = 'pass' | 'reject' | 'degraded';
|
|
export type TurnstileCheck = {
|
|
outcome: TurnstileOutcome;
|
|
code?: string;
|
|
};
|
|
/**
|
|
* Verifies a Turnstile token with Cloudflare and classifies the result (server-only).
|
|
*
|
|
* Secret: SiteIntegrations → Turnstile (panel) first, then TURNSTILE_SECRET_KEY.
|
|
* Every result is logged when not `pass`, and the integration status on
|
|
* SiteIntegrations is updated (throttled) so the panel shows failures.
|
|
*/
|
|
export declare function checkTurnstile({ token, payload, ip, sendRemoteIp, }: Omit<VerifyTurnstileArgs, 'onConfigError'>): Promise<TurnstileCheck>;
|
|
/**
|
|
* Boolean wrapper used by submitForm. `onConfigError` decides what a `degraded`
|
|
* result means: 'allow' (default) keeps the site's forms working while the
|
|
* panel shows the problem; 'reject' fails closed.
|
|
*/
|
|
export declare function verifyTurnstile({ onConfigError, ...args }: VerifyTurnstileArgs): Promise<boolean>;
|
|
export {};
|