@sharadox/account-web-library 0.1.0-code-request.8 → 0.1.0-development.12
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 +17 -0
- package/distribution/account-database/type.d.ts +21 -0
- package/distribution/account-database/type.d.ts.map +1 -1
- package/distribution/account-migration/index.d.ts.map +1 -1
- package/distribution/account-migration/index.js +24 -0
- package/distribution/account-migration/index.js.map +1 -1
- package/distribution/account-session-revocation/index.d.ts +14 -0
- package/distribution/account-session-revocation/index.d.ts.map +1 -0
- package/distribution/account-session-revocation/index.js +25 -0
- package/distribution/account-session-revocation/index.js.map +1 -0
- package/distribution/account-sign-in/index.d.ts +15 -0
- package/distribution/account-sign-in/index.d.ts.map +1 -0
- package/distribution/account-sign-in/index.js +76 -0
- package/distribution/account-sign-in/index.js.map +1 -0
- package/distribution/account-sign-in/type.d.ts +20 -0
- package/distribution/account-sign-in/type.d.ts.map +1 -0
- package/distribution/account-sign-in/type.js +2 -0
- package/distribution/account-sign-in/type.js.map +1 -0
- package/distribution/account-sign-out/index.d.ts +16 -0
- package/distribution/account-sign-out/index.d.ts.map +1 -0
- package/distribution/account-sign-out/index.js +34 -0
- package/distribution/account-sign-out/index.js.map +1 -0
- package/distribution/browser-session-read/index.d.ts +17 -0
- package/distribution/browser-session-read/index.d.ts.map +1 -0
- package/distribution/browser-session-read/index.js +48 -0
- package/distribution/browser-session-read/index.js.map +1 -0
- package/distribution/browser-session-read/type.d.ts +14 -0
- package/distribution/browser-session-read/type.d.ts.map +1 -0
- package/distribution/browser-session-read/type.js +2 -0
- package/distribution/browser-session-read/type.js.map +1 -0
- package/distribution/session-prune/index.d.ts +17 -0
- package/distribution/session-prune/index.d.ts.map +1 -0
- package/distribution/session-prune/index.js +30 -0
- package/distribution/session-prune/index.js.map +1 -0
- package/distribution/session-token/index.d.ts +19 -0
- package/distribution/session-token/index.d.ts.map +1 -0
- package/distribution/session-token/index.js +22 -0
- package/distribution/session-token/index.js.map +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -151,6 +151,23 @@ pass the limit; after the last attempt even the right code is refused.
|
|
|
151
151
|
|
|
152
152
|
### `code-request-prune`: `pruneCodeRequests(database, { before })` → deleted count
|
|
153
153
|
|
|
154
|
+
### Sessions and the account selector
|
|
155
|
+
|
|
156
|
+
A browser holds one cookie with an opaque token (only its SHA-256 is stored). On the server, that browser session
|
|
157
|
+
points to any number of account sessions, so the selector has no limit and the cookie never grows. Use a
|
|
158
|
+
`__Host-` cookie: HttpOnly, Secure, SameSite=Lax, path `/`.
|
|
159
|
+
|
|
160
|
+
- `signIn(database, { accountId, token?, secondFactor?, accountSessionSeconds = 30 days, browserSessionSeconds = 400 days, now? })`
|
|
161
|
+
→ `{ token, browserSessionId, accountSessionId }`. Adds the account to the browser's accounts (or starts a
|
|
162
|
+
browser session). The browser gets a **new token at every sign-in**: set it as the cookie.
|
|
163
|
+
- `readBrowserSession(database, { token, idleSeconds = 7 days, now? })` → `{ browserSessionId, accounts }` or
|
|
164
|
+
undefined. `accounts` are `{ accountSessionId, accountId, username, signedInAt, secondFactorAt }`, in sign-in
|
|
165
|
+
order, without ended or idle sessions. It marks them as seen.
|
|
166
|
+
- `signOut(database, { token, accountId? })` → `{ signedOut }`: one account, or all and the browser session.
|
|
167
|
+
- `revokeAccountSessions(database, { accountId, actorAccountId? })` → count: signed out on every browser.
|
|
168
|
+
- `pruneSessions(database, { idleSeconds?, now? })` → `{ accountSessions, browserSessions }`, for a daily job.
|
|
169
|
+
- `createSessionToken()`, `hashSessionToken(token)` (`session-token`).
|
|
170
|
+
|
|
154
171
|
### `contact-kind`: `normalizeContact({ kind, value })`, `CONTACT_KINDS`
|
|
155
172
|
|
|
156
173
|
`email` in this version; unknown kinds and invalid values throw.
|
|
@@ -61,6 +61,25 @@ export interface CodeRequestTable {
|
|
|
61
61
|
/** When a newer request for the same contact and purpose replaced it. */
|
|
62
62
|
voided_at: Date | null;
|
|
63
63
|
}
|
|
64
|
+
/** One browser (the account site's cookie holds its token; only the token's hash is stored). */
|
|
65
|
+
export interface BrowserSessionTable {
|
|
66
|
+
id: string;
|
|
67
|
+
token_hash: string;
|
|
68
|
+
created_at: Date;
|
|
69
|
+
last_seen_at: Date;
|
|
70
|
+
expires_at: Date;
|
|
71
|
+
}
|
|
72
|
+
/** One account signed in on one browser: a browser can hold any number of them (the account selector). */
|
|
73
|
+
export interface AccountSessionTable {
|
|
74
|
+
id: string;
|
|
75
|
+
browser_session_id: string;
|
|
76
|
+
account_id: string;
|
|
77
|
+
created_at: Date;
|
|
78
|
+
last_seen_at: Date;
|
|
79
|
+
expires_at: Date;
|
|
80
|
+
/** When two-factor was last passed in this session; null without it. */
|
|
81
|
+
second_factor_at: Date | null;
|
|
82
|
+
}
|
|
64
83
|
/** The tables `migrateAccount` creates, for typing a Kysely instance. */
|
|
65
84
|
export interface AccountDatabase {
|
|
66
85
|
application: ApplicationTable;
|
|
@@ -68,6 +87,8 @@ export interface AccountDatabase {
|
|
|
68
87
|
contact: ContactTable;
|
|
69
88
|
audit_event: AuditEventTable;
|
|
70
89
|
code_request: CodeRequestTable;
|
|
90
|
+
browser_session: BrowserSessionTable;
|
|
91
|
+
account_session: AccountSessionTable;
|
|
71
92
|
}
|
|
72
93
|
export {};
|
|
73
94
|
//# sourceMappingURL=type.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/account-database/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AAEpD,wDAAwD;AACxD,KAAK,WAAW,GAAG,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,SAAS,EAAE,IAAI,CAAC,CAAC;AAE5D,sFAAsF;AACtF,MAAM,WAAW,gBAAgB;IAC/B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,WAA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,MAAM,WAAW,mBAAmB;IAClC,EAAE,EAAE,MAAM,CAAC;IACX,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,IAAI,CAAC;IACjB,YAAY,EAAE,IAAI,CAAC;IACnB,UAAU,EAAE,IAAI,CAAC;CAClB;AAED,0GAA0G;AAC1G,MAAM,WAAW,mBAAmB;IAClC,EAAE,EAAE,MAAM,CAAC;IACX,kBAAkB,EAAE,MAAM,CAAC;IAC3B,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,IAAI,CAAC;IACjB,YAAY,EAAE,IAAI,CAAC;IACnB,UAAU,EAAE,IAAI,CAAC;IACjB,wEAAwE;IACxE,gBAAgB,EAAE,IAAI,GAAG,IAAI,CAAC;CAC/B;AAED,yEAAyE;AACzE,MAAM,WAAW,eAAe;IAC9B,WAAW,EAAE,gBAAgB,CAAC;IAC9B,OAAO,EAAE,YAAY,CAAC;IACtB,OAAO,EAAE,YAAY,CAAC;IACtB,WAAW,EAAE,eAAe,CAAC;IAC7B,YAAY,EAAE,gBAAgB,CAAC;IAC/B,eAAe,EAAE,mBAAmB,CAAC;IACrC,eAAe,EAAE,mBAAmB,CAAC;CACtC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-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,iHAAiH;AACjH,eAAO,MAAM,uBAAuB,sBAAsB,CAAC;AAE3D;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,EAAE,SAAS,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-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,iHAAiH;AACjH,eAAO,MAAM,uBAAuB,sBAAsB,CAAC;AAE3D;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,EAAE,SAAS,EA+HxC,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAElF"}
|
|
@@ -109,6 +109,30 @@ export const accountMigrations = [
|
|
|
109
109
|
create index code_request_expires_at on code_request (expires_at);
|
|
110
110
|
`,
|
|
111
111
|
},
|
|
112
|
+
{
|
|
113
|
+
name: "0003-session",
|
|
114
|
+
sql: `
|
|
115
|
+
create table browser_session (
|
|
116
|
+
id uuid primary key check (uuid_extract_version(id) = 7),
|
|
117
|
+
token_hash text not null unique,
|
|
118
|
+
created_at timestamptz not null,
|
|
119
|
+
last_seen_at timestamptz not null,
|
|
120
|
+
expires_at timestamptz not null
|
|
121
|
+
);
|
|
122
|
+
create index browser_session_expires_at on browser_session (expires_at);
|
|
123
|
+
create table account_session (
|
|
124
|
+
id uuid primary key check (uuid_extract_version(id) = 7),
|
|
125
|
+
browser_session_id uuid not null references browser_session (id) on delete cascade,
|
|
126
|
+
account_id uuid not null references account (id) on delete cascade,
|
|
127
|
+
created_at timestamptz not null,
|
|
128
|
+
last_seen_at timestamptz not null,
|
|
129
|
+
expires_at timestamptz not null,
|
|
130
|
+
second_factor_at timestamptz,
|
|
131
|
+
unique (browser_session_id, account_id)
|
|
132
|
+
);
|
|
133
|
+
create index account_session_account on account_session (account_id);
|
|
134
|
+
`,
|
|
135
|
+
},
|
|
112
136
|
];
|
|
113
137
|
/**
|
|
114
138
|
* Creates or updates the account tables, tracked in their own migration table. Returns the names applied.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-migration/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAA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AE,iBAAiB,EAAE,EAAE,KAAK,EAAE,uBAAuB,EAAE,CAAC,CAAC;AAClF,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Kysely } from "kysely";
|
|
2
|
+
import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
|
|
3
|
+
/**
|
|
4
|
+
* Signs an account out on every browser at once: after a ban, a lost device, a recovery, or when its owner asks.
|
|
5
|
+
* Records who did it. Returns how many sessions ended.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* await revokeAccountSessions(database, { accountId, actorAccountId: moderatorId });
|
|
9
|
+
*/
|
|
10
|
+
export declare function revokeAccountSessions(database: Kysely<AccountDatabase>, { accountId, actorAccountId }: {
|
|
11
|
+
accountId: string;
|
|
12
|
+
actorAccountId?: string | null;
|
|
13
|
+
}): Promise<number>;
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-session-revocation/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAG3F;;;;;;GAMG;AACH,wBAAsB,qBAAqB,CACzC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,SAAS,EAAE,cAAqB,EAAE,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,cAAc,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,GAC1F,OAAO,CAAC,MAAM,CAAC,CAejB"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
|
|
2
|
+
/**
|
|
3
|
+
* Signs an account out on every browser at once: after a ban, a lost device, a recovery, or when its owner asks.
|
|
4
|
+
* Records who did it. Returns how many sessions ended.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* await revokeAccountSessions(database, { accountId, actorAccountId: moderatorId });
|
|
8
|
+
*/
|
|
9
|
+
export async function revokeAccountSessions(database, { accountId, actorAccountId = null }) {
|
|
10
|
+
return database.transaction().execute(async (transaction) => {
|
|
11
|
+
const removed = await transaction
|
|
12
|
+
.deleteFrom("account_session")
|
|
13
|
+
.where("account_id", "=", accountId)
|
|
14
|
+
.returning("id")
|
|
15
|
+
.execute();
|
|
16
|
+
await recordAuditEvent(transaction, {
|
|
17
|
+
accountId,
|
|
18
|
+
actorAccountId,
|
|
19
|
+
action: "session.revoked",
|
|
20
|
+
detail: { sessions: removed.length },
|
|
21
|
+
});
|
|
22
|
+
return removed.length;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-session-revocation/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAE7E;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CACzC,QAAiC,EACjC,EAAE,SAAS,EAAE,cAAc,GAAG,IAAI,EAAyD;IAE3F,OAAO,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,EAAE,EAAE;QAC1D,MAAM,OAAO,GAAG,MAAM,WAAW;aAC9B,UAAU,CAAC,iBAAiB,CAAC;aAC7B,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,SAAS,CAAC;aACnC,SAAS,CAAC,IAAI,CAAC;aACf,OAAO,EAAE,CAAC;QACb,MAAM,gBAAgB,CAAC,WAAW,EAAE;YAClC,SAAS;YACT,cAAc;YACd,MAAM,EAAE,iBAAiB;YACzB,MAAM,EAAE,EAAE,QAAQ,EAAE,OAAO,CAAC,MAAM,EAAE;SACrC,CAAC,CAAC;QACH,OAAO,OAAO,CAAC,MAAM,CAAC;IACxB,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Kysely } from "kysely";
|
|
2
|
+
import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
|
|
3
|
+
import type { SignInOptions, SignInResult } from "@sharadox/account-web-library/account-sign-in/type";
|
|
4
|
+
/**
|
|
5
|
+
* Signs an account in on a browser, after its code (and two-factor, when it has it) passed: the account joins the
|
|
6
|
+
* accounts already signed in there (the account selector has no limit), or a new browser session starts. The browser
|
|
7
|
+
* gets a new token every time, so a token planted before sign-in is worthless. Signing in again with the same account
|
|
8
|
+
* replaces its session there.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const { token } = await signIn(database, { accountId, token: cookies.get("__Host-account")?.value });
|
|
12
|
+
* cookies.set("__Host-account", token, { httpOnly: true, secure: true, sameSite: "lax", path: "/" });
|
|
13
|
+
*/
|
|
14
|
+
export declare function signIn(database: Kysely<AccountDatabase>, { accountId, token, secondFactor, accountSessionSeconds, browserSessionSeconds, now, }: SignInOptions): Promise<SignInResult>;
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-sign-in/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAC3F,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,oDAAoD,CAAC;AAMtG;;;;;;;;;GASG;AACH,wBAAsB,MAAM,CAC1B,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EACE,SAAS,EACT,KAAK,EACL,YAAoB,EACpB,qBAAgC,EAChC,qBAAiC,EACjC,GAAgB,GACjB,EAAE,aAAa,GACf,OAAO,CAAC,YAAY,CAAC,CA2DvB"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { uuidVersion7 } from "@sharadox/web-library/identifier/uuid-version-7";
|
|
2
|
+
import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
|
|
3
|
+
import { createSessionToken, hashSessionToken } from "@sharadox/account-web-library/session-token";
|
|
4
|
+
const DAY = 86_400;
|
|
5
|
+
/**
|
|
6
|
+
* Signs an account in on a browser, after its code (and two-factor, when it has it) passed: the account joins the
|
|
7
|
+
* accounts already signed in there (the account selector has no limit), or a new browser session starts. The browser
|
|
8
|
+
* gets a new token every time, so a token planted before sign-in is worthless. Signing in again with the same account
|
|
9
|
+
* replaces its session there.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const { token } = await signIn(database, { accountId, token: cookies.get("__Host-account")?.value });
|
|
13
|
+
* cookies.set("__Host-account", token, { httpOnly: true, secure: true, sameSite: "lax", path: "/" });
|
|
14
|
+
*/
|
|
15
|
+
export async function signIn(database, { accountId, token, secondFactor = false, accountSessionSeconds = 30 * DAY, browserSessionSeconds = 400 * DAY, now = Date.now(), }) {
|
|
16
|
+
const at = new Date(now);
|
|
17
|
+
const fresh = createSessionToken();
|
|
18
|
+
return database.transaction().execute(async (transaction) => {
|
|
19
|
+
const browser = token
|
|
20
|
+
? await transaction
|
|
21
|
+
.selectFrom("browser_session")
|
|
22
|
+
.select("id")
|
|
23
|
+
.where("token_hash", "=", hashSessionToken(token))
|
|
24
|
+
.where("expires_at", ">", at)
|
|
25
|
+
.forUpdate()
|
|
26
|
+
.executeTakeFirst()
|
|
27
|
+
: undefined;
|
|
28
|
+
const browserSessionId = browser?.id ?? uuidVersion7({ now });
|
|
29
|
+
if (browser) {
|
|
30
|
+
await transaction
|
|
31
|
+
.updateTable("browser_session")
|
|
32
|
+
.set({ token_hash: fresh.hash, last_seen_at: at })
|
|
33
|
+
.where("id", "=", browserSessionId)
|
|
34
|
+
.execute();
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
await transaction
|
|
38
|
+
.insertInto("browser_session")
|
|
39
|
+
.values({
|
|
40
|
+
id: browserSessionId,
|
|
41
|
+
token_hash: fresh.hash,
|
|
42
|
+
created_at: at,
|
|
43
|
+
last_seen_at: at,
|
|
44
|
+
expires_at: new Date(now + browserSessionSeconds * 1000),
|
|
45
|
+
})
|
|
46
|
+
.execute();
|
|
47
|
+
}
|
|
48
|
+
await transaction
|
|
49
|
+
.deleteFrom("account_session")
|
|
50
|
+
.where("browser_session_id", "=", browserSessionId)
|
|
51
|
+
.where("account_id", "=", accountId)
|
|
52
|
+
.execute();
|
|
53
|
+
const accountSessionId = uuidVersion7({ now });
|
|
54
|
+
await transaction
|
|
55
|
+
.insertInto("account_session")
|
|
56
|
+
.values({
|
|
57
|
+
id: accountSessionId,
|
|
58
|
+
browser_session_id: browserSessionId,
|
|
59
|
+
account_id: accountId,
|
|
60
|
+
created_at: at,
|
|
61
|
+
last_seen_at: at,
|
|
62
|
+
expires_at: new Date(now + accountSessionSeconds * 1000),
|
|
63
|
+
second_factor_at: secondFactor ? at : null,
|
|
64
|
+
})
|
|
65
|
+
.execute();
|
|
66
|
+
await transaction.updateTable("account").set({ last_active_at: at }).where("id", "=", accountId).execute();
|
|
67
|
+
await recordAuditEvent(transaction, {
|
|
68
|
+
accountId,
|
|
69
|
+
action: "session.signed-in",
|
|
70
|
+
detail: { accountSessionId, secondFactor },
|
|
71
|
+
now,
|
|
72
|
+
});
|
|
73
|
+
return { token: fresh.token, browserSessionId, accountSessionId };
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-sign-in/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,iDAAiD,CAAC;AAG/E,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAC7E,OAAO,EAA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AY,EAAE;YAC1C,GAAG;SACJ,CAAC,CAAC;QACH,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,CAAC;IACpE,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface SignInOptions {
|
|
2
|
+
accountId: string;
|
|
3
|
+
/** The browser's current token (its cookie), if it has one: the account joins that browser's accounts. */
|
|
4
|
+
token?: string;
|
|
5
|
+
/** Whether two-factor was passed for this sign-in. */
|
|
6
|
+
secondFactor?: boolean;
|
|
7
|
+
/** 30 days by default. */
|
|
8
|
+
accountSessionSeconds?: number;
|
|
9
|
+
/** 400 days by default (the longest a browser keeps a cookie). */
|
|
10
|
+
browserSessionSeconds?: number;
|
|
11
|
+
/** Milliseconds since 1970 (now by default). */
|
|
12
|
+
now?: number;
|
|
13
|
+
}
|
|
14
|
+
export interface SignInResult {
|
|
15
|
+
/** The browser's new token: set it as the cookie (it replaces the old one at every sign-in). */
|
|
16
|
+
token: string;
|
|
17
|
+
browserSessionId: string;
|
|
18
|
+
accountSessionId: string;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/account-sign-in/type.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAC5B,SAAS,EAAE,MAAM,CAAC;IAClB,0GAA0G;IAC1G,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,sDAAsD;IACtD,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,0BAA0B;IAC1B,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B,kEAAkE;IAClE,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,YAAY;IAC3B,gGAAgG;IAChG,KAAK,EAAE,MAAM,CAAC;IACd,gBAAgB,EAAE,MAAM,CAAC;IACzB,gBAAgB,EAAE,MAAM,CAAC;CAC1B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/account-sign-in/type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Kysely } from "kysely";
|
|
2
|
+
import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
|
|
3
|
+
/**
|
|
4
|
+
* Signs one account out of a browser (`accountId`), or every account and the browser session with it. Returns the
|
|
5
|
+
* accounts signed out; tell their applications (back-channel logout) so they end their own sessions too.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* await signOut(database, { token: cookies.get("__Host-account")?.value, accountId });
|
|
9
|
+
*/
|
|
10
|
+
export declare function signOut(database: Kysely<AccountDatabase>, { token, accountId }: {
|
|
11
|
+
token: string | undefined;
|
|
12
|
+
accountId?: string;
|
|
13
|
+
}): Promise<{
|
|
14
|
+
signedOut: string[];
|
|
15
|
+
}>;
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/account-sign-out/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAI3F;;;;;;GAMG;AACH,wBAAsB,OAAO,CAC3B,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,KAAK,EAAE,SAAS,EAAE,EAAE;IAAE,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE,GACtE,OAAO,CAAC;IAAE,SAAS,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC,CAmBlC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { recordAuditEvent } from "@sharadox/account-web-library/audit-event";
|
|
2
|
+
import { hashSessionToken } from "@sharadox/account-web-library/session-token";
|
|
3
|
+
/**
|
|
4
|
+
* Signs one account out of a browser (`accountId`), or every account and the browser session with it. Returns the
|
|
5
|
+
* accounts signed out; tell their applications (back-channel logout) so they end their own sessions too.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* await signOut(database, { token: cookies.get("__Host-account")?.value, accountId });
|
|
9
|
+
*/
|
|
10
|
+
export async function signOut(database, { token, accountId }) {
|
|
11
|
+
if (!token)
|
|
12
|
+
return { signedOut: [] };
|
|
13
|
+
return database.transaction().execute(async (transaction) => {
|
|
14
|
+
const browser = await transaction
|
|
15
|
+
.selectFrom("browser_session")
|
|
16
|
+
.select("id")
|
|
17
|
+
.where("token_hash", "=", hashSessionToken(token))
|
|
18
|
+
.forUpdate()
|
|
19
|
+
.executeTakeFirst();
|
|
20
|
+
if (!browser)
|
|
21
|
+
return { signedOut: [] };
|
|
22
|
+
let removal = transaction.deleteFrom("account_session").where("browser_session_id", "=", browser.id);
|
|
23
|
+
if (accountId)
|
|
24
|
+
removal = removal.where("account_id", "=", accountId);
|
|
25
|
+
const removed = await removal.returning("account_id").execute();
|
|
26
|
+
if (!accountId)
|
|
27
|
+
await transaction.deleteFrom("browser_session").where("id", "=", browser.id).execute();
|
|
28
|
+
for (const { account_id } of removed) {
|
|
29
|
+
await recordAuditEvent(transaction, { accountId: account_id, action: "session.signed-out" });
|
|
30
|
+
}
|
|
31
|
+
return { signedOut: removed.map(({ account_id }) => account_id) };
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/account-sign-out/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAC7E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M,EAAE,oBAAoB,EAAE,CAAC,CAAC;QAC/F,CAAC;QACD,OAAO,EAAE,SAAS,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC,UAAU,CAAC,EAAE,CAAC;IACpE,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Kysely } from "kysely";
|
|
2
|
+
import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
|
|
3
|
+
import type { BrowserSession } from "@sharadox/account-web-library/browser-session-read/type";
|
|
4
|
+
/**
|
|
5
|
+
* The accounts signed in on a browser, from its cookie's token, leaving out sessions past their end or idle longer
|
|
6
|
+
* than `idleSeconds` (7 days by default); marks the browser and those sessions as seen now. Undefined for a missing,
|
|
7
|
+
* unknown or expired token.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const session = await readBrowserSession(database, { token: cookies.get("__Host-account")?.value });
|
|
11
|
+
*/
|
|
12
|
+
export declare function readBrowserSession(database: Kysely<AccountDatabase>, { token, idleSeconds, now, }: {
|
|
13
|
+
token: string | undefined;
|
|
14
|
+
idleSeconds?: number;
|
|
15
|
+
now?: number;
|
|
16
|
+
}): Promise<BrowserSession | undefined>;
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/browser-session-read/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAC3F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yDAAyD,CAAC;AAG9F;;;;;;;GAOG;AACH,wBAAsB,kBAAkB,CACtC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EACE,KAAK,EACL,WAAwB,EACxB,GAAgB,GACjB,EAAE;IAAE,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAA;CAAE,GACnE,OAAO,CAAC,cAAc,GAAG,SAAS,CAAC,CAuCrC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { hashSessionToken } from "@sharadox/account-web-library/session-token";
|
|
2
|
+
/**
|
|
3
|
+
* The accounts signed in on a browser, from its cookie's token, leaving out sessions past their end or idle longer
|
|
4
|
+
* than `idleSeconds` (7 days by default); marks the browser and those sessions as seen now. Undefined for a missing,
|
|
5
|
+
* unknown or expired token.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const session = await readBrowserSession(database, { token: cookies.get("__Host-account")?.value });
|
|
9
|
+
*/
|
|
10
|
+
export async function readBrowserSession(database, { token, idleSeconds = 7 * 86_400, now = Date.now(), }) {
|
|
11
|
+
if (!token)
|
|
12
|
+
return undefined;
|
|
13
|
+
const at = new Date(now);
|
|
14
|
+
const browser = await database
|
|
15
|
+
.selectFrom("browser_session")
|
|
16
|
+
.select("id")
|
|
17
|
+
.where("token_hash", "=", hashSessionToken(token))
|
|
18
|
+
.where("expires_at", ">", at)
|
|
19
|
+
.executeTakeFirst();
|
|
20
|
+
if (!browser)
|
|
21
|
+
return undefined;
|
|
22
|
+
const rows = await database
|
|
23
|
+
.selectFrom("account_session")
|
|
24
|
+
.innerJoin("account", "account.id", "account_session.account_id")
|
|
25
|
+
.select([
|
|
26
|
+
"account_session.id as accountSessionId",
|
|
27
|
+
"account.id as accountId",
|
|
28
|
+
"account.username as username",
|
|
29
|
+
"account_session.created_at as signedInAt",
|
|
30
|
+
"account_session.second_factor_at as secondFactorAt",
|
|
31
|
+
])
|
|
32
|
+
.where("account_session.browser_session_id", "=", browser.id)
|
|
33
|
+
.where("account_session.expires_at", ">", at)
|
|
34
|
+
.where("account_session.last_seen_at", ">", new Date(now - idleSeconds * 1000))
|
|
35
|
+
.orderBy("account_session.created_at")
|
|
36
|
+
.orderBy("account_session.id")
|
|
37
|
+
.execute();
|
|
38
|
+
await database.updateTable("browser_session").set({ last_seen_at: at }).where("id", "=", browser.id).execute();
|
|
39
|
+
if (rows.length > 0) {
|
|
40
|
+
await database
|
|
41
|
+
.updateTable("account_session")
|
|
42
|
+
.set({ last_seen_at: at })
|
|
43
|
+
.where("id", "in", rows.map(({ accountSessionId }) => accountSessionId))
|
|
44
|
+
.execute();
|
|
45
|
+
}
|
|
46
|
+
return { browserSessionId: browser.id, accounts: rows };
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/browser-session-read/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,gBAAgB,EAAE,MAAM,6CAA6C,CAAC;AAE/E;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,QAAiC,EACjC,EACE,KAAK,EACL,WAAW,GAAG,CAAC,GAAG,MAAM,EACxB,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,GACkD;IAEpE,IAAI,CAAC,KAAK;QAAE,OAAO,SAAS,CAAC;IAC7B,MAAM,EAAE,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC;IACzB,MAAM,OAAO,GAAG,MAAM,QAAQ;SAC3B,UAAU,CAAC,iBAAiB,CAAC;SAC7B,MAAM,CAAC,IAAI,CAAC;SACZ,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,gBAAgB,CAAC,KAAK,CAAC,CAAC;SACjD,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,EAAE,CAAC;SAC5B,gBAAgB,EAAE,CAAC;IACtB,IAAI,CAAC,OAAO;QAAE,OAAO,SAAS,CAAC;IAC/B,MAAM,IAAI,GAAG,MAAM,QAAQ;SACxB,UAAU,CAAC,iBAAiB,CAAC;SAC7B,SAAS,CAAC,SAAS,EAAE,YAAY,EAAE,4BAA4B,CAAC;SAChE,MAAM,CAAC;QACN,wCAAwC;QACxC,yBAAyB;QACzB,8BAA8B;QAC9B,0CAA0C;QAC1C,oDAAoD;KACrD,CAAC;SACD,KAAK,CAAC,oCAAoC,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,CAAC;SAC5D,KAAK,CAAC,4BAA4B,EAAE,GAAG,EAAE,EAAE,CAAC;SAC5C,KAAK,CAAC,8BAA8B,EAAE,GAAG,EAAE,IAAI,IAAI,CAAC,GAAG,GAAG,WAAW,GAAG,IAAI,CAAC,CAAC;SAC9E,OAAO,CAAC,4BAA4B,CAAC;SACrC,OAAO,CAAC,oBAAoB,CAAC;SAC7B,OAAO,EAAE,CAAC;IACb,MAAM,QAAQ,CAAC,WAAW,CAAC,iBAAiB,CAAC,CAAC,GAAG,CAAC,EAAE,YAAY,EAAE,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC;IAC/G,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACpB,MAAM,QAAQ;aACX,WAAW,CAAC,iBAAiB,CAAC;aAC9B,GAAG,CAAC,EAAE,YAAY,EAAE,EAAE,EAAE,CAAC;aACzB,KAAK,CACJ,IAAI,EACJ,IAAI,EACJ,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,gBAAgB,EAAE,EAAE,EAAE,CAAC,gBAAgB,CAAC,CACrD;aACA,OAAO,EAAE,CAAC;IACf,CAAC;IACD,OAAO,EAAE,gBAAgB,EAAE,OAAO,CAAC,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;AAC1D,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** An account signed in on the browser, as the account selector shows it. */
|
|
2
|
+
export interface SignedInAccount {
|
|
3
|
+
accountSessionId: string;
|
|
4
|
+
accountId: string;
|
|
5
|
+
username: string;
|
|
6
|
+
signedInAt: Date;
|
|
7
|
+
secondFactorAt: Date | null;
|
|
8
|
+
}
|
|
9
|
+
export interface BrowserSession {
|
|
10
|
+
browserSessionId: string;
|
|
11
|
+
/** In the order they signed in. */
|
|
12
|
+
accounts: SignedInAccount[];
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/browser-session-read/type.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,MAAM,WAAW,eAAe;IAC9B,gBAAgB,EAAE,MAAM,CAAC;IACzB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,IAAI,CAAC;IACjB,cAAc,EAAE,IAAI,GAAG,IAAI,CAAC;CAC7B;AAED,MAAM,WAAW,cAAc;IAC7B,gBAAgB,EAAE,MAAM,CAAC;IACzB,mCAAmC;IACnC,QAAQ,EAAE,eAAe,EAAE,CAAC;CAC7B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/browser-session-read/type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type Kysely } from "kysely";
|
|
2
|
+
import type { AccountDatabase } from "@sharadox/account-web-library/account-database/type";
|
|
3
|
+
/**
|
|
4
|
+
* Deletes account sessions past their end or idle longer than `idleSeconds` (7 days by default, as
|
|
5
|
+
* `readBrowserSession`), then browser sessions that ended or hold no account; from a daily job. Returns the counts.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* await pruneSessions(database);
|
|
9
|
+
*/
|
|
10
|
+
export declare function pruneSessions(database: Kysely<AccountDatabase>, { idleSeconds, now }?: {
|
|
11
|
+
idleSeconds?: number;
|
|
12
|
+
now?: number;
|
|
13
|
+
}): Promise<{
|
|
14
|
+
accountSessions: number;
|
|
15
|
+
browserSessions: number;
|
|
16
|
+
}>;
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/session-prune/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAO,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC1C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qDAAqD,CAAC;AAE3F;;;;;;GAMG;AACH,wBAAsB,aAAa,CACjC,QAAQ,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,EAAE,WAAwB,EAAE,GAAgB,EAAE,GAAE;IAAE,WAAW,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAA;CAAO,GAC1F,OAAO,CAAC;IAAE,eAAe,EAAE,MAAM,CAAC;IAAC,eAAe,EAAE,MAAM,CAAA;CAAE,CAAC,CA4B/D"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { sql } from "kysely";
|
|
2
|
+
/**
|
|
3
|
+
* Deletes account sessions past their end or idle longer than `idleSeconds` (7 days by default, as
|
|
4
|
+
* `readBrowserSession`), then browser sessions that ended or hold no account; from a daily job. Returns the counts.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* await pruneSessions(database);
|
|
8
|
+
*/
|
|
9
|
+
export async function pruneSessions(database, { idleSeconds = 7 * 86_400, now = Date.now() } = {}) {
|
|
10
|
+
const at = new Date(now);
|
|
11
|
+
const accountSessions = await database
|
|
12
|
+
.deleteFrom("account_session")
|
|
13
|
+
.where((where) => where.or([where("expires_at", "<=", at), where("last_seen_at", "<=", new Date(now - idleSeconds * 1000))]))
|
|
14
|
+
.executeTakeFirst();
|
|
15
|
+
const browserSessions = await database
|
|
16
|
+
.deleteFrom("browser_session")
|
|
17
|
+
.where((where) => where.or([
|
|
18
|
+
where("expires_at", "<=", at),
|
|
19
|
+
where.not(where.exists(where
|
|
20
|
+
.selectFrom("account_session")
|
|
21
|
+
.select(sql `1`.as("one"))
|
|
22
|
+
.whereRef("account_session.browser_session_id", "=", "browser_session.id"))),
|
|
23
|
+
]))
|
|
24
|
+
.executeTakeFirst();
|
|
25
|
+
return {
|
|
26
|
+
accountSessions: Number(accountSessions.numDeletedRows),
|
|
27
|
+
browserSessions: Number(browserSessions.numDeletedRows),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/session-prune/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAe,MAAM,QAAQ,CAAC;AAG1C;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,QAAiC,EACjC,EAAE,WAAW,GAAG,CAAC,GAAG,MAAM,EAAE,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,KAA6C,EAAE;IAE3F,MAAM,EAAE,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC;IACzB,MAAM,eAAe,GAAG,MAAM,QAAQ;SACnC,UAAU,CAAC,iBAAiB,CAAC;SAC7B,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CACf,KAAK,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,YAAY,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,KAAK,CAAC,cAAc,EAAE,IAAI,EAAE,IAAI,IAAI,CAAC,GAAG,GAAG,WAAW,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,CAC3G;SACA,gBAAgB,EAAE,CAAC;IACtB,MAAM,eAAe,GAAG,MAAM,QAAQ;SACnC,UAAU,CAAC,iBAAiB,CAAC;SAC7B,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CACf,KAAK,CAAC,EAAE,CAAC;QACP,KAAK,CAAC,YAAY,EAAE,IAAI,EAAE,EAAE,CAAC;QAC7B,KAAK,CAAC,GAAG,CACP,KAAK,CAAC,MAAM,CACV,KAAK;aACF,UAAU,CAAC,iBAAiB,CAAC;aAC7B,MAAM,CAAC,GAAG,CAAA,GAAG,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC;aACxB,QAAQ,CAAC,oCAAoC,EAAE,GAAG,EAAE,oBAAoB,CAAC,CAC7E,CACF;KACF,CAAC,CACH;SACA,gBAAgB,EAAE,CAAC;IACtB,OAAO;QACL,eAAe,EAAE,MAAM,CAAC,eAAe,CAAC,cAAc,CAAC;QACvD,eAAe,EAAE,MAAM,CAAC,eAAe,CAAC,cAAc,CAAC;KACxD,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A new session token (256 random bits, base64url) for a cookie, and the hash to store instead: a leaked table gives
|
|
3
|
+
* no sessions. A plain SHA-256 is enough for tokens this random.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* const { token, hash } = createSessionToken();
|
|
7
|
+
*/
|
|
8
|
+
export declare function createSessionToken(): {
|
|
9
|
+
token: string;
|
|
10
|
+
hash: string;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* The stored form of a session token.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* hashSessionToken(cookie); // compare with browser_session.token_hash
|
|
17
|
+
*/
|
|
18
|
+
export declare function hashSessionToken(token: string): string;
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/session-token/index.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,wBAAgB,kBAAkB,IAAI;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAGpE;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEtD"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
2
|
+
/**
|
|
3
|
+
* A new session token (256 random bits, base64url) for a cookie, and the hash to store instead: a leaked table gives
|
|
4
|
+
* no sessions. A plain SHA-256 is enough for tokens this random.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const { token, hash } = createSessionToken();
|
|
8
|
+
*/
|
|
9
|
+
export function createSessionToken() {
|
|
10
|
+
const token = randomBytes(32).toString("base64url");
|
|
11
|
+
return { token, hash: hashSessionToken(token) };
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The stored form of a session token.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* hashSessionToken(cookie); // compare with browser_session.token_hash
|
|
18
|
+
*/
|
|
19
|
+
export function hashSessionToken(token) {
|
|
20
|
+
return createHash("sha256").update(token).digest("base64url");
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/session-token/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAEtD;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB;IAChC,MAAM,KAAK,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;IACpD,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,gBAAgB,CAAC,KAAK,CAAC,EAAE,CAAC;AAClD,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAa;IAC5C,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;AAChE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sharadox/account-web-library",
|
|
3
|
-
"version": "0.1.0-
|
|
3
|
+
"version": "0.1.0-development.12",
|
|
4
4
|
"description": "Passwordless accounts for web applications: one-time codes, two-factor, roles, bans and OpenID Connect for people and agents.",
|
|
5
5
|
"private": false,
|
|
6
6
|
"license": "SEE LICENSE IN LICENCA.md",
|