@drax/identity-back 0.0.12 → 0.0.14
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/config/IdentityConfig.js +12 -0
- package/dist/factory/RoleServiceFactory.js +16 -13
- package/dist/factory/UserServiceFactory.js +16 -14
- package/dist/graphql/resolvers/role.resolvers.js +5 -1
- package/dist/graphql/resolvers/user.resolvers.js +1 -1
- package/dist/index.js +11 -1
- package/dist/middleware/rbacMiddleware.js +4 -3
- package/dist/routes/RoleRoutes.js +5 -1
- package/dist/routes/UserRoutes.js +1 -1
- package/dist/services/RoleService.js +1 -5
- package/dist/setup/CreateOrUpdateRole.js +15 -0
- package/dist/setup/CreateUserIfNotExist.js +17 -0
- package/dist/setup/LoadConfigFromEnv.js +12 -0
- package/dist/setup/LoadPermissions.js +8 -0
- package/dist/setup/RecoveryUserPassword.js +9 -0
- package/dist/utils/AuthUtils.js +9 -5
- package/dist/utils/DbSetupUtils.js +10 -8
- package/package.json +2 -2
- package/src/config/IdentityConfig.ts +17 -0
- package/src/factory/RoleServiceFactory.ts +19 -13
- package/src/factory/UserServiceFactory.ts +18 -14
- package/src/graphql/resolvers/role.resolvers.ts +7 -1
- package/src/graphql/resolvers/user.resolvers.ts +1 -1
- package/src/index.ts +18 -1
- package/src/middleware/rbacMiddleware.ts +4 -3
- package/src/routes/RoleRoutes.ts +7 -1
- package/src/routes/UserRoutes.ts +1 -1
- package/src/services/RoleService.ts +0 -4
- package/src/setup/CreateOrUpdateRole.ts +19 -0
- package/src/setup/CreateUserIfNotExist.ts +21 -0
- package/src/setup/LoadConfigFromEnv.ts +16 -0
- package/src/setup/LoadPermissions.ts +12 -0
- package/src/setup/RecoveryUserPassword.ts +13 -0
- package/src/utils/AuthUtils.ts +10 -5
- package/src/utils/DbSetupUtils.ts +10 -8
- package/tsconfig.tsbuildinfo +1 -1
- package/types/config/IdentityConfig.d.ts +12 -0
- package/types/config/IdentityConfig.d.ts.map +1 -0
- package/types/factory/RoleServiceFactory.d.ts +2 -2
- package/types/factory/RoleServiceFactory.d.ts.map +1 -1
- package/types/factory/UserServiceFactory.d.ts +2 -2
- package/types/factory/UserServiceFactory.d.ts.map +1 -1
- package/types/graphql/resolvers/role.resolvers.d.ts.map +1 -1
- package/types/index.d.ts +7 -1
- package/types/index.d.ts.map +1 -1
- package/types/interfaces/IID.d.ts +1 -1
- package/types/middleware/rbacMiddleware.d.ts.map +1 -1
- package/types/routes/RoleRoutes.d.ts.map +1 -1
- package/types/routes/UserRoutes.d.ts +1 -1
- package/types/services/RoleService.d.ts.map +1 -1
- package/types/setup/CreateOrUpdateRole.d.ts +5 -0
- package/types/setup/CreateOrUpdateRole.d.ts.map +1 -0
- package/types/setup/CreateUserIfNotExist.d.ts +5 -0
- package/types/setup/CreateUserIfNotExist.d.ts.map +1 -0
- package/types/setup/LoadConfigFromEnv.d.ts +4 -0
- package/types/setup/LoadConfigFromEnv.d.ts.map +1 -0
- package/types/setup/LoadPermissions.d.ts +4 -0
- package/types/setup/LoadPermissions.d.ts.map +1 -0
- package/types/setup/RecoveryUserPassword.d.ts +4 -0
- package/types/setup/RecoveryUserPassword.d.ts.map +1 -0
- package/types/utils/AuthUtils.d.ts.map +1 -1
- package/types/utils/DbSetupUtils.d.ts +1 -1
- package/types/utils/DbSetupUtils.d.ts.map +1 -1
- package/types/zod/UserZod.d.ts +6 -6
- package/dist/i18n/messages/validation-i18n.js +0 -21
- package/dist/routes/authRoutes.js +0 -29
- package/dist/services/AuthService.js +0 -6
- package/types/i18n/messages/validation-i18n.d.ts +0 -4
- package/types/i18n/messages/validation-i18n.d.ts.map +0 -1
- package/types/routes/authRoutes.d.ts +0 -4
- package/types/routes/authRoutes.d.ts.map +0 -1
- package/types/services/AuthService.d.ts +0 -7
- package/types/services/AuthService.d.ts.map +0 -1
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import RoleServiceFactory from "../factory/RoleServiceFactory.js"
|
|
2
|
+
import UserServiceFactory from "../factory/UserServiceFactory.js"
|
|
3
|
+
import {IUserCreate} from "../interfaces/IUser";
|
|
4
|
+
|
|
5
|
+
async function CreateUserIfNotExist(userData: IUserCreate) {
|
|
6
|
+
const userService = UserServiceFactory()
|
|
7
|
+
const roleService = RoleServiceFactory()
|
|
8
|
+
const user = await userService.findByUsername(userData.username)
|
|
9
|
+
if (user) {
|
|
10
|
+
} else {
|
|
11
|
+
const role = await roleService.findByName(userData.role as string)
|
|
12
|
+
userData.role = role.id as string
|
|
13
|
+
const r = await userService.create(userData)
|
|
14
|
+
console.log("User Created. Username: " + userData.username)
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default CreateUserIfNotExist
|
|
19
|
+
export {
|
|
20
|
+
CreateUserIfNotExist
|
|
21
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {DraxConfig} from "@drax/common-back";
|
|
2
|
+
import IdentityConfig from "../config/IdentityConfig.js";
|
|
3
|
+
|
|
4
|
+
function LoadConfigFromEnv() {
|
|
5
|
+
DraxConfig.set(IdentityConfig.DbEngine, process.env[IdentityConfig.DbEngine])
|
|
6
|
+
DraxConfig.set(IdentityConfig.SqliteDbFile, process.env[IdentityConfig.SqliteDbFile])
|
|
7
|
+
DraxConfig.set(IdentityConfig.MongoDbUri, process.env[IdentityConfig.MongoDbUri])
|
|
8
|
+
DraxConfig.set(IdentityConfig.JwtSecret, process.env[IdentityConfig.JwtSecret])
|
|
9
|
+
DraxConfig.set(IdentityConfig.JwtExpiration, process.env[IdentityConfig.JwtExpiration])
|
|
10
|
+
DraxConfig.set(IdentityConfig.JwtIssuer, process.env[IdentityConfig.JwtIssuer])
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default LoadConfigFromEnv
|
|
14
|
+
export {
|
|
15
|
+
LoadConfigFromEnv
|
|
16
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import {PermissionService} from "../services/PermissionService.js";
|
|
2
|
+
|
|
3
|
+
function LoadPermissions(permissions: string[]) {
|
|
4
|
+
for(const permission of permissions){
|
|
5
|
+
PermissionService.addPermission(permission)
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export default LoadPermissions
|
|
10
|
+
export {
|
|
11
|
+
LoadPermissions
|
|
12
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import UserServiceFactory from "../factory/UserServiceFactory.js"
|
|
2
|
+
|
|
3
|
+
async function RecoveryUserPassword(username:string, password:string) {
|
|
4
|
+
const userService = UserServiceFactory()
|
|
5
|
+
const user = await userService.findByUsername(username)
|
|
6
|
+
const r = await userService.changeUserPassword(user.id as string, password)
|
|
7
|
+
console.log("User password recovered. Username: " + username)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default RecoveryUserPassword
|
|
11
|
+
export {
|
|
12
|
+
RecoveryUserPassword
|
|
13
|
+
}
|
package/src/utils/AuthUtils.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import bcryptjs from "bcryptjs";
|
|
2
2
|
import jsonwebtoken, {SignOptions, VerifyOptions} from "jsonwebtoken";
|
|
3
|
+
import {DraxConfig} from "@drax/common-back";
|
|
4
|
+
import IdentityConfig from "../config/IdentityConfig.js";
|
|
3
5
|
|
|
4
6
|
class AuthUtils{
|
|
5
7
|
|
|
6
8
|
static verifyToken(token : string) {
|
|
7
|
-
const JWT_SECRET =
|
|
9
|
+
const JWT_SECRET = DraxConfig.getOrLoad(IdentityConfig.JwtSecret)
|
|
8
10
|
if(!JWT_SECRET){
|
|
9
|
-
throw new Error("JWT_SECRET
|
|
11
|
+
throw new Error("DraxConfig.JWT_SECRET must be provided")
|
|
10
12
|
}
|
|
11
13
|
const options : VerifyOptions = {
|
|
12
14
|
algorithms: ['HS256'],
|
|
@@ -40,17 +42,20 @@ class AuthUtils{
|
|
|
40
42
|
static generateToken(userId : string, username: string, roleId: string, session : string) {
|
|
41
43
|
const payload = AuthUtils.tokenSignPayload(userId, username, roleId, session)
|
|
42
44
|
|
|
43
|
-
const JWT_SECRET =
|
|
45
|
+
const JWT_SECRET = DraxConfig.getOrLoad(IdentityConfig.JwtSecret)
|
|
44
46
|
if(!JWT_SECRET){
|
|
45
47
|
throw new Error("JWT_SECRET ENV must be provided")
|
|
46
48
|
}
|
|
47
49
|
|
|
50
|
+
const JWT_EXPIRATION = DraxConfig.getOrLoad(IdentityConfig.JwtExpiration) || '1h'
|
|
51
|
+
const JWT_ISSUER = DraxConfig.getOrLoad(IdentityConfig.JwtIssuer) || 'DRAX'
|
|
52
|
+
|
|
48
53
|
const options : SignOptions = {
|
|
49
|
-
expiresIn:
|
|
54
|
+
expiresIn: JWT_EXPIRATION,
|
|
50
55
|
jwtid: userId,
|
|
51
56
|
algorithm: 'HS256',
|
|
52
57
|
audience: username,
|
|
53
|
-
issuer:
|
|
58
|
+
issuer: JWT_ISSUER
|
|
54
59
|
}
|
|
55
60
|
|
|
56
61
|
let token = jsonwebtoken.sign(
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import IdentityConfig from "../config/IdentityConfig.js";
|
|
2
|
+
import {DraxConfig} from "@drax/common-back";
|
|
1
3
|
|
|
2
4
|
enum DbEngine{
|
|
3
5
|
Sqlite = "sqlite",
|
|
@@ -9,24 +11,24 @@ class DbSetupUtils{
|
|
|
9
11
|
|
|
10
12
|
|
|
11
13
|
static getDbEngine() {
|
|
12
|
-
if (!
|
|
13
|
-
throw new Error("
|
|
14
|
+
if (!DraxConfig.getOrLoad(IdentityConfig.DbEngine)) {
|
|
15
|
+
throw new Error("DraxConfig.DB_ENGINE is not defined");
|
|
14
16
|
}
|
|
15
|
-
const dbEngine =
|
|
17
|
+
const dbEngine = DraxConfig.getOrLoad(IdentityConfig.DbEngine) as DbEngine;
|
|
16
18
|
if (!Object.values(DbEngine).includes(dbEngine)) {
|
|
17
|
-
throw new Error("
|
|
19
|
+
throw new Error("DraxConfig.DB_ENGINE must be one of " + Object.values(DbEngine).join(", "));
|
|
18
20
|
}
|
|
19
21
|
return dbEngine;
|
|
20
22
|
}
|
|
21
23
|
|
|
22
|
-
static
|
|
24
|
+
static getDbConfig(){
|
|
23
25
|
switch (DbSetupUtils.getDbEngine()) {
|
|
24
26
|
case DbEngine.Mongo:
|
|
25
|
-
return
|
|
27
|
+
return DraxConfig.getOrLoad(IdentityConfig.MongoDbUri);
|
|
26
28
|
case DbEngine.Sqlite:
|
|
27
|
-
return
|
|
29
|
+
return DraxConfig.getOrLoad(IdentityConfig.SqliteDbFile);
|
|
28
30
|
default:
|
|
29
|
-
throw new Error("
|
|
31
|
+
throw new Error("DraxConfig.DB_ENGINE must be one of " + Object.values(DbEngine).join(", "));
|
|
30
32
|
}
|
|
31
33
|
}
|
|
32
34
|
|