@sharadox/protection-web-library 0.1.0-development.6 → 0.1.0-identifier-version-7.11
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 +8 -1
- package/distribution/proof-of-work/index.d.ts.map +1 -1
- package/distribution/proof-of-work/index.js +3 -2
- package/distribution/proof-of-work/index.js.map +1 -1
- package/distribution/protection-database/type.d.ts +12 -0
- package/distribution/protection-database/type.d.ts.map +1 -1
- package/distribution/protection-migration/index.d.ts +4 -1
- package/distribution/protection-migration/index.d.ts.map +1 -1
- package/distribution/protection-migration/index.js +33 -1
- package/distribution/protection-migration/index.js.map +1 -1
- package/distribution/solution-redemption/index.d.ts +19 -0
- package/distribution/solution-redemption/index.d.ts.map +1 -0
- package/distribution/solution-redemption/index.js +22 -0
- package/distribution/solution-redemption/index.js.map +1 -0
- package/distribution/used-challenge-prune/index.d.ts +13 -0
- package/distribution/used-challenge-prune/index.d.ts.map +1 -0
- package/distribution/used-challenge-prune/index.js +12 -0
- package/distribution/used-challenge-prune/index.js.map +1 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -86,7 +86,8 @@ not for secrets.
|
|
|
86
86
|
|
|
87
87
|
### `protection-migration`: `migrateProtection(database)`, `protectionMigrations`, `PROTECTION_MIGRATION_TABLE`
|
|
88
88
|
|
|
89
|
-
Creates `rate_limit_bucket` and `
|
|
89
|
+
Creates `rate_limit_bucket`, `spending_cap_usage` and `used_challenge`, every row with a UUIDv7 `id` (challenge ids
|
|
90
|
+
are UUIDv7 too). Type a Kysely instance with `ProtectionDatabase`
|
|
90
91
|
(`protection-database/type`), or include it in the application's own schema type.
|
|
91
92
|
|
|
92
93
|
### `rate-limit`: `limitRate(database, layers, { secret, now? })` → `{ allowed, retryAfterSeconds, blockedBy }`
|
|
@@ -109,6 +110,12 @@ const result = await limitRate(
|
|
|
109
110
|
);
|
|
110
111
|
```
|
|
111
112
|
|
|
113
|
+
### `solution-redemption`: `redeemSolution(database, solution, { secret, now? })`
|
|
114
|
+
|
|
115
|
+
`verifySolution` plus spending it: a valid solution counts once (its challenge id is kept until the challenge
|
|
116
|
+
expires), so a solved challenge can't be replayed. Reasons: `signature`, `expired`, `work`, `used`.
|
|
117
|
+
`pruneUsedChallenges(database, { now? })` (`used-challenge-prune`) forgets expired ones, from a daily job.
|
|
118
|
+
|
|
112
119
|
### `rate-limit-prune`: `pruneRateLimits(database, { before })` → deleted count
|
|
113
120
|
|
|
114
121
|
For a daily job. Pick `before` so that every bucket untouched since then would be full again.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/proof-of-work/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/proof-of-work/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,SAAS,EACT,sBAAsB,EACtB,QAAQ,EACR,YAAY,EACZ,qBAAqB,EACtB,MAAM,qDAAqD,CAAC;AAS7D;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,EAC9B,MAAM,EACN,UAAU,EACV,GAAgB,EAChB,eAAqB,GACtB,EAAE,sBAAsB,GAAG,SAAS,CASpC;AAED;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAC5B,EAAE,SAAS,EAAE,KAAK,EAAE,EAAE,QAAQ,EAC9B,EAAE,MAAM,EAAE,GAAgB,EAAE,EAAE,qBAAqB,GAClD,YAAY,CAWd"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { createHash, createHmac, randomBytes,
|
|
1
|
+
import { createHash, createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
|
2
|
+
import { uuidVersion7 } from "@sharadox/web-library/identifier/uuid-version-7";
|
|
2
3
|
import { leadingZeroBits } from "@sharadox/protection-web-library/leading-zero-bits";
|
|
3
4
|
function checkSecret(secret) {
|
|
4
5
|
if (secret.length < 32)
|
|
@@ -15,7 +16,7 @@ const sign = (secret, { id, salt, difficulty, expiresAt }) => createHmac("sha256
|
|
|
15
16
|
export function createChallenge({ secret, difficulty, now = Date.now(), lifetimeSeconds = 300, }) {
|
|
16
17
|
checkSecret(secret);
|
|
17
18
|
const fields = {
|
|
18
|
-
id:
|
|
19
|
+
id: uuidVersion7({ now }),
|
|
19
20
|
salt: randomBytes(16).toString("base64url"),
|
|
20
21
|
difficulty,
|
|
21
22
|
expiresAt: now + lifetimeSeconds * 1000,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/proof-of-work/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,WAAW,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/proof-of-work/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACnF,OAAO,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAC/E,OAAO,EAAE,eAAe,EAAE,MAAM,oDAAoD,CAAC;AASrF,SAAS,WAAW,CAAC,MAAc;IACjC,IAAI,MAAM,CAAC,MAAM,GAAG,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,2DAA2D,CAAC,CAAC;AACvG,CAAC;AAED,MAAM,IAAI,GAAG,CAAC,MAAc,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,UAAU,EAAE,SAAS,EAAgC,EAAE,EAAE,CACjG,UAAU,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,GAAG,EAAE,IAAI,IAAI,IAAI,UAAU,IAAI,SAAS,EAAE,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;AAEtG;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAAC,EAC9B,MAAM,EACN,UAAU,EACV,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAChB,eAAe,GAAG,GAAG,GACE;IACvB,WAAW,CAAC,MAAM,CAAC,CAAC;IACpB,MAAM,MAAM,GAAG;QACb,EAAE,EAAE,YAAY,CAAC,EAAE,GAAG,EAAE,CAAC;QACzB,IAAI,EAAE,WAAW,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC;QAC3C,UAAU;QACV,SAAS,EAAE,GAAG,GAAG,eAAe,GAAG,IAAI;KACxC,CAAC;IACF,OAAO,EAAE,GAAG,MAAM,EAAE,SAAS,EAAE,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAAC;AACxD,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,cAAc,CAC5B,EAAE,SAAS,EAAE,KAAK,EAAY,EAC9B,EAAE,MAAM,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,EAAyB;IAEnD,WAAW,CAAC,MAAM,CAAC,CAAC;IACpB,MAAM,EAAE,SAAS,EAAE,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC;IAC3C,MAAM,QAAQ,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IACnD,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC;IAC7C,IAAI,QAAQ,CAAC,MAAM,KAAK,KAAK,CAAC,MAAM,IAAI,CAAC,eAAe,CAAC,QAAQ,EAAE,KAAK,CAAC;QACvE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,CAAC;IAC/C,IAAI,GAAG,GAAG,SAAS,CAAC,SAAS;QAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;IAC1E,MAAM,IAAI,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,GAAG,SAAS,CAAC,IAAI,IAAI,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC;IAChF,IAAI,eAAe,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC,UAAU;QAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;IAC1F,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE,EAAE,SAAS,CAAC,EAAE,EAAE,CAAC;AAC3C,CAAC"}
|
|
@@ -1,18 +1,30 @@
|
|
|
1
|
+
import type { Generated } from "kysely";
|
|
1
2
|
/** One rate-limit bucket: its key (an HMAC, never the raw contact or address), tokens left and when last changed. */
|
|
2
3
|
export interface RateLimitBucketTable {
|
|
4
|
+
/** UUIDv7, made by the database. */
|
|
5
|
+
id: Generated<string>;
|
|
3
6
|
key: string;
|
|
4
7
|
tokens: number;
|
|
5
8
|
updated_at: Date;
|
|
6
9
|
}
|
|
7
10
|
/** How much of a spending cap one period has used. */
|
|
8
11
|
export interface SpendingCapUsageTable {
|
|
12
|
+
/** UUIDv7, made by the database. */
|
|
13
|
+
id: Generated<string>;
|
|
9
14
|
name: string;
|
|
10
15
|
period_start: Date;
|
|
11
16
|
used: number;
|
|
12
17
|
}
|
|
18
|
+
/** A proof-of-work challenge already answered, kept until it expires so its solution counts once. */
|
|
19
|
+
export interface UsedChallengeTable {
|
|
20
|
+
/** The challenge's UUIDv7. */
|
|
21
|
+
id: string;
|
|
22
|
+
expires_at: Date;
|
|
23
|
+
}
|
|
13
24
|
/** The tables `protectionMigrations` creates, for typing a Kysely instance. */
|
|
14
25
|
export interface ProtectionDatabase {
|
|
15
26
|
rate_limit_bucket: RateLimitBucketTable;
|
|
16
27
|
spending_cap_usage: SpendingCapUsageTable;
|
|
28
|
+
used_challenge: UsedChallengeTable;
|
|
17
29
|
}
|
|
18
30
|
//# sourceMappingURL=type.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/protection-database/type.ts"],"names":[],"mappings":"AAAA,qHAAqH;AACrH,MAAM,WAAW,oBAAoB;IACnC,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,IAAI,CAAC;CAClB;AAED,sDAAsD;AACtD,MAAM,WAAW,qBAAqB;IACpC,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,IAAI,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;CACd;AAED,+EAA+E;AAC/E,MAAM,WAAW,kBAAkB;IACjC,iBAAiB,EAAE,oBAAoB,CAAC;IACxC,kBAAkB,EAAE,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/protection-database/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AAExC,qHAAqH;AACrH,MAAM,WAAW,oBAAoB;IACnC,oCAAoC;IACpC,EAAE,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;IACtB,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,IAAI,CAAC;CAClB;AAED,sDAAsD;AACtD,MAAM,WAAW,qBAAqB;IACpC,oCAAoC;IACpC,EAAE,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,IAAI,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;CACd;AAED,qGAAqG;AACrG,MAAM,WAAW,kBAAkB;IACjC,8BAA8B;IAC9B,EAAE,EAAE,MAAM,CAAC;IACX,UAAU,EAAE,IAAI,CAAC;CAClB;AAED,+EAA+E;AAC/E,MAAM,WAAW,kBAAkB;IACjC,iBAAiB,EAAE,oBAAoB,CAAC;IACxC,kBAAkB,EAAE,qBAAqB,CAAC;IAC1C,cAAc,EAAE,kBAAkB,CAAC;CACpC"}
|
|
@@ -2,7 +2,10 @@ import type { Kysely } from "kysely";
|
|
|
2
2
|
import type { Migration } from "@sharadox/database-web-library/migration/type";
|
|
3
3
|
/** The table that records which protection migrations a database has applied (apart from the application's own). */
|
|
4
4
|
export declare const PROTECTION_MIGRATION_TABLE = "protection_migration";
|
|
5
|
-
/**
|
|
5
|
+
/**
|
|
6
|
+
* The protection tables, forward only: rate-limit buckets, spending-cap usage and used challenges, each row with a
|
|
7
|
+
* UUIDv7 `id`.
|
|
8
|
+
*/
|
|
6
9
|
export declare const protectionMigrations: Migration[];
|
|
7
10
|
/**
|
|
8
11
|
* Creates or updates the protection tables, tracked in their own migration table so they never clash with the
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/protection-migration/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,+CAA+C,CAAC;AAE/E,oHAAoH;AACpH,eAAO,MAAM,0BAA0B,yBAAyB,CAAC;AAEjE
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/protection-migration/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,+CAA+C,CAAC;AAE/E,oHAAoH;AACpH,eAAO,MAAM,0BAA0B,yBAAyB,CAAC;AAEjE;;;GAGG;AACH,eAAO,MAAM,oBAAoB,EAAE,SAAS,EA+C3C,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAErF"}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { migrate } from "@sharadox/database-web-library/migration";
|
|
2
2
|
/** The table that records which protection migrations a database has applied (apart from the application's own). */
|
|
3
3
|
export const PROTECTION_MIGRATION_TABLE = "protection_migration";
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* The protection tables, forward only: rate-limit buckets, spending-cap usage and used challenges, each row with a
|
|
6
|
+
* UUIDv7 `id`.
|
|
7
|
+
*/
|
|
5
8
|
export const protectionMigrations = [
|
|
6
9
|
{
|
|
7
10
|
name: "0001-rate-limit-and-spending-cap",
|
|
@@ -20,6 +23,35 @@ export const protectionMigrations = [
|
|
|
20
23
|
);
|
|
21
24
|
`,
|
|
22
25
|
},
|
|
26
|
+
{
|
|
27
|
+
name: "0002-used-challenge",
|
|
28
|
+
sql: `
|
|
29
|
+
create table used_challenge (
|
|
30
|
+
id text primary key check (length(id) <= 100),
|
|
31
|
+
expires_at timestamptz not null
|
|
32
|
+
);
|
|
33
|
+
create index used_challenge_expires_at on used_challenge (expires_at);
|
|
34
|
+
`,
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
name: "0003-identifier-version-7",
|
|
38
|
+
sql: `
|
|
39
|
+
alter table rate_limit_bucket drop constraint rate_limit_bucket_pkey;
|
|
40
|
+
alter table rate_limit_bucket add constraint rate_limit_bucket_key_key unique (key);
|
|
41
|
+
alter table rate_limit_bucket
|
|
42
|
+
add column id uuid primary key default uuidv7() check (uuid_extract_version(id) = 7);
|
|
43
|
+
alter table spending_cap_usage drop constraint spending_cap_usage_pkey;
|
|
44
|
+
alter table spending_cap_usage
|
|
45
|
+
add constraint spending_cap_usage_name_period_start_key unique (name, period_start);
|
|
46
|
+
alter table spending_cap_usage
|
|
47
|
+
add column id uuid primary key default uuidv7() check (uuid_extract_version(id) = 7);
|
|
48
|
+
delete from used_challenge where expires_at < now()
|
|
49
|
+
or id !~ '^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$';
|
|
50
|
+
alter table used_challenge drop constraint used_challenge_id_check;
|
|
51
|
+
alter table used_challenge alter column id type uuid using id::uuid;
|
|
52
|
+
alter table used_challenge add constraint used_challenge_id_check check (uuid_extract_version(id) = 7);
|
|
53
|
+
`,
|
|
54
|
+
},
|
|
23
55
|
];
|
|
24
56
|
/**
|
|
25
57
|
* Creates or updates the protection tables, tracked in their own migration table so they never clash with the
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/protection-migration/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,0CAA0C,CAAC;AAGnE,oHAAoH;AACpH,MAAM,CAAC,MAAM,0BAA0B,GAAG,sBAAsB,CAAC;AAEjE
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/protection-migration/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,0CAA0C,CAAC;AAGnE,oHAAoH;AACpH,MAAM,CAAC,MAAM,0BAA0B,GAAG,sBAAsB,CAAC;AAEjE;;;GAGG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAgB;IAC/C;QACE,IAAI,EAAE,kCAAkC;QACxC,GAAG,EAAE;;;;;;;;;;;;;KAaJ;KACF;IACD;QACE,IAAI,EAAE,qBAAqB;QAC3B,GAAG,EAAE;;;;;;KAMJ;KACF;IACD;QACE,IAAI,EAAE,2BAA2B;QACjC,GAAG,EAAE;;;;;;;;;;;;;;;KAeJ;KACF;CACF,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAAS,QAAwB;IAChE,OAAO,OAAO,CAAC,QAAQ,EAAE,oBAAoB,EAAE,EAAE,KAAK,EAAE,0BAA0B,EAAE,CAAC,CAAC;AACxF,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Kysely } from "kysely";
|
|
2
|
+
import type { Solution, VerifySolutionOptions } from "@sharadox/protection-web-library/proof-of-work/type";
|
|
3
|
+
import type { ProtectionDatabase } from "@sharadox/protection-web-library/protection-database/type";
|
|
4
|
+
/**
|
|
5
|
+
* Checks a proof-of-work solution and spends it: a valid one counts once (its challenge id is kept until the
|
|
6
|
+
* challenge expires), so one solved challenge can't be replayed. Nothing is stored for invalid solutions.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const verification = await redeemSolution(database, solution, { secret: process.env.PROOF_OF_WORK_SECRET! });
|
|
10
|
+
* if (!verification.valid) return refuse(verification.reason);
|
|
11
|
+
*/
|
|
12
|
+
export declare function redeemSolution(database: Kysely<ProtectionDatabase>, solution: Solution, options: VerifySolutionOptions): Promise<{
|
|
13
|
+
valid: true;
|
|
14
|
+
id: string;
|
|
15
|
+
} | {
|
|
16
|
+
valid: false;
|
|
17
|
+
reason: "signature" | "expired" | "work" | "used";
|
|
18
|
+
}>;
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/solution-redemption/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,QAAQ,EAAE,qBAAqB,EAAE,MAAM,qDAAqD,CAAC;AAE3G,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,2DAA2D,CAAC;AAEpG;;;;;;;GAOG;AACH,wBAAsB,cAAc,CAClC,QAAQ,EAAE,MAAM,CAAC,kBAAkB,CAAC,EACpC,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC;IAAE,KAAK,EAAE,IAAI,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,WAAW,GAAG,SAAS,GAAG,MAAM,GAAG,MAAM,CAAA;CAAE,CAAC,CAU5G"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { verifySolution } from "@sharadox/protection-web-library/proof-of-work";
|
|
2
|
+
/**
|
|
3
|
+
* Checks a proof-of-work solution and spends it: a valid one counts once (its challenge id is kept until the
|
|
4
|
+
* challenge expires), so one solved challenge can't be replayed. Nothing is stored for invalid solutions.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const verification = await redeemSolution(database, solution, { secret: process.env.PROOF_OF_WORK_SECRET! });
|
|
8
|
+
* if (!verification.valid) return refuse(verification.reason);
|
|
9
|
+
*/
|
|
10
|
+
export async function redeemSolution(database, solution, options) {
|
|
11
|
+
const verification = verifySolution(solution, options);
|
|
12
|
+
if (!verification.valid)
|
|
13
|
+
return verification;
|
|
14
|
+
const stored = await database
|
|
15
|
+
.insertInto("used_challenge")
|
|
16
|
+
.values({ id: verification.id, expires_at: new Date(solution.challenge.expiresAt) })
|
|
17
|
+
.onConflict((conflict) => conflict.column("id").doNothing())
|
|
18
|
+
.returning("id")
|
|
19
|
+
.executeTakeFirst();
|
|
20
|
+
return stored ? verification : { valid: false, reason: "used" };
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/solution-redemption/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,gDAAgD,CAAC;AAGhF;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,QAAoC,EACpC,QAAkB,EAClB,OAA8B;IAE9B,MAAM,YAAY,GAAG,cAAc,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IACvD,IAAI,CAAC,YAAY,CAAC,KAAK;QAAE,OAAO,YAAY,CAAC;IAC7C,MAAM,MAAM,GAAG,MAAM,QAAQ;SAC1B,UAAU,CAAC,gBAAgB,CAAC;SAC5B,MAAM,CAAC,EAAE,EAAE,EAAE,YAAY,CAAC,EAAE,EAAE,UAAU,EAAE,IAAI,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,SAAS,CAAC,EAAE,CAAC;SACnF,UAAU,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,SAAS,EAAE,CAAC;SAC3D,SAAS,CAAC,IAAI,CAAC;SACf,gBAAgB,EAAE,CAAC;IACtB,OAAO,MAAM,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;AAClE,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Kysely } from "kysely";
|
|
2
|
+
import type { ProtectionDatabase } from "@sharadox/protection-web-library/protection-database/type";
|
|
3
|
+
/**
|
|
4
|
+
* Forgets used challenges that have expired (their solutions are refused as expired anyway), from a daily job.
|
|
5
|
+
* Returns how many were deleted.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* await pruneUsedChallenges(database);
|
|
9
|
+
*/
|
|
10
|
+
export declare function pruneUsedChallenges(database: Kysely<ProtectionDatabase>, { now }?: {
|
|
11
|
+
now?: number;
|
|
12
|
+
}): Promise<number>;
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/used-challenge-prune/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,2DAA2D,CAAC;AAEpG;;;;;;GAMG;AACH,wBAAsB,mBAAmB,CACvC,QAAQ,EAAE,MAAM,CAAC,kBAAkB,CAAC,EACpC,EAAE,GAAgB,EAAE,GAAE;IAAE,GAAG,CAAC,EAAE,MAAM,CAAA;CAAO,GAC1C,OAAO,CAAC,MAAM,CAAC,CAGjB"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Forgets used challenges that have expired (their solutions are refused as expired anyway), from a daily job.
|
|
3
|
+
* Returns how many were deleted.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* await pruneUsedChallenges(database);
|
|
7
|
+
*/
|
|
8
|
+
export async function pruneUsedChallenges(database, { now = Date.now() } = {}) {
|
|
9
|
+
const result = await database.deleteFrom("used_challenge").where("expires_at", "<", new Date(now)).executeTakeFirst();
|
|
10
|
+
return Number(result.numDeletedRows);
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/used-challenge-prune/index.ts"],"names":[],"mappings":"AAGA;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACvC,QAAoC,EACpC,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,KAAuB,EAAE;IAE3C,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,UAAU,CAAC,gBAAgB,CAAC,CAAC,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,gBAAgB,EAAE,CAAC;IACtH,OAAO,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;AACvC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sharadox/protection-web-library",
|
|
3
|
-
"version": "0.1.0-
|
|
3
|
+
"version": "0.1.0-identifier-version-7.11",
|
|
4
4
|
"description": "Protection for web forms and APIs: layered rate limits, proof-of-work challenges and spending caps.",
|
|
5
5
|
"private": false,
|
|
6
6
|
"license": "SEE LICENSE IN LICENCA.md",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"*.{json,md,yml,yaml}": "prettier --write"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@sharadox/database-web-library": "0.1.0-development.
|
|
43
|
+
"@sharadox/database-web-library": "0.1.0-development.9",
|
|
44
44
|
"@sharadox/web-library": "^0.11.1",
|
|
45
45
|
"kysely": "^0.28.19"
|
|
46
46
|
},
|