@binary1702/auth 0.2.5 → 0.4.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/AGENTS.md +155 -0
- package/CHANGELOG.md +49 -0
- package/INSTALL.md +27 -16
- package/PROMPT.md +325 -36
- package/README.md +338 -53
- package/dist/cli.js +248 -17
- package/dist/core/admin-html.d.ts +32 -0
- package/dist/core/admin-html.js +350 -0
- package/dist/core/admin.d.ts +56 -0
- package/dist/core/admin.js +90 -0
- package/dist/core/config.d.ts +10 -3
- package/dist/core/config.js +22 -8
- package/dist/core/db.d.ts +155 -10
- package/dist/core/db.js +449 -69
- package/dist/core/email.d.ts +49 -0
- package/dist/core/email.js +213 -0
- package/dist/core/guard.d.ts +52 -0
- package/dist/core/guard.js +106 -0
- package/dist/core/html.d.ts +43 -0
- package/dist/core/html.js +122 -28
- package/dist/core/invite.d.ts +7 -0
- package/dist/core/invite.js +30 -0
- package/dist/core/options.d.ts +95 -0
- package/dist/core/options.js +377 -0
- package/dist/core/org.d.ts +43 -0
- package/dist/core/org.js +65 -0
- package/dist/core/pages.d.ts +34 -0
- package/dist/core/pages.js +73 -0
- package/dist/core/password.d.ts +16 -0
- package/dist/core/password.js +41 -12
- package/dist/core/rate.d.ts +26 -0
- package/dist/core/rate.js +70 -0
- package/dist/core/reset.d.ts +9 -0
- package/dist/core/reset.js +31 -0
- package/dist/core/session.d.ts +11 -1
- package/dist/core/session.js +27 -7
- package/dist/core/signup.d.ts +13 -6
- package/dist/core/signup.js +30 -15
- package/dist/core/tokens.d.ts +29 -0
- package/dist/core/tokens.js +64 -0
- package/dist/core/verify.d.ts +8 -0
- package/dist/core/verify.js +33 -0
- package/dist/core/yaml.d.ts +10 -0
- package/dist/core/yaml.js +85 -0
- package/dist/index.d.ts +18 -6
- package/dist/index.js +18 -6
- package/dist/next/index.d.ts +10 -3
- package/dist/next/index.js +649 -63
- package/dist/next/proxy.d.ts +2 -1
- package/dist/next/proxy.js +22 -10
- package/dist/react/index.d.ts +72 -0
- package/dist/react/index.js +107 -0
- package/package.json +18 -4
package/dist/core/db.d.ts
CHANGED
|
@@ -1,26 +1,171 @@
|
|
|
1
1
|
import { TABLE } from "./config.ts";
|
|
2
2
|
import type { User } from "./session.ts";
|
|
3
|
-
export
|
|
4
|
-
|
|
3
|
+
export type Statement = {
|
|
4
|
+
text: string;
|
|
5
|
+
params?: unknown[];
|
|
6
|
+
};
|
|
7
|
+
/** What the package needs from a database: parameterised queries, and a transaction if it has one. */
|
|
8
|
+
export interface Db {
|
|
9
|
+
query<T = Record<string, unknown>>(text: string, params?: unknown[]): Promise<T[]>;
|
|
10
|
+
/** Run statements in one transaction. A store without it runs them one by one. */
|
|
11
|
+
transaction?(statements: Statement[]): Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
/** The real store, created once per process from DATABASE_URL. */
|
|
14
|
+
export declare function db(): Db;
|
|
15
|
+
export type Migration = {
|
|
16
|
+
id: number;
|
|
17
|
+
name: string;
|
|
18
|
+
statements: string[];
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Ordered, append-only. Never edit a step that has shipped: add a new
|
|
22
|
+
* one. Step 1 is the 0.2.5 schema exactly, so an existing install
|
|
23
|
+
* applies it as a no-op through IF NOT EXISTS.
|
|
24
|
+
*/
|
|
25
|
+
export declare const MIGRATIONS: Migration[];
|
|
26
|
+
/**
|
|
27
|
+
* The package's tables have plain names (users, orgs, members, tokens).
|
|
28
|
+
* A database that already holds a `users` table of its own would be
|
|
29
|
+
* silently shared by CREATE TABLE IF NOT EXISTS, so a fresh install
|
|
30
|
+
* checks first. Pure: decides from what the store reports.
|
|
31
|
+
*/
|
|
32
|
+
export declare function foreignUsersTable(columns: string[]): boolean;
|
|
33
|
+
export declare const FOREIGN_USERS_TABLE = "This database already has a `users` table that is not this package's. Point DATABASE_URL at a database (or Postgres schema) of its own.";
|
|
34
|
+
/** The steps still to run, in order. Pure. */
|
|
35
|
+
export declare function pendingSteps(applied: number[], steps?: Migration[]): Migration[];
|
|
36
|
+
/** Everything a pending step needs to run, including recording that it ran. Pure. */
|
|
37
|
+
export declare function migrationStatements(steps: Migration[]): Statement[];
|
|
38
|
+
/**
|
|
39
|
+
* Apply pending migrations. Memoized per store and per process, so
|
|
40
|
+
* route handlers can call it on every request for free after the first.
|
|
41
|
+
* A failure clears the memo so the next call retries.
|
|
42
|
+
*/
|
|
43
|
+
export declare function ensureSchema(store?: Db): Promise<void>;
|
|
44
|
+
export type UserRow = {
|
|
5
45
|
id: string;
|
|
6
46
|
email: string;
|
|
7
47
|
password_hash: string;
|
|
8
48
|
epoch: number;
|
|
49
|
+
/** When the email was verified, or null */
|
|
50
|
+
verified_at: string | Date | null;
|
|
9
51
|
};
|
|
10
|
-
export declare function findUser(email: string): Promise<
|
|
11
|
-
|
|
52
|
+
export declare function findUser(email: string, store?: Db): Promise<UserRow | null>;
|
|
53
|
+
/** Two sign-ups raced on one email and the second lost: the handler answers 409. */
|
|
54
|
+
export declare class DuplicateUserError extends Error {
|
|
55
|
+
constructor(email: string);
|
|
56
|
+
}
|
|
57
|
+
/** Postgres's unique_violation */
|
|
58
|
+
export declare const isUniqueViolation: (err: unknown) => boolean;
|
|
59
|
+
export declare function createUser(email: string, passwordHash: string, opts?: {
|
|
60
|
+
verified?: boolean;
|
|
61
|
+
}, store?: Db): Promise<User & {
|
|
12
62
|
epoch: number;
|
|
63
|
+
verified: boolean;
|
|
13
64
|
}>;
|
|
65
|
+
/** Replace a hash with one made under stronger parameters. No epoch bump: nothing about the session changes. */
|
|
66
|
+
export declare function rehashPassword(id: string, passwordHash: string, store?: Db): Promise<void>;
|
|
67
|
+
/** What a session check needs about the user: current epoch and verification. Null if the user is gone. */
|
|
68
|
+
export type UserState = {
|
|
69
|
+
epoch: number;
|
|
70
|
+
verified: boolean;
|
|
71
|
+
};
|
|
72
|
+
export declare function userState(id: string, store?: Db): Promise<UserState | null>;
|
|
14
73
|
/** The user's current session_epoch, or null if the user is gone */
|
|
15
|
-
export declare function sessionEpoch(id: string): Promise<number | null>;
|
|
74
|
+
export declare function sessionEpoch(id: string, store?: Db): Promise<number | null>;
|
|
75
|
+
/** Everything a session needs, in one query: the user's state, their active organization, and what the admin page saved for their role. */
|
|
76
|
+
export type SessionRow = UserState & {
|
|
77
|
+
id: string;
|
|
78
|
+
email: string;
|
|
79
|
+
org_id: string | null;
|
|
80
|
+
org_name: string | null;
|
|
81
|
+
role: string | null;
|
|
82
|
+
/** permissions saved on /auth/admin for this role in this organization, or null */
|
|
83
|
+
stored_org?: string[] | null;
|
|
84
|
+
/** permissions saved on /auth/admin for this role in every organization, or null */
|
|
85
|
+
stored_all?: string[] | null;
|
|
86
|
+
};
|
|
87
|
+
export declare function sessionRow(id: string, store?: Db): Promise<SessionRow | null>;
|
|
88
|
+
export type StoredRole = {
|
|
89
|
+
scope: string;
|
|
90
|
+
name: string;
|
|
91
|
+
permissions: string[];
|
|
92
|
+
updated_at: string;
|
|
93
|
+
};
|
|
94
|
+
/** Every saved role, all scopes. */
|
|
95
|
+
export declare function storedRoles(store?: Db): Promise<StoredRole[]>;
|
|
96
|
+
/** Save what a role may do: for every organization (scope "") or one (its id). Replaces the previous list. */
|
|
97
|
+
export declare function setRolePermissions(scope: string, name: string, permissions: string[], store?: Db): Promise<void>;
|
|
98
|
+
/** Forget an organization's own rule for a role: it follows the rule for every organization again. False if there was none. */
|
|
99
|
+
export declare function resetRole(scope: string, name: string, store?: Db): Promise<boolean>;
|
|
100
|
+
/**
|
|
101
|
+
* The starting point, written once: every role from b17_auth.yaml `roles:`
|
|
102
|
+
* that has no row for every organization yet. A role the admin page has
|
|
103
|
+
* already saved is never touched. Returns the names written.
|
|
104
|
+
*/
|
|
105
|
+
export declare function seedRoles(defaults: Record<string, string[]>, store?: Db): Promise<string[]>;
|
|
106
|
+
/** The user's role in one organization, or null when they are not a member. */
|
|
107
|
+
export declare function memberRole(userId: string, orgId: string, store?: Db): Promise<string | null>;
|
|
108
|
+
export type OrgRow = {
|
|
109
|
+
id: string;
|
|
110
|
+
name: string;
|
|
111
|
+
created_at: string;
|
|
112
|
+
};
|
|
113
|
+
export declare function createOrg(name: string, store?: Db): Promise<OrgRow>;
|
|
114
|
+
/** By id (UUID) or exact name. Null if there is no such organization. */
|
|
115
|
+
export declare function findOrg(idOrName: string, store?: Db): Promise<OrgRow | null>;
|
|
116
|
+
export declare function listOrgs(store?: Db): Promise<(OrgRow & {
|
|
117
|
+
members: number;
|
|
118
|
+
})[]>;
|
|
119
|
+
/** Add or update a membership. Becomes the user's active org if they had none. */
|
|
120
|
+
export declare function addMember(orgId: string, userId: string, role: string, store?: Db): Promise<void>;
|
|
121
|
+
export declare function removeMember(orgId: string, userId: string, store?: Db): Promise<boolean>;
|
|
122
|
+
export declare function removeOrg(orgId: string, store?: Db): Promise<boolean>;
|
|
123
|
+
export type Membership = {
|
|
124
|
+
org_id: string;
|
|
125
|
+
org: string;
|
|
126
|
+
role: string;
|
|
127
|
+
};
|
|
128
|
+
export type AdminUser = {
|
|
129
|
+
id: string;
|
|
130
|
+
email: string;
|
|
131
|
+
created_at: string;
|
|
132
|
+
verified: boolean;
|
|
133
|
+
memberships: Membership[];
|
|
134
|
+
};
|
|
135
|
+
export type AdminOrg = {
|
|
136
|
+
id: string;
|
|
137
|
+
name: string;
|
|
138
|
+
created_at: string;
|
|
139
|
+
members: {
|
|
140
|
+
user_id: string;
|
|
141
|
+
email: string;
|
|
142
|
+
role: string;
|
|
143
|
+
}[];
|
|
144
|
+
};
|
|
145
|
+
/** Every user with their memberships, one query. */
|
|
146
|
+
export declare function adminUsers(store?: Db): Promise<AdminUser[]>;
|
|
147
|
+
/** Every organization with its members, one query. */
|
|
148
|
+
export declare function adminOrgs(store?: Db): Promise<AdminOrg[]>;
|
|
149
|
+
/** Switch the user's active organization. False unless they are a member of it: the client's choice is checked, never trusted. */
|
|
150
|
+
export declare function setActiveOrg(userId: string, orgId: string, store?: Db): Promise<boolean>;
|
|
151
|
+
/** Record that the user followed the verification link. Idempotent. Null if the user is gone. */
|
|
152
|
+
export declare function markVerified(id: string, store?: Db): Promise<User | null>;
|
|
16
153
|
/** Revoke every session of this user. False if there is no such user. */
|
|
17
|
-
export declare function revokeSessions(email: string): Promise<boolean>;
|
|
154
|
+
export declare function revokeSessions(email: string, store?: Db): Promise<boolean>;
|
|
18
155
|
/** Delete the user. Their sessions die with the row. False if none. */
|
|
19
|
-
export declare function removeUser(email: string): Promise<boolean>;
|
|
20
|
-
export declare function countUsers(): Promise<number>;
|
|
21
|
-
export declare function listUsers(): Promise<(User & {
|
|
156
|
+
export declare function removeUser(email: string, store?: Db): Promise<boolean>;
|
|
157
|
+
export declare function countUsers(store?: Db): Promise<number>;
|
|
158
|
+
export declare function listUsers(store?: Db): Promise<(User & {
|
|
22
159
|
created_at: string;
|
|
23
160
|
})[]>;
|
|
24
161
|
/** Change a password. Also revokes every session, old password's included. */
|
|
25
|
-
export declare function setPassword(email: string, passwordHash: string): Promise<boolean>;
|
|
162
|
+
export declare function setPassword(email: string, passwordHash: string, store?: Db): Promise<boolean>;
|
|
163
|
+
/**
|
|
164
|
+
* Same, by id, for a reset link. Also marks the email verified: following
|
|
165
|
+
* the link proved the address. Returns the user with the new epoch so the
|
|
166
|
+
* caller can sign them in; null if the user is gone.
|
|
167
|
+
*/
|
|
168
|
+
export declare function setPasswordById(id: string, passwordHash: string, store?: Db): Promise<(User & {
|
|
169
|
+
epoch: number;
|
|
170
|
+
}) | null>;
|
|
26
171
|
export { TABLE };
|