Publish Docker image / build (linux/amd64, ubuntu-latest, amd64) (push) Canceled after 0s
Publish Docker image / build (linux/arm64, ubuntu-24.04-arm, arm64) (push) Canceled after 0s
Publish Docker image / merge (push) Canceled after 0s
ci / ci (22, ubuntu-latest) (push) Canceled after 0s
GitHub Release / release (push) Canceled after 0s
37 lines
890 B
TypeScript
37 lines
890 B
TypeScript
interface TurnstileVerifyResult {
|
|
success?: boolean
|
|
}
|
|
|
|
export async function verifyTurnstileToken(
|
|
secret: string,
|
|
token: string,
|
|
remoteIp?: string
|
|
): Promise<boolean> {
|
|
if (!secret || !token) return false
|
|
|
|
const params = new URLSearchParams({
|
|
secret,
|
|
response: token
|
|
})
|
|
if (remoteIp) {
|
|
params.set('remoteip', remoteIp)
|
|
}
|
|
|
|
const controller = new AbortController()
|
|
const timeout = setTimeout(() => controller.abort(), 5000)
|
|
try {
|
|
const response = await fetch('https://challenges.cloudflare.com/turnstile/v0/siteverify', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
body: params,
|
|
signal: controller.signal
|
|
})
|
|
const result = await response.json() as TurnstileVerifyResult
|
|
return result.success === true
|
|
} catch {
|
|
return false
|
|
} finally {
|
|
clearTimeout(timeout)
|
|
}
|
|
}
|