@aooth/auth 0.1.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 aoothjs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,40 @@
1
+ <p align="center">
2
+ <a href="https://aooth.moost.org">
3
+ <img src="https://aooth.moost.org/logo.svg" alt="aoothjs" width="120" />
4
+ </a>
5
+ </p>
6
+
7
+ <h1 align="center">@aooth/auth</h1>
8
+
9
+ <p align="center">
10
+ Framework-agnostic auth method layer for aoothjs — session and token stores (in-memory, JWT, encapsulated AES-GCM), refresh rotation, denylist, magic-link + email primitives.
11
+ </p>
12
+
13
+ <p align="center">
14
+ <a href="https://aooth.moost.org/auth/"><strong>Documentation →</strong></a>
15
+ </p>
16
+
17
+ ---
18
+
19
+ ## Install
20
+
21
+ ```bash
22
+ pnpm add @aooth/auth @aooth/user
23
+ ```
24
+
25
+ ## Documentation
26
+
27
+ Full docs, API reference, and recipes: **https://aooth.moost.org/auth/**
28
+
29
+ - [Quick start](https://aooth.moost.org/guide/quick-start)
30
+ - [Credentials orchestrator](https://aooth.moost.org/auth/credentials)
31
+ - [Token stores](https://aooth.moost.org/auth/tokens)
32
+ - [Refresh rotation](https://aooth.moost.org/auth/refresh)
33
+ - [Magic links](https://aooth.moost.org/auth/magic-links)
34
+ - [Password reset](https://aooth.moost.org/auth/password-reset)
35
+ - [Stores (Redis, atscript-db)](https://aooth.moost.org/auth/stores)
36
+ - [API reference](https://aooth.moost.org/api/auth)
37
+
38
+ ## License
39
+
40
+ MIT
@@ -0,0 +1,113 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let node_crypto = require("node:crypto");
3
+ //#region src/atscript-db/index.ts
4
+ /**
5
+ * atscript-db-backed `CredentialStore`.
6
+ *
7
+ * - `persist` generates a random UUID token id, inserts a row.
8
+ * - `retrieve` filters by `{ token }` and rejects rows past `expiresAt`,
9
+ * deleting the dead row opportunistically.
10
+ * - `revokeAllForUser` uses `deleteMany({ userId })` — one round trip.
11
+ * - `listForUser` uses `findMany({ filter: { userId } })` — native.
12
+ *
13
+ * The store assumes the table is keyed on `token` (matches the shipped
14
+ * `.as` model). Custom tables must keep `token` as PK or override the
15
+ * adapter.
16
+ */
17
+ var CredentialStoreAtscriptDb = class {
18
+ table;
19
+ constructor(opts) {
20
+ this.table = opts.table;
21
+ }
22
+ async persist(state, ttl) {
23
+ const token = (0, node_crypto.randomUUID)();
24
+ const expiresAt = typeof ttl === "number" ? Date.now() + ttl : state.expiresAt;
25
+ const row = {
26
+ token,
27
+ userId: state.userId,
28
+ issuedAt: state.issuedAt,
29
+ expiresAt,
30
+ kind: state.kind,
31
+ claims: state.claims,
32
+ metadata: state.metadata,
33
+ parentCredentialId: state.parentCredentialId,
34
+ rotatedAt: state.rotatedAt
35
+ };
36
+ await this.table.insertOne(row);
37
+ return token;
38
+ }
39
+ async retrieve(token) {
40
+ const row = await this.table.findOne({ filter: { token } });
41
+ if (!row) return null;
42
+ if (row.expiresAt <= Date.now()) {
43
+ await this.table.deleteOne(token).catch(() => {});
44
+ return null;
45
+ }
46
+ return rowToState(row);
47
+ }
48
+ async consume(token) {
49
+ const state = await this.retrieve(token);
50
+ if (!state) return null;
51
+ await this.revoke(token);
52
+ return state;
53
+ }
54
+ async update(token, state) {
55
+ if (!await this.table.findOne({ filter: { token } })) return token;
56
+ if (state.expiresAt <= Date.now()) {
57
+ await this.revoke(token);
58
+ return token;
59
+ }
60
+ const row = {
61
+ token,
62
+ userId: state.userId,
63
+ issuedAt: state.issuedAt,
64
+ expiresAt: state.expiresAt,
65
+ kind: state.kind,
66
+ claims: state.claims,
67
+ metadata: state.metadata,
68
+ parentCredentialId: state.parentCredentialId,
69
+ rotatedAt: state.rotatedAt
70
+ };
71
+ await this.table.replaceOne(row);
72
+ return token;
73
+ }
74
+ async revoke(token) {
75
+ await this.table.deleteOne(token);
76
+ }
77
+ async revokeAllForUser(userId) {
78
+ return (await this.table.deleteMany({ userId })).deletedCount;
79
+ }
80
+ async listForUser(userId) {
81
+ const now = Date.now();
82
+ const rows = await this.table.findMany({ filter: { userId } });
83
+ const out = [];
84
+ const expired = [];
85
+ for (const row of rows) {
86
+ if (row.expiresAt <= now) {
87
+ expired.push(row.token);
88
+ continue;
89
+ }
90
+ out.push({
91
+ ...rowToState(row),
92
+ token: row.token
93
+ });
94
+ }
95
+ if (expired.length > 0) await this.table.deleteMany({ token: { $in: expired } }).catch(() => {});
96
+ return out;
97
+ }
98
+ };
99
+ function rowToState(row) {
100
+ const state = {
101
+ userId: row.userId,
102
+ issuedAt: row.issuedAt,
103
+ expiresAt: row.expiresAt
104
+ };
105
+ if (row.claims !== void 0) state.claims = row.claims;
106
+ if (row.metadata !== void 0) state.metadata = row.metadata;
107
+ if (row.kind === "access" || row.kind === "refresh") state.kind = row.kind;
108
+ if (row.parentCredentialId !== void 0) state.parentCredentialId = row.parentCredentialId;
109
+ if (row.rotatedAt !== void 0) state.rotatedAt = row.rotatedAt;
110
+ return state;
111
+ }
112
+ //#endregion
113
+ exports.CredentialStoreAtscriptDb = CredentialStoreAtscriptDb;
@@ -0,0 +1,85 @@
1
+ import { a as CredentialState, t as CredentialStore } from "./store-untAtWQz.cjs";
2
+
3
+ //#region src/atscript-db/index.d.ts
4
+ /**
5
+ * Persisted row shape — mirrors `AoothAuthCredential` from
6
+ * `./auth-credential.as`. Re-declared here as a plain TS interface so
7
+ * consumers can use the adapter without running the atscript build (and so
8
+ * `@aooth/auth` doesn't need to depend on `@atscript/typescript` at build
9
+ * time). When you DO wire the `.as` model, the shapes match by construction.
10
+ */
11
+ interface AuthCredentialRow<TClaims extends object = object> {
12
+ token: string;
13
+ userId: string;
14
+ issuedAt: number;
15
+ expiresAt: number;
16
+ kind?: string;
17
+ claims?: TClaims;
18
+ metadata?: {
19
+ ip?: string;
20
+ userAgent?: string;
21
+ fingerprint?: string;
22
+ label?: string;
23
+ };
24
+ parentCredentialId?: string;
25
+ rotatedAt?: number;
26
+ }
27
+ /**
28
+ * Structural surface of `AtscriptDbTable` covering exactly the methods this
29
+ * adapter calls. Kept loose to avoid pulling `@atscript/db` types into the
30
+ * `@aooth/auth` public surface — consumers pass `db.getTable(AoothAuthCredential)`
31
+ * directly and TypeScript matches by-shape.
32
+ */
33
+ interface AuthCredentialTable<TClaims extends object = object> {
34
+ insertOne(row: AuthCredentialRow<TClaims>): Promise<{
35
+ insertedId: unknown;
36
+ }>;
37
+ findOne(query: {
38
+ filter: Record<string, unknown>;
39
+ }): Promise<AuthCredentialRow<TClaims> | null>;
40
+ findMany(query: {
41
+ filter?: Record<string, unknown>;
42
+ controls?: Record<string, unknown>;
43
+ }): Promise<AuthCredentialRow<TClaims>[]>;
44
+ replaceOne(row: AuthCredentialRow<TClaims>): Promise<{
45
+ matchedCount: number;
46
+ modifiedCount: number;
47
+ }>;
48
+ deleteOne(idOrPk: unknown): Promise<{
49
+ deletedCount: number;
50
+ }>;
51
+ deleteMany(filter: Record<string, unknown>): Promise<{
52
+ deletedCount: number;
53
+ }>;
54
+ }
55
+ interface CredentialStoreAtscriptDbOptions<TClaims extends object> {
56
+ table: AuthCredentialTable<TClaims>;
57
+ }
58
+ /**
59
+ * atscript-db-backed `CredentialStore`.
60
+ *
61
+ * - `persist` generates a random UUID token id, inserts a row.
62
+ * - `retrieve` filters by `{ token }` and rejects rows past `expiresAt`,
63
+ * deleting the dead row opportunistically.
64
+ * - `revokeAllForUser` uses `deleteMany({ userId })` — one round trip.
65
+ * - `listForUser` uses `findMany({ filter: { userId } })` — native.
66
+ *
67
+ * The store assumes the table is keyed on `token` (matches the shipped
68
+ * `.as` model). Custom tables must keep `token` as PK or override the
69
+ * adapter.
70
+ */
71
+ declare class CredentialStoreAtscriptDb<TClaims extends object = object> implements CredentialStore<TClaims> {
72
+ private readonly table;
73
+ constructor(opts: CredentialStoreAtscriptDbOptions<TClaims>);
74
+ persist(state: CredentialState<TClaims>, ttl?: number): Promise<string>;
75
+ retrieve(token: string): Promise<CredentialState<TClaims> | null>;
76
+ consume(token: string): Promise<CredentialState<TClaims> | null>;
77
+ update(token: string, state: CredentialState<TClaims>): Promise<string>;
78
+ revoke(token: string): Promise<void>;
79
+ revokeAllForUser(userId: string): Promise<number>;
80
+ listForUser(userId: string): Promise<Array<CredentialState<TClaims> & {
81
+ token: string;
82
+ }>>;
83
+ }
84
+ //#endregion
85
+ export { AuthCredentialRow, AuthCredentialTable, CredentialStoreAtscriptDb };
@@ -0,0 +1,85 @@
1
+ import { a as CredentialState, t as CredentialStore } from "./store-B1t8KkfA.mjs";
2
+
3
+ //#region src/atscript-db/index.d.ts
4
+ /**
5
+ * Persisted row shape — mirrors `AoothAuthCredential` from
6
+ * `./auth-credential.as`. Re-declared here as a plain TS interface so
7
+ * consumers can use the adapter without running the atscript build (and so
8
+ * `@aooth/auth` doesn't need to depend on `@atscript/typescript` at build
9
+ * time). When you DO wire the `.as` model, the shapes match by construction.
10
+ */
11
+ interface AuthCredentialRow<TClaims extends object = object> {
12
+ token: string;
13
+ userId: string;
14
+ issuedAt: number;
15
+ expiresAt: number;
16
+ kind?: string;
17
+ claims?: TClaims;
18
+ metadata?: {
19
+ ip?: string;
20
+ userAgent?: string;
21
+ fingerprint?: string;
22
+ label?: string;
23
+ };
24
+ parentCredentialId?: string;
25
+ rotatedAt?: number;
26
+ }
27
+ /**
28
+ * Structural surface of `AtscriptDbTable` covering exactly the methods this
29
+ * adapter calls. Kept loose to avoid pulling `@atscript/db` types into the
30
+ * `@aooth/auth` public surface — consumers pass `db.getTable(AoothAuthCredential)`
31
+ * directly and TypeScript matches by-shape.
32
+ */
33
+ interface AuthCredentialTable<TClaims extends object = object> {
34
+ insertOne(row: AuthCredentialRow<TClaims>): Promise<{
35
+ insertedId: unknown;
36
+ }>;
37
+ findOne(query: {
38
+ filter: Record<string, unknown>;
39
+ }): Promise<AuthCredentialRow<TClaims> | null>;
40
+ findMany(query: {
41
+ filter?: Record<string, unknown>;
42
+ controls?: Record<string, unknown>;
43
+ }): Promise<AuthCredentialRow<TClaims>[]>;
44
+ replaceOne(row: AuthCredentialRow<TClaims>): Promise<{
45
+ matchedCount: number;
46
+ modifiedCount: number;
47
+ }>;
48
+ deleteOne(idOrPk: unknown): Promise<{
49
+ deletedCount: number;
50
+ }>;
51
+ deleteMany(filter: Record<string, unknown>): Promise<{
52
+ deletedCount: number;
53
+ }>;
54
+ }
55
+ interface CredentialStoreAtscriptDbOptions<TClaims extends object> {
56
+ table: AuthCredentialTable<TClaims>;
57
+ }
58
+ /**
59
+ * atscript-db-backed `CredentialStore`.
60
+ *
61
+ * - `persist` generates a random UUID token id, inserts a row.
62
+ * - `retrieve` filters by `{ token }` and rejects rows past `expiresAt`,
63
+ * deleting the dead row opportunistically.
64
+ * - `revokeAllForUser` uses `deleteMany({ userId })` — one round trip.
65
+ * - `listForUser` uses `findMany({ filter: { userId } })` — native.
66
+ *
67
+ * The store assumes the table is keyed on `token` (matches the shipped
68
+ * `.as` model). Custom tables must keep `token` as PK or override the
69
+ * adapter.
70
+ */
71
+ declare class CredentialStoreAtscriptDb<TClaims extends object = object> implements CredentialStore<TClaims> {
72
+ private readonly table;
73
+ constructor(opts: CredentialStoreAtscriptDbOptions<TClaims>);
74
+ persist(state: CredentialState<TClaims>, ttl?: number): Promise<string>;
75
+ retrieve(token: string): Promise<CredentialState<TClaims> | null>;
76
+ consume(token: string): Promise<CredentialState<TClaims> | null>;
77
+ update(token: string, state: CredentialState<TClaims>): Promise<string>;
78
+ revoke(token: string): Promise<void>;
79
+ revokeAllForUser(userId: string): Promise<number>;
80
+ listForUser(userId: string): Promise<Array<CredentialState<TClaims> & {
81
+ token: string;
82
+ }>>;
83
+ }
84
+ //#endregion
85
+ export { AuthCredentialRow, AuthCredentialTable, CredentialStoreAtscriptDb };
@@ -0,0 +1,112 @@
1
+ import { randomUUID } from "node:crypto";
2
+ //#region src/atscript-db/index.ts
3
+ /**
4
+ * atscript-db-backed `CredentialStore`.
5
+ *
6
+ * - `persist` generates a random UUID token id, inserts a row.
7
+ * - `retrieve` filters by `{ token }` and rejects rows past `expiresAt`,
8
+ * deleting the dead row opportunistically.
9
+ * - `revokeAllForUser` uses `deleteMany({ userId })` — one round trip.
10
+ * - `listForUser` uses `findMany({ filter: { userId } })` — native.
11
+ *
12
+ * The store assumes the table is keyed on `token` (matches the shipped
13
+ * `.as` model). Custom tables must keep `token` as PK or override the
14
+ * adapter.
15
+ */
16
+ var CredentialStoreAtscriptDb = class {
17
+ table;
18
+ constructor(opts) {
19
+ this.table = opts.table;
20
+ }
21
+ async persist(state, ttl) {
22
+ const token = randomUUID();
23
+ const expiresAt = typeof ttl === "number" ? Date.now() + ttl : state.expiresAt;
24
+ const row = {
25
+ token,
26
+ userId: state.userId,
27
+ issuedAt: state.issuedAt,
28
+ expiresAt,
29
+ kind: state.kind,
30
+ claims: state.claims,
31
+ metadata: state.metadata,
32
+ parentCredentialId: state.parentCredentialId,
33
+ rotatedAt: state.rotatedAt
34
+ };
35
+ await this.table.insertOne(row);
36
+ return token;
37
+ }
38
+ async retrieve(token) {
39
+ const row = await this.table.findOne({ filter: { token } });
40
+ if (!row) return null;
41
+ if (row.expiresAt <= Date.now()) {
42
+ await this.table.deleteOne(token).catch(() => {});
43
+ return null;
44
+ }
45
+ return rowToState(row);
46
+ }
47
+ async consume(token) {
48
+ const state = await this.retrieve(token);
49
+ if (!state) return null;
50
+ await this.revoke(token);
51
+ return state;
52
+ }
53
+ async update(token, state) {
54
+ if (!await this.table.findOne({ filter: { token } })) return token;
55
+ if (state.expiresAt <= Date.now()) {
56
+ await this.revoke(token);
57
+ return token;
58
+ }
59
+ const row = {
60
+ token,
61
+ userId: state.userId,
62
+ issuedAt: state.issuedAt,
63
+ expiresAt: state.expiresAt,
64
+ kind: state.kind,
65
+ claims: state.claims,
66
+ metadata: state.metadata,
67
+ parentCredentialId: state.parentCredentialId,
68
+ rotatedAt: state.rotatedAt
69
+ };
70
+ await this.table.replaceOne(row);
71
+ return token;
72
+ }
73
+ async revoke(token) {
74
+ await this.table.deleteOne(token);
75
+ }
76
+ async revokeAllForUser(userId) {
77
+ return (await this.table.deleteMany({ userId })).deletedCount;
78
+ }
79
+ async listForUser(userId) {
80
+ const now = Date.now();
81
+ const rows = await this.table.findMany({ filter: { userId } });
82
+ const out = [];
83
+ const expired = [];
84
+ for (const row of rows) {
85
+ if (row.expiresAt <= now) {
86
+ expired.push(row.token);
87
+ continue;
88
+ }
89
+ out.push({
90
+ ...rowToState(row),
91
+ token: row.token
92
+ });
93
+ }
94
+ if (expired.length > 0) await this.table.deleteMany({ token: { $in: expired } }).catch(() => {});
95
+ return out;
96
+ }
97
+ };
98
+ function rowToState(row) {
99
+ const state = {
100
+ userId: row.userId,
101
+ issuedAt: row.issuedAt,
102
+ expiresAt: row.expiresAt
103
+ };
104
+ if (row.claims !== void 0) state.claims = row.claims;
105
+ if (row.metadata !== void 0) state.metadata = row.metadata;
106
+ if (row.kind === "access" || row.kind === "refresh") state.kind = row.kind;
107
+ if (row.parentCredentialId !== void 0) state.parentCredentialId = row.parentCredentialId;
108
+ if (row.rotatedAt !== void 0) state.rotatedAt = row.rotatedAt;
109
+ return state;
110
+ }
111
+ //#endregion
112
+ export { CredentialStoreAtscriptDb };