@veap/auth 0.0.0-stage → 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 +167 -2
- package/dist/actions/auth.actions.d.ts +23 -0
- package/dist/actions/auth.actions.js +40 -0
- package/dist/actions/index.d.ts +1 -0
- package/dist/actions/index.js +1 -0
- package/dist/client/auth.context.d.ts +29 -0
- package/dist/client/auth.context.js +64 -0
- package/dist/client/index.d.ts +1 -0
- package/dist/client/index.js +1 -0
- package/dist/common/augment.d.ts +26 -0
- package/dist/common/augment.js +63 -0
- package/dist/common/context.d.ts +30 -0
- package/dist/common/context.js +36 -0
- package/dist/common/index.d.ts +7 -0
- package/dist/common/index.js +7 -0
- package/dist/common/logic.d.ts +64 -0
- package/dist/common/logic.js +133 -0
- package/dist/common/registry.d.ts +28 -0
- package/dist/common/registry.js +76 -0
- package/dist/common/routes.d.ts +3 -0
- package/dist/common/routes.js +2 -0
- package/dist/common/tokens.d.ts +10 -0
- package/dist/common/tokens.js +8 -0
- package/dist/common/types.d.ts +178 -0
- package/dist/common/types.js +9 -0
- package/dist/crypto/aes-cipher.d.ts +8 -0
- package/dist/crypto/aes-cipher.js +12 -0
- package/dist/crypto/bcrypt-hasher.d.ts +12 -0
- package/dist/crypto/bcrypt-hasher.js +37 -0
- package/dist/crypto/contracts.d.ts +36 -0
- package/dist/crypto/contracts.js +3 -0
- package/dist/crypto/encryption.d.ts +18 -0
- package/dist/crypto/encryption.js +69 -0
- package/dist/crypto/index.d.ts +6 -0
- package/dist/crypto/index.js +6 -0
- package/dist/crypto/password.d.ts +19 -0
- package/dist/crypto/password.js +27 -0
- package/dist/crypto/token-generator.d.ts +10 -0
- package/dist/crypto/token-generator.js +25 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +11 -0
- package/dist/migrations/0001_create_auth_tables.d.ts +4 -0
- package/dist/migrations/0001_create_auth_tables.js +122 -0
- package/dist/migrations/index.d.ts +4 -0
- package/dist/migrations/index.js +7 -0
- package/dist/models/EmailVerification.d.ts +23 -0
- package/dist/models/EmailVerification.js +19 -0
- package/dist/models/PasswordResetSession.d.ts +25 -0
- package/dist/models/PasswordResetSession.js +21 -0
- package/dist/models/Permission.d.ts +20 -0
- package/dist/models/Permission.js +19 -0
- package/dist/models/Role.d.ts +20 -0
- package/dist/models/Role.js +19 -0
- package/dist/models/Session.d.ts +22 -0
- package/dist/models/Session.js +21 -0
- package/dist/models/User.d.ts +32 -0
- package/dist/models/User.js +31 -0
- package/dist/models/index.d.ts +6 -0
- package/dist/models/index.js +6 -0
- package/dist/models.d.ts +1 -0
- package/dist/models.js +1 -0
- package/dist/passwords/contracts.d.ts +28 -0
- package/dist/passwords/contracts.js +2 -0
- package/dist/passwords/index.d.ts +4 -0
- package/dist/passwords/index.js +4 -0
- package/dist/passwords/password-reset.facade.d.ts +14 -0
- package/dist/passwords/password-reset.facade.js +57 -0
- package/dist/passwords/password-reset.repository.d.ts +10 -0
- package/dist/passwords/password-reset.repository.js +39 -0
- package/dist/passwords/password-reset.service.d.ts +38 -0
- package/dist/passwords/password-reset.service.js +242 -0
- package/dist/providers/auth.provider.d.ts +9 -0
- package/dist/providers/auth.provider.js +199 -0
- package/dist/providers/index.d.ts +2 -0
- package/dist/providers/index.js +2 -0
- package/dist/providers/setup.d.ts +7 -0
- package/dist/providers/setup.js +29 -0
- package/dist/rbac/contracts.d.ts +75 -0
- package/dist/rbac/contracts.js +4 -0
- package/dist/rbac/index.d.ts +4 -0
- package/dist/rbac/index.js +4 -0
- package/dist/rbac/rbac.facade.d.ts +23 -0
- package/dist/rbac/rbac.facade.js +85 -0
- package/dist/rbac/rbac.repository.d.ts +30 -0
- package/dist/rbac/rbac.repository.js +137 -0
- package/dist/rbac/rbac.service.d.ts +35 -0
- package/dist/rbac/rbac.service.js +143 -0
- package/dist/server.d.ts +3 -0
- package/dist/server.js +3 -0
- package/dist/session/auth.service.d.ts +35 -0
- package/dist/session/auth.service.js +207 -0
- package/dist/session/contracts.d.ts +33 -0
- package/dist/session/contracts.js +2 -0
- package/dist/session/index.d.ts +5 -0
- package/dist/session/index.js +5 -0
- package/dist/session/session.facade.d.ts +14 -0
- package/dist/session/session.facade.js +75 -0
- package/dist/session/session.repository.d.ts +15 -0
- package/dist/session/session.repository.js +55 -0
- package/dist/session/session.service.d.ts +63 -0
- package/dist/session/session.service.js +221 -0
- package/dist/users/contracts.d.ts +33 -0
- package/dist/users/contracts.js +2 -0
- package/dist/users/index.d.ts +4 -0
- package/dist/users/index.js +4 -0
- package/dist/users/user.facade.d.ts +28 -0
- package/dist/users/user.facade.js +92 -0
- package/dist/users/user.repository.d.ts +16 -0
- package/dist/users/user.repository.js +60 -0
- package/dist/users/user.service.d.ts +80 -0
- package/dist/users/user.service.js +201 -0
- package/dist/validation/index.d.ts +90 -0
- package/dist/validation/index.js +46 -0
- package/dist/verification/contracts.d.ts +25 -0
- package/dist/verification/contracts.js +2 -0
- package/dist/verification/index.d.ts +4 -0
- package/dist/verification/index.js +4 -0
- package/dist/verification/verification.facade.d.ts +10 -0
- package/dist/verification/verification.facade.js +49 -0
- package/dist/verification/verification.repository.d.ts +7 -0
- package/dist/verification/verification.repository.js +23 -0
- package/dist/verification/verification.service.d.ts +44 -0
- package/dist/verification/verification.service.js +122 -0
- package/package.json +84 -4
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hashes the password using bcrypt.
|
|
3
|
+
* @param password Password to be hashed.
|
|
4
|
+
* @returns Returns the hashed password.
|
|
5
|
+
*/
|
|
6
|
+
export declare function hashPassword(password: string): Promise<string>;
|
|
7
|
+
/**
|
|
8
|
+
* Verifies the password hash.
|
|
9
|
+
* @param hash bcrypt hash.
|
|
10
|
+
* @param password Password for comparison.
|
|
11
|
+
* @returns Returns true if the password is correct, false otherwise.
|
|
12
|
+
*/
|
|
13
|
+
export declare function verifyPasswordHash(hash: string, password: string): Promise<boolean>;
|
|
14
|
+
/**
|
|
15
|
+
* Validates password strength.
|
|
16
|
+
* @param password Password to validate.
|
|
17
|
+
* @returns Returns true if the password meets complexity requirements.
|
|
18
|
+
*/
|
|
19
|
+
export declare function verifyPasswordStrength(password: string): Promise<boolean>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import bcrypt from "bcryptjs";
|
|
2
|
+
/**
|
|
3
|
+
* Hashes the password using bcrypt.
|
|
4
|
+
* @param password Password to be hashed.
|
|
5
|
+
* @returns Returns the hashed password.
|
|
6
|
+
*/
|
|
7
|
+
export async function hashPassword(password) {
|
|
8
|
+
const saltRounds = 10;
|
|
9
|
+
return await bcrypt.hash(password, saltRounds);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Verifies the password hash.
|
|
13
|
+
* @param hash bcrypt hash.
|
|
14
|
+
* @param password Password for comparison.
|
|
15
|
+
* @returns Returns true if the password is correct, false otherwise.
|
|
16
|
+
*/
|
|
17
|
+
export async function verifyPasswordHash(hash, password) {
|
|
18
|
+
return await bcrypt.compare(password, hash);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Validates password strength.
|
|
22
|
+
* @param password Password to validate.
|
|
23
|
+
* @returns Returns true if the password meets complexity requirements.
|
|
24
|
+
*/
|
|
25
|
+
export async function verifyPasswordStrength(password) {
|
|
26
|
+
return password.length >= 8 && password.length <= 255;
|
|
27
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ITokenGenerator } from "./contracts.js";
|
|
2
|
+
/**
|
|
3
|
+
* Oslo-based adapter for the ITokenGenerator port.
|
|
4
|
+
*/
|
|
5
|
+
export declare class OsloTokenGenerator implements ITokenGenerator {
|
|
6
|
+
generateOtp(length?: number): string;
|
|
7
|
+
generateRecoveryCode(): string;
|
|
8
|
+
generateSessionToken(): string;
|
|
9
|
+
hashToken(token: string): string;
|
|
10
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { sha256 } from "@oslojs/crypto/sha2";
|
|
2
|
+
import { encodeBase32LowerCaseNoPadding, encodeBase32UpperCaseNoPadding, encodeHexLowerCase, } from "@oslojs/encoding";
|
|
3
|
+
/**
|
|
4
|
+
* Oslo-based adapter for the ITokenGenerator port.
|
|
5
|
+
*/
|
|
6
|
+
export class OsloTokenGenerator {
|
|
7
|
+
generateOtp(length = 6) {
|
|
8
|
+
const bytes = new Uint8Array(5);
|
|
9
|
+
crypto.getRandomValues(bytes);
|
|
10
|
+
return encodeBase32UpperCaseNoPadding(bytes).substring(0, length);
|
|
11
|
+
}
|
|
12
|
+
generateRecoveryCode() {
|
|
13
|
+
const recoveryCodeBytes = new Uint8Array(10);
|
|
14
|
+
crypto.getRandomValues(recoveryCodeBytes);
|
|
15
|
+
return encodeBase32UpperCaseNoPadding(recoveryCodeBytes);
|
|
16
|
+
}
|
|
17
|
+
generateSessionToken() {
|
|
18
|
+
const tokenBytes = new Uint8Array(20);
|
|
19
|
+
crypto.getRandomValues(tokenBytes);
|
|
20
|
+
return encodeBase32LowerCaseNoPadding(tokenBytes).toLowerCase();
|
|
21
|
+
}
|
|
22
|
+
hashToken(token) {
|
|
23
|
+
return encodeHexLowerCase(sha256(new TextEncoder().encode(token)));
|
|
24
|
+
}
|
|
25
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from "./actions/index.js";
|
|
2
|
+
export { AuthProvider, useAuthRoutes, useUser } from "./client/index.js";
|
|
3
|
+
export * from "./common/index.js";
|
|
4
|
+
export * from "./crypto/index.js";
|
|
5
|
+
export * from "./passwords/index.js";
|
|
6
|
+
export * from "./providers/index.js";
|
|
7
|
+
export * from "./rbac/index.js";
|
|
8
|
+
export * from "./session/index.js";
|
|
9
|
+
export * from "./users/index.js";
|
|
10
|
+
export * from "./validation/index.js";
|
|
11
|
+
export * from "./verification/index.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from "./actions/index.js";
|
|
2
|
+
export { AuthProvider, useAuthRoutes, useUser } from "./client/index.js";
|
|
3
|
+
export * from "./common/index.js";
|
|
4
|
+
export * from "./crypto/index.js";
|
|
5
|
+
export * from "./passwords/index.js";
|
|
6
|
+
export * from "./providers/index.js";
|
|
7
|
+
export * from "./rbac/index.js";
|
|
8
|
+
export * from "./session/index.js";
|
|
9
|
+
export * from "./users/index.js";
|
|
10
|
+
export * from "./validation/index.js";
|
|
11
|
+
export * from "./verification/index.js";
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
export const name = "0001_create_auth_tables";
|
|
2
|
+
export async function up(_db, schema) {
|
|
3
|
+
// 1. Users table
|
|
4
|
+
await schema.createTable("users", (table) => {
|
|
5
|
+
table.text("id").primaryKey();
|
|
6
|
+
table.text("email").notNull().unique();
|
|
7
|
+
table.text("name").notNull();
|
|
8
|
+
table.text("password");
|
|
9
|
+
table.text("image");
|
|
10
|
+
table.bytea("recovery_code").notNull();
|
|
11
|
+
table.timestamp("email_verified_at");
|
|
12
|
+
table.timestamp("created_at").notNull().defaultNow();
|
|
13
|
+
table.timestamp("updated_at");
|
|
14
|
+
});
|
|
15
|
+
// 2. Roles table
|
|
16
|
+
await schema.createTable("roles", (table) => {
|
|
17
|
+
table.text("id").primaryKey();
|
|
18
|
+
table.text("name").notNull().unique();
|
|
19
|
+
table.text("description");
|
|
20
|
+
});
|
|
21
|
+
// 3. Permissions table
|
|
22
|
+
await schema.createTable("permissions", (table) => {
|
|
23
|
+
table.text("id").primaryKey();
|
|
24
|
+
table.text("name").notNull().unique();
|
|
25
|
+
table.text("description");
|
|
26
|
+
});
|
|
27
|
+
// 4. Users to Roles (Pivot)
|
|
28
|
+
await schema.createTable("users_to_roles", (table) => {
|
|
29
|
+
table
|
|
30
|
+
.text("user_id")
|
|
31
|
+
.notNull()
|
|
32
|
+
.references("users", "id")
|
|
33
|
+
.onDelete("CASCADE");
|
|
34
|
+
table
|
|
35
|
+
.text("role_id")
|
|
36
|
+
.notNull()
|
|
37
|
+
.references("roles", "id")
|
|
38
|
+
.onDelete("CASCADE");
|
|
39
|
+
table.primary(["user_id", "role_id"]);
|
|
40
|
+
});
|
|
41
|
+
// 5. Users to Permissions (Pivot)
|
|
42
|
+
await schema.createTable("users_to_permissions", (table) => {
|
|
43
|
+
table
|
|
44
|
+
.text("user_id")
|
|
45
|
+
.notNull()
|
|
46
|
+
.references("users", "id")
|
|
47
|
+
.onDelete("CASCADE");
|
|
48
|
+
table
|
|
49
|
+
.text("permission_id")
|
|
50
|
+
.notNull()
|
|
51
|
+
.references("permissions", "id")
|
|
52
|
+
.onDelete("CASCADE");
|
|
53
|
+
table.primary(["user_id", "permission_id"]);
|
|
54
|
+
});
|
|
55
|
+
// 6. Roles to Permissions (Pivot)
|
|
56
|
+
await schema.createTable("roles_to_permissions", (table) => {
|
|
57
|
+
table
|
|
58
|
+
.text("role_id")
|
|
59
|
+
.notNull()
|
|
60
|
+
.references("roles", "id")
|
|
61
|
+
.onDelete("CASCADE");
|
|
62
|
+
table
|
|
63
|
+
.text("permission_id")
|
|
64
|
+
.notNull()
|
|
65
|
+
.references("permissions", "id")
|
|
66
|
+
.onDelete("CASCADE");
|
|
67
|
+
table.primary(["role_id", "permission_id"]);
|
|
68
|
+
});
|
|
69
|
+
// 7. Sessions table
|
|
70
|
+
await schema.createTable("sessions", (table) => {
|
|
71
|
+
table.text("id").primaryKey();
|
|
72
|
+
table
|
|
73
|
+
.text("user_id")
|
|
74
|
+
.notNull()
|
|
75
|
+
.references("users", "id")
|
|
76
|
+
.onDelete("CASCADE");
|
|
77
|
+
table.json("metadata");
|
|
78
|
+
table.timestamp("expires_at").notNull();
|
|
79
|
+
table.timestamp("created_at").notNull().defaultNow();
|
|
80
|
+
table.timestamp("updated_at");
|
|
81
|
+
});
|
|
82
|
+
// 8. Email Verification Requests
|
|
83
|
+
await schema.createTable("verification_requests", (table) => {
|
|
84
|
+
table.text("id").primaryKey();
|
|
85
|
+
table.text("email").notNull();
|
|
86
|
+
table.text("code").notNull();
|
|
87
|
+
table
|
|
88
|
+
.text("user_id")
|
|
89
|
+
.notNull()
|
|
90
|
+
.references("users", "id")
|
|
91
|
+
.onDelete("CASCADE");
|
|
92
|
+
table.timestamp("expires_at").notNull();
|
|
93
|
+
table.timestamp("created_at").notNull().defaultNow();
|
|
94
|
+
table.timestamp("updated_at");
|
|
95
|
+
});
|
|
96
|
+
// 9. Password Reset Sessions
|
|
97
|
+
await schema.createTable("reset_sessions", (table) => {
|
|
98
|
+
table.text("id").primaryKey();
|
|
99
|
+
table.text("email").notNull();
|
|
100
|
+
table.text("code").notNull();
|
|
101
|
+
table.boolean("email_verified").default(false);
|
|
102
|
+
table
|
|
103
|
+
.text("user_id")
|
|
104
|
+
.notNull()
|
|
105
|
+
.references("users", "id")
|
|
106
|
+
.onDelete("CASCADE");
|
|
107
|
+
table.timestamp("expires_at").notNull();
|
|
108
|
+
table.timestamp("created_at").notNull().defaultNow();
|
|
109
|
+
table.timestamp("updated_at");
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
export async function down(_db, schema) {
|
|
113
|
+
await schema.dropTable("reset_sessions");
|
|
114
|
+
await schema.dropTable("verification_requests");
|
|
115
|
+
await schema.dropTable("sessions");
|
|
116
|
+
await schema.dropTable("roles_to_permissions");
|
|
117
|
+
await schema.dropTable("users_to_permissions");
|
|
118
|
+
await schema.dropTable("users_to_roles");
|
|
119
|
+
await schema.dropTable("permissions");
|
|
120
|
+
await schema.dropTable("roles");
|
|
121
|
+
await schema.dropTable("users");
|
|
122
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { runMigrations } from "@veap/database";
|
|
2
|
+
import * as m0001 from "./0001_create_auth_tables.js";
|
|
3
|
+
export const authMigrations = [m0001];
|
|
4
|
+
export async function runAuthMigrations() {
|
|
5
|
+
await runMigrations("auth", authMigrations);
|
|
6
|
+
}
|
|
7
|
+
export default authMigrations;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type CastType, Model } from "@veap/database";
|
|
2
|
+
import { User } from "./User.js";
|
|
3
|
+
export interface EmailVerificationAttributes {
|
|
4
|
+
id: string;
|
|
5
|
+
email: string;
|
|
6
|
+
code: string;
|
|
7
|
+
userId: string;
|
|
8
|
+
user_id?: string;
|
|
9
|
+
expiresAt: Date;
|
|
10
|
+
expires_at?: Date;
|
|
11
|
+
createdAt?: Date;
|
|
12
|
+
created_at?: Date;
|
|
13
|
+
updatedAt?: Date | null;
|
|
14
|
+
updated_at?: Date | null;
|
|
15
|
+
}
|
|
16
|
+
export declare class EmailVerification extends Model<EmailVerificationAttributes> {
|
|
17
|
+
static table: string;
|
|
18
|
+
static casts: Record<string, CastType>;
|
|
19
|
+
/**
|
|
20
|
+
* Associated user relation.
|
|
21
|
+
*/
|
|
22
|
+
user(): import("@veap/database").BelongsTo<this, User>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Model } from "@veap/database";
|
|
2
|
+
import { User } from "./User.js";
|
|
3
|
+
export class EmailVerification extends Model {
|
|
4
|
+
static table = "verification_requests";
|
|
5
|
+
static casts = {
|
|
6
|
+
expiresAt: "datetime",
|
|
7
|
+
expires_at: "datetime",
|
|
8
|
+
createdAt: "datetime",
|
|
9
|
+
created_at: "datetime",
|
|
10
|
+
updatedAt: "datetime",
|
|
11
|
+
updated_at: "datetime",
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Associated user relation.
|
|
15
|
+
*/
|
|
16
|
+
user() {
|
|
17
|
+
return this.belongsTo(User, "user_id");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type CastType, Model } from "@veap/database";
|
|
2
|
+
import { User } from "./User.js";
|
|
3
|
+
export interface PasswordResetSessionAttributes {
|
|
4
|
+
id: string;
|
|
5
|
+
email: string;
|
|
6
|
+
code: string;
|
|
7
|
+
emailVerified?: boolean;
|
|
8
|
+
email_verified?: boolean;
|
|
9
|
+
userId: string;
|
|
10
|
+
user_id?: string;
|
|
11
|
+
expiresAt: Date;
|
|
12
|
+
expires_at?: Date;
|
|
13
|
+
createdAt?: Date;
|
|
14
|
+
created_at?: Date;
|
|
15
|
+
updatedAt?: Date | null;
|
|
16
|
+
updated_at?: Date | null;
|
|
17
|
+
}
|
|
18
|
+
export declare class PasswordResetSession extends Model<PasswordResetSessionAttributes> {
|
|
19
|
+
static table: string;
|
|
20
|
+
static casts: Record<string, CastType>;
|
|
21
|
+
/**
|
|
22
|
+
* Associated user relation.
|
|
23
|
+
*/
|
|
24
|
+
user(): import("@veap/database").BelongsTo<this, User>;
|
|
25
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Model } from "@veap/database";
|
|
2
|
+
import { User } from "./User.js";
|
|
3
|
+
export class PasswordResetSession extends Model {
|
|
4
|
+
static table = "reset_sessions";
|
|
5
|
+
static casts = {
|
|
6
|
+
emailVerified: "boolean",
|
|
7
|
+
email_verified: "boolean",
|
|
8
|
+
expiresAt: "datetime",
|
|
9
|
+
expires_at: "datetime",
|
|
10
|
+
createdAt: "datetime",
|
|
11
|
+
created_at: "datetime",
|
|
12
|
+
updatedAt: "datetime",
|
|
13
|
+
updated_at: "datetime",
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Associated user relation.
|
|
17
|
+
*/
|
|
18
|
+
user() {
|
|
19
|
+
return this.belongsTo(User, "user_id");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Model } from "@veap/database";
|
|
2
|
+
import { Role } from "./Role.js";
|
|
3
|
+
import { User } from "./User.js";
|
|
4
|
+
export interface PermissionAttributes {
|
|
5
|
+
id: string;
|
|
6
|
+
name: string;
|
|
7
|
+
description?: string | null;
|
|
8
|
+
}
|
|
9
|
+
export declare class Permission extends Model<PermissionAttributes> {
|
|
10
|
+
static table: string;
|
|
11
|
+
static timestamps: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Roles that have this permission.
|
|
14
|
+
*/
|
|
15
|
+
roles(): import("@veap/database").BelongsToMany<this, Role>;
|
|
16
|
+
/**
|
|
17
|
+
* Users that have this permission directly.
|
|
18
|
+
*/
|
|
19
|
+
users(): import("@veap/database").BelongsToMany<this, User>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Model } from "@veap/database";
|
|
2
|
+
import { Role } from "./Role.js";
|
|
3
|
+
import { User } from "./User.js";
|
|
4
|
+
export class Permission extends Model {
|
|
5
|
+
static table = "permissions";
|
|
6
|
+
static timestamps = false;
|
|
7
|
+
/**
|
|
8
|
+
* Roles that have this permission.
|
|
9
|
+
*/
|
|
10
|
+
roles() {
|
|
11
|
+
return this.belongsToMany(Role, "roles_to_permissions", "permission_id", "role_id", "id", "id");
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Users that have this permission directly.
|
|
15
|
+
*/
|
|
16
|
+
users() {
|
|
17
|
+
return this.belongsToMany(User, "users_to_permissions", "permission_id", "user_id", "id", "id");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Model } from "@veap/database";
|
|
2
|
+
import { Permission } from "./Permission.js";
|
|
3
|
+
import { User } from "./User.js";
|
|
4
|
+
export interface RoleAttributes {
|
|
5
|
+
id: string;
|
|
6
|
+
name: string;
|
|
7
|
+
description?: string | null;
|
|
8
|
+
}
|
|
9
|
+
export declare class Role extends Model<RoleAttributes> {
|
|
10
|
+
static table: string;
|
|
11
|
+
static timestamps: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Users assigned to this role.
|
|
14
|
+
*/
|
|
15
|
+
users(): import("@veap/database").BelongsToMany<this, User>;
|
|
16
|
+
/**
|
|
17
|
+
* Permissions granted to this role.
|
|
18
|
+
*/
|
|
19
|
+
permissions(): import("@veap/database").BelongsToMany<this, Permission>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Model } from "@veap/database";
|
|
2
|
+
import { Permission } from "./Permission.js";
|
|
3
|
+
import { User } from "./User.js";
|
|
4
|
+
export class Role extends Model {
|
|
5
|
+
static table = "roles";
|
|
6
|
+
static timestamps = false;
|
|
7
|
+
/**
|
|
8
|
+
* Users assigned to this role.
|
|
9
|
+
*/
|
|
10
|
+
users() {
|
|
11
|
+
return this.belongsToMany(User, "users_to_roles", "role_id", "user_id", "id", "id");
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Permissions granted to this role.
|
|
15
|
+
*/
|
|
16
|
+
permissions() {
|
|
17
|
+
return this.belongsToMany(Permission, "roles_to_permissions", "role_id", "permission_id", "id", "id");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type CastType, Model } from "@veap/database";
|
|
2
|
+
import { User } from "./User.js";
|
|
3
|
+
export interface SessionAttributes {
|
|
4
|
+
id: string;
|
|
5
|
+
userId?: string;
|
|
6
|
+
user_id?: string;
|
|
7
|
+
metadata?: Record<string, any> | null;
|
|
8
|
+
expiresAt: Date;
|
|
9
|
+
expires_at?: Date;
|
|
10
|
+
createdAt?: Date;
|
|
11
|
+
created_at?: Date;
|
|
12
|
+
updatedAt?: Date | null;
|
|
13
|
+
updated_at?: Date | null;
|
|
14
|
+
}
|
|
15
|
+
export declare class Session extends Model<SessionAttributes> {
|
|
16
|
+
static table: string;
|
|
17
|
+
static casts: Record<string, CastType>;
|
|
18
|
+
/**
|
|
19
|
+
* Session owner user relation.
|
|
20
|
+
*/
|
|
21
|
+
user(): import("@veap/database").BelongsTo<this, User>;
|
|
22
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Model } from "@veap/database";
|
|
2
|
+
import { User } from "./User.js";
|
|
3
|
+
export class Session extends Model {
|
|
4
|
+
static table = "sessions";
|
|
5
|
+
static casts = {
|
|
6
|
+
metadata: "json",
|
|
7
|
+
expiresAt: "datetime",
|
|
8
|
+
expires_at: "datetime",
|
|
9
|
+
createdAt: "datetime",
|
|
10
|
+
created_at: "datetime",
|
|
11
|
+
updatedAt: "datetime",
|
|
12
|
+
updated_at: "datetime",
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Session owner user relation.
|
|
16
|
+
*/
|
|
17
|
+
user() {
|
|
18
|
+
const fk = this.getAttribute("user_id") !== undefined ? "user_id" : "userId";
|
|
19
|
+
return this.belongsTo(User, fk);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type CastType, Model } from "@veap/database";
|
|
2
|
+
import { Permission } from "./Permission.js";
|
|
3
|
+
import { Role } from "./Role.js";
|
|
4
|
+
import { Session } from "./Session.js";
|
|
5
|
+
export interface UserAttributes {
|
|
6
|
+
id: string;
|
|
7
|
+
email: string;
|
|
8
|
+
name: string;
|
|
9
|
+
password?: string | null;
|
|
10
|
+
image?: string | null;
|
|
11
|
+
recovery_code?: any;
|
|
12
|
+
emailVerifiedAt?: Date | null;
|
|
13
|
+
createdAt?: Date;
|
|
14
|
+
updatedAt?: Date | null;
|
|
15
|
+
}
|
|
16
|
+
export declare class User extends Model<UserAttributes> {
|
|
17
|
+
static table: string;
|
|
18
|
+
static hidden: string[];
|
|
19
|
+
static casts: Record<string, CastType>;
|
|
20
|
+
/**
|
|
21
|
+
* User sessions relation.
|
|
22
|
+
*/
|
|
23
|
+
sessions(): import("@veap/database").HasMany<this, Session>;
|
|
24
|
+
/**
|
|
25
|
+
* User roles relation via users_to_roles pivot table.
|
|
26
|
+
*/
|
|
27
|
+
roles(): import("@veap/database").BelongsToMany<this, Role>;
|
|
28
|
+
/**
|
|
29
|
+
* User direct permissions relation via users_to_permissions pivot table.
|
|
30
|
+
*/
|
|
31
|
+
permissions(): import("@veap/database").BelongsToMany<this, Permission>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Model } from "@veap/database";
|
|
2
|
+
import { Permission } from "./Permission.js";
|
|
3
|
+
import { Role } from "./Role.js";
|
|
4
|
+
import { Session } from "./Session.js";
|
|
5
|
+
export class User extends Model {
|
|
6
|
+
static table = "users";
|
|
7
|
+
static hidden = ["password", "recovery_code"];
|
|
8
|
+
static casts = {
|
|
9
|
+
emailVerifiedAt: "datetime",
|
|
10
|
+
createdAt: "datetime",
|
|
11
|
+
updatedAt: "datetime",
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* User sessions relation.
|
|
15
|
+
*/
|
|
16
|
+
sessions() {
|
|
17
|
+
return this.hasMany(Session, "user_id");
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* User roles relation via users_to_roles pivot table.
|
|
21
|
+
*/
|
|
22
|
+
roles() {
|
|
23
|
+
return this.belongsToMany(Role, "users_to_roles", "user_id", "role_id", "id", "id");
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* User direct permissions relation via users_to_permissions pivot table.
|
|
27
|
+
*/
|
|
28
|
+
permissions() {
|
|
29
|
+
return this.belongsToMany(Permission, "users_to_permissions", "user_id", "permission_id", "id", "id");
|
|
30
|
+
}
|
|
31
|
+
}
|
package/dist/models.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./models/index.js";
|
package/dist/models.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./models/index.js";
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { PasswordResetAuthSession, PasswordResetSession } from "../common/index.js";
|
|
2
|
+
export type { PasswordResetAuthSession, PasswordResetSession };
|
|
3
|
+
/** A password reset session together with its owner. */
|
|
4
|
+
export interface PasswordResetWithUser {
|
|
5
|
+
session: PasswordResetSession;
|
|
6
|
+
user: Record<string, any>;
|
|
7
|
+
}
|
|
8
|
+
export interface CreatePasswordResetRecord {
|
|
9
|
+
id: string;
|
|
10
|
+
userId: string;
|
|
11
|
+
email: string;
|
|
12
|
+
code: string;
|
|
13
|
+
expiresAt: Date;
|
|
14
|
+
}
|
|
15
|
+
/** Password reset session persistence port. */
|
|
16
|
+
export interface IPasswordResetRepository {
|
|
17
|
+
create(record: CreatePasswordResetRecord): Promise<PasswordResetSession>;
|
|
18
|
+
/**
|
|
19
|
+
* Returns the reset session with its owner eager-loaded, or `null` when
|
|
20
|
+
* either of them is missing.
|
|
21
|
+
*/
|
|
22
|
+
findWithUser(id: string): Promise<PasswordResetWithUser | null>;
|
|
23
|
+
setEmailVerified(id: string): Promise<void>;
|
|
24
|
+
remove(id: string): Promise<void>;
|
|
25
|
+
removeByUserId(userId: string): Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
/** Injection token for the password reset repository port. */
|
|
28
|
+
export declare const PASSWORD_RESET_REPOSITORY: unique symbol;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { PasswordResetAuthSession, PasswordResetSession as PasswordResetSessionType } from "../common/index.js";
|
|
2
|
+
export declare function createPasswordResetSession(token: string, userId: string, email: string): Promise<PasswordResetSessionType>;
|
|
3
|
+
export declare function validatePasswordResetSessionToken(token: string): Promise<PasswordResetAuthSession>;
|
|
4
|
+
export declare function setPasswordResetSessionAsEmailVerified(sessionId: string): Promise<void>;
|
|
5
|
+
export declare function invalidateUserPasswordResetSessions(userId: string): Promise<void>;
|
|
6
|
+
export declare function getCurrentPasswordResetSession(): Promise<PasswordResetAuthSession>;
|
|
7
|
+
export declare function setPasswordResetSessionTokenCookie(token: string, expiresAt: Date): Promise<void>;
|
|
8
|
+
export declare function deletePasswordResetSessionTokenCookie(): Promise<void>;
|
|
9
|
+
export declare function sendPasswordResetEmail(email: string, code: string): Promise<void>;
|
|
10
|
+
export declare function createDummyPasswordResetSession(token: string, email: string): Promise<PasswordResetSessionType>;
|
|
11
|
+
export declare function verifyResetCode(sessionId: string, code: string): Promise<{
|
|
12
|
+
valid: boolean;
|
|
13
|
+
error?: string;
|
|
14
|
+
}>;
|