@shware/security 7.1.0 → 8.0.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 +108 -30
- package/dist/auth/attributes.cjs +5 -5
- package/dist/auth/attributes.cjs.map +1 -1
- package/dist/auth/attributes.d.cts +3 -5
- package/dist/auth/attributes.d.cts.map +1 -1
- package/dist/auth/attributes.d.mts +3 -5
- package/dist/auth/attributes.d.mts.map +1 -1
- package/dist/auth/attributes.mjs +5 -5
- package/dist/auth/attributes.mjs.map +1 -1
- package/dist/auth/desktop-codes.cjs +57 -0
- package/dist/auth/desktop-codes.cjs.map +1 -0
- package/dist/auth/desktop-codes.d.cts +24 -0
- package/dist/auth/desktop-codes.d.cts.map +1 -0
- package/dist/auth/desktop-codes.d.mts +24 -0
- package/dist/auth/desktop-codes.d.mts.map +1 -0
- package/dist/auth/desktop-codes.mjs +55 -0
- package/dist/auth/desktop-codes.mjs.map +1 -0
- package/dist/auth/email-codes.cjs +77 -0
- package/dist/auth/email-codes.cjs.map +1 -0
- package/dist/auth/email-codes.d.cts +39 -0
- package/dist/auth/email-codes.d.cts.map +1 -0
- package/dist/auth/email-codes.d.mts +39 -0
- package/dist/auth/email-codes.d.mts.map +1 -0
- package/dist/auth/email-codes.mjs +75 -0
- package/dist/auth/email-codes.mjs.map +1 -0
- package/dist/auth/error.cjs +47 -0
- package/dist/auth/error.cjs.map +1 -0
- package/dist/auth/error.d.cts +88 -0
- package/dist/auth/error.d.cts.map +1 -0
- package/dist/auth/error.d.mts +88 -0
- package/dist/auth/error.d.mts.map +1 -0
- package/dist/auth/error.mjs +45 -0
- package/dist/auth/error.mjs.map +1 -0
- package/dist/auth/index.cjs +245 -385
- package/dist/auth/index.cjs.map +1 -1
- package/dist/auth/index.d.cts +66 -57
- package/dist/auth/index.d.cts.map +1 -1
- package/dist/auth/index.d.mts +66 -57
- package/dist/auth/index.d.mts.map +1 -1
- package/dist/auth/index.mjs +248 -388
- package/dist/auth/index.mjs.map +1 -1
- package/dist/auth/respond.cjs +99 -0
- package/dist/auth/respond.cjs.map +1 -0
- package/dist/auth/respond.d.cts +39 -0
- package/dist/auth/respond.d.cts.map +1 -0
- package/dist/auth/respond.d.mts +39 -0
- package/dist/auth/respond.d.mts.map +1 -0
- package/dist/auth/respond.mjs +92 -0
- package/dist/auth/respond.mjs.map +1 -0
- package/dist/auth/types.d.cts +27 -13
- package/dist/auth/types.d.cts.map +1 -1
- package/dist/auth/types.d.mts +27 -13
- package/dist/auth/types.d.mts.map +1 -1
- package/dist/core/index.cjs +1 -2
- package/dist/core/index.d.cts +0 -2
- package/dist/core/index.d.cts.map +1 -1
- package/dist/core/index.d.mts +0 -2
- package/dist/core/index.d.mts.map +1 -1
- package/dist/core/index.mjs +1 -2
- package/dist/email/schema.cjs +1 -2
- package/dist/email/schema.d.cts +0 -2
- package/dist/email/schema.d.cts.map +1 -1
- package/dist/email/schema.d.mts +0 -2
- package/dist/email/schema.d.mts.map +1 -1
- package/dist/email/schema.mjs +1 -2
- package/dist/index.cjs +5 -0
- package/dist/index.d.cts +4 -1
- package/dist/index.d.mts +4 -1
- package/dist/index.mjs +3 -1
- package/dist/oauth2/client.cjs +1 -2
- package/dist/oauth2/client.d.cts +0 -2
- package/dist/oauth2/client.d.cts.map +1 -1
- package/dist/oauth2/client.d.mts +0 -2
- package/dist/oauth2/client.d.mts.map +1 -1
- package/dist/oauth2/client.mjs +1 -2
- package/dist/oauth2/error.cjs +1 -9
- package/dist/oauth2/error.cjs.map +1 -1
- package/dist/oauth2/error.d.cts +0 -5
- package/dist/oauth2/error.d.cts.map +1 -1
- package/dist/oauth2/error.d.mts +0 -5
- package/dist/oauth2/error.d.mts.map +1 -1
- package/dist/oauth2/error.mjs +2 -9
- package/dist/oauth2/error.mjs.map +1 -1
- package/dist/oauth2/name.cjs +1 -2
- package/dist/oauth2/name.d.cts +0 -2
- package/dist/oauth2/name.d.cts.map +1 -1
- package/dist/oauth2/name.d.mts +0 -2
- package/dist/oauth2/name.d.mts.map +1 -1
- package/dist/oauth2/name.mjs +1 -2
- package/dist/oauth2/provider/apple-client-secret.cjs +1 -2
- package/dist/oauth2/provider/apple-client-secret.d.cts +0 -2
- package/dist/oauth2/provider/apple-client-secret.d.cts.map +1 -1
- package/dist/oauth2/provider/apple-client-secret.d.mts +0 -2
- package/dist/oauth2/provider/apple-client-secret.d.mts.map +1 -1
- package/dist/oauth2/provider/apple-client-secret.mjs +1 -2
- package/dist/oauth2/provider/apple.cjs +1 -2
- package/dist/oauth2/provider/apple.d.cts +0 -2
- package/dist/oauth2/provider/apple.d.cts.map +1 -1
- package/dist/oauth2/provider/apple.d.mts +0 -2
- package/dist/oauth2/provider/apple.d.mts.map +1 -1
- package/dist/oauth2/provider/apple.mjs +1 -2
- package/dist/oauth2/provider/common.cjs +2 -3
- package/dist/oauth2/provider/common.cjs.map +1 -1
- package/dist/oauth2/provider/common.d.cts +0 -2
- package/dist/oauth2/provider/common.d.cts.map +1 -1
- package/dist/oauth2/provider/common.d.mts +0 -2
- package/dist/oauth2/provider/common.d.mts.map +1 -1
- package/dist/oauth2/provider/common.mjs +2 -3
- package/dist/oauth2/provider/common.mjs.map +1 -1
- package/dist/oauth2/provider/facebook.cjs +3 -6
- package/dist/oauth2/provider/facebook.cjs.map +1 -1
- package/dist/oauth2/provider/facebook.d.cts +0 -3
- package/dist/oauth2/provider/facebook.d.cts.map +1 -1
- package/dist/oauth2/provider/facebook.d.mts +0 -3
- package/dist/oauth2/provider/facebook.d.mts.map +1 -1
- package/dist/oauth2/provider/facebook.mjs +3 -6
- package/dist/oauth2/provider/facebook.mjs.map +1 -1
- package/dist/oauth2/provider/github.cjs +1 -2
- package/dist/oauth2/provider/github.d.cts +0 -2
- package/dist/oauth2/provider/github.d.cts.map +1 -1
- package/dist/oauth2/provider/github.d.mts +0 -2
- package/dist/oauth2/provider/github.d.mts.map +1 -1
- package/dist/oauth2/provider/github.mjs +1 -2
- package/dist/oauth2/provider/google.cjs +1 -2
- package/dist/oauth2/provider/google.d.cts +0 -2
- package/dist/oauth2/provider/google.d.cts.map +1 -1
- package/dist/oauth2/provider/google.d.mts +0 -2
- package/dist/oauth2/provider/google.d.mts.map +1 -1
- package/dist/oauth2/provider/google.mjs +1 -2
- package/dist/oauth2/provider/wechat.cjs +1 -2
- package/dist/oauth2/provider/wechat.d.cts +0 -2
- package/dist/oauth2/provider/wechat.d.cts.map +1 -1
- package/dist/oauth2/provider/wechat.d.mts +0 -2
- package/dist/oauth2/provider/wechat.d.mts.map +1 -1
- package/dist/oauth2/provider/wechat.mjs +1 -2
- package/dist/oauth2/provider/x.cjs +7 -6
- package/dist/oauth2/provider/x.cjs.map +1 -1
- package/dist/oauth2/provider/x.d.cts +3 -4
- package/dist/oauth2/provider/x.d.cts.map +1 -1
- package/dist/oauth2/provider/x.d.mts +3 -4
- package/dist/oauth2/provider/x.d.mts.map +1 -1
- package/dist/oauth2/provider/x.mjs +7 -6
- package/dist/oauth2/provider/x.mjs.map +1 -1
- package/dist/oauth2/types.cjs +1 -2
- package/dist/oauth2/types.cjs.map +1 -1
- package/dist/oauth2/types.d.cts +5 -2
- package/dist/oauth2/types.d.cts.map +1 -1
- package/dist/oauth2/types.d.mts +5 -2
- package/dist/oauth2/types.d.mts.map +1 -1
- package/dist/oauth2/types.mjs +1 -2
- package/dist/oauth2/types.mjs.map +1 -1
- package/dist/session/common.cjs +11 -15
- package/dist/session/common.cjs.map +1 -1
- package/dist/session/common.d.cts +7 -5
- package/dist/session/common.d.cts.map +1 -1
- package/dist/session/common.d.mts +7 -5
- package/dist/session/common.d.mts.map +1 -1
- package/dist/session/common.mjs +11 -14
- package/dist/session/common.mjs.map +1 -1
- package/dist/session/db.cjs +3 -4
- package/dist/session/db.cjs.map +1 -1
- package/dist/session/db.d.cts +0 -2
- package/dist/session/db.d.cts.map +1 -1
- package/dist/session/db.d.mts +0 -2
- package/dist/session/db.d.mts.map +1 -1
- package/dist/session/db.mjs +4 -5
- package/dist/session/db.mjs.map +1 -1
- package/dist/session/map-session.cjs +1 -2
- package/dist/session/map-session.d.cts +0 -2
- package/dist/session/map-session.d.cts.map +1 -1
- package/dist/session/map-session.d.mts +0 -2
- package/dist/session/map-session.d.mts.map +1 -1
- package/dist/session/map-session.mjs +1 -2
- package/dist/session/redis.cjs +7 -10
- package/dist/session/redis.cjs.map +1 -1
- package/dist/session/redis.d.cts +0 -2
- package/dist/session/redis.d.cts.map +1 -1
- package/dist/session/redis.d.mts +0 -2
- package/dist/session/redis.d.mts.map +1 -1
- package/dist/session/redis.mjs +8 -11
- package/dist/session/redis.mjs.map +1 -1
- package/dist/session/types.d.cts +0 -14
- package/dist/session/types.d.cts.map +1 -1
- package/dist/session/types.d.mts +0 -14
- package/dist/session/types.d.mts.map +1 -1
- package/dist/utils/http.cjs +1 -2
- package/dist/utils/http.d.cts +0 -2
- package/dist/utils/http.d.cts.map +1 -1
- package/dist/utils/http.d.mts +0 -2
- package/dist/utils/http.d.mts.map +1 -1
- package/dist/utils/http.mjs +1 -2
- package/dist/utils/oauth2.cjs +1 -2
- package/dist/utils/oauth2.d.cts +0 -2
- package/dist/utils/oauth2.d.cts.map +1 -1
- package/dist/utils/oauth2.d.mts +0 -2
- package/dist/utils/oauth2.d.mts.map +1 -1
- package/dist/utils/oauth2.mjs +1 -2
- package/dist/utils/timing.cjs +1 -2
- package/dist/utils/timing.d.cts +0 -2
- package/dist/utils/timing.d.cts.map +1 -1
- package/dist/utils/timing.d.mts +0 -2
- package/dist/utils/timing.d.mts.map +1 -1
- package/dist/utils/timing.mjs +1 -2
- package/dist/utils/valid.cjs +35 -54
- package/dist/utils/valid.cjs.map +1 -1
- package/dist/utils/valid.d.cts +8 -25
- package/dist/utils/valid.d.cts.map +1 -1
- package/dist/utils/valid.d.mts +8 -25
- package/dist/utils/valid.d.mts.map +1 -1
- package/dist/utils/valid.mjs +33 -52
- package/dist/utils/valid.mjs.map +1 -1
- package/package.json +4 -4
package/dist/utils/valid.cjs
CHANGED
|
@@ -1,69 +1,50 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}]
|
|
12
|
-
} };
|
|
2
|
+
const require_auth_respond = require("../auth/respond.cjs");
|
|
3
|
+
// src/utils/valid.ts
|
|
4
|
+
/** The body as JSON, or a 400 for one that is not JSON. */
|
|
5
|
+
async function readJson(request) {
|
|
6
|
+
try {
|
|
7
|
+
return await request.json();
|
|
8
|
+
} catch {
|
|
9
|
+
throw require_auth_respond.invalidArgument("INVALID_JSON", "Request body is not valid JSON");
|
|
10
|
+
}
|
|
13
11
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
message,
|
|
19
|
-
details: [{
|
|
20
|
-
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
|
|
21
|
-
reason
|
|
22
|
-
}]
|
|
23
|
-
} };
|
|
12
|
+
/** The body as a form, or a 400 for one that is not `application/x-www-form-urlencoded`. */
|
|
13
|
+
async function readForm(request) {
|
|
14
|
+
if (!(request.headers.get("content-type")?.toLowerCase() ?? "").startsWith("application/x-www-form-urlencoded")) throw require_auth_respond.invalidArgument("INVALID_CONTENT_TYPE", "Expected application/x-www-form-urlencoded");
|
|
15
|
+
return Object.fromEntries(new URLSearchParams(await request.text()));
|
|
24
16
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
message
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
localizedMessage: {
|
|
43
|
-
locale: "en-US",
|
|
44
|
-
message
|
|
45
|
-
}
|
|
46
|
-
}))
|
|
47
|
-
}]
|
|
48
|
-
} },
|
|
49
|
-
data: null
|
|
50
|
-
};
|
|
17
|
+
/** `value` as `schema` reads it, or a 400 `BadRequest` naming each field that failed. */
|
|
18
|
+
function parse(schema, value) {
|
|
19
|
+
const result = schema.safeParse(value);
|
|
20
|
+
if (result.success) return result.data;
|
|
21
|
+
const fieldViolations = result.error.issues.map(({ code, path, message }) => ({
|
|
22
|
+
field: path.join("."),
|
|
23
|
+
description: message,
|
|
24
|
+
reason: code.toUpperCase(),
|
|
25
|
+
localizedMessage: {
|
|
26
|
+
locale: "en-US",
|
|
27
|
+
message
|
|
28
|
+
}
|
|
29
|
+
}));
|
|
30
|
+
throw require_auth_respond.invalidArgument("INVALID_ARGUMENT", "Invalid request", [{
|
|
31
|
+
"@type": "type.googleapis.com/google.rpc.BadRequest",
|
|
32
|
+
fieldViolations
|
|
33
|
+
}]);
|
|
51
34
|
}
|
|
52
35
|
async function verifyTurnstileToken(secret, token) {
|
|
53
|
-
if ((await (await fetch("https://challenges.cloudflare.com/turnstile/v0/siteverify", {
|
|
36
|
+
if (!(await (await fetch("https://challenges.cloudflare.com/turnstile/v0/siteverify", {
|
|
54
37
|
method: "POST",
|
|
55
38
|
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
56
39
|
body: new URLSearchParams({
|
|
57
40
|
secret,
|
|
58
41
|
response: token
|
|
59
42
|
})
|
|
60
|
-
})).json()).success)
|
|
61
|
-
return { error: invalidArgument("TURNSTILE_VERIFICATION_FAILED") };
|
|
43
|
+
})).json()).success) throw require_auth_respond.invalidArgument("TURNSTILE_VERIFICATION_FAILED", "Turnstile verification failed");
|
|
62
44
|
}
|
|
63
|
-
|
|
64
|
-
exports.
|
|
65
|
-
exports.
|
|
66
|
-
exports.valid = valid;
|
|
45
|
+
exports.parse = parse;
|
|
46
|
+
exports.readForm = readForm;
|
|
47
|
+
exports.readJson = readJson;
|
|
67
48
|
exports.verifyTurnstileToken = verifyTurnstileToken;
|
|
68
49
|
|
|
69
50
|
//# sourceMappingURL=valid.cjs.map
|
package/dist/utils/valid.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"valid.cjs","names":[],"sources":["../../src/utils/valid.ts"],"sourcesContent":["import type { ZodType, output } from 'zod';\nimport type { ZodMiniType, output as outputMini } from 'zod/mini';\n\ntype
|
|
1
|
+
{"version":3,"file":"valid.cjs","names":["invalidArgument"],"sources":["../../src/utils/valid.ts"],"sourcesContent":["import type { ZodType, output } from 'zod';\nimport type { ZodMiniType, output as outputMini } from 'zod/mini';\nimport { invalidArgument } from '../auth/respond';\n\ntype Output<S extends ZodType | ZodMiniType> = S extends ZodType ? output<S> : outputMini<S>;\n\n/** The body as JSON, or a 400 for one that is not JSON. */\nexport async function readJson(request: Request): Promise<unknown> {\n try {\n return await request.json();\n } catch {\n throw invalidArgument('INVALID_JSON', 'Request body is not valid JSON');\n }\n}\n\n/** The body as a form, or a 400 for one that is not `application/x-www-form-urlencoded`. */\nexport async function readForm(request: Request): Promise<Record<string, string>> {\n const type = request.headers.get('content-type')?.toLowerCase() ?? '';\n if (!type.startsWith('application/x-www-form-urlencoded')) {\n throw invalidArgument('INVALID_CONTENT_TYPE', 'Expected application/x-www-form-urlencoded');\n }\n return Object.fromEntries(new URLSearchParams(await request.text()));\n}\n\n/** `value` as `schema` reads it, or a 400 `BadRequest` naming each field that failed. */\nexport function parse<S extends ZodType | ZodMiniType>(schema: S, value: unknown): Output<S> {\n const result = schema.safeParse(value);\n if (result.success) return result.data as Output<S>;\n const fieldViolations = result.error.issues.map(({ code, path, message }) => ({\n field: path.join('.'),\n description: message,\n reason: code.toUpperCase(),\n localizedMessage: { locale: 'en-US', message },\n }));\n throw invalidArgument('INVALID_ARGUMENT', 'Invalid request', [\n { '@type': 'type.googleapis.com/google.rpc.BadRequest', fieldViolations },\n ]);\n}\n\nexport async function verifyTurnstileToken(secret: string, token: string): Promise<void> {\n const response = await fetch('https://challenges.cloudflare.com/turnstile/v0/siteverify', {\n method: 'POST',\n headers: { 'Content-Type': 'application/x-www-form-urlencoded' },\n body: new URLSearchParams({ secret, response: token }),\n });\n const data = (await response.json()) as { success: boolean };\n if (!data.success) {\n throw invalidArgument('TURNSTILE_VERIFICATION_FAILED', 'Turnstile verification failed');\n }\n}\n"],"mappings":";;;;AAOA,eAAsB,SAAS,SAAoC;CACjE,IAAI;EACF,OAAO,MAAM,QAAQ,KAAK;CAC5B,QAAQ;EACN,MAAMA,qBAAAA,gBAAgB,gBAAgB,gCAAgC;CACxE;AACF;;AAGA,eAAsB,SAAS,SAAmD;CAEhF,IAAI,EADS,QAAQ,QAAQ,IAAI,cAAc,CAAC,EAAE,YAAY,KAAK,GAAA,CACzD,WAAW,mCAAmC,GACtD,MAAMA,qBAAAA,gBAAgB,wBAAwB,4CAA4C;CAE5F,OAAO,OAAO,YAAY,IAAI,gBAAgB,MAAM,QAAQ,KAAK,CAAC,CAAC;AACrE;;AAGA,SAAgB,MAAuC,QAAW,OAA2B;CAC3F,MAAM,SAAS,OAAO,UAAU,KAAK;CACrC,IAAI,OAAO,SAAS,OAAO,OAAO;CAClC,MAAM,kBAAkB,OAAO,MAAM,OAAO,KAAK,EAAE,MAAM,MAAM,eAAe;EAC5E,OAAO,KAAK,KAAK,GAAG;EACpB,aAAa;EACb,QAAQ,KAAK,YAAY;EACzB,kBAAkB;GAAE,QAAQ;GAAS;EAAQ;CAC/C,EAAE;CACF,MAAMA,qBAAAA,gBAAgB,oBAAoB,mBAAmB,CAC3D;EAAE,SAAS;EAA6C;CAAgB,CAC1E,CAAC;AACH;AAEA,eAAsB,qBAAqB,QAAgB,OAA8B;CAOvF,IAAI,EAAC,OADe,MALG,MAAM,6DAA6D;EACxF,QAAQ;EACR,SAAS,EAAE,gBAAgB,oCAAoC;EAC/D,MAAM,IAAI,gBAAgB;GAAE;GAAQ,UAAU;EAAM,CAAC;CACvD,CAAC,EAAA,CAC4B,KAAK,EAAA,CACxB,SACR,MAAMA,qBAAAA,gBAAgB,iCAAiC,+BAA+B;AAE1F"}
|
package/dist/utils/valid.d.cts
CHANGED
|
@@ -1,29 +1,12 @@
|
|
|
1
1
|
import { ZodType, output } from "zod";
|
|
2
2
|
import { ZodMiniType, output as output$1 } from "zod/mini";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
};
|
|
12
|
-
export declare function invalidArgument(reason: string): ErrorBody;
|
|
13
|
-
export declare function resourceExhausted(reason: string, message: string): ErrorBody;
|
|
14
|
-
type Result<S extends ZodType | ZodMiniType> = {
|
|
15
|
-
error: null;
|
|
16
|
-
data: S extends ZodType ? output<S> : output$1<S>;
|
|
17
|
-
} | {
|
|
18
|
-
error: ErrorBody;
|
|
19
|
-
data: null;
|
|
20
|
-
};
|
|
21
|
-
export declare function valid<S extends ZodType | ZodMiniType>(schema: S, json: unknown): Result<S>;
|
|
22
|
-
export declare function verifyTurnstileToken(secret: string, token: string): Promise<{
|
|
23
|
-
error: null;
|
|
24
|
-
} | {
|
|
25
|
-
error: ErrorBody;
|
|
26
|
-
}>;
|
|
27
|
-
//#endregion
|
|
3
|
+
type Output<S extends ZodType | ZodMiniType> = S extends ZodType ? output<S> : output$1<S>;
|
|
4
|
+
/** The body as JSON, or a 400 for one that is not JSON. */
|
|
5
|
+
export declare function readJson(request: Request): Promise<unknown>;
|
|
6
|
+
/** The body as a form, or a 400 for one that is not `application/x-www-form-urlencoded`. */
|
|
7
|
+
export declare function readForm(request: Request): Promise<Record<string, string>>;
|
|
8
|
+
/** `value` as `schema` reads it, or a 400 `BadRequest` naming each field that failed. */
|
|
9
|
+
export declare function parse<S extends ZodType | ZodMiniType>(schema: S, value: unknown): Output<S>;
|
|
10
|
+
export declare function verifyTurnstileToken(secret: string, token: string): Promise<void>;
|
|
28
11
|
export {};
|
|
29
12
|
//# sourceMappingURL=valid.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"valid.d.cts","names":["outputMini"],"sources":["../../src/utils/valid.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"valid.d.cts","names":["outputMini"],"sources":["../../src/utils/valid.ts"],"mappings":";;KAIK,OAAO,UAAU,UAAU,eAAe,UAAU,UAAU,OAAO,KAAKA,SAAW;;wBAGpE,SAAS,SAAS,UAAU;;wBAS5B,SAAS,SAAS,UAAU,QAAQ;;wBAS1C,MAAM,UAAU,UAAU,aAAa,QAAQ,GAAG,iBAAiB,OAAO;wBAcpE,qBAAqB,gBAAgB,gBAAgB"}
|
package/dist/utils/valid.d.mts
CHANGED
|
@@ -1,29 +1,12 @@
|
|
|
1
1
|
import { ZodMiniType, output } from "zod/mini";
|
|
2
2
|
import { ZodType, output as output$1 } from "zod";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
};
|
|
12
|
-
export declare function invalidArgument(reason: string): ErrorBody;
|
|
13
|
-
export declare function resourceExhausted(reason: string, message: string): ErrorBody;
|
|
14
|
-
type Result<S extends ZodType | ZodMiniType> = {
|
|
15
|
-
error: null;
|
|
16
|
-
data: S extends ZodType ? output$1<S> : output<S>;
|
|
17
|
-
} | {
|
|
18
|
-
error: ErrorBody;
|
|
19
|
-
data: null;
|
|
20
|
-
};
|
|
21
|
-
export declare function valid<S extends ZodType | ZodMiniType>(schema: S, json: unknown): Result<S>;
|
|
22
|
-
export declare function verifyTurnstileToken(secret: string, token: string): Promise<{
|
|
23
|
-
error: null;
|
|
24
|
-
} | {
|
|
25
|
-
error: ErrorBody;
|
|
26
|
-
}>;
|
|
27
|
-
//#endregion
|
|
3
|
+
type Output<S extends ZodType | ZodMiniType> = S extends ZodType ? output$1<S> : output<S>;
|
|
4
|
+
/** The body as JSON, or a 400 for one that is not JSON. */
|
|
5
|
+
export declare function readJson(request: Request): Promise<unknown>;
|
|
6
|
+
/** The body as a form, or a 400 for one that is not `application/x-www-form-urlencoded`. */
|
|
7
|
+
export declare function readForm(request: Request): Promise<Record<string, string>>;
|
|
8
|
+
/** `value` as `schema` reads it, or a 400 `BadRequest` naming each field that failed. */
|
|
9
|
+
export declare function parse<S extends ZodType | ZodMiniType>(schema: S, value: unknown): Output<S>;
|
|
10
|
+
export declare function verifyTurnstileToken(secret: string, token: string): Promise<void>;
|
|
28
11
|
export {};
|
|
29
12
|
//# sourceMappingURL=valid.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"valid.d.mts","names":["output","outputMini"],"sources":["../../src/utils/valid.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"valid.d.mts","names":["output","outputMini"],"sources":["../../src/utils/valid.ts"],"mappings":";;KAIK,OAAO,UAAU,UAAU,eAAe,UAAU,UAAUA,SAAO,KAAKC,OAAW;;wBAGpE,SAAS,SAAS,UAAU;;wBAS5B,SAAS,SAAS,UAAU,QAAQ;;wBAS1C,MAAM,UAAU,UAAU,aAAa,QAAQ,GAAG,iBAAiB,OAAO;wBAcpE,qBAAqB,gBAAgB,gBAAgB"}
|
package/dist/utils/valid.mjs
CHANGED
|
@@ -1,65 +1,46 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}]
|
|
11
|
-
} };
|
|
1
|
+
import { invalidArgument } from "../auth/respond.mjs";
|
|
2
|
+
// src/utils/valid.ts
|
|
3
|
+
/** The body as JSON, or a 400 for one that is not JSON. */
|
|
4
|
+
async function readJson(request) {
|
|
5
|
+
try {
|
|
6
|
+
return await request.json();
|
|
7
|
+
} catch {
|
|
8
|
+
throw invalidArgument("INVALID_JSON", "Request body is not valid JSON");
|
|
9
|
+
}
|
|
12
10
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
message,
|
|
18
|
-
details: [{
|
|
19
|
-
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
|
|
20
|
-
reason
|
|
21
|
-
}]
|
|
22
|
-
} };
|
|
11
|
+
/** The body as a form, or a 400 for one that is not `application/x-www-form-urlencoded`. */
|
|
12
|
+
async function readForm(request) {
|
|
13
|
+
if (!(request.headers.get("content-type")?.toLowerCase() ?? "").startsWith("application/x-www-form-urlencoded")) throw invalidArgument("INVALID_CONTENT_TYPE", "Expected application/x-www-form-urlencoded");
|
|
14
|
+
return Object.fromEntries(new URLSearchParams(await request.text()));
|
|
23
15
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
message
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
localizedMessage: {
|
|
42
|
-
locale: "en-US",
|
|
43
|
-
message
|
|
44
|
-
}
|
|
45
|
-
}))
|
|
46
|
-
}]
|
|
47
|
-
} },
|
|
48
|
-
data: null
|
|
49
|
-
};
|
|
16
|
+
/** `value` as `schema` reads it, or a 400 `BadRequest` naming each field that failed. */
|
|
17
|
+
function parse(schema, value) {
|
|
18
|
+
const result = schema.safeParse(value);
|
|
19
|
+
if (result.success) return result.data;
|
|
20
|
+
const fieldViolations = result.error.issues.map(({ code, path, message }) => ({
|
|
21
|
+
field: path.join("."),
|
|
22
|
+
description: message,
|
|
23
|
+
reason: code.toUpperCase(),
|
|
24
|
+
localizedMessage: {
|
|
25
|
+
locale: "en-US",
|
|
26
|
+
message
|
|
27
|
+
}
|
|
28
|
+
}));
|
|
29
|
+
throw invalidArgument("INVALID_ARGUMENT", "Invalid request", [{
|
|
30
|
+
"@type": "type.googleapis.com/google.rpc.BadRequest",
|
|
31
|
+
fieldViolations
|
|
32
|
+
}]);
|
|
50
33
|
}
|
|
51
34
|
async function verifyTurnstileToken(secret, token) {
|
|
52
|
-
if ((await (await fetch("https://challenges.cloudflare.com/turnstile/v0/siteverify", {
|
|
35
|
+
if (!(await (await fetch("https://challenges.cloudflare.com/turnstile/v0/siteverify", {
|
|
53
36
|
method: "POST",
|
|
54
37
|
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
55
38
|
body: new URLSearchParams({
|
|
56
39
|
secret,
|
|
57
40
|
response: token
|
|
58
41
|
})
|
|
59
|
-
})).json()).success)
|
|
60
|
-
return { error: invalidArgument("TURNSTILE_VERIFICATION_FAILED") };
|
|
42
|
+
})).json()).success) throw invalidArgument("TURNSTILE_VERIFICATION_FAILED", "Turnstile verification failed");
|
|
61
43
|
}
|
|
62
|
-
|
|
63
|
-
export { invalidArgument, resourceExhausted, valid, verifyTurnstileToken };
|
|
44
|
+
export { parse, readForm, readJson, verifyTurnstileToken };
|
|
64
45
|
|
|
65
46
|
//# sourceMappingURL=valid.mjs.map
|
package/dist/utils/valid.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"valid.mjs","names":[],"sources":["../../src/utils/valid.ts"],"sourcesContent":["import type { ZodType, output } from 'zod';\nimport type { ZodMiniType, output as outputMini } from 'zod/mini';\n\ntype
|
|
1
|
+
{"version":3,"file":"valid.mjs","names":[],"sources":["../../src/utils/valid.ts"],"sourcesContent":["import type { ZodType, output } from 'zod';\nimport type { ZodMiniType, output as outputMini } from 'zod/mini';\nimport { invalidArgument } from '../auth/respond';\n\ntype Output<S extends ZodType | ZodMiniType> = S extends ZodType ? output<S> : outputMini<S>;\n\n/** The body as JSON, or a 400 for one that is not JSON. */\nexport async function readJson(request: Request): Promise<unknown> {\n try {\n return await request.json();\n } catch {\n throw invalidArgument('INVALID_JSON', 'Request body is not valid JSON');\n }\n}\n\n/** The body as a form, or a 400 for one that is not `application/x-www-form-urlencoded`. */\nexport async function readForm(request: Request): Promise<Record<string, string>> {\n const type = request.headers.get('content-type')?.toLowerCase() ?? '';\n if (!type.startsWith('application/x-www-form-urlencoded')) {\n throw invalidArgument('INVALID_CONTENT_TYPE', 'Expected application/x-www-form-urlencoded');\n }\n return Object.fromEntries(new URLSearchParams(await request.text()));\n}\n\n/** `value` as `schema` reads it, or a 400 `BadRequest` naming each field that failed. */\nexport function parse<S extends ZodType | ZodMiniType>(schema: S, value: unknown): Output<S> {\n const result = schema.safeParse(value);\n if (result.success) return result.data as Output<S>;\n const fieldViolations = result.error.issues.map(({ code, path, message }) => ({\n field: path.join('.'),\n description: message,\n reason: code.toUpperCase(),\n localizedMessage: { locale: 'en-US', message },\n }));\n throw invalidArgument('INVALID_ARGUMENT', 'Invalid request', [\n { '@type': 'type.googleapis.com/google.rpc.BadRequest', fieldViolations },\n ]);\n}\n\nexport async function verifyTurnstileToken(secret: string, token: string): Promise<void> {\n const response = await fetch('https://challenges.cloudflare.com/turnstile/v0/siteverify', {\n method: 'POST',\n headers: { 'Content-Type': 'application/x-www-form-urlencoded' },\n body: new URLSearchParams({ secret, response: token }),\n });\n const data = (await response.json()) as { success: boolean };\n if (!data.success) {\n throw invalidArgument('TURNSTILE_VERIFICATION_FAILED', 'Turnstile verification failed');\n }\n}\n"],"mappings":";;;AAOA,eAAsB,SAAS,SAAoC;CACjE,IAAI;EACF,OAAO,MAAM,QAAQ,KAAK;CAC5B,QAAQ;EACN,MAAM,gBAAgB,gBAAgB,gCAAgC;CACxE;AACF;;AAGA,eAAsB,SAAS,SAAmD;CAEhF,IAAI,EADS,QAAQ,QAAQ,IAAI,cAAc,CAAC,EAAE,YAAY,KAAK,GAAA,CACzD,WAAW,mCAAmC,GACtD,MAAM,gBAAgB,wBAAwB,4CAA4C;CAE5F,OAAO,OAAO,YAAY,IAAI,gBAAgB,MAAM,QAAQ,KAAK,CAAC,CAAC;AACrE;;AAGA,SAAgB,MAAuC,QAAW,OAA2B;CAC3F,MAAM,SAAS,OAAO,UAAU,KAAK;CACrC,IAAI,OAAO,SAAS,OAAO,OAAO;CAClC,MAAM,kBAAkB,OAAO,MAAM,OAAO,KAAK,EAAE,MAAM,MAAM,eAAe;EAC5E,OAAO,KAAK,KAAK,GAAG;EACpB,aAAa;EACb,QAAQ,KAAK,YAAY;EACzB,kBAAkB;GAAE,QAAQ;GAAS;EAAQ;CAC/C,EAAE;CACF,MAAM,gBAAgB,oBAAoB,mBAAmB,CAC3D;EAAE,SAAS;EAA6C;CAAgB,CAC1E,CAAC;AACH;AAEA,eAAsB,qBAAqB,QAAgB,OAA8B;CAOvF,IAAI,EAAC,OADe,MALG,MAAM,6DAA6D;EACxF,QAAQ;EACR,SAAS,EAAE,gBAAgB,oCAAoC;EAC/D,MAAM,IAAI,gBAAgB;GAAE;GAAQ,UAAU;EAAM,CAAC;CACvD,CAAC,EAAA,CAC4B,KAAK,EAAA,CACxB,SACR,MAAM,gBAAgB,iCAAiC,+BAA+B;AAE1F"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shware/security",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -36,9 +36,9 @@
|
|
|
36
36
|
"access": "public"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@shware/utils": "^2.0.
|
|
39
|
+
"@shware/utils": "^2.0.1",
|
|
40
40
|
"cookie": "^2.0.1",
|
|
41
|
-
"jose": "^6.2.
|
|
41
|
+
"jose": "^6.2.13",
|
|
42
42
|
"path-to-regexp": "^8.4.2"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
"dev": "tsc --watch",
|
|
63
63
|
"build": "tsdown",
|
|
64
64
|
"test": "vitest run",
|
|
65
|
-
"check-types": "tsc --noEmit",
|
|
65
|
+
"check-types": "tsc --noEmit && tsc --noEmit -p tsconfig.typetests.json",
|
|
66
66
|
"lint": "oxlint --type-aware src --fix"
|
|
67
67
|
}
|
|
68
68
|
}
|