@scryan7371/sdr-security 0.1.10 → 0.1.11
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/dist/nest/entities/password-reset-token.entity.js +2 -2
- package/dist/nest/entities/security-role.entity.js +2 -2
- package/dist/nest/entities/security-user-role.entity.js +2 -2
- package/dist/nest/security-auth.service.js +4 -4
- package/dist/nest/security-workflows.service.js +4 -4
- package/dist/nest/uuid-v7.d.ts +1 -0
- package/dist/nest/uuid-v7.js +21 -0
- package/package.json +2 -3
- package/src/nest/entities/password-reset-token.entity.ts +1 -1
- package/src/nest/entities/security-role.entity.ts +1 -1
- package/src/nest/entities/security-user-role.entity.ts +1 -1
- package/src/nest/security-auth.service.ts +1 -1
- package/src/nest/security-workflows.service.ts +1 -1
- package/src/nest/uuid-v7.ts +23 -0
|
@@ -11,7 +11,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.PasswordResetTokenEntity = void 0;
|
|
13
13
|
const typeorm_1 = require("typeorm");
|
|
14
|
-
const
|
|
14
|
+
const uuid_v7_1 = require("../uuid-v7");
|
|
15
15
|
let PasswordResetTokenEntity = class PasswordResetTokenEntity {
|
|
16
16
|
id;
|
|
17
17
|
userId;
|
|
@@ -21,7 +21,7 @@ let PasswordResetTokenEntity = class PasswordResetTokenEntity {
|
|
|
21
21
|
createdAt;
|
|
22
22
|
ensureId() {
|
|
23
23
|
if (!this.id) {
|
|
24
|
-
this.id = (0,
|
|
24
|
+
this.id = (0, uuid_v7_1.uuidv7)();
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
};
|
|
@@ -10,7 +10,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.SecurityRoleEntity = void 0;
|
|
13
|
-
const
|
|
13
|
+
const uuid_v7_1 = require("../uuid-v7");
|
|
14
14
|
const typeorm_1 = require("typeorm");
|
|
15
15
|
let SecurityRoleEntity = class SecurityRoleEntity {
|
|
16
16
|
id;
|
|
@@ -19,7 +19,7 @@ let SecurityRoleEntity = class SecurityRoleEntity {
|
|
|
19
19
|
isSystem;
|
|
20
20
|
ensureId() {
|
|
21
21
|
if (!this.id) {
|
|
22
|
-
this.id = (0,
|
|
22
|
+
this.id = (0, uuid_v7_1.uuidv7)();
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
};
|
|
@@ -10,7 +10,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.SecurityUserRoleEntity = void 0;
|
|
13
|
-
const
|
|
13
|
+
const uuid_v7_1 = require("../uuid-v7");
|
|
14
14
|
const typeorm_1 = require("typeorm");
|
|
15
15
|
let SecurityUserRoleEntity = class SecurityUserRoleEntity {
|
|
16
16
|
id;
|
|
@@ -18,7 +18,7 @@ let SecurityUserRoleEntity = class SecurityUserRoleEntity {
|
|
|
18
18
|
roleId;
|
|
19
19
|
ensureId() {
|
|
20
20
|
if (!this.id) {
|
|
21
|
-
this.id = (0,
|
|
21
|
+
this.id = (0, uuid_v7_1.uuidv7)();
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
};
|
|
@@ -17,7 +17,7 @@ const common_1 = require("@nestjs/common");
|
|
|
17
17
|
const crypto_1 = require("crypto");
|
|
18
18
|
const bcryptjs_1 = require("bcryptjs");
|
|
19
19
|
const jsonwebtoken_1 = require("jsonwebtoken");
|
|
20
|
-
const
|
|
20
|
+
const uuid_v7_1 = require("./uuid-v7");
|
|
21
21
|
const typeorm_1 = require("@nestjs/typeorm");
|
|
22
22
|
const typeorm_2 = require("typeorm");
|
|
23
23
|
const roles_1 = require("../api/roles");
|
|
@@ -59,7 +59,7 @@ let SecurityAuthService = class SecurityAuthService {
|
|
|
59
59
|
throw new common_1.BadRequestException("Email already in use");
|
|
60
60
|
}
|
|
61
61
|
const appUser = await this.appUsersRepo.save(this.appUsersRepo.create({
|
|
62
|
-
id: (0,
|
|
62
|
+
id: (0, uuid_v7_1.uuidv7)(),
|
|
63
63
|
email,
|
|
64
64
|
}));
|
|
65
65
|
const securityUser = await this.securityUsersRepo.save(this.securityUsersRepo.create({
|
|
@@ -163,7 +163,7 @@ let SecurityAuthService = class SecurityAuthService {
|
|
|
163
163
|
const expiresAt = new Date(Date.now() +
|
|
164
164
|
(this.options.passwordResetTokenExpiresInMinutes ?? 30) * 60_000);
|
|
165
165
|
await this.passwordResetRepo.save(this.passwordResetRepo.create({
|
|
166
|
-
id: (0,
|
|
166
|
+
id: (0, uuid_v7_1.uuidv7)(),
|
|
167
167
|
userId: appUser.id,
|
|
168
168
|
token,
|
|
169
169
|
expiresAt,
|
|
@@ -223,7 +223,7 @@ let SecurityAuthService = class SecurityAuthService {
|
|
|
223
223
|
const refreshTokenExpiresAt = new Date(Date.now() +
|
|
224
224
|
(this.options.refreshTokenExpiresInDays ?? 30) * 24 * 60 * 60 * 1000);
|
|
225
225
|
await this.refreshTokenRepo.save(this.refreshTokenRepo.create({
|
|
226
|
-
id: (0,
|
|
226
|
+
id: (0, uuid_v7_1.uuidv7)(),
|
|
227
227
|
userId: appUser.id,
|
|
228
228
|
tokenHash: refreshTokenHash,
|
|
229
229
|
expiresAt: refreshTokenExpiresAt,
|
|
@@ -16,7 +16,7 @@ exports.SecurityWorkflowsService = void 0;
|
|
|
16
16
|
const common_1 = require("@nestjs/common");
|
|
17
17
|
const typeorm_1 = require("@nestjs/typeorm");
|
|
18
18
|
const typeorm_2 = require("typeorm");
|
|
19
|
-
const
|
|
19
|
+
const uuid_v7_1 = require("./uuid-v7");
|
|
20
20
|
const contracts_1 = require("../api/contracts");
|
|
21
21
|
const roles_1 = require("../api/roles");
|
|
22
22
|
const app_user_entity_1 = require("./entities/app-user.entity");
|
|
@@ -95,7 +95,7 @@ let SecurityWorkflowsService = class SecurityWorkflowsService {
|
|
|
95
95
|
let role = await this.rolesRepo.findOne({ where: { roleKey } });
|
|
96
96
|
if (!role) {
|
|
97
97
|
role = this.rolesRepo.create({
|
|
98
|
-
id: (0,
|
|
98
|
+
id: (0, uuid_v7_1.uuidv7)(),
|
|
99
99
|
roleKey,
|
|
100
100
|
description: description?.trim() || null,
|
|
101
101
|
isSystem: roleKey === contracts_1.ADMIN_ROLE,
|
|
@@ -141,7 +141,7 @@ let SecurityWorkflowsService = class SecurityWorkflowsService {
|
|
|
141
141
|
await this.userRolesRepo.delete({ userId });
|
|
142
142
|
if (roles.length > 0) {
|
|
143
143
|
await this.userRolesRepo.save(roles.map((role) => this.userRolesRepo.create({
|
|
144
|
-
id: (0,
|
|
144
|
+
id: (0, uuid_v7_1.uuidv7)(),
|
|
145
145
|
userId,
|
|
146
146
|
roleId: role.id,
|
|
147
147
|
})));
|
|
@@ -185,7 +185,7 @@ let SecurityWorkflowsService = class SecurityWorkflowsService {
|
|
|
185
185
|
return;
|
|
186
186
|
}
|
|
187
187
|
await this.rolesRepo.save(missing.map((roleKey) => this.rolesRepo.create({
|
|
188
|
-
id: (0,
|
|
188
|
+
id: (0, uuid_v7_1.uuidv7)(),
|
|
189
189
|
roleKey,
|
|
190
190
|
description: null,
|
|
191
191
|
isSystem: roleKey === contracts_1.ADMIN_ROLE,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function uuidv7(): string;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.uuidv7 = uuidv7;
|
|
4
|
+
const crypto_1 = require("crypto");
|
|
5
|
+
function bytesToHex(bytes) {
|
|
6
|
+
return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
7
|
+
}
|
|
8
|
+
function uuidv7() {
|
|
9
|
+
const bytes = (0, crypto_1.randomBytes)(16);
|
|
10
|
+
const timestamp = BigInt(Date.now());
|
|
11
|
+
bytes[0] = Number((timestamp >> 40n) & 0xffn);
|
|
12
|
+
bytes[1] = Number((timestamp >> 32n) & 0xffn);
|
|
13
|
+
bytes[2] = Number((timestamp >> 24n) & 0xffn);
|
|
14
|
+
bytes[3] = Number((timestamp >> 16n) & 0xffn);
|
|
15
|
+
bytes[4] = Number((timestamp >> 8n) & 0xffn);
|
|
16
|
+
bytes[5] = Number(timestamp & 0xffn);
|
|
17
|
+
bytes[6] = (bytes[6] & 0x0f) | 0x70;
|
|
18
|
+
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
19
|
+
const hex = bytesToHex(bytes);
|
|
20
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20, 32)}`;
|
|
21
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scryan7371/sdr-security",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.11",
|
|
4
4
|
"description": "Reusable auth/security capability for API and app clients.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -45,8 +45,7 @@
|
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@babel/runtime": "7.29.2",
|
|
47
47
|
"bcryptjs": "3.0.3",
|
|
48
|
-
"jsonwebtoken": "9.0.3"
|
|
49
|
-
"uuid": "13.0.0"
|
|
48
|
+
"jsonwebtoken": "9.0.3"
|
|
50
49
|
},
|
|
51
50
|
"peerDependencies": {
|
|
52
51
|
"@nestjs/common": "^11.0.0",
|
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
import { randomBytes } from "crypto";
|
|
8
8
|
import { compare, hash } from "bcryptjs";
|
|
9
9
|
import { sign, type SignOptions } from "jsonwebtoken";
|
|
10
|
-
import {
|
|
10
|
+
import { uuidv7 } from "./uuid-v7";
|
|
11
11
|
import { InjectRepository } from "@nestjs/typeorm";
|
|
12
12
|
import { In, IsNull, Repository } from "typeorm";
|
|
13
13
|
import { AuthResponse, RegisterResponse } from "../api/contracts";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Inject, Injectable, NotFoundException } from "@nestjs/common";
|
|
2
2
|
import { InjectRepository } from "@nestjs/typeorm";
|
|
3
3
|
import { In, Repository } from "typeorm";
|
|
4
|
-
import {
|
|
4
|
+
import { uuidv7 } from "./uuid-v7";
|
|
5
5
|
import { ADMIN_ROLE } from "../api/contracts";
|
|
6
6
|
import { normalizeRoleName } from "../api/roles";
|
|
7
7
|
import { AppUserEntity } from "./entities/app-user.entity";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { randomBytes } from "crypto";
|
|
2
|
+
|
|
3
|
+
function bytesToHex(bytes: Uint8Array) {
|
|
4
|
+
return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function uuidv7(): string {
|
|
8
|
+
const bytes = randomBytes(16);
|
|
9
|
+
const timestamp = BigInt(Date.now());
|
|
10
|
+
|
|
11
|
+
bytes[0] = Number((timestamp >> 40n) & 0xffn);
|
|
12
|
+
bytes[1] = Number((timestamp >> 32n) & 0xffn);
|
|
13
|
+
bytes[2] = Number((timestamp >> 24n) & 0xffn);
|
|
14
|
+
bytes[3] = Number((timestamp >> 16n) & 0xffn);
|
|
15
|
+
bytes[4] = Number((timestamp >> 8n) & 0xffn);
|
|
16
|
+
bytes[5] = Number(timestamp & 0xffn);
|
|
17
|
+
|
|
18
|
+
bytes[6] = (bytes[6] & 0x0f) | 0x70;
|
|
19
|
+
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
20
|
+
|
|
21
|
+
const hex = bytesToHex(bytes);
|
|
22
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20, 32)}`;
|
|
23
|
+
}
|