@onderwijsin/nuxt-turnstile 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -103,9 +103,10 @@ export default defineEventHandler(async (event) => {
|
|
|
103
103
|
});
|
|
104
104
|
```
|
|
105
105
|
|
|
106
|
-
The helper reads `x-turnstile-token` and verifies it
|
|
107
|
-
missing, failed, unavailable, or mismatched-action tokens. In development, an
|
|
108
|
-
local forms usable; in production, it produces
|
|
106
|
+
The helper reads `x-turnstile-token` and verifies it directly with Cloudflare's Turnstile siteverify
|
|
107
|
+
endpoint. It rejects missing, failed, unavailable, or mismatched-action tokens. In development, an
|
|
108
|
+
absent secret leaves local forms usable; in production, it produces
|
|
109
|
+
`TURNSTILE_SERVER_MISCONFIGURED`.
|
|
109
110
|
|
|
110
111
|
Cloudflare's official test credentials return `metadata.result_with_testing_key: true` without an
|
|
111
112
|
`action`. The helper accepts only that verified test-key response without action matching, so test
|
|
@@ -135,7 +136,7 @@ Stable error codes are `TURNSTILE_TOKEN_MISSING`, `TURNSTILE_VALIDATION_FAILED`,
|
|
|
135
136
|
`TURNSTILE_SERVER_MISCONFIGURED`. Apply authorization and business validation in the consuming route
|
|
136
137
|
after Turnstile succeeds; the module does not replace the route's existing submit or business logic.
|
|
137
138
|
|
|
138
|
-
`
|
|
139
|
+
`verifyTurnstileToken` is also auto-imported for server-only use when the raw Cloudflare
|
|
139
140
|
verification response is needed. Prefer `assertTurnstileToken` for protected application routes.
|
|
140
141
|
|
|
141
142
|
## Compatibility
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { defineNuxtModule, useLogger, createResolver, addTypeTemplate, addImportsDir, addServerScanDir } from '@nuxt/kit';
|
|
2
2
|
import { defu } from 'defu';
|
|
3
3
|
import { enabled, resolveModuleName, resolveLoggerScope, moduleSetup, validateModuleOptions, transpileRuntime, moduleDependenciesWhenEnabled } from '@onderwijsin/nuxt-module-utils/build';
|
|
4
|
+
import { isRecord, hasKey, isArray, isString } from '@onderwijsin/nuxt-module-utils/shared';
|
|
4
5
|
import { z } from 'zod';
|
|
5
6
|
|
|
6
|
-
const version = "0.3.
|
|
7
|
+
const version = "0.3.2";
|
|
7
8
|
|
|
8
9
|
const turnstileOptionsSchema = z.object({
|
|
9
10
|
enabled,
|
|
@@ -22,6 +23,9 @@ const DEFAULTS = {
|
|
|
22
23
|
adminToken: "",
|
|
23
24
|
adminHeaderName: "x-admin-token"
|
|
24
25
|
};
|
|
26
|
+
function isUpstreamTurnstileVerifierPreset(preset) {
|
|
27
|
+
return isRecord(preset) && isString(preset.from) && preset.from.includes("@nuxtjs/turnstile") && isArray(preset.imports) && preset.imports.includes("verifyTurnstileToken");
|
|
28
|
+
}
|
|
25
29
|
const module$1 = defineNuxtModule({
|
|
26
30
|
meta: {
|
|
27
31
|
name: MODULE_NAME,
|
|
@@ -59,6 +63,15 @@ const module$1 = defineNuxtModule({
|
|
|
59
63
|
);
|
|
60
64
|
nuxt.options.turnstile = defu(nuxt.options.turnstile, {});
|
|
61
65
|
nuxt.options.turnstile.siteKey ??= options.siteKey;
|
|
66
|
+
nuxt.hook("modules:done", () => {
|
|
67
|
+
nuxt.hook("nitro:config", (nitro) => {
|
|
68
|
+
const imports = nitro.imports;
|
|
69
|
+
if (!isRecord(imports) || !hasKey(imports, "presets")) return;
|
|
70
|
+
const presets = imports.presets;
|
|
71
|
+
if (!isArray(presets)) return;
|
|
72
|
+
imports.presets = presets.filter((preset) => !isUpstreamTurnstileVerifierPreset(preset));
|
|
73
|
+
});
|
|
74
|
+
});
|
|
62
75
|
transpileRuntime(nuxt, runtimeDir);
|
|
63
76
|
addImportsDir(resolver.resolve(runtimeDir, "app", "composables"));
|
|
64
77
|
addServerScanDir(resolver.resolve(runtimeDir, "server"));
|
|
@@ -37,7 +37,7 @@ export type TurnstileValidationResponse = z.infer<typeof turnstileValidationResp
|
|
|
37
37
|
* @param signal - Optional AbortSignal for request cancellation.
|
|
38
38
|
* @returns The parsed Turnstile validation response.
|
|
39
39
|
*/
|
|
40
|
-
export declare const
|
|
40
|
+
export declare const verifyTurnstileToken: (token: string, event?: H3Event, signal?: AbortSignal) => Promise<TurnstileValidationResponse>;
|
|
41
41
|
/**
|
|
42
42
|
* Validates the Turnstile token from a protected request.
|
|
43
43
|
* @param event - H3 request event.
|
|
@@ -28,7 +28,7 @@ export const turnstileValidationResponseSchema = z.object({
|
|
|
28
28
|
metadata: z.object({ result_with_testing_key: z.boolean().optional() }).optional()
|
|
29
29
|
});
|
|
30
30
|
const endpoint = "https://challenges.cloudflare.com/turnstile/v0/siteverify";
|
|
31
|
-
export const
|
|
31
|
+
export const verifyTurnstileToken = async (token, event, signal) => {
|
|
32
32
|
const secretKey = useRuntimeConfig(event).turnstile.secretKey;
|
|
33
33
|
const response = await $fetch(endpoint, {
|
|
34
34
|
method: "POST",
|
|
@@ -65,7 +65,7 @@ export async function assertTurnstileToken(event, expectedAction) {
|
|
|
65
65
|
"TURNSTILE_TOKEN_MISSING",
|
|
66
66
|
expectedAction
|
|
67
67
|
);
|
|
68
|
-
const result = await attempt(() =>
|
|
68
|
+
const result = await attempt(() => verifyTurnstileToken(token));
|
|
69
69
|
if (result.error !== null) {
|
|
70
70
|
if (isErrorWithStatusCode(result.error)) throw result.error;
|
|
71
71
|
throw createError({
|
package/package.json
CHANGED