@onderwijsin/nuxt-turnstile 0.2.9 → 0.3.0

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
@@ -135,6 +135,9 @@ Stable error codes are `TURNSTILE_TOKEN_MISSING`, `TURNSTILE_VALIDATION_FAILED`,
135
135
  `TURNSTILE_SERVER_MISCONFIGURED`. Apply authorization and business validation in the consuming route
136
136
  after Turnstile succeeds; the module does not replace the route's existing submit or business logic.
137
137
 
138
+ `verifyTokenWithTurnstile` is also auto-imported for server-only use when the raw Cloudflare
139
+ verification response is needed. Prefer `assertTurnstileToken` for protected application routes.
140
+
138
141
  ## Compatibility
139
142
 
140
143
  - Nuxt 4
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@onderwijsin/nuxt-turnstile",
3
3
  "configKey": "turnstile",
4
- "version": "0.2.9",
4
+ "version": "0.3.0",
5
5
  "compatibility": {
6
6
  "nuxt": "^4.0.0"
7
7
  },
package/dist/module.mjs CHANGED
@@ -3,7 +3,7 @@ import { defu } from 'defu';
3
3
  import { enabled, resolveModuleName, resolveLoggerScope, moduleSetup, validateModuleOptions, transpileRuntime, moduleDependenciesWhenEnabled } from '@onderwijsin/nuxt-module-utils/build';
4
4
  import { z } from 'zod';
5
5
 
6
- const version = "0.2.9";
6
+ const version = "0.3.0";
7
7
 
8
8
  const turnstileOptionsSchema = z.object({
9
9
  enabled,
@@ -1,5 +1,43 @@
1
1
  import type { H3Event } from "h3";
2
+ import { z } from "zod";
2
3
  import type { TurnstileErrorCode, TurnstileErrorData } from "../../types/errors.js";
4
+ export declare const turnstileValidationErrorCodeSchema: z.ZodEnum<{
5
+ "missing-input-secret": "missing-input-secret";
6
+ "invalid-input-secret": "invalid-input-secret";
7
+ "missing-input-response": "missing-input-response";
8
+ "invalid-input-response": "invalid-input-response";
9
+ "bad-request": "bad-request";
10
+ "timeout-or-duplicate": "timeout-or-duplicate";
11
+ "internal-error": "internal-error";
12
+ }>;
13
+ export declare const turnstileValidationResponseSchema: z.ZodObject<{
14
+ success: z.ZodBoolean;
15
+ hostname: z.ZodString;
16
+ "error-codes": z.ZodArray<z.ZodEnum<{
17
+ "missing-input-secret": "missing-input-secret";
18
+ "invalid-input-secret": "invalid-input-secret";
19
+ "missing-input-response": "missing-input-response";
20
+ "invalid-input-response": "invalid-input-response";
21
+ "bad-request": "bad-request";
22
+ "timeout-or-duplicate": "timeout-or-duplicate";
23
+ "internal-error": "internal-error";
24
+ }>>;
25
+ challenge_ts: z.ZodOptional<z.ZodString>;
26
+ action: z.ZodOptional<z.ZodString>;
27
+ cdata: z.ZodOptional<z.ZodString>;
28
+ metadata: z.ZodOptional<z.ZodObject<{
29
+ result_with_testing_key: z.ZodOptional<z.ZodBoolean>;
30
+ }, z.core.$strip>>;
31
+ }, z.core.$strip>;
32
+ export type TurnstileValidationResponse = z.infer<typeof turnstileValidationResponseSchema>;
33
+ /**
34
+ * Verifies a Turnstile token with the Cloudflare Turnstile API.
35
+ * @param token - The Turnstile token to verify.
36
+ * @param event - Optional H3 request event.
37
+ * @param signal - Optional AbortSignal for request cancellation.
38
+ * @returns The parsed Turnstile validation response.
39
+ */
40
+ export declare const verifyTokenWithTurnstile: (token: string, event?: H3Event, signal?: AbortSignal) => Promise<TurnstileValidationResponse>;
3
41
  /**
4
42
  * Validates the Turnstile token from a protected request.
5
43
  * @param event - H3 request event.
@@ -1,6 +1,5 @@
1
1
  import { createError, getRequestHeader } from "h3";
2
2
  import { useRuntimeConfig } from "#imports";
3
- import { verifyTurnstileToken } from "@nuxtjs/turnstile/runtime/server/utils/verify.js";
4
3
  import { isAdmin } from "@onderwijsin/nuxt-module-utils/server";
5
4
  import { attempt, hasKey, isNumber, isRecord } from "@onderwijsin/nuxt-module-utils/shared";
6
5
  import { z } from "zod";
@@ -10,6 +9,38 @@ const turnstileVerificationSchema = z.object({
10
9
  action: z.string().optional(),
11
10
  metadata: z.object({ result_with_testing_key: z.boolean().optional() }).optional()
12
11
  });
12
+ export const turnstileValidationErrorCodeSchema = z.enum([
13
+ "missing-input-secret",
14
+ "invalid-input-secret",
15
+ "missing-input-response",
16
+ "invalid-input-response",
17
+ "bad-request",
18
+ "timeout-or-duplicate",
19
+ "internal-error"
20
+ ]);
21
+ export const turnstileValidationResponseSchema = z.object({
22
+ success: z.boolean(),
23
+ hostname: z.string(),
24
+ "error-codes": z.array(turnstileValidationErrorCodeSchema),
25
+ challenge_ts: z.string().optional(),
26
+ action: z.string().optional(),
27
+ cdata: z.string().optional(),
28
+ metadata: z.object({ result_with_testing_key: z.boolean().optional() }).optional()
29
+ });
30
+ const endpoint = "https://challenges.cloudflare.com/turnstile/v0/siteverify";
31
+ export const verifyTokenWithTurnstile = async (token, event, signal) => {
32
+ const secretKey = useRuntimeConfig(event).turnstile.secretKey;
33
+ const response = await $fetch(endpoint, {
34
+ method: "POST",
35
+ body: `secret=${encodeURIComponent(secretKey)}&response=${encodeURIComponent(token)}`,
36
+ headers: {
37
+ "content-type": "application/x-www-form-urlencoded"
38
+ },
39
+ signal
40
+ });
41
+ const data = turnstileValidationResponseSchema.parse(response);
42
+ return data;
43
+ };
13
44
  export async function assertTurnstileToken(event, expectedAction) {
14
45
  const config = useRuntimeConfig(event);
15
46
  const turnstileConfig = config.turnstile;
@@ -34,7 +65,7 @@ export async function assertTurnstileToken(event, expectedAction) {
34
65
  "TURNSTILE_TOKEN_MISSING",
35
66
  expectedAction
36
67
  );
37
- const result = await attempt(() => verifyTurnstileToken(token));
68
+ const result = await attempt(() => verifyTokenWithTurnstile(token));
38
69
  if (result.error !== null) {
39
70
  if (isErrorWithStatusCode(result.error)) throw result.error;
40
71
  throw createError({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onderwijsin/nuxt-turnstile",
3
- "version": "0.2.9",
3
+ "version": "0.3.0",
4
4
  "description": "Nuxt Turnstile integration with client helpers and server-side validation.",
5
5
  "homepage": "https://github.com/onderwijsin/nuxt-modules#readme",
6
6
  "bugs": {