@aooth/user 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 +21 -0
- package/README.md +38 -0
- package/dist/atscript-db.cjs +44 -0
- package/dist/atscript-db.d.cts +55 -0
- package/dist/atscript-db.d.mts +55 -0
- package/dist/atscript-db.mjs +43 -0
- package/dist/index.cjs +824 -0
- package/dist/index.d.cts +233 -0
- package/dist/index.d.mts +233 -0
- package/dist/index.mjs +799 -0
- package/dist/user-store-B_l9vqlQ.mjs +96 -0
- package/dist/user-store-CdWrTeqR.cjs +149 -0
- package/dist/user-store-Dbc9unW3.d.mts +187 -0
- package/dist/user-store-VJPWNgdp.d.cts +187 -0
- package/package.json +75 -0
- package/src/atscript-db/user-credentials.as +42 -0
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,38 @@
|
|
|
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/user</h1>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
User credential primitives for aoothjs — password hashing, MFA (TOTP, codes, backup codes), lockout, policy engine, and pluggable storage.
|
|
11
|
+
</p>
|
|
12
|
+
|
|
13
|
+
<p align="center">
|
|
14
|
+
<a href="https://aooth.moost.org/user/"><strong>Documentation →</strong></a>
|
|
15
|
+
</p>
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pnpm add @aooth/user
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Documentation
|
|
26
|
+
|
|
27
|
+
Full docs, API reference, and recipes: **https://aooth.moost.org/user/**
|
|
28
|
+
|
|
29
|
+
- [Quick start](https://aooth.moost.org/guide/quick-start)
|
|
30
|
+
- [`UserService`](https://aooth.moost.org/user/service)
|
|
31
|
+
- [Password + policy](https://aooth.moost.org/user/password)
|
|
32
|
+
- [MFA](https://aooth.moost.org/user/mfa)
|
|
33
|
+
- [Stores](https://aooth.moost.org/user/stores)
|
|
34
|
+
- [API reference](https://aooth.moost.org/api/user)
|
|
35
|
+
|
|
36
|
+
## License
|
|
37
|
+
|
|
38
|
+
MIT
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_user_store = require("./user-store-CdWrTeqR.cjs");
|
|
3
|
+
//#region src/atscript-db/index.ts
|
|
4
|
+
function isConflict(err) {
|
|
5
|
+
return typeof err === "object" && err !== null && err.code === "CONFLICT";
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* `@atscript/db`-backed `UserStore`. Pass the resolved table for the
|
|
9
|
+
* `AoothUserCredentials` (or a `.as` interface extending it) shipped at the
|
|
10
|
+
* `@aooth/user/atscript-db/model.as` subpath.
|
|
11
|
+
*/
|
|
12
|
+
var UsersStoreAtscriptDb = class extends require_user_store.UserStore {
|
|
13
|
+
table;
|
|
14
|
+
constructor(opts) {
|
|
15
|
+
super();
|
|
16
|
+
this.table = opts.table;
|
|
17
|
+
}
|
|
18
|
+
async exists(username) {
|
|
19
|
+
return await this.table.count({ filter: { username } }) > 0;
|
|
20
|
+
}
|
|
21
|
+
async findByUsername(username) {
|
|
22
|
+
return await this.table.findOne({ filter: { username } }) ?? null;
|
|
23
|
+
}
|
|
24
|
+
async create(data) {
|
|
25
|
+
try {
|
|
26
|
+
await this.table.insertOne(data);
|
|
27
|
+
} catch (e) {
|
|
28
|
+
if (isConflict(e)) throw new require_user_store.UserAuthError("ALREADY_EXISTS", `User "${data.username}" already exists`);
|
|
29
|
+
throw e;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async update(username, update) {
|
|
33
|
+
const patch = { username };
|
|
34
|
+
if (update.set) Object.assign(patch, update.set);
|
|
35
|
+
if (update.inc) for (const [path, amount] of Object.entries(update.inc)) require_user_store.setAtPath(patch, path, { $inc: amount });
|
|
36
|
+
if (Object.keys(patch).length <= 1) return true;
|
|
37
|
+
return (await this.table.updateOne(patch)).matchedCount > 0;
|
|
38
|
+
}
|
|
39
|
+
async delete(username) {
|
|
40
|
+
return (await this.table.deleteMany({ username })).deletedCount > 0;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
//#endregion
|
|
44
|
+
exports.UsersStoreAtscriptDb = UsersStoreAtscriptDb;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { C as UserStoreUpdate, t as UserStore, x as UserCredentials } from "./user-store-VJPWNgdp.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/atscript-db/index.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Persisted row shape — `UserCredentials` plus the consumer's custom user
|
|
6
|
+
* fields. The `.as` model shipped at `@aooth/user/atscript-db/model.as`
|
|
7
|
+
* (`AoothUserCredentials`) matches by construction; consumers extend it with
|
|
8
|
+
* their own `.as` interface to add custom columns.
|
|
9
|
+
*/
|
|
10
|
+
type UserCredentialsRow<TUserCustom extends object = object> = UserCredentials & TUserCustom;
|
|
11
|
+
/**
|
|
12
|
+
* Structural surface of `AtscriptDbTable` covering exactly the methods this
|
|
13
|
+
* adapter calls. Kept loose to avoid pulling `@atscript/db` types into the
|
|
14
|
+
* `@aooth/user` public surface — consumers pass `db.getTable(AoothUserCredentials)`
|
|
15
|
+
* directly and TypeScript matches by-shape.
|
|
16
|
+
*
|
|
17
|
+
* Mirrors `AuthCredentialTable` from `@aooth/auth/atscript-db`.
|
|
18
|
+
*/
|
|
19
|
+
interface AuthUserTable<TUserCustom extends object = object> {
|
|
20
|
+
count(query: {
|
|
21
|
+
filter: Record<string, unknown>;
|
|
22
|
+
}): Promise<number>;
|
|
23
|
+
findOne(query: {
|
|
24
|
+
filter: Record<string, unknown>;
|
|
25
|
+
}): Promise<UserCredentialsRow<TUserCustom> | null>;
|
|
26
|
+
insertOne(row: Record<string, unknown>): Promise<{
|
|
27
|
+
insertedId: unknown;
|
|
28
|
+
}>;
|
|
29
|
+
updateOne(patch: Record<string, unknown>): Promise<{
|
|
30
|
+
matchedCount: number;
|
|
31
|
+
modifiedCount: number;
|
|
32
|
+
}>;
|
|
33
|
+
deleteMany(filter: Record<string, unknown>): Promise<{
|
|
34
|
+
deletedCount: number;
|
|
35
|
+
}>;
|
|
36
|
+
}
|
|
37
|
+
interface UsersStoreAtscriptDbOptions<TUserCustom extends object> {
|
|
38
|
+
table: AuthUserTable<TUserCustom>;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* `@atscript/db`-backed `UserStore`. Pass the resolved table for the
|
|
42
|
+
* `AoothUserCredentials` (or a `.as` interface extending it) shipped at the
|
|
43
|
+
* `@aooth/user/atscript-db/model.as` subpath.
|
|
44
|
+
*/
|
|
45
|
+
declare class UsersStoreAtscriptDb<TUserCustom extends object = object> extends UserStore<TUserCustom> {
|
|
46
|
+
protected table: AuthUserTable<TUserCustom>;
|
|
47
|
+
constructor(opts: UsersStoreAtscriptDbOptions<TUserCustom>);
|
|
48
|
+
exists(username: string): Promise<boolean>;
|
|
49
|
+
findByUsername(username: string): Promise<(UserCredentials & TUserCustom) | null>;
|
|
50
|
+
create(data: UserCredentials & TUserCustom): Promise<void>;
|
|
51
|
+
update(username: string, update: UserStoreUpdate): Promise<boolean>;
|
|
52
|
+
delete(username: string): Promise<boolean>;
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
55
|
+
export { AuthUserTable, UserCredentialsRow, UsersStoreAtscriptDb };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { C as UserStoreUpdate, t as UserStore, x as UserCredentials } from "./user-store-Dbc9unW3.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/atscript-db/index.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Persisted row shape — `UserCredentials` plus the consumer's custom user
|
|
6
|
+
* fields. The `.as` model shipped at `@aooth/user/atscript-db/model.as`
|
|
7
|
+
* (`AoothUserCredentials`) matches by construction; consumers extend it with
|
|
8
|
+
* their own `.as` interface to add custom columns.
|
|
9
|
+
*/
|
|
10
|
+
type UserCredentialsRow<TUserCustom extends object = object> = UserCredentials & TUserCustom;
|
|
11
|
+
/**
|
|
12
|
+
* Structural surface of `AtscriptDbTable` covering exactly the methods this
|
|
13
|
+
* adapter calls. Kept loose to avoid pulling `@atscript/db` types into the
|
|
14
|
+
* `@aooth/user` public surface — consumers pass `db.getTable(AoothUserCredentials)`
|
|
15
|
+
* directly and TypeScript matches by-shape.
|
|
16
|
+
*
|
|
17
|
+
* Mirrors `AuthCredentialTable` from `@aooth/auth/atscript-db`.
|
|
18
|
+
*/
|
|
19
|
+
interface AuthUserTable<TUserCustom extends object = object> {
|
|
20
|
+
count(query: {
|
|
21
|
+
filter: Record<string, unknown>;
|
|
22
|
+
}): Promise<number>;
|
|
23
|
+
findOne(query: {
|
|
24
|
+
filter: Record<string, unknown>;
|
|
25
|
+
}): Promise<UserCredentialsRow<TUserCustom> | null>;
|
|
26
|
+
insertOne(row: Record<string, unknown>): Promise<{
|
|
27
|
+
insertedId: unknown;
|
|
28
|
+
}>;
|
|
29
|
+
updateOne(patch: Record<string, unknown>): Promise<{
|
|
30
|
+
matchedCount: number;
|
|
31
|
+
modifiedCount: number;
|
|
32
|
+
}>;
|
|
33
|
+
deleteMany(filter: Record<string, unknown>): Promise<{
|
|
34
|
+
deletedCount: number;
|
|
35
|
+
}>;
|
|
36
|
+
}
|
|
37
|
+
interface UsersStoreAtscriptDbOptions<TUserCustom extends object> {
|
|
38
|
+
table: AuthUserTable<TUserCustom>;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* `@atscript/db`-backed `UserStore`. Pass the resolved table for the
|
|
42
|
+
* `AoothUserCredentials` (or a `.as` interface extending it) shipped at the
|
|
43
|
+
* `@aooth/user/atscript-db/model.as` subpath.
|
|
44
|
+
*/
|
|
45
|
+
declare class UsersStoreAtscriptDb<TUserCustom extends object = object> extends UserStore<TUserCustom> {
|
|
46
|
+
protected table: AuthUserTable<TUserCustom>;
|
|
47
|
+
constructor(opts: UsersStoreAtscriptDbOptions<TUserCustom>);
|
|
48
|
+
exists(username: string): Promise<boolean>;
|
|
49
|
+
findByUsername(username: string): Promise<(UserCredentials & TUserCustom) | null>;
|
|
50
|
+
create(data: UserCredentials & TUserCustom): Promise<void>;
|
|
51
|
+
update(username: string, update: UserStoreUpdate): Promise<boolean>;
|
|
52
|
+
delete(username: string): Promise<boolean>;
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
55
|
+
export { AuthUserTable, UserCredentialsRow, UsersStoreAtscriptDb };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { c as setAtPath, l as UserAuthError, t as UserStore } from "./user-store-B_l9vqlQ.mjs";
|
|
2
|
+
//#region src/atscript-db/index.ts
|
|
3
|
+
function isConflict(err) {
|
|
4
|
+
return typeof err === "object" && err !== null && err.code === "CONFLICT";
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* `@atscript/db`-backed `UserStore`. Pass the resolved table for the
|
|
8
|
+
* `AoothUserCredentials` (or a `.as` interface extending it) shipped at the
|
|
9
|
+
* `@aooth/user/atscript-db/model.as` subpath.
|
|
10
|
+
*/
|
|
11
|
+
var UsersStoreAtscriptDb = class extends UserStore {
|
|
12
|
+
table;
|
|
13
|
+
constructor(opts) {
|
|
14
|
+
super();
|
|
15
|
+
this.table = opts.table;
|
|
16
|
+
}
|
|
17
|
+
async exists(username) {
|
|
18
|
+
return await this.table.count({ filter: { username } }) > 0;
|
|
19
|
+
}
|
|
20
|
+
async findByUsername(username) {
|
|
21
|
+
return await this.table.findOne({ filter: { username } }) ?? null;
|
|
22
|
+
}
|
|
23
|
+
async create(data) {
|
|
24
|
+
try {
|
|
25
|
+
await this.table.insertOne(data);
|
|
26
|
+
} catch (e) {
|
|
27
|
+
if (isConflict(e)) throw new UserAuthError("ALREADY_EXISTS", `User "${data.username}" already exists`);
|
|
28
|
+
throw e;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async update(username, update) {
|
|
32
|
+
const patch = { username };
|
|
33
|
+
if (update.set) Object.assign(patch, update.set);
|
|
34
|
+
if (update.inc) for (const [path, amount] of Object.entries(update.inc)) setAtPath(patch, path, { $inc: amount });
|
|
35
|
+
if (Object.keys(patch).length <= 1) return true;
|
|
36
|
+
return (await this.table.updateOne(patch)).matchedCount > 0;
|
|
37
|
+
}
|
|
38
|
+
async delete(username) {
|
|
39
|
+
return (await this.table.deleteMany({ username })).deletedCount > 0;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
//#endregion
|
|
43
|
+
export { UsersStoreAtscriptDb };
|