init commit for iPAL-kit plugin

This commit is contained in:
2026-07-18 20:30:23 +02:00
parent 10638127a9
commit 5733a9c8bf
119 changed files with 6892 additions and 411 deletions
+62
View File
@@ -0,0 +1,62 @@
import 'server-only'
import type { BasePayload } from 'payload'
import { getSiteIntegrations } from '../payload/index.js'
type SiteverifyResponse = {
challenge_ts?: string
'error-codes'?: string[]
hostname?: string
success: boolean
}
type IntegrationsWithTurnstile = {
turnstileSecretKey?: null | string
}
type VerifyTurnstileArgs = {
/** Optional client IP for stricter verification. */
ip?: string
/** Payload instance — used to read the secret from SiteIntegrations. */
payload: BasePayload
/** Token produced by the client-side widget. */
token: string
}
/**
* Verifies a Turnstile token with Cloudflare, server-side only.
*
* The secret comes from the SiteIntegrations global (editor-managed, per the
* plugin's "secrets in the panel" model), read via the Local API which bypasses
* access control. `server-only` guarantees this never reaches the browser
* bundle, keeping the secret off the client.
*
* Returns false on any failure (missing secret/token, network error, rejected
* challenge) — callers treat false as "do not trust this submission".
*/
export async function verifyTurnstile({
ip,
payload,
token,
}: VerifyTurnstileArgs): Promise<boolean> {
if (!token) {return false}
const integrations = await getSiteIntegrations<IntegrationsWithTurnstile>(payload)
const secret = integrations.turnstileSecretKey
if (!secret) {return false}
const body = new URLSearchParams({ response: token, secret })
if (ip) {body.append('remoteip', ip)}
try {
const res = await fetch('https://challenges.cloudflare.com/turnstile/v0/siteverify', {
body,
method: 'POST',
})
const data = (await res.json()) as SiteverifyResponse
return data.success
} catch {
return false
}
}