dpdp-kit 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +205 -2
- package/dist/audit.d.ts +15 -0
- package/dist/chunk-67Q5GFD6.js +26 -0
- package/dist/chunk-FGZAH2NF.js +16 -0
- package/dist/chunk-HRKHRKV4.js +185 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +77 -0
- package/dist/config.d.ts +103 -0
- package/dist/dpdp.d.ts +68 -0
- package/dist/drizzle.d.ts +216 -0
- package/dist/drizzle.js +97 -0
- package/dist/duration.d.ts +7 -0
- package/dist/erasure.d.ts +72 -0
- package/dist/errors.d.ts +19 -0
- package/dist/express.d.ts +23 -0
- package/dist/express.js +21 -0
- package/dist/guard.d.ts +13 -0
- package/dist/hash.d.ts +3 -0
- package/dist/hono.d.ts +20 -0
- package/dist/hono.js +16 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +320 -0
- package/dist/languages.d.ts +33 -0
- package/dist/run-audit.d.ts +9 -0
- package/dist/store.d.ts +38 -0
- package/package.json +80 -4
package/dist/dpdp.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { DpdpConfig } from "./config.js";
|
|
2
|
+
import { type ErasurePlan } from "./erasure.js";
|
|
3
|
+
import { type NoticeLanguage } from "./languages.js";
|
|
4
|
+
import type { ConsentEvent, ConsentStore } from "./store.js";
|
|
5
|
+
export interface ConsentMeta {
|
|
6
|
+
noticeVersion?: string;
|
|
7
|
+
language?: NoticeLanguage;
|
|
8
|
+
channel?: string;
|
|
9
|
+
evidence?: Record<string, string>;
|
|
10
|
+
}
|
|
11
|
+
export type VerifyResult = {
|
|
12
|
+
ok: true;
|
|
13
|
+
events: number;
|
|
14
|
+
} | {
|
|
15
|
+
ok: false;
|
|
16
|
+
events: number;
|
|
17
|
+
brokenAt: number;
|
|
18
|
+
reason: "hash-mismatch" | "chain-broken" | "seq-gap";
|
|
19
|
+
};
|
|
20
|
+
export interface DpdpOptions<P extends string> {
|
|
21
|
+
config: DpdpConfig<P>;
|
|
22
|
+
store: ConsentStore;
|
|
23
|
+
/** Clock, for tests. */
|
|
24
|
+
now?: () => Date;
|
|
25
|
+
/** Attempts when two writes for the same person race. Default 5. */
|
|
26
|
+
maxRetries?: number;
|
|
27
|
+
}
|
|
28
|
+
export interface Dpdp<P extends string> {
|
|
29
|
+
readonly config: DpdpConfig<P>;
|
|
30
|
+
readonly consent: {
|
|
31
|
+
/** Record that the person agreed to these consent purposes. */
|
|
32
|
+
grant(subjectId: string, purposes: readonly P[], meta?: ConsentMeta): Promise<ConsentEvent>;
|
|
33
|
+
/**
|
|
34
|
+
* Record that the person withdrew consent. Returns what must now be erased; nothing is
|
|
35
|
+
* erased for you in v0.1.
|
|
36
|
+
*/
|
|
37
|
+
withdraw(subjectId: string, purposes: readonly P[], meta?: ConsentMeta): Promise<{
|
|
38
|
+
event: ConsentEvent;
|
|
39
|
+
erasure: ErasurePlan;
|
|
40
|
+
}>;
|
|
41
|
+
/**
|
|
42
|
+
* Whether you hold consent, or don't need it, for this purpose. For consent purposes: true when
|
|
43
|
+
* the person's latest choice was a grant. For legitimate-use purposes: always true, because
|
|
44
|
+
* there's nothing to record. But under Section 7(a) you must stop once the person tells you they
|
|
45
|
+
* don't want their data used; v0.1 doesn't record that yet.
|
|
46
|
+
*/
|
|
47
|
+
has(subjectId: string, purpose: P): Promise<boolean>;
|
|
48
|
+
/** Consent purposes this person currently has granted. */
|
|
49
|
+
active(subjectId: string): Promise<P[]>;
|
|
50
|
+
history(subjectId: string): Promise<ConsentEvent[]>;
|
|
51
|
+
/** Recompute every hash for this person and check nothing was edited, removed or reordered. */
|
|
52
|
+
verify(subjectId: string): Promise<VerifyResult>;
|
|
53
|
+
};
|
|
54
|
+
readonly erasure: {
|
|
55
|
+
/** What deleting this person's account would erase, delete or hold. Changes nothing. */
|
|
56
|
+
plan(subjectId: string, input: {
|
|
57
|
+
reason: "account-deletion";
|
|
58
|
+
}): Promise<ErasurePlan>;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
type HashedFields = Omit<ConsentEvent, "hash">;
|
|
62
|
+
export declare function hashEvent(event: HashedFields): string;
|
|
63
|
+
/**
|
|
64
|
+
* Create the runtime. Throws `DpdpError("invalid-config")` if `audit()` finds errors, so a broken
|
|
65
|
+
* data map fails at start-up instead of during an erasure.
|
|
66
|
+
*/
|
|
67
|
+
export declare function createDpdp<const P extends string>(options: DpdpOptions<P>): Dpdp<P>;
|
|
68
|
+
export {};
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { type PgDatabase } from "drizzle-orm/pg-core";
|
|
2
|
+
import type { ConsentStore } from "./store.js";
|
|
3
|
+
/**
|
|
4
|
+
* The consent events table. Export it from your Drizzle schema so drizzle-kit creates it:
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* export { consentEvents } from "dpdp-kit/drizzle";
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
export declare const consentEvents: import("drizzle-orm/pg-core").PgTableWithColumns<{
|
|
11
|
+
name: "dpdp_consent_events";
|
|
12
|
+
schema: undefined;
|
|
13
|
+
columns: {
|
|
14
|
+
subjectId: import("drizzle-orm/pg-core").PgColumn<{
|
|
15
|
+
name: "subject_id";
|
|
16
|
+
tableName: "dpdp_consent_events";
|
|
17
|
+
dataType: "string";
|
|
18
|
+
columnType: "PgText";
|
|
19
|
+
data: string;
|
|
20
|
+
driverParam: string;
|
|
21
|
+
notNull: true;
|
|
22
|
+
hasDefault: false;
|
|
23
|
+
isPrimaryKey: false;
|
|
24
|
+
isAutoincrement: false;
|
|
25
|
+
hasRuntimeDefault: false;
|
|
26
|
+
enumValues: [string, ...string[]];
|
|
27
|
+
baseColumn: never;
|
|
28
|
+
identity: undefined;
|
|
29
|
+
generated: undefined;
|
|
30
|
+
}, {}, {}>;
|
|
31
|
+
seq: import("drizzle-orm/pg-core").PgColumn<{
|
|
32
|
+
name: "seq";
|
|
33
|
+
tableName: "dpdp_consent_events";
|
|
34
|
+
dataType: "number";
|
|
35
|
+
columnType: "PgInteger";
|
|
36
|
+
data: number;
|
|
37
|
+
driverParam: string | number;
|
|
38
|
+
notNull: true;
|
|
39
|
+
hasDefault: false;
|
|
40
|
+
isPrimaryKey: false;
|
|
41
|
+
isAutoincrement: false;
|
|
42
|
+
hasRuntimeDefault: false;
|
|
43
|
+
enumValues: undefined;
|
|
44
|
+
baseColumn: never;
|
|
45
|
+
identity: undefined;
|
|
46
|
+
generated: undefined;
|
|
47
|
+
}, {}, {}>;
|
|
48
|
+
action: import("drizzle-orm/pg-core").PgColumn<{
|
|
49
|
+
name: "action";
|
|
50
|
+
tableName: "dpdp_consent_events";
|
|
51
|
+
dataType: "string";
|
|
52
|
+
columnType: "PgText";
|
|
53
|
+
data: "grant" | "withdraw";
|
|
54
|
+
driverParam: string;
|
|
55
|
+
notNull: true;
|
|
56
|
+
hasDefault: false;
|
|
57
|
+
isPrimaryKey: false;
|
|
58
|
+
isAutoincrement: false;
|
|
59
|
+
hasRuntimeDefault: false;
|
|
60
|
+
enumValues: ["grant", "withdraw"];
|
|
61
|
+
baseColumn: never;
|
|
62
|
+
identity: undefined;
|
|
63
|
+
generated: undefined;
|
|
64
|
+
}, {}, {}>;
|
|
65
|
+
purposes: import("drizzle-orm/pg-core").PgColumn<{
|
|
66
|
+
name: "purposes";
|
|
67
|
+
tableName: "dpdp_consent_events";
|
|
68
|
+
dataType: "json";
|
|
69
|
+
columnType: "PgJsonb";
|
|
70
|
+
data: string[];
|
|
71
|
+
driverParam: unknown;
|
|
72
|
+
notNull: true;
|
|
73
|
+
hasDefault: false;
|
|
74
|
+
isPrimaryKey: false;
|
|
75
|
+
isAutoincrement: false;
|
|
76
|
+
hasRuntimeDefault: false;
|
|
77
|
+
enumValues: undefined;
|
|
78
|
+
baseColumn: never;
|
|
79
|
+
identity: undefined;
|
|
80
|
+
generated: undefined;
|
|
81
|
+
}, {}, {
|
|
82
|
+
$type: string[];
|
|
83
|
+
}>;
|
|
84
|
+
noticeVersion: import("drizzle-orm/pg-core").PgColumn<{
|
|
85
|
+
name: "notice_version";
|
|
86
|
+
tableName: "dpdp_consent_events";
|
|
87
|
+
dataType: "string";
|
|
88
|
+
columnType: "PgText";
|
|
89
|
+
data: string;
|
|
90
|
+
driverParam: string;
|
|
91
|
+
notNull: false;
|
|
92
|
+
hasDefault: false;
|
|
93
|
+
isPrimaryKey: false;
|
|
94
|
+
isAutoincrement: false;
|
|
95
|
+
hasRuntimeDefault: false;
|
|
96
|
+
enumValues: [string, ...string[]];
|
|
97
|
+
baseColumn: never;
|
|
98
|
+
identity: undefined;
|
|
99
|
+
generated: undefined;
|
|
100
|
+
}, {}, {}>;
|
|
101
|
+
language: import("drizzle-orm/pg-core").PgColumn<{
|
|
102
|
+
name: "language";
|
|
103
|
+
tableName: "dpdp_consent_events";
|
|
104
|
+
dataType: "string";
|
|
105
|
+
columnType: "PgText";
|
|
106
|
+
data: string;
|
|
107
|
+
driverParam: string;
|
|
108
|
+
notNull: false;
|
|
109
|
+
hasDefault: false;
|
|
110
|
+
isPrimaryKey: false;
|
|
111
|
+
isAutoincrement: false;
|
|
112
|
+
hasRuntimeDefault: false;
|
|
113
|
+
enumValues: [string, ...string[]];
|
|
114
|
+
baseColumn: never;
|
|
115
|
+
identity: undefined;
|
|
116
|
+
generated: undefined;
|
|
117
|
+
}, {}, {}>;
|
|
118
|
+
channel: import("drizzle-orm/pg-core").PgColumn<{
|
|
119
|
+
name: "channel";
|
|
120
|
+
tableName: "dpdp_consent_events";
|
|
121
|
+
dataType: "string";
|
|
122
|
+
columnType: "PgText";
|
|
123
|
+
data: string;
|
|
124
|
+
driverParam: string;
|
|
125
|
+
notNull: false;
|
|
126
|
+
hasDefault: false;
|
|
127
|
+
isPrimaryKey: false;
|
|
128
|
+
isAutoincrement: false;
|
|
129
|
+
hasRuntimeDefault: false;
|
|
130
|
+
enumValues: [string, ...string[]];
|
|
131
|
+
baseColumn: never;
|
|
132
|
+
identity: undefined;
|
|
133
|
+
generated: undefined;
|
|
134
|
+
}, {}, {}>;
|
|
135
|
+
evidence: import("drizzle-orm/pg-core").PgColumn<{
|
|
136
|
+
name: "evidence";
|
|
137
|
+
tableName: "dpdp_consent_events";
|
|
138
|
+
dataType: "json";
|
|
139
|
+
columnType: "PgJsonb";
|
|
140
|
+
data: Record<string, string>;
|
|
141
|
+
driverParam: unknown;
|
|
142
|
+
notNull: false;
|
|
143
|
+
hasDefault: false;
|
|
144
|
+
isPrimaryKey: false;
|
|
145
|
+
isAutoincrement: false;
|
|
146
|
+
hasRuntimeDefault: false;
|
|
147
|
+
enumValues: undefined;
|
|
148
|
+
baseColumn: never;
|
|
149
|
+
identity: undefined;
|
|
150
|
+
generated: undefined;
|
|
151
|
+
}, {}, {
|
|
152
|
+
$type: Record<string, string>;
|
|
153
|
+
}>;
|
|
154
|
+
at: import("drizzle-orm/pg-core").PgColumn<{
|
|
155
|
+
name: "at";
|
|
156
|
+
tableName: "dpdp_consent_events";
|
|
157
|
+
dataType: "date";
|
|
158
|
+
columnType: "PgTimestamp";
|
|
159
|
+
data: Date;
|
|
160
|
+
driverParam: string;
|
|
161
|
+
notNull: true;
|
|
162
|
+
hasDefault: false;
|
|
163
|
+
isPrimaryKey: false;
|
|
164
|
+
isAutoincrement: false;
|
|
165
|
+
hasRuntimeDefault: false;
|
|
166
|
+
enumValues: undefined;
|
|
167
|
+
baseColumn: never;
|
|
168
|
+
identity: undefined;
|
|
169
|
+
generated: undefined;
|
|
170
|
+
}, {}, {}>;
|
|
171
|
+
prevHash: import("drizzle-orm/pg-core").PgColumn<{
|
|
172
|
+
name: "prev_hash";
|
|
173
|
+
tableName: "dpdp_consent_events";
|
|
174
|
+
dataType: "string";
|
|
175
|
+
columnType: "PgText";
|
|
176
|
+
data: string;
|
|
177
|
+
driverParam: string;
|
|
178
|
+
notNull: false;
|
|
179
|
+
hasDefault: false;
|
|
180
|
+
isPrimaryKey: false;
|
|
181
|
+
isAutoincrement: false;
|
|
182
|
+
hasRuntimeDefault: false;
|
|
183
|
+
enumValues: [string, ...string[]];
|
|
184
|
+
baseColumn: never;
|
|
185
|
+
identity: undefined;
|
|
186
|
+
generated: undefined;
|
|
187
|
+
}, {}, {}>;
|
|
188
|
+
hash: import("drizzle-orm/pg-core").PgColumn<{
|
|
189
|
+
name: "hash";
|
|
190
|
+
tableName: "dpdp_consent_events";
|
|
191
|
+
dataType: "string";
|
|
192
|
+
columnType: "PgText";
|
|
193
|
+
data: string;
|
|
194
|
+
driverParam: string;
|
|
195
|
+
notNull: true;
|
|
196
|
+
hasDefault: false;
|
|
197
|
+
isPrimaryKey: false;
|
|
198
|
+
isAutoincrement: false;
|
|
199
|
+
hasRuntimeDefault: false;
|
|
200
|
+
enumValues: [string, ...string[]];
|
|
201
|
+
baseColumn: never;
|
|
202
|
+
identity: undefined;
|
|
203
|
+
generated: undefined;
|
|
204
|
+
}, {}, {}>;
|
|
205
|
+
};
|
|
206
|
+
dialect: 'pg';
|
|
207
|
+
}>;
|
|
208
|
+
/** The same table as plain SQL, if you don't use drizzle-kit for migrations. */
|
|
209
|
+
export declare const CONSENT_EVENTS_DDL = "CREATE TABLE IF NOT EXISTS dpdp_consent_events (\n subject_id text NOT NULL,\n seq integer NOT NULL,\n action text NOT NULL CHECK (action IN ('grant', 'withdraw')),\n purposes jsonb NOT NULL,\n notice_version text,\n language text,\n channel text,\n evidence jsonb,\n at timestamptz NOT NULL,\n prev_hash text,\n hash text NOT NULL,\n PRIMARY KEY (subject_id, seq)\n);";
|
|
210
|
+
type AnyPgDatabase = PgDatabase<any, any, any>;
|
|
211
|
+
/**
|
|
212
|
+
* Consent store on Postgres through Drizzle. Works with node-postgres, postgres.js, Neon,
|
|
213
|
+
* PGlite and other Drizzle Postgres drivers.
|
|
214
|
+
*/
|
|
215
|
+
export declare function drizzleStore(db: AnyPgDatabase): ConsentStore;
|
|
216
|
+
export {};
|
package/dist/drizzle.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SeqConflictError
|
|
3
|
+
} from "./chunk-67Q5GFD6.js";
|
|
4
|
+
|
|
5
|
+
// src/drizzle.ts
|
|
6
|
+
import { asc, desc, eq } from "drizzle-orm";
|
|
7
|
+
import { integer, jsonb, pgTable, primaryKey, text, timestamp } from "drizzle-orm/pg-core";
|
|
8
|
+
var consentEvents = pgTable(
|
|
9
|
+
"dpdp_consent_events",
|
|
10
|
+
{
|
|
11
|
+
subjectId: text("subject_id").notNull(),
|
|
12
|
+
seq: integer("seq").notNull(),
|
|
13
|
+
action: text("action", { enum: ["grant", "withdraw"] }).notNull(),
|
|
14
|
+
purposes: jsonb("purposes").$type().notNull(),
|
|
15
|
+
noticeVersion: text("notice_version"),
|
|
16
|
+
language: text("language"),
|
|
17
|
+
channel: text("channel"),
|
|
18
|
+
evidence: jsonb("evidence").$type(),
|
|
19
|
+
at: timestamp("at", { withTimezone: true, mode: "date" }).notNull(),
|
|
20
|
+
prevHash: text("prev_hash"),
|
|
21
|
+
hash: text("hash").notNull()
|
|
22
|
+
},
|
|
23
|
+
(t) => [primaryKey({ columns: [t.subjectId, t.seq] })]
|
|
24
|
+
);
|
|
25
|
+
var CONSENT_EVENTS_DDL = `CREATE TABLE IF NOT EXISTS dpdp_consent_events (
|
|
26
|
+
subject_id text NOT NULL,
|
|
27
|
+
seq integer NOT NULL,
|
|
28
|
+
action text NOT NULL CHECK (action IN ('grant', 'withdraw')),
|
|
29
|
+
purposes jsonb NOT NULL,
|
|
30
|
+
notice_version text,
|
|
31
|
+
language text,
|
|
32
|
+
channel text,
|
|
33
|
+
evidence jsonb,
|
|
34
|
+
at timestamptz NOT NULL,
|
|
35
|
+
prev_hash text,
|
|
36
|
+
hash text NOT NULL,
|
|
37
|
+
PRIMARY KEY (subject_id, seq)
|
|
38
|
+
);`;
|
|
39
|
+
function fromRow(row) {
|
|
40
|
+
return {
|
|
41
|
+
subjectId: row.subjectId,
|
|
42
|
+
seq: row.seq,
|
|
43
|
+
action: row.action,
|
|
44
|
+
purposes: row.purposes,
|
|
45
|
+
...row.noticeVersion !== null ? { noticeVersion: row.noticeVersion } : {},
|
|
46
|
+
...row.language !== null ? { language: row.language } : {},
|
|
47
|
+
...row.channel !== null ? { channel: row.channel } : {},
|
|
48
|
+
...row.evidence !== null ? { evidence: row.evidence } : {},
|
|
49
|
+
at: row.at.toISOString(),
|
|
50
|
+
prevHash: row.prevHash,
|
|
51
|
+
hash: row.hash
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function isUniqueViolation(err) {
|
|
55
|
+
for (let e = err, depth = 0; e && depth < 5; depth++) {
|
|
56
|
+
if (typeof e === "object" && e.code === "23505") return true;
|
|
57
|
+
e = e.cause;
|
|
58
|
+
}
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
function drizzleStore(db) {
|
|
62
|
+
return {
|
|
63
|
+
async lastEvent(subjectId) {
|
|
64
|
+
const rows = await db.select().from(consentEvents).where(eq(consentEvents.subjectId, subjectId)).orderBy(desc(consentEvents.seq)).limit(1);
|
|
65
|
+
return rows[0] ? fromRow(rows[0]) : void 0;
|
|
66
|
+
},
|
|
67
|
+
async insert(event) {
|
|
68
|
+
try {
|
|
69
|
+
await db.insert(consentEvents).values({
|
|
70
|
+
subjectId: event.subjectId,
|
|
71
|
+
seq: event.seq,
|
|
72
|
+
action: event.action,
|
|
73
|
+
purposes: event.purposes,
|
|
74
|
+
noticeVersion: event.noticeVersion ?? null,
|
|
75
|
+
language: event.language ?? null,
|
|
76
|
+
channel: event.channel ?? null,
|
|
77
|
+
evidence: event.evidence ?? null,
|
|
78
|
+
at: new Date(event.at),
|
|
79
|
+
prevHash: event.prevHash,
|
|
80
|
+
hash: event.hash
|
|
81
|
+
});
|
|
82
|
+
} catch (err) {
|
|
83
|
+
if (isUniqueViolation(err)) throw new SeqConflictError(event.subjectId, event.seq);
|
|
84
|
+
throw err;
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
async events(subjectId) {
|
|
88
|
+
const rows = await db.select().from(consentEvents).where(eq(consentEvents.subjectId, subjectId)).orderBy(asc(consentEvents.seq));
|
|
89
|
+
return rows.map(fromRow);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
export {
|
|
94
|
+
CONSENT_EVENTS_DDL,
|
|
95
|
+
consentEvents,
|
|
96
|
+
drizzleStore
|
|
97
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** A length of time: hours, days or years, e.g. `"48h"`, `"30d"`, `"1y"`. A year counts as 365 days. */
|
|
2
|
+
export type Duration = `${number}${"h" | "d" | "y"}`;
|
|
3
|
+
export declare function isDuration(value: unknown): value is Duration;
|
|
4
|
+
/** Milliseconds in a duration. Throws on anything that isn't `<whole number><h|d|y>`. */
|
|
5
|
+
export declare function durationMs(value: Duration): number;
|
|
6
|
+
/** The longer of two durations. */
|
|
7
|
+
export declare function maxDuration(a: Duration, b: Duration): Duration;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { DpdpConfig, LegalHold } from "./config.js";
|
|
2
|
+
import { type Duration } from "./duration.js";
|
|
3
|
+
export type ErasureReason = "withdrawal" | "account-deletion";
|
|
4
|
+
export interface FieldPlan {
|
|
5
|
+
field: string;
|
|
6
|
+
/**
|
|
7
|
+
* - `erase`: no purpose is left for this field; erase it after `after`.
|
|
8
|
+
* - `keep`: still needed for `keptFor`.
|
|
9
|
+
* - `hold`: would be erased, but the table is under a legal hold.
|
|
10
|
+
*/
|
|
11
|
+
action: "erase" | "keep" | "hold";
|
|
12
|
+
/** For `erase`: how long you may wait before erasing. */
|
|
13
|
+
after?: Duration;
|
|
14
|
+
/** For `keep`: the purposes that still need it. */
|
|
15
|
+
keptFor?: string[];
|
|
16
|
+
}
|
|
17
|
+
export interface TablePlan {
|
|
18
|
+
/** Key in `config.data`. */
|
|
19
|
+
key: string;
|
|
20
|
+
table: string;
|
|
21
|
+
subject: string;
|
|
22
|
+
/**
|
|
23
|
+
* - `delete-rows`: delete this person's rows (account deletion, every field erasable).
|
|
24
|
+
* - `erase-fields`: null out or anonymise the fields marked `erase`.
|
|
25
|
+
* - `hold`: keep the rows until the legal hold ends, then apply `hold.then`.
|
|
26
|
+
* - `keep`: nothing to do.
|
|
27
|
+
*/
|
|
28
|
+
action: "delete-rows" | "erase-fields" | "hold" | "keep";
|
|
29
|
+
fields: FieldPlan[];
|
|
30
|
+
hold?: LegalHold;
|
|
31
|
+
}
|
|
32
|
+
export interface ProcessorPlan {
|
|
33
|
+
name: string;
|
|
34
|
+
/**
|
|
35
|
+
* - `erase`: every purpose you shared data for has ended; you must make them erase it
|
|
36
|
+
* (Section 8(7)(b)).
|
|
37
|
+
* - `restrict`: some purposes ended; make them stop using it for `ended` (Section 6(6) requires
|
|
38
|
+
* you to make your processors stop after a withdrawal).
|
|
39
|
+
*/
|
|
40
|
+
action: "erase" | "restrict";
|
|
41
|
+
ended: string[];
|
|
42
|
+
}
|
|
43
|
+
/** What erasing a person's data would involve. Nothing is changed by building a plan. */
|
|
44
|
+
export interface ErasurePlan {
|
|
45
|
+
subjectId: string;
|
|
46
|
+
reason: ErasureReason;
|
|
47
|
+
ended: string[];
|
|
48
|
+
remaining: string[];
|
|
49
|
+
tables: TablePlan[];
|
|
50
|
+
processors: ProcessorPlan[];
|
|
51
|
+
/**
|
|
52
|
+
* From `config.minimumRetention`. Rule 8(3): personal data, traffic data and logs of each
|
|
53
|
+
* processing must be kept for at least this long from the date of that processing, even when
|
|
54
|
+
* erasure is due (the Rule's own example: "even if X deletes her account"). So `erase` and
|
|
55
|
+
* `delete-rows` mean: stop using the data now, keep it only for the Seventh Schedule purposes
|
|
56
|
+
* (requests from the State), and erase it once this long has passed since its last processing.
|
|
57
|
+
*/
|
|
58
|
+
minimumRetention?: Duration;
|
|
59
|
+
}
|
|
60
|
+
export interface PlanInput {
|
|
61
|
+
subjectId: string;
|
|
62
|
+
reason: ErasureReason;
|
|
63
|
+
/** Purposes that have ended for this person. */
|
|
64
|
+
ended: readonly string[];
|
|
65
|
+
/** Purposes that still apply to this person. */
|
|
66
|
+
remaining: readonly string[];
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Work out, from the data map alone, which of a person's fields must be erased now that some
|
|
70
|
+
* purposes have ended. A field is erased only when none of its purposes remain.
|
|
71
|
+
*/
|
|
72
|
+
export declare function planErasure(config: DpdpConfig<string>, input: PlanInput): ErasurePlan;
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Issue } from "./audit.js";
|
|
2
|
+
export type DpdpErrorCode = "invalid-config" | "invalid-subject" | "unknown-purpose" | "not-a-consent-purpose" | "no-purposes" | "invalid-language" | "write-conflict";
|
|
3
|
+
/** Thrown for mistakes in how the library is called. Messages are for developers, not end users. */
|
|
4
|
+
export declare class DpdpError extends Error {
|
|
5
|
+
readonly code: DpdpErrorCode;
|
|
6
|
+
readonly issues?: readonly Issue[] | undefined;
|
|
7
|
+
readonly name = "DpdpError";
|
|
8
|
+
constructor(code: DpdpErrorCode, message: string, issues?: readonly Issue[] | undefined);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A store throws this when an event with the same (subjectId, seq) already exists. The ledger
|
|
12
|
+
* catches it and retries, so two writes for the same person never fork the chain.
|
|
13
|
+
*/
|
|
14
|
+
export declare class SeqConflictError extends Error {
|
|
15
|
+
readonly subjectId: string;
|
|
16
|
+
readonly seq: number;
|
|
17
|
+
readonly name = "SeqConflictError";
|
|
18
|
+
constructor(subjectId: string, seq: number);
|
|
19
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Dpdp } from "./dpdp.js";
|
|
2
|
+
import { type GuardOptions } from "./guard.js";
|
|
3
|
+
/** The parts of Express's response this middleware uses. */
|
|
4
|
+
interface ResponseLike {
|
|
5
|
+
status(code: number): {
|
|
6
|
+
json(body: unknown): unknown;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
type Next = (err?: unknown) => void;
|
|
10
|
+
export interface ExpressGuardOptions<Req, Res> extends GuardOptions<Req> {
|
|
11
|
+
/** Send your own response when consent is missing. Default: 403 with a JSON body. */
|
|
12
|
+
onDenied?: (req: Req, res: Res, purpose: string) => void;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Express middleware that lets the request through only if the person may be processed for
|
|
16
|
+
* `purpose`.
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* app.post("/promo", requireConsent(dpdp, "marketing_sms", { subject: (req) => req.user?.id }), handler);
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare function requireConsent<P extends string, Req = any, Res extends ResponseLike = any>(dpdp: Dpdp<P>, purpose: P, options: ExpressGuardOptions<Req, Res>): (req: Req, res: Res, next: Next) => void;
|
|
23
|
+
export {};
|
package/dist/express.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CONSENT_REQUIRED_BODY,
|
|
3
|
+
checkConsent
|
|
4
|
+
} from "./chunk-FGZAH2NF.js";
|
|
5
|
+
|
|
6
|
+
// src/express.ts
|
|
7
|
+
function requireConsent(dpdp, purpose, options) {
|
|
8
|
+
return (req, res, next) => {
|
|
9
|
+
checkConsent(dpdp, purpose, options, req).then(
|
|
10
|
+
(ok) => {
|
|
11
|
+
if (ok) return next();
|
|
12
|
+
if (options.onDenied) return options.onDenied(req, res, purpose);
|
|
13
|
+
res.status(403).json(CONSENT_REQUIRED_BODY(purpose));
|
|
14
|
+
},
|
|
15
|
+
(err) => next(err)
|
|
16
|
+
);
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export {
|
|
20
|
+
requireConsent
|
|
21
|
+
};
|
package/dist/guard.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Dpdp } from "./dpdp.js";
|
|
2
|
+
export interface GuardOptions<Ctx> {
|
|
3
|
+
/** Read the signed-in person's id from the request. Return nothing when nobody is signed in. */
|
|
4
|
+
subject: (ctx: Ctx) => string | null | undefined | Promise<string | null | undefined>;
|
|
5
|
+
}
|
|
6
|
+
/** Body sent when consent is missing. Safe to show to end users. */
|
|
7
|
+
export declare const CONSENT_REQUIRED_BODY: (purpose: string) => {
|
|
8
|
+
error: "consent_required";
|
|
9
|
+
purpose: string;
|
|
10
|
+
message: string;
|
|
11
|
+
};
|
|
12
|
+
/** Shared by the Express and Hono middleware. */
|
|
13
|
+
export declare function checkConsent<P extends string, Ctx>(dpdp: Dpdp<P>, purpose: P, options: GuardOptions<Ctx>, ctx: Ctx): Promise<boolean>;
|
package/dist/hash.d.ts
ADDED
package/dist/hono.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Dpdp } from "./dpdp.js";
|
|
2
|
+
import { type GuardOptions } from "./guard.js";
|
|
3
|
+
/** The parts of Hono's context this middleware uses. */
|
|
4
|
+
interface ContextLike {
|
|
5
|
+
json(body: unknown, status?: number): Response;
|
|
6
|
+
}
|
|
7
|
+
export interface HonoGuardOptions<C> extends GuardOptions<C> {
|
|
8
|
+
/** Return your own response when consent is missing. Default: 403 with a JSON body. */
|
|
9
|
+
onDenied?: (c: C, purpose: string) => Response | Promise<Response>;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Hono middleware that lets the request through only if the person may be processed for
|
|
13
|
+
* `purpose`.
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* app.post("/promo", requireConsent(dpdp, "marketing_sms", { subject: (c) => c.get("userId") }), handler);
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare function requireConsent<P extends string, C extends ContextLike = any>(dpdp: Dpdp<P>, purpose: P, options: HonoGuardOptions<C>): (c: C, next: () => Promise<void>) => Promise<Response | void>;
|
|
20
|
+
export {};
|
package/dist/hono.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CONSENT_REQUIRED_BODY,
|
|
3
|
+
checkConsent
|
|
4
|
+
} from "./chunk-FGZAH2NF.js";
|
|
5
|
+
|
|
6
|
+
// src/hono.ts
|
|
7
|
+
function requireConsent(dpdp, purpose, options) {
|
|
8
|
+
return async (c, next) => {
|
|
9
|
+
if (await checkConsent(dpdp, purpose, options, c)) return next();
|
|
10
|
+
if (options.onDenied) return options.onDenied(c, purpose);
|
|
11
|
+
return c.json(CONSENT_REQUIRED_BODY(purpose), 403);
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export {
|
|
15
|
+
requireConsent
|
|
16
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { audit, hasErrors, type Issue, type IssueLevel } from "./audit.js";
|
|
2
|
+
export { type Basis, type Category, type DpdpConfig, defineConfig, type FiduciaryDef, type LegalHold, type PersonalField, type ProcessorDef, type PurposeDef, type PurposeOf, purposeNames, type Retention, type TableDef, } from "./config.js";
|
|
3
|
+
export { type ConsentMeta, createDpdp, type Dpdp, type DpdpOptions, hashEvent, type VerifyResult, } from "./dpdp.js";
|
|
4
|
+
export { type Duration, durationMs, isDuration } from "./duration.js";
|
|
5
|
+
export { type ErasurePlan, type ErasureReason, type FieldPlan, type PlanInput, type ProcessorPlan, planErasure, type TablePlan, } from "./erasure.js";
|
|
6
|
+
export { DpdpError, type DpdpErrorCode, SeqConflictError } from "./errors.js";
|
|
7
|
+
export { CONSENT_REQUIRED_BODY, type GuardOptions } from "./guard.js";
|
|
8
|
+
export { isNoticeLanguage, NOTICE_LANGUAGES, type NoticeLanguage } from "./languages.js";
|
|
9
|
+
export { type ConsentAction, type ConsentEvent, type ConsentStore, memoryStore } from "./store.js";
|