@substrat-run/vertical-auth 0.35.2 → 0.36.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.
@@ -0,0 +1,56 @@
1
+ /** The table behind {@link sqlRefreshStore}, and the index its sweep reads. */
2
+ export const SESSION_REFRESH_DDL = [
3
+ `CREATE TABLE IF NOT EXISTS session_refresh (sid TEXT PRIMARY KEY, sub TEXT NOT NULL, refresh_token TEXT NOT NULL,
4
+ updated_at TEXT NOT NULL)`,
5
+ `CREATE INDEX IF NOT EXISTS session_refresh_updated_at ON session_refresh (updated_at)`,
6
+ ];
7
+ /**
8
+ * How long a session lives: `SESSION_MAXAGE` in `@substrat-run/oidc-rp`, restated rather than
9
+ * imported because that module's root pulls hono into the identity DO's bundle. A test holds the
10
+ * two equal.
11
+ */
12
+ export const SESSION_REFRESH_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000;
13
+ /** Rows a single sweep deletes at most. */
14
+ export const SESSION_REFRESH_SWEEP_BATCH = 32;
15
+ /**
16
+ * A {@link RefreshStore} over the directory's own SQL seam — the identity DO's storage, or a
17
+ * SQLite file on a dev server. The caller runs {@link SESSION_REFRESH_DDL} first. `updated_at` is
18
+ * when the token was last replaced.
19
+ *
20
+ * Every `put` first deletes up to `sweepBatch` rows whose `updated_at` is older than the session
21
+ * lifetime. A session is minted once, at the sign-in that wrote its row, and never extended, so
22
+ * such a row's session has expired and no cookie can name it any more. Each sign-in adds at most
23
+ * one row and sweeps up to a batch, so stale rows cannot pile up while people keep signing in;
24
+ * the cost is one indexed range delete. The refresh token is NOT revoked at the issuer when its
25
+ * row is swept: the RP has no revocation call. It stays valid there until the issuer expires it.
26
+ */
27
+ export function sqlRefreshStore(sql, options = {}) {
28
+ const now = options.now ?? (() => new Date());
29
+ const maxAgeMs = options.maxAgeMs ?? SESSION_REFRESH_MAX_AGE_MS;
30
+ const batch = options.sweepBatch ?? SESSION_REFRESH_SWEEP_BATCH;
31
+ return {
32
+ async put(sid, entry) {
33
+ const at = now();
34
+ sql.exec(`DELETE FROM session_refresh WHERE sid IN
35
+ (SELECT sid FROM session_refresh WHERE updated_at < ? ORDER BY updated_at LIMIT ?)`, new Date(at.getTime() - maxAgeMs).toISOString(), batch);
36
+ sql.exec(`INSERT INTO session_refresh (sid, sub, refresh_token, updated_at) VALUES (?, ?, ?, ?)
37
+ ON CONFLICT(sid) DO UPDATE SET sub = excluded.sub, refresh_token = excluded.refresh_token, updated_at = excluded.updated_at`, sid, entry.sub, entry.refreshToken, at.toISOString());
38
+ },
39
+ async get(sid) {
40
+ const row = [...sql.exec('SELECT sub, refresh_token FROM session_refresh WHERE sid = ?', sid)][0];
41
+ return row ? { sub: row.sub, refreshToken: row.refresh_token } : null;
42
+ },
43
+ async delete(sid) {
44
+ sql.exec('DELETE FROM session_refresh WHERE sid = ?', sid);
45
+ },
46
+ };
47
+ }
48
+ /** A {@link RefreshStore} over the tenant's identity DO, for a hosted worker. */
49
+ export function identityRefreshStore(directory) {
50
+ return {
51
+ put: (sid, entry) => directory.putSessionRefresh(sid, entry),
52
+ get: (sid) => directory.sessionRefresh(sid),
53
+ delete: (sid) => directory.deleteSessionRefresh(sid),
54
+ };
55
+ }
56
+ //# sourceMappingURL=session-refresh.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session-refresh.js","sourceRoot":"","sources":["../src/session-refresh.ts"],"names":[],"mappings":"AA0BA,+EAA+E;AAC/E,MAAM,CAAC,MAAM,mBAAmB,GAAsB;IACpD;8BAC4B;IAC5B,uFAAuF;CACxF,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAElE,2CAA2C;AAC3C,MAAM,CAAC,MAAM,2BAA2B,GAAG,EAAE,CAAC;AAW9C;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,eAAe,CAAC,GAAgB,EAAE,OAAO,GAA2B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,EAAE,KAAK,CAAC;QAC5D,GAAG,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,SAAS,CAAC,cAAc,CAAC,GAAG,CAAC;QAC3C,MAAM,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,SAAS,CAAC,oBAAoB,CAAC,GAAG,CAAC;KACrD,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@substrat-run/vertical-auth",
3
- "version": "0.35.2",
3
+ "version": "0.36.0",
4
4
  "description": "OIDC authentication for Substrat verticals — token verification, a full relying-party login flow, and a per-tenant identity directory Durable Object. Workers-targeted; the vertical worker bundles it.",
5
5
  "license": "AGPL-3.0-only",
6
6
  "repository": {
@@ -51,6 +51,14 @@
51
51
  "./people": {
52
52
  "types": "./dist/people.d.ts",
53
53
  "default": "./dist/people.js"
54
+ },
55
+ "./delegate": {
56
+ "types": "./dist/delegate.d.ts",
57
+ "default": "./dist/delegate.js"
58
+ },
59
+ "./session-refresh": {
60
+ "types": "./dist/session-refresh.d.ts",
61
+ "default": "./dist/session-refresh.js"
54
62
  }
55
63
  },
56
64
  "files": [
@@ -58,10 +66,10 @@
58
66
  ],
59
67
  "dependencies": {
60
68
  "jose": "^6.2.3",
61
- "@substrat-run/contracts": "^0.197.1",
62
- "@substrat-run/kernel": "^0.197.1",
63
- "@substrat-run/oidc-rp": "^0.13.0",
64
- "@substrat-run/psl": "^0.2.5"
69
+ "@substrat-run/contracts": "^0.198.0",
70
+ "@substrat-run/oidc-rp": "^0.14.0",
71
+ "@substrat-run/psl": "^0.2.5",
72
+ "@substrat-run/kernel": "^0.198.0"
65
73
  },
66
74
  "peerDependencies": {
67
75
  "hono": "^4.13.13"