@lunora/auth 1.0.0-alpha.21 → 1.0.0-alpha.211
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/LICENSE.md +32 -0
- package/README.md +99 -10
- package/dist/adapter.d.mts +4 -43
- package/dist/adapter.d.ts +4 -43
- package/dist/adapter.mjs +1 -48
- package/dist/audit.d.mts +142 -0
- package/dist/audit.d.ts +142 -0
- package/dist/audit.mjs +12 -0
- package/dist/cimd-node.d.mts +1 -0
- package/dist/cimd-node.d.ts +1 -0
- package/dist/cimd-node.mjs +1 -0
- package/dist/cimd-workers.d.mts +32 -0
- package/dist/cimd-workers.d.ts +32 -0
- package/dist/cimd-workers.mjs +1 -0
- package/dist/email-guard.d.mts +127 -0
- package/dist/email-guard.d.ts +127 -0
- package/dist/email-guard.mjs +1 -0
- package/dist/index.d.mts +769 -173
- package/dist/index.d.ts +769 -173
- package/dist/index.mjs +1 -12
- package/dist/middleware.d.mts +165 -155
- package/dist/middleware.d.ts +165 -155
- package/dist/middleware.mjs +1 -56
- package/dist/packem_shared/AUTH_DO_AUDIT_PATH-1kIGHxKo.mjs +1 -0
- package/dist/packem_shared/DEFAULT_AUTH_BASE_PATH-DneiLGpv.mjs +1 -0
- package/dist/packem_shared/LunoraAuthAdminError-uhKG1MgQ.mjs +1 -0
- package/dist/packem_shared/adapter-CBZ17jKi.mjs +2 -0
- package/dist/packem_shared/adapter.d-CElAPJ1U.d.ts +88 -0
- package/dist/packem_shared/adapter.d-CaPwF2v3.d.mts +88 -0
- package/dist/packem_shared/auth-do-DEd6xEKe.mjs +1 -0
- package/dist/packem_shared/authAuditHook-DPTKdjBe.mjs +1 -0
- package/dist/packem_shared/authDiscoveryPaths-B14lM14u.mjs +1 -0
- package/dist/packem_shared/authDoColumnAdditions-UwF3Rl41.mjs +1 -0
- package/dist/packem_shared/compileMigrationsSql-BiYsLz91.mjs +1 -0
- package/dist/packem_shared/create-auth.d-D1CKOFWc.d.mts +142 -0
- package/dist/packem_shared/create-auth.d-D1CKOFWc.d.ts +142 -0
- package/dist/packem_shared/createAuth-BdVltKyf.mjs +1 -0
- package/dist/packem_shared/createDoAuthWiring-B-c6qEcK.mjs +1 -0
- package/dist/packem_shared/createLunoraAuthClient-DRcFNwp9.mjs +1 -0
- package/dist/packem_shared/createSignUpInvitation-DHZL9fNl.mjs +1 -0
- package/dist/packem_shared/emailGateDatabaseHooks-CAygR3iq.mjs +1 -0
- package/dist/packem_shared/invite-only.d-CsPJWAwM.d.ts +126 -0
- package/dist/packem_shared/invite-only.d-DKyHHkkQ.d.mts +126 -0
- package/dist/packem_shared/legacyIssuerCleanupStatements-D9Kf8OD1.mjs +1 -0
- package/dist/packem_shared/mcp-CgX1hTOv.mjs +1 -0
- package/dist/packem_shared/on-cloudflare-edge-Dx6gFjBO.mjs +1 -0
- package/dist/packem_shared/quote-identifier-CGiYFBvY.mjs +1 -0
- package/dist/packem_shared/sessionPresets-C867Mlo4.mjs +1 -0
- package/dist/packem_shared/sql-store-uMubPYI9.mjs +1 -0
- package/dist/packem_shared/uiConfig-B8pN6pks.mjs +1 -0
- package/dist/plugins-client.d.mts +250 -1
- package/dist/plugins-client.d.ts +250 -1
- package/dist/plugins-client.mjs +1 -2
- package/dist/plugins-enterprise-client.d.mts +13 -0
- package/dist/plugins-enterprise-client.d.ts +13 -0
- package/dist/plugins-enterprise-client.mjs +1 -0
- package/dist/plugins-enterprise.d.mts +1668 -0
- package/dist/plugins-enterprise.d.ts +1668 -0
- package/dist/plugins-enterprise.mjs +1 -0
- package/dist/plugins.d.mts +167 -2
- package/dist/plugins.d.ts +167 -2
- package/dist/plugins.mjs +1 -22
- package/dist/schema.d.mts +39 -39
- package/dist/schema.d.ts +39 -39
- package/dist/schema.mjs +1 -62
- package/dist/sql-store.d.mts +43 -28
- package/dist/sql-store.d.ts +43 -28
- package/dist/sql-store.mjs +1 -184
- package/dist/store.d.mts +65 -47
- package/dist/store.d.ts +65 -47
- package/dist/store.mjs +1 -183
- package/dist/turnstile-middleware.d.mts +73 -55
- package/dist/turnstile-middleware.d.ts +73 -55
- package/dist/turnstile-middleware.mjs +1 -45
- package/dist/turnstile.d.mts +42 -59
- package/dist/turnstile.d.ts +42 -59
- package/dist/turnstile.mjs +1 -61
- package/package.json +48 -6
- package/dist/packem_shared/DEFAULT_AUTH_BASE_PATH-DjcUWEQl.mjs +0 -11
- package/dist/packem_shared/LunoraAuthAdminError-D4L7n6gN.mjs +0 -510
- package/dist/packem_shared/compileMigrationsSql-B9bj-mJv.mjs +0 -29
- package/dist/packem_shared/create-auth.d-Mwhb4gSc.d.mts +0 -128
- package/dist/packem_shared/create-auth.d-Mwhb4gSc.d.ts +0 -128
- package/dist/packem_shared/createAuth-BVMMllTm.mjs +0 -87
- package/dist/packem_shared/sessionPresets-Dwwd74_J.mjs +0 -38
|
@@ -1,45 +1 @@
|
|
|
1
|
-
import { verifyTurnstile }
|
|
2
|
-
|
|
3
|
-
const verifyTurnstileMiddleware = (options) => async ({ ctx, next }) => {
|
|
4
|
-
const token = options.token(ctx);
|
|
5
|
-
if (token === void 0 || token === "") {
|
|
6
|
-
throw Object.assign(new Error(options.message ?? "turnstile token missing"), {
|
|
7
|
-
code: "FORBIDDEN",
|
|
8
|
-
name: "LunoraError",
|
|
9
|
-
status: 403
|
|
10
|
-
});
|
|
11
|
-
}
|
|
12
|
-
let result;
|
|
13
|
-
try {
|
|
14
|
-
result = await verifyTurnstile({
|
|
15
|
-
expectedAction: options.expectedAction,
|
|
16
|
-
expectedHostname: options.expectedHostname,
|
|
17
|
-
fetch: options.fetch,
|
|
18
|
-
remoteip: options.remoteip?.(ctx),
|
|
19
|
-
secret: options.secret,
|
|
20
|
-
token
|
|
21
|
-
});
|
|
22
|
-
} catch (error) {
|
|
23
|
-
console.error(`@lunora/auth: verifyTurnstileMiddleware siteverify threw; ${options.failOpen ? "failing open" : "failing closed"}`, error);
|
|
24
|
-
if (options.failOpen) {
|
|
25
|
-
return next();
|
|
26
|
-
}
|
|
27
|
-
throw Object.assign(new Error(options.message ?? "turnstile verification unavailable"), {
|
|
28
|
-
cause: error,
|
|
29
|
-
code: "FORBIDDEN",
|
|
30
|
-
name: "LunoraError",
|
|
31
|
-
status: 403
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
if (!result.success || options.validate !== void 0 && !options.validate(result)) {
|
|
35
|
-
throw Object.assign(new Error(options.message ?? "turnstile verification failed"), {
|
|
36
|
-
code: "FORBIDDEN",
|
|
37
|
-
errorCodes: result.errorCodes,
|
|
38
|
-
name: "LunoraError",
|
|
39
|
-
status: 403
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
return next();
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
export { verifyTurnstileMiddleware };
|
|
1
|
+
import{LunoraError as i}from"@lunora/errors";import{tagPerDispatchMiddleware as d}from"@lunora/server";import{verifyTurnstile as n}from"./turnstile.mjs";const u=e=>d(async({ctx:a,next:l})=>{const t=e.token(a);if(t===void 0||t==="")throw new i("FORBIDDEN",e.message??"turnstile token missing");let r;try{r=await n({expectedAction:e.expectedAction,expectedHostname:e.expectedHostname,fetch:e.fetch,remoteip:e.remoteip?.(a),secret:e.secret,token:t})}catch(c){if(console.error(`@lunora/auth: verifyTurnstileMiddleware siteverify threw; ${e.failOpen?"failing open":"failing closed"}`,c),e.failOpen)return l();throw new i("FORBIDDEN",e.message??"turnstile verification unavailable",{cause:c})}if(!r.success||e.validate!==void 0&&e.validate(r)!==!0)throw new i("FORBIDDEN",e.message??"turnstile verification failed",{data:r.errorCodes.length>0?{errorCodes:r.errorCodes}:void 0});return l()});export{u as verifyTurnstileMiddleware};
|
package/dist/turnstile.d.mts
CHANGED
|
@@ -1,13 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Cloudflare Turnstile server-side verification.
|
|
3
|
-
*
|
|
4
|
-
* Turnstile has **no Cloudflare binding** — verification is a single HTTPS POST
|
|
5
|
-
* to the public `siteverify` endpoint with your secret key. The secret lives in
|
|
6
|
-
* a plain env var / `.dev.vars` (conventionally `TURNSTILE_SECRET_KEY`), not in
|
|
7
|
-
* `wrangler.jsonc`. This module is therefore pure, transport-agnostic, and
|
|
8
|
-
* usable from any mutation/action — not just the auth flow.
|
|
9
|
-
* @see https://developers.cloudflare.com/turnstile/get-started/server-side-validation/
|
|
10
|
-
*/
|
|
11
1
|
/** Cloudflare's public Turnstile `siteverify` endpoint. */
|
|
12
2
|
declare const TURNSTILE_VERIFY_ENDPOINT = "https://challenges.cloudflare.com/turnstile/v0/siteverify";
|
|
13
3
|
/** Minimal `fetch` shape we depend on, so callers can inject a stub in tests. */
|
|
@@ -18,32 +8,32 @@ type FetchLike = (input: string, init?: {
|
|
|
18
8
|
}) => Promise<Response>;
|
|
19
9
|
interface VerifyTurnstileOptions {
|
|
20
10
|
/**
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
11
|
+
* Assert the widget `action` the token was solved for. Cloudflare echoes the
|
|
12
|
+
* customer-supplied `action` back in the siteverify response; when this is
|
|
13
|
+
* set and the returned `action` does not match, the verdict is downgraded to
|
|
14
|
+
* `success: false` (with error code `action-mismatch`). Leave unset to skip
|
|
15
|
+
* the check.
|
|
16
|
+
*/
|
|
27
17
|
expectedAction?: string;
|
|
28
18
|
/**
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
19
|
+
* Assert the `hostname` the challenge was solved on. Cloudflare returns the
|
|
20
|
+
* solving hostname in the siteverify response; when this is set and the
|
|
21
|
+
* returned `hostname` does not match, the verdict is downgraded to
|
|
22
|
+
* `success: false` (with error code `hostname-mismatch`). Set this when a
|
|
23
|
+
* single secret/sitekey is shared across multiple domains to stop a token
|
|
24
|
+
* harvested on one origin from being replayed against another. Leave unset
|
|
25
|
+
* to skip the check.
|
|
26
|
+
*/
|
|
37
27
|
expectedHostname?: string;
|
|
38
28
|
/**
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
29
|
+
* Inject a `fetch` implementation. Defaults to `globalThis.fetch`. Primarily
|
|
30
|
+
* for unit tests — production callers can omit it.
|
|
31
|
+
*/
|
|
42
32
|
fetch?: FetchLike;
|
|
43
33
|
/**
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
34
|
+
* The visitor's IP address, if known. Optional; Cloudflare uses it as an
|
|
35
|
+
* extra signal but verification works without it.
|
|
36
|
+
*/
|
|
47
37
|
remoteip?: string;
|
|
48
38
|
/** Your Turnstile secret key (the `TURNSTILE_SECRET_KEY` env var). */
|
|
49
39
|
secret: string;
|
|
@@ -51,13 +41,13 @@ interface VerifyTurnstileOptions {
|
|
|
51
41
|
token: string;
|
|
52
42
|
}
|
|
53
43
|
/**
|
|
54
|
-
* The normalized result of a Turnstile verification. Snake-cased fields from
|
|
55
|
-
* Cloudflare (`error-codes`, `challenge_ts`) are mapped to camelCase.
|
|
56
|
-
*
|
|
57
|
-
* A `success: false` verdict is a **bot/invalid-token** outcome, not an error —
|
|
58
|
-
* it is returned, never thrown. `verifyTurnstile` only throws on transport
|
|
59
|
-
* failure (network error, non-2xx response).
|
|
60
|
-
*/
|
|
44
|
+
* The normalized result of a Turnstile verification. Snake-cased fields from
|
|
45
|
+
* Cloudflare (`error-codes`, `challenge_ts`) are mapped to camelCase.
|
|
46
|
+
*
|
|
47
|
+
* A `success: false` verdict is a **bot/invalid-token** outcome, not an error —
|
|
48
|
+
* it is returned, never thrown. `verifyTurnstile` only throws on transport
|
|
49
|
+
* failure (network error, non-2xx response).
|
|
50
|
+
*/
|
|
61
51
|
interface TurnstileVerifyResult {
|
|
62
52
|
/** The customer-supplied `action` the widget was rendered with, if any. */
|
|
63
53
|
action?: string;
|
|
@@ -66,9 +56,9 @@ interface TurnstileVerifyResult {
|
|
|
66
56
|
/** ISO timestamp of the challenge, if Cloudflare returned one. */
|
|
67
57
|
challengeTs?: string;
|
|
68
58
|
/**
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
59
|
+
* Cloudflare error codes for a failed verification (e.g.
|
|
60
|
+
* `"invalid-input-response"`, `"timeout-or-duplicate"`). Empty on success.
|
|
61
|
+
*/
|
|
72
62
|
errorCodes: string[];
|
|
73
63
|
/** Hostname the challenge was solved on, if Cloudflare returned one. */
|
|
74
64
|
hostname?: string;
|
|
@@ -76,23 +66,16 @@ interface TurnstileVerifyResult {
|
|
|
76
66
|
success: boolean;
|
|
77
67
|
}
|
|
78
68
|
/**
|
|
79
|
-
* Verify a Turnstile token against Cloudflare's `siteverify` endpoint.
|
|
80
|
-
*
|
|
81
|
-
* POSTs `application/x-www-form-urlencoded` (`secret`, `response`=token, and an
|
|
82
|
-
* optional `remoteip`) and returns the parsed verdict. A `success: false`
|
|
83
|
-
* outcome (bot / invalid / expired token) is **returned**, not thrown — callers
|
|
84
|
-
* decide how to react. The function throws a structural `LunoraError`-shaped
|
|
85
|
-
* error (`{ name: "LunoraError", code: "SERVICE_UNAVAILABLE", status: 503 }`)
|
|
86
|
-
* only when the siteverify call itself fails (network error or non-2xx), so a
|
|
87
|
-
* "siteverify is down" failure is distinguishable from a "this is a bot"
|
|
88
|
-
* verdict.
|
|
89
|
-
*/
|
|
90
|
-
declare const verifyTurnstile: ({
|
|
91
|
-
expectedAction,
|
|
92
|
-
expectedHostname,
|
|
93
|
-
fetch,
|
|
94
|
-
remoteip,
|
|
95
|
-
secret,
|
|
96
|
-
token
|
|
97
|
-
}: VerifyTurnstileOptions) => Promise<TurnstileVerifyResult>;
|
|
69
|
+
* Verify a Turnstile token against Cloudflare's `siteverify` endpoint.
|
|
70
|
+
*
|
|
71
|
+
* POSTs `application/x-www-form-urlencoded` (`secret`, `response`=token, and an
|
|
72
|
+
* optional `remoteip`) and returns the parsed verdict. A `success: false`
|
|
73
|
+
* outcome (bot / invalid / expired token) is **returned**, not thrown — callers
|
|
74
|
+
* decide how to react. The function throws a structural `LunoraError`-shaped
|
|
75
|
+
* error (`{ name: "LunoraError", code: "SERVICE_UNAVAILABLE", status: 503 }`)
|
|
76
|
+
* only when the siteverify call itself fails (network error or non-2xx), so a
|
|
77
|
+
* "siteverify is down" failure is distinguishable from a "this is a bot"
|
|
78
|
+
* verdict.
|
|
79
|
+
*/
|
|
80
|
+
declare const verifyTurnstile: ({ expectedAction, expectedHostname, fetch, remoteip, secret, token }: VerifyTurnstileOptions) => Promise<TurnstileVerifyResult>;
|
|
98
81
|
export { type FetchLike, TURNSTILE_VERIFY_ENDPOINT, type TurnstileVerifyResult, type VerifyTurnstileOptions, verifyTurnstile };
|
package/dist/turnstile.d.ts
CHANGED
|
@@ -1,13 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Cloudflare Turnstile server-side verification.
|
|
3
|
-
*
|
|
4
|
-
* Turnstile has **no Cloudflare binding** — verification is a single HTTPS POST
|
|
5
|
-
* to the public `siteverify` endpoint with your secret key. The secret lives in
|
|
6
|
-
* a plain env var / `.dev.vars` (conventionally `TURNSTILE_SECRET_KEY`), not in
|
|
7
|
-
* `wrangler.jsonc`. This module is therefore pure, transport-agnostic, and
|
|
8
|
-
* usable from any mutation/action — not just the auth flow.
|
|
9
|
-
* @see https://developers.cloudflare.com/turnstile/get-started/server-side-validation/
|
|
10
|
-
*/
|
|
11
1
|
/** Cloudflare's public Turnstile `siteverify` endpoint. */
|
|
12
2
|
declare const TURNSTILE_VERIFY_ENDPOINT = "https://challenges.cloudflare.com/turnstile/v0/siteverify";
|
|
13
3
|
/** Minimal `fetch` shape we depend on, so callers can inject a stub in tests. */
|
|
@@ -18,32 +8,32 @@ type FetchLike = (input: string, init?: {
|
|
|
18
8
|
}) => Promise<Response>;
|
|
19
9
|
interface VerifyTurnstileOptions {
|
|
20
10
|
/**
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
11
|
+
* Assert the widget `action` the token was solved for. Cloudflare echoes the
|
|
12
|
+
* customer-supplied `action` back in the siteverify response; when this is
|
|
13
|
+
* set and the returned `action` does not match, the verdict is downgraded to
|
|
14
|
+
* `success: false` (with error code `action-mismatch`). Leave unset to skip
|
|
15
|
+
* the check.
|
|
16
|
+
*/
|
|
27
17
|
expectedAction?: string;
|
|
28
18
|
/**
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
19
|
+
* Assert the `hostname` the challenge was solved on. Cloudflare returns the
|
|
20
|
+
* solving hostname in the siteverify response; when this is set and the
|
|
21
|
+
* returned `hostname` does not match, the verdict is downgraded to
|
|
22
|
+
* `success: false` (with error code `hostname-mismatch`). Set this when a
|
|
23
|
+
* single secret/sitekey is shared across multiple domains to stop a token
|
|
24
|
+
* harvested on one origin from being replayed against another. Leave unset
|
|
25
|
+
* to skip the check.
|
|
26
|
+
*/
|
|
37
27
|
expectedHostname?: string;
|
|
38
28
|
/**
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
29
|
+
* Inject a `fetch` implementation. Defaults to `globalThis.fetch`. Primarily
|
|
30
|
+
* for unit tests — production callers can omit it.
|
|
31
|
+
*/
|
|
42
32
|
fetch?: FetchLike;
|
|
43
33
|
/**
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
34
|
+
* The visitor's IP address, if known. Optional; Cloudflare uses it as an
|
|
35
|
+
* extra signal but verification works without it.
|
|
36
|
+
*/
|
|
47
37
|
remoteip?: string;
|
|
48
38
|
/** Your Turnstile secret key (the `TURNSTILE_SECRET_KEY` env var). */
|
|
49
39
|
secret: string;
|
|
@@ -51,13 +41,13 @@ interface VerifyTurnstileOptions {
|
|
|
51
41
|
token: string;
|
|
52
42
|
}
|
|
53
43
|
/**
|
|
54
|
-
* The normalized result of a Turnstile verification. Snake-cased fields from
|
|
55
|
-
* Cloudflare (`error-codes`, `challenge_ts`) are mapped to camelCase.
|
|
56
|
-
*
|
|
57
|
-
* A `success: false` verdict is a **bot/invalid-token** outcome, not an error —
|
|
58
|
-
* it is returned, never thrown. `verifyTurnstile` only throws on transport
|
|
59
|
-
* failure (network error, non-2xx response).
|
|
60
|
-
*/
|
|
44
|
+
* The normalized result of a Turnstile verification. Snake-cased fields from
|
|
45
|
+
* Cloudflare (`error-codes`, `challenge_ts`) are mapped to camelCase.
|
|
46
|
+
*
|
|
47
|
+
* A `success: false` verdict is a **bot/invalid-token** outcome, not an error —
|
|
48
|
+
* it is returned, never thrown. `verifyTurnstile` only throws on transport
|
|
49
|
+
* failure (network error, non-2xx response).
|
|
50
|
+
*/
|
|
61
51
|
interface TurnstileVerifyResult {
|
|
62
52
|
/** The customer-supplied `action` the widget was rendered with, if any. */
|
|
63
53
|
action?: string;
|
|
@@ -66,9 +56,9 @@ interface TurnstileVerifyResult {
|
|
|
66
56
|
/** ISO timestamp of the challenge, if Cloudflare returned one. */
|
|
67
57
|
challengeTs?: string;
|
|
68
58
|
/**
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
59
|
+
* Cloudflare error codes for a failed verification (e.g.
|
|
60
|
+
* `"invalid-input-response"`, `"timeout-or-duplicate"`). Empty on success.
|
|
61
|
+
*/
|
|
72
62
|
errorCodes: string[];
|
|
73
63
|
/** Hostname the challenge was solved on, if Cloudflare returned one. */
|
|
74
64
|
hostname?: string;
|
|
@@ -76,23 +66,16 @@ interface TurnstileVerifyResult {
|
|
|
76
66
|
success: boolean;
|
|
77
67
|
}
|
|
78
68
|
/**
|
|
79
|
-
* Verify a Turnstile token against Cloudflare's `siteverify` endpoint.
|
|
80
|
-
*
|
|
81
|
-
* POSTs `application/x-www-form-urlencoded` (`secret`, `response`=token, and an
|
|
82
|
-
* optional `remoteip`) and returns the parsed verdict. A `success: false`
|
|
83
|
-
* outcome (bot / invalid / expired token) is **returned**, not thrown — callers
|
|
84
|
-
* decide how to react. The function throws a structural `LunoraError`-shaped
|
|
85
|
-
* error (`{ name: "LunoraError", code: "SERVICE_UNAVAILABLE", status: 503 }`)
|
|
86
|
-
* only when the siteverify call itself fails (network error or non-2xx), so a
|
|
87
|
-
* "siteverify is down" failure is distinguishable from a "this is a bot"
|
|
88
|
-
* verdict.
|
|
89
|
-
*/
|
|
90
|
-
declare const verifyTurnstile: ({
|
|
91
|
-
expectedAction,
|
|
92
|
-
expectedHostname,
|
|
93
|
-
fetch,
|
|
94
|
-
remoteip,
|
|
95
|
-
secret,
|
|
96
|
-
token
|
|
97
|
-
}: VerifyTurnstileOptions) => Promise<TurnstileVerifyResult>;
|
|
69
|
+
* Verify a Turnstile token against Cloudflare's `siteverify` endpoint.
|
|
70
|
+
*
|
|
71
|
+
* POSTs `application/x-www-form-urlencoded` (`secret`, `response`=token, and an
|
|
72
|
+
* optional `remoteip`) and returns the parsed verdict. A `success: false`
|
|
73
|
+
* outcome (bot / invalid / expired token) is **returned**, not thrown — callers
|
|
74
|
+
* decide how to react. The function throws a structural `LunoraError`-shaped
|
|
75
|
+
* error (`{ name: "LunoraError", code: "SERVICE_UNAVAILABLE", status: 503 }`)
|
|
76
|
+
* only when the siteverify call itself fails (network error or non-2xx), so a
|
|
77
|
+
* "siteverify is down" failure is distinguishable from a "this is a bot"
|
|
78
|
+
* verdict.
|
|
79
|
+
*/
|
|
80
|
+
declare const verifyTurnstile: ({ expectedAction, expectedHostname, fetch, remoteip, secret, token }: VerifyTurnstileOptions) => Promise<TurnstileVerifyResult>;
|
|
98
81
|
export { type FetchLike, TURNSTILE_VERIFY_ENDPOINT, type TurnstileVerifyResult, type VerifyTurnstileOptions, verifyTurnstile };
|
package/dist/turnstile.mjs
CHANGED
|
@@ -1,61 +1 @@
|
|
|
1
|
-
const
|
|
2
|
-
const asString = (value) => typeof value === "string" ? value : void 0;
|
|
3
|
-
const asStringArray = (value) => Array.isArray(value) ? value.filter((entry) => typeof entry === "string") : [];
|
|
4
|
-
const verifyTurnstile = async ({
|
|
5
|
-
expectedAction,
|
|
6
|
-
expectedHostname,
|
|
7
|
-
fetch = globalThis.fetch,
|
|
8
|
-
remoteip,
|
|
9
|
-
secret,
|
|
10
|
-
token
|
|
11
|
-
}) => {
|
|
12
|
-
const body = new URLSearchParams({ response: token, secret });
|
|
13
|
-
if (remoteip !== void 0 && remoteip !== "") {
|
|
14
|
-
body.set("remoteip", remoteip);
|
|
15
|
-
}
|
|
16
|
-
let response;
|
|
17
|
-
try {
|
|
18
|
-
response = await fetch(TURNSTILE_VERIFY_ENDPOINT, {
|
|
19
|
-
body: body.toString(),
|
|
20
|
-
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
21
|
-
method: "POST"
|
|
22
|
-
});
|
|
23
|
-
} catch (error) {
|
|
24
|
-
throw Object.assign(new Error("turnstile siteverify request failed"), {
|
|
25
|
-
cause: error,
|
|
26
|
-
code: "SERVICE_UNAVAILABLE",
|
|
27
|
-
name: "LunoraError",
|
|
28
|
-
status: 503
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
if (!response.ok) {
|
|
32
|
-
throw Object.assign(new Error(`turnstile siteverify returned ${String(response.status)}`), {
|
|
33
|
-
code: "SERVICE_UNAVAILABLE",
|
|
34
|
-
name: "LunoraError",
|
|
35
|
-
status: 503
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
const raw = await response.json();
|
|
39
|
-
const action = asString(raw.action);
|
|
40
|
-
const hostname = asString(raw.hostname);
|
|
41
|
-
const errorCodes = asStringArray(raw["error-codes"]);
|
|
42
|
-
let success = raw.success === true;
|
|
43
|
-
if (success && expectedHostname !== void 0 && hostname !== expectedHostname) {
|
|
44
|
-
success = false;
|
|
45
|
-
errorCodes.push("hostname-mismatch");
|
|
46
|
-
}
|
|
47
|
-
if (success && expectedAction !== void 0 && action !== expectedAction) {
|
|
48
|
-
success = false;
|
|
49
|
-
errorCodes.push("action-mismatch");
|
|
50
|
-
}
|
|
51
|
-
return {
|
|
52
|
-
action,
|
|
53
|
-
cdata: asString(raw.cdata),
|
|
54
|
-
challengeTs: asString(raw.challenge_ts),
|
|
55
|
-
errorCodes,
|
|
56
|
-
hostname,
|
|
57
|
-
success
|
|
58
|
-
};
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
export { TURNSTILE_VERIFY_ENDPOINT, verifyTurnstile };
|
|
1
|
+
import{LunoraError as l}from"@lunora/errors";const E="https://challenges.cloudflare.com/turnstile/v0/siteverify",a=t=>typeof t=="string"?t:void 0,m=t=>Array.isArray(t)?t.filter(e=>typeof e=="string"):[],S=async({expectedAction:t,expectedHostname:e,fetch:d=globalThis.fetch,remoteip:n,secret:y,token:w})=>{const h=new URLSearchParams({response:w,secret:y});n!==void 0&&n!==""&&h.set("remoteip",n);let o;try{o=await d(E,{body:h.toString(),headers:{"content-type":"application/x-www-form-urlencoded"},method:"POST"})}catch(c){throw new l("SERVICE_UNAVAILABLE","turnstile siteverify request failed",{cause:c,status:503})}if(!o.ok)throw new l("SERVICE_UNAVAILABLE",`turnstile siteverify returned ${String(o.status)}`,{status:503});let r;try{r=await o.json()}catch(c){throw new l("SERVICE_UNAVAILABLE","turnstile siteverify returned a non-JSON body",{cause:c,status:503})}const u=a(r.action),f=a(r.hostname),i=m(r["error-codes"]);let s=r.success===!0;return s&&e!==void 0&&f!==e&&(s=!1,i.push("hostname-mismatch")),s&&t!==void 0&&u!==t&&(s=!1,i.push("action-mismatch")),{action:u,cdata:a(r.cdata),challengeTs:a(r.challenge_ts),errorCodes:i,hostname:f,success:s}};export{E as TURNSTILE_VERIFY_ENDPOINT,S as verifyTurnstile};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lunora/auth",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.211",
|
|
4
4
|
"description": "Auth for Lunora — a thin better-auth wrapper: email/password, OAuth, plugins, D1-backed",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"auth",
|
|
@@ -48,10 +48,34 @@
|
|
|
48
48
|
"types": "./dist/plugins-client.d.ts",
|
|
49
49
|
"import": "./dist/plugins-client.mjs"
|
|
50
50
|
},
|
|
51
|
+
"./plugins/enterprise": {
|
|
52
|
+
"types": "./dist/plugins-enterprise.d.ts",
|
|
53
|
+
"import": "./dist/plugins-enterprise.mjs"
|
|
54
|
+
},
|
|
55
|
+
"./plugins/enterprise/client": {
|
|
56
|
+
"types": "./dist/plugins-enterprise-client.d.ts",
|
|
57
|
+
"import": "./dist/plugins-enterprise-client.mjs"
|
|
58
|
+
},
|
|
59
|
+
"./cimd/node": {
|
|
60
|
+
"types": "./dist/cimd-node.d.ts",
|
|
61
|
+
"import": "./dist/cimd-node.mjs"
|
|
62
|
+
},
|
|
63
|
+
"./cimd/workers": {
|
|
64
|
+
"types": "./dist/cimd-workers.d.ts",
|
|
65
|
+
"import": "./dist/cimd-workers.mjs"
|
|
66
|
+
},
|
|
51
67
|
"./middleware": {
|
|
52
68
|
"types": "./dist/middleware.d.ts",
|
|
53
69
|
"import": "./dist/middleware.mjs"
|
|
54
70
|
},
|
|
71
|
+
"./audit": {
|
|
72
|
+
"types": "./dist/audit.d.ts",
|
|
73
|
+
"import": "./dist/audit.mjs"
|
|
74
|
+
},
|
|
75
|
+
"./email-guard": {
|
|
76
|
+
"types": "./dist/email-guard.d.ts",
|
|
77
|
+
"import": "./dist/email-guard.mjs"
|
|
78
|
+
},
|
|
55
79
|
"./turnstile": {
|
|
56
80
|
"types": "./dist/turnstile.d.ts",
|
|
57
81
|
"import": "./dist/turnstile.mjs"
|
|
@@ -82,11 +106,29 @@
|
|
|
82
106
|
"access": "public"
|
|
83
107
|
},
|
|
84
108
|
"dependencies": {
|
|
85
|
-
"@better-auth/
|
|
86
|
-
"@
|
|
87
|
-
"@
|
|
88
|
-
"@
|
|
89
|
-
"better-auth": "
|
|
109
|
+
"@better-auth/api-key": "1.7.7",
|
|
110
|
+
"@better-auth/cimd": "1.7.7",
|
|
111
|
+
"@better-auth/mcp": "1.7.7",
|
|
112
|
+
"@better-auth/oauth-provider": "1.7.7",
|
|
113
|
+
"@better-auth/passkey": "1.7.7",
|
|
114
|
+
"@better-auth/scim": "1.7.7",
|
|
115
|
+
"@lunora/errors": ">=1.0.0-alpha.51 <2.0.0-0",
|
|
116
|
+
"@lunora/values": "1.0.0-alpha.64",
|
|
117
|
+
"@visulima/disposable-email-domains": "1.1.25",
|
|
118
|
+
"@visulima/email-verifier": "1.0.42",
|
|
119
|
+
"@visulima/free-email-domains": "1.0.2",
|
|
120
|
+
"@visulima/redact": "5.0.0"
|
|
121
|
+
},
|
|
122
|
+
"peerDependencies": {
|
|
123
|
+
"@better-auth/core": ">=1.7.3 <2.0.0-0",
|
|
124
|
+
"@better-auth/sso": ">=1.7.3 <2.0.0-0",
|
|
125
|
+
"@lunora/server": ">=1.0.0-alpha.24 <2.0.0-0",
|
|
126
|
+
"better-auth": ">=1.7.3 <2.0.0-0"
|
|
127
|
+
},
|
|
128
|
+
"peerDependenciesMeta": {
|
|
129
|
+
"@better-auth/sso": {
|
|
130
|
+
"optional": true
|
|
131
|
+
}
|
|
90
132
|
},
|
|
91
133
|
"engines": {
|
|
92
134
|
"node": "^22.15.0 || >=24.11.0"
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
const DEFAULT_AUTH_BASE_PATH = "/api/auth";
|
|
2
|
-
const handleAuthRequest = async (auth, request, basePath = DEFAULT_AUTH_BASE_PATH) => {
|
|
3
|
-
const url = new URL(request.url);
|
|
4
|
-
const base = basePath.endsWith("/") ? basePath.slice(0, -1) : basePath;
|
|
5
|
-
if (url.pathname !== base && !url.pathname.startsWith(`${base}/`)) {
|
|
6
|
-
return void 0;
|
|
7
|
-
}
|
|
8
|
-
return auth.handler(request);
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export { DEFAULT_AUTH_BASE_PATH, handleAuthRequest };
|