@drax/identity-back 0.36.0 → 0.37.2
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/controllers/UserController.js +5 -1
- package/dist/controllers/UserLoginFailController.js +54 -0
- package/dist/controllers/UserSessionController.js +54 -0
- package/dist/factory/UserLoginFailServiceFactory.js +25 -0
- package/dist/factory/UserSessionServiceFactory.js +25 -0
- package/dist/factory/services/UserLoginFailServiceFactory.js +15 -0
- package/dist/factory/services/UserSessionServiceFactory.js +15 -0
- package/dist/graphql/resolvers/role.resolvers.js +9 -9
- package/dist/graphql/resolvers/tenant.resolvers.js +8 -8
- package/dist/graphql/resolvers/user-api-key.resolvers.js +7 -7
- package/dist/graphql/resolvers/user.resolvers.js +3 -1
- package/dist/index.js +6 -3
- package/dist/interfaces/IUserLoginFail.js +1 -0
- package/dist/interfaces/IUserLoginFailRepository.js +1 -0
- package/dist/interfaces/IUserSession.js +1 -0
- package/dist/interfaces/IUserSessionRepository.js +1 -0
- package/dist/models/RoleModel.js +2 -0
- package/dist/models/UserLoginFailModel.js +20 -0
- package/dist/models/UserSessionModel.js +21 -0
- package/dist/permissions/UserLoginFailPermissions.js +8 -0
- package/dist/permissions/UserSessionPermissions.js +8 -0
- package/dist/permissions/index.js +9 -5
- package/dist/rbac/Rbac.js +1 -1
- package/dist/repository/mongo/UserLoginFailMongoRepository.js +12 -0
- package/dist/repository/mongo/UserSessionMongoRepository.js +12 -0
- package/dist/repository/sqlite/UserLoginFailSqliteRepository.js +31 -0
- package/dist/repository/sqlite/UserSessionSqliteRepository.js +31 -0
- package/dist/routes/UserLoginFailRoutes.js +12 -0
- package/dist/routes/UserRoutes.js +1 -0
- package/dist/routes/UserSessionRoutes.js +12 -0
- package/dist/schemas/RoleSchema.js +4 -0
- package/dist/schemas/UserLoginFailSchema.js +13 -0
- package/dist/schemas/UserSchema.js +2 -0
- package/dist/schemas/UserSessionSchema.js +15 -0
- package/dist/services/UserLoginFailService.js +8 -0
- package/dist/services/UserService.js +18 -3
- package/dist/services/UserSessionService.js +8 -0
- package/package.json +7 -7
- package/src/controllers/UserController.ts +6 -1
- package/src/controllers/UserLoginFailController.ts +64 -0
- package/src/controllers/UserSessionController.ts +64 -0
- package/src/factory/UserLoginFailServiceFactory.ts +31 -0
- package/src/factory/UserSessionServiceFactory.ts +31 -0
- package/src/graphql/resolvers/role.resolvers.ts +9 -9
- package/src/graphql/resolvers/tenant.resolvers.ts +8 -8
- package/src/graphql/resolvers/user-api-key.resolvers.ts +7 -7
- package/src/graphql/resolvers/user.resolvers.ts +3 -1
- package/src/index.ts +8 -3
- package/src/interfaces/IUserLoginFailRepository.ts +11 -0
- package/src/interfaces/IUserSessionRepository.ts +11 -0
- package/src/models/RoleModel.ts +2 -0
- package/src/models/UserLoginFailModel.ts +34 -0
- package/src/models/UserSessionModel.ts +36 -0
- package/src/permissions/UserLoginFailPermissions.ts +12 -0
- package/src/permissions/UserSessionPermissions.ts +12 -0
- package/src/permissions/index.ts +9 -5
- package/src/rbac/Rbac.ts +1 -1
- package/src/repository/mongo/UserLoginFailMongoRepository.ts +21 -0
- package/src/repository/mongo/UserSessionMongoRepository.ts +21 -0
- package/src/repository/sqlite/RoleSqliteRepository.ts +0 -7
- package/src/repository/sqlite/UserLoginFailSqliteRepository.ts +44 -0
- package/src/repository/sqlite/UserSessionSqliteRepository.ts +44 -0
- package/src/routes/UserLoginFailRoutes.ts +20 -0
- package/src/routes/UserRoutes.ts +2 -0
- package/src/routes/UserSessionRoutes.ts +20 -0
- package/src/schemas/RoleSchema.ts +4 -0
- package/src/schemas/UserLoginFailSchema.ts +18 -0
- package/src/schemas/UserSchema.ts +2 -0
- package/src/schemas/UserSessionSchema.ts +20 -0
- package/src/services/UserLoginFailService.ts +16 -0
- package/src/services/UserService.ts +18 -3
- package/src/services/UserSessionService.ts +16 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/types/controllers/UserController.d.ts.map +1 -1
- package/types/controllers/UserLoginFailController.d.ts +13 -0
- package/types/controllers/UserLoginFailController.d.ts.map +1 -0
- package/types/controllers/UserSessionController.d.ts +13 -0
- package/types/controllers/UserSessionController.d.ts.map +1 -0
- package/types/factory/UserLoginFailServiceFactory.d.ts +4 -0
- package/types/factory/UserLoginFailServiceFactory.d.ts.map +1 -0
- package/types/factory/UserSessionServiceFactory.d.ts +4 -0
- package/types/factory/UserSessionServiceFactory.d.ts.map +1 -0
- package/types/factory/services/UserLoginFailServiceFactory.d.ts +8 -0
- package/types/factory/services/UserLoginFailServiceFactory.d.ts.map +1 -0
- package/types/factory/services/UserSessionServiceFactory.d.ts +8 -0
- package/types/factory/services/UserSessionServiceFactory.d.ts.map +1 -0
- package/types/graphql/resolvers/user.resolvers.d.ts.map +1 -1
- package/types/index.d.ts +5 -2
- package/types/index.d.ts.map +1 -1
- package/types/interfaces/IUserLoginFail.d.ts +17 -0
- package/types/interfaces/IUserLoginFail.d.ts.map +1 -0
- package/types/interfaces/IUserLoginFailRepository.d.ts +6 -0
- package/types/interfaces/IUserLoginFailRepository.d.ts.map +1 -0
- package/types/interfaces/IUserSession.d.ts +19 -0
- package/types/interfaces/IUserSession.d.ts.map +1 -0
- package/types/interfaces/IUserSessionRepository.d.ts +6 -0
- package/types/interfaces/IUserSessionRepository.d.ts.map +1 -0
- package/types/models/RoleModel.d.ts.map +1 -1
- package/types/models/UserLoginFailModel.d.ts +15 -0
- package/types/models/UserLoginFailModel.d.ts.map +1 -0
- package/types/models/UserSessionModel.d.ts +15 -0
- package/types/models/UserSessionModel.d.ts.map +1 -0
- package/types/permissions/UserLoginFailPermissions.d.ts +8 -0
- package/types/permissions/UserLoginFailPermissions.d.ts.map +1 -0
- package/types/permissions/UserSessionPermissions.d.ts +8 -0
- package/types/permissions/UserSessionPermissions.d.ts.map +1 -0
- package/types/permissions/index.d.ts +7 -5
- package/types/permissions/index.d.ts.map +1 -1
- package/types/repository/mongo/UserLoginFailMongoRepository.d.ts +9 -0
- package/types/repository/mongo/UserLoginFailMongoRepository.d.ts.map +1 -0
- package/types/repository/mongo/UserSessionMongoRepository.d.ts +9 -0
- package/types/repository/mongo/UserSessionMongoRepository.d.ts.map +1 -0
- package/types/repository/sqlite/RoleSqliteRepository.d.ts.map +1 -1
- package/types/repository/sqlite/UserLoginFailSqliteRepository.d.ts +23 -0
- package/types/repository/sqlite/UserLoginFailSqliteRepository.d.ts.map +1 -0
- package/types/repository/sqlite/UserSessionSqliteRepository.d.ts +23 -0
- package/types/repository/sqlite/UserSessionSqliteRepository.d.ts.map +1 -0
- package/types/routes/UserLoginFailRoutes.d.ts +4 -0
- package/types/routes/UserLoginFailRoutes.d.ts.map +1 -0
- package/types/routes/UserRoutes.d.ts.map +1 -1
- package/types/routes/UserSessionRoutes.d.ts +4 -0
- package/types/routes/UserSessionRoutes.d.ts.map +1 -0
- package/types/schemas/RoleSchema.d.ts +24 -10
- package/types/schemas/RoleSchema.d.ts.map +1 -1
- package/types/schemas/UserLoginFailSchema.d.ts +37 -0
- package/types/schemas/UserLoginFailSchema.d.ts.map +1 -0
- package/types/schemas/UserSchema.d.ts +31 -21
- package/types/schemas/UserSchema.d.ts.map +1 -1
- package/types/schemas/UserSessionSchema.d.ts +59 -0
- package/types/schemas/UserSessionSchema.d.ts.map +1 -0
- package/types/services/UserLoginFailService.d.ts +10 -0
- package/types/services/UserLoginFailService.d.ts.map +1 -0
- package/types/services/UserService.d.ts +4 -1
- package/types/services/UserService.d.ts.map +1 -1
- package/types/services/UserSessionService.d.ts +10 -0
- package/types/services/UserSessionService.d.ts.map +1 -0
- package/src/permissions/IdentityPermissions.ts +0 -35
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { AbstractSqliteRepository } from "@drax/crud-back";
|
|
2
|
+
class UserSessionSqliteRepository extends AbstractSqliteRepository {
|
|
3
|
+
constructor() {
|
|
4
|
+
super(...arguments);
|
|
5
|
+
this.tableName = 'user_sessions';
|
|
6
|
+
this.searchFields = [];
|
|
7
|
+
this.booleanFields = [];
|
|
8
|
+
this.identifier = '_id';
|
|
9
|
+
this.populateFields = [{ field: 'user', table: 'users', identifier: '_id' }];
|
|
10
|
+
this.tableFields = [
|
|
11
|
+
{ name: "uuid", type: "TEXT", unique: false, primary: false },
|
|
12
|
+
{ name: "user", type: "TEXT", unique: false, primary: false },
|
|
13
|
+
{ name: "agent", type: "TEXT", unique: false, primary: false },
|
|
14
|
+
{ name: "ip", type: "TEXT", unique: false, primary: false },
|
|
15
|
+
{ name: "createdAt", type: "TEXT", unique: false, primary: false },
|
|
16
|
+
{ name: "updatedAt", type: "TEXT", unique: false, primary: false },
|
|
17
|
+
];
|
|
18
|
+
}
|
|
19
|
+
async prepareData(data) {
|
|
20
|
+
// Transform data before saving to database if needed
|
|
21
|
+
}
|
|
22
|
+
async prepareItem(item) {
|
|
23
|
+
if (item.createdAt && typeof item.createdAt === 'string') {
|
|
24
|
+
item.createdAt = new Date(item.createdAt);
|
|
25
|
+
}
|
|
26
|
+
if (item.updatedAt && typeof item.updatedAt === 'string') {
|
|
27
|
+
item.updatedAt = new Date(item.updatedAt);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export default UserSessionSqliteRepository;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import UserLoginFailController from "../controllers/UserLoginFailController.js";
|
|
2
|
+
import { CrudSchemaBuilder } from "@drax/crud-back";
|
|
3
|
+
import { UserLoginFailSchema, UserLoginFailBaseSchema } from '../schemas/UserLoginFailSchema.js';
|
|
4
|
+
async function UserLoginFailRoutes(fastify, options) {
|
|
5
|
+
const controller = new UserLoginFailController();
|
|
6
|
+
const schemas = new CrudSchemaBuilder(UserLoginFailSchema, UserLoginFailBaseSchema, UserLoginFailBaseSchema, 'UserLoginFail', 'openApi3', ['Identity']);
|
|
7
|
+
fastify.get('/api/user-login-fails', { schema: schemas.paginateSchema }, (req, rep) => controller.paginate(req, rep));
|
|
8
|
+
fastify.get('/api/user-login-fails/group-by', { schema: schemas.groupBySchema }, (req, rep) => controller.groupBy(req, rep));
|
|
9
|
+
fastify.get('/api/user-login-fails/export', { schema: schemas.exportSchema }, (req, rep) => controller.export(req, rep));
|
|
10
|
+
}
|
|
11
|
+
export default UserLoginFailRoutes;
|
|
12
|
+
export { UserLoginFailRoutes };
|
|
@@ -10,6 +10,7 @@ async function UserRoutes(fastify, options) {
|
|
|
10
10
|
const controller = new UserController();
|
|
11
11
|
const schemas = new CrudSchemaBuilder(UserSchema, UserCreateSchema, UserUpdateSchema, 'tenant', 'openApi3', ['Identity']);
|
|
12
12
|
fastify.get('/api/users/search', { schema: schemas.searchSchema }, async (req, rep) => await controller.search(req, rep));
|
|
13
|
+
fastify.get('/api/users/group-by', { schema: schemas.groupBySchema }, async (req, rep) => await controller.groupBy(req, rep));
|
|
13
14
|
fastify.get('/api/users/export', { schema: schemas.exportSchema }, (req, rep) => controller.export(req, rep));
|
|
14
15
|
fastify.get('/api/users', { schema: schemas.paginateSchema }, (req, rep) => controller.paginate(req, rep));
|
|
15
16
|
fastify.post('/api/users', { schema: schemas.createSchema }, (req, rep) => controller.create(req, rep));
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import UserSessionController from "../controllers/UserSessionController.js";
|
|
2
|
+
import { CrudSchemaBuilder } from "@drax/crud-back";
|
|
3
|
+
import { UserSessionSchema, UserSessionBaseSchema } from '../schemas/UserSessionSchema.js';
|
|
4
|
+
async function UserSessionRoutes(fastify, options) {
|
|
5
|
+
const controller = new UserSessionController();
|
|
6
|
+
const schemas = new CrudSchemaBuilder(UserSessionSchema, UserSessionBaseSchema, UserSessionBaseSchema, 'UserSession', 'openApi3', ['Identity']);
|
|
7
|
+
fastify.get('/api/user-sessions', { schema: schemas.paginateSchema }, (req, rep) => controller.paginate(req, rep));
|
|
8
|
+
fastify.get('/api/user-sessions/group-by', { schema: schemas.groupBySchema }, (req, rep) => controller.groupBy(req, rep));
|
|
9
|
+
fastify.get('/api/user-sessions/export', { schema: schemas.exportSchema }, (req, rep) => controller.export(req, rep));
|
|
10
|
+
}
|
|
11
|
+
export default UserSessionRoutes;
|
|
12
|
+
export { UserSessionRoutes };
|
|
@@ -4,12 +4,16 @@ const RoleBaseSchema = object({
|
|
|
4
4
|
.min(1, "validation.required")
|
|
5
5
|
.regex(/^[A-Z]/, "validation.startWithUpperCase"),
|
|
6
6
|
permissions: array(string()).optional(),
|
|
7
|
+
icon: string().optional(),
|
|
8
|
+
color: string().optional(),
|
|
7
9
|
childRoles: array(string()).optional(),
|
|
8
10
|
});
|
|
9
11
|
const RoleSchema = RoleBaseSchema.extend({
|
|
10
12
|
_id: string(),
|
|
11
13
|
id: string().optional(),
|
|
12
14
|
permissions: array(string()).optional(),
|
|
15
|
+
icon: string().optional(),
|
|
16
|
+
color: string().optional(),
|
|
13
17
|
readonly: boolean(),
|
|
14
18
|
childRoles: array(object({
|
|
15
19
|
_id: string(),
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
const UserLoginFailBaseSchema = z.object({
|
|
3
|
+
username: z.string().min(1, 'validation.required'),
|
|
4
|
+
userAgent: z.string().optional(),
|
|
5
|
+
ip: z.string().optional(),
|
|
6
|
+
});
|
|
7
|
+
const UserLoginFailSchema = UserLoginFailBaseSchema
|
|
8
|
+
.extend({
|
|
9
|
+
_id: z.string(),
|
|
10
|
+
createdAt: z.coerce.date().nullable().optional()
|
|
11
|
+
});
|
|
12
|
+
export default UserLoginFailSchema;
|
|
13
|
+
export { UserLoginFailSchema, UserLoginFailBaseSchema };
|
|
@@ -26,6 +26,8 @@ const UserSchema = UserBaseSchema
|
|
|
26
26
|
_id: string(),
|
|
27
27
|
id: string().optional(),
|
|
28
28
|
name: string(),
|
|
29
|
+
icon: string().optional(),
|
|
30
|
+
color: string().optional(),
|
|
29
31
|
permissions: array(string()),
|
|
30
32
|
childRoles: array(object({ _id: string(), name: string() })).optional()
|
|
31
33
|
}).optional(),
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
const UserSessionBaseSchema = z.object({
|
|
3
|
+
uuid: z.string().min(1, 'validation.required'),
|
|
4
|
+
user: z.string().min(1, 'validation.required'),
|
|
5
|
+
userAgent: z.string().optional(),
|
|
6
|
+
ip: z.string().optional(),
|
|
7
|
+
});
|
|
8
|
+
const UserSessionSchema = UserSessionBaseSchema
|
|
9
|
+
.extend({
|
|
10
|
+
_id: z.string(),
|
|
11
|
+
user: z.object({ _id: z.string(), username: z.string() }),
|
|
12
|
+
createdAt: z.coerce.date().nullable().optional()
|
|
13
|
+
});
|
|
14
|
+
export default UserSessionSchema;
|
|
15
|
+
export { UserSessionSchema, UserSessionBaseSchema };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { AbstractService } from "@drax/crud-back";
|
|
2
|
+
class UserLoginFailService extends AbstractService {
|
|
3
|
+
constructor(UserLoginFailRepository, schema) {
|
|
4
|
+
super(UserLoginFailRepository, schema);
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export default UserLoginFailService;
|
|
8
|
+
export { UserLoginFailService };
|
|
@@ -5,23 +5,38 @@ import { UserCreateSchema, UserUpdateSchema, UserBaseSchema } from "../schemas/U
|
|
|
5
5
|
import BadCredentialsError from "../errors/BadCredentialsError.js";
|
|
6
6
|
import { AbstractService } from "@drax/crud-back";
|
|
7
7
|
import { randomUUID } from "crypto";
|
|
8
|
+
import UserLoginFailServiceFactory from "../factory/UserLoginFailServiceFactory.js";
|
|
9
|
+
import UserSessionServiceFactory from "../factory/UserSessionServiceFactory.js";
|
|
8
10
|
class UserService extends AbstractService {
|
|
9
11
|
constructor(userRepository) {
|
|
10
12
|
super(userRepository, UserBaseSchema);
|
|
11
13
|
this._repository = userRepository;
|
|
12
14
|
console.log("UserService constructor");
|
|
13
15
|
}
|
|
14
|
-
async auth(username, password) {
|
|
16
|
+
async auth(username, password, { userAgent, ip }) {
|
|
15
17
|
let user = null;
|
|
16
18
|
console.log("auth username", username);
|
|
17
19
|
user = await this.findByUsernameWithPassword(username);
|
|
18
20
|
if (user && user.active && AuthUtils.checkPassword(password, user.password)) {
|
|
19
21
|
//TODO: Generar session
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
+
const sessionUUID = randomUUID();
|
|
23
|
+
const sessionService = UserSessionServiceFactory();
|
|
24
|
+
await sessionService.create({
|
|
25
|
+
user: user._id.toString(),
|
|
26
|
+
uuid: sessionUUID,
|
|
27
|
+
userAgent: userAgent,
|
|
28
|
+
ip: ip
|
|
29
|
+
});
|
|
30
|
+
const accessToken = AuthUtils.generateToken(user._id.toString(), user.username, user.role._id, user.tenant?._id, sessionUUID);
|
|
22
31
|
return { accessToken: accessToken };
|
|
23
32
|
}
|
|
24
33
|
else {
|
|
34
|
+
const userLoginFailService = UserLoginFailServiceFactory();
|
|
35
|
+
await userLoginFailService.create({
|
|
36
|
+
username: username,
|
|
37
|
+
userAgent: userAgent,
|
|
38
|
+
ip: ip
|
|
39
|
+
});
|
|
25
40
|
throw new BadCredentialsError();
|
|
26
41
|
}
|
|
27
42
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { AbstractService } from "@drax/crud-back";
|
|
2
|
+
class UserSessionService extends AbstractService {
|
|
3
|
+
constructor(UserSessionRepository, schema) {
|
|
4
|
+
super(UserSessionRepository, schema);
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export default UserSessionService;
|
|
8
|
+
export { UserSessionService };
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "0.
|
|
6
|
+
"version": "0.37.2",
|
|
7
7
|
"description": "Identity module for user management, authentication and authorization.",
|
|
8
8
|
"main": "dist/index.js",
|
|
9
9
|
"types": "types/index.d.ts",
|
|
@@ -28,11 +28,11 @@
|
|
|
28
28
|
"author": "Cristian Incarnato & Drax Team",
|
|
29
29
|
"license": "ISC",
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@drax/common-back": "^0.
|
|
32
|
-
"@drax/crud-back": "^0.
|
|
33
|
-
"@drax/crud-share": "^0.
|
|
34
|
-
"@drax/email-back": "^0.
|
|
35
|
-
"@drax/identity-share": "^0.
|
|
31
|
+
"@drax/common-back": "^0.37.2",
|
|
32
|
+
"@drax/crud-back": "^0.37.2",
|
|
33
|
+
"@drax/crud-share": "^0.37.0",
|
|
34
|
+
"@drax/email-back": "^0.37.0",
|
|
35
|
+
"@drax/identity-share": "^0.37.0",
|
|
36
36
|
"bcryptjs": "^2.4.3",
|
|
37
37
|
"graphql": "^16.8.2",
|
|
38
38
|
"jsonwebtoken": "^9.0.2"
|
|
@@ -63,5 +63,5 @@
|
|
|
63
63
|
"debug": "0"
|
|
64
64
|
}
|
|
65
65
|
},
|
|
66
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "85833fa79a0d1df3899c13a3a47ed3e7e3fe0448"
|
|
67
67
|
}
|
|
@@ -31,14 +31,19 @@ class UserController extends AbstractFastifyController<IUser, IUserCreate, IUser
|
|
|
31
31
|
|
|
32
32
|
constructor() {
|
|
33
33
|
super(UserServiceFactory(), UserPermissions)
|
|
34
|
+
this.tenantField = "tenant";
|
|
35
|
+
this.tenantFilter = true;
|
|
34
36
|
}
|
|
35
37
|
|
|
36
38
|
async auth(request, reply) {
|
|
37
39
|
try {
|
|
38
40
|
const username = request.body.username
|
|
39
41
|
const password = request.body.password
|
|
42
|
+
const userAgent = request.headers['user-agent'];
|
|
43
|
+
const ip = request.ip;
|
|
40
44
|
const userService = UserServiceFactory()
|
|
41
|
-
|
|
45
|
+
|
|
46
|
+
return await userService.auth(username, password,{userAgent, ip})
|
|
42
47
|
} catch (e) {
|
|
43
48
|
console.error('/api/auth error', e)
|
|
44
49
|
if (e instanceof BadCredentialsError) {
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
|
|
2
|
+
import UserLoginFailServiceFactory from "../factory/UserLoginFailServiceFactory.js";
|
|
3
|
+
import {AbstractFastifyController} from "@drax/crud-back";
|
|
4
|
+
import UserLoginFailPermissions from "../permissions/UserLoginFailPermissions.js";
|
|
5
|
+
import type {IUserLoginFail, IUserLoginFailBase} from "@drax/identity-share";
|
|
6
|
+
import type {FastifyReply, FastifyRequest} from "fastify";
|
|
7
|
+
import {MethodNotAllowedError} from "@drax/common-back";
|
|
8
|
+
|
|
9
|
+
class UserLoginFailController extends AbstractFastifyController<IUserLoginFail, IUserLoginFailBase, IUserLoginFailBase> {
|
|
10
|
+
|
|
11
|
+
constructor() {
|
|
12
|
+
super(UserLoginFailServiceFactory(), UserLoginFailPermissions)
|
|
13
|
+
this.tenantField = "tenant";
|
|
14
|
+
this.userField = "user";
|
|
15
|
+
this.tenantFilter = false;
|
|
16
|
+
this.userFilter = true;
|
|
17
|
+
this.tenantSetter = false;
|
|
18
|
+
this.userSetter = true;
|
|
19
|
+
this.tenantAssert = false;
|
|
20
|
+
this.userAssert = true;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async create(request: FastifyRequest, reply: FastifyReply): Promise<IUserLoginFail> {
|
|
24
|
+
try {
|
|
25
|
+
throw new MethodNotAllowedError()
|
|
26
|
+
} catch (e) {
|
|
27
|
+
this.handleError(e, reply)
|
|
28
|
+
}
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async update(request: FastifyRequest, reply: FastifyReply): Promise<IUserLoginFail> {
|
|
33
|
+
try {
|
|
34
|
+
throw new MethodNotAllowedError()
|
|
35
|
+
} catch (e) {
|
|
36
|
+
this.handleError(e, reply)
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async updatePartial(request: FastifyRequest, reply: FastifyReply): Promise<IUserLoginFail> {
|
|
42
|
+
try {
|
|
43
|
+
throw new MethodNotAllowedError()
|
|
44
|
+
} catch (e) {
|
|
45
|
+
this.handleError(e, reply)
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async delete(request: FastifyRequest, reply: FastifyReply) {
|
|
51
|
+
try {
|
|
52
|
+
throw new MethodNotAllowedError()
|
|
53
|
+
} catch (e) {
|
|
54
|
+
this.handleError(e, reply)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export default UserLoginFailController;
|
|
61
|
+
export {
|
|
62
|
+
UserLoginFailController
|
|
63
|
+
}
|
|
64
|
+
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
|
|
2
|
+
import UserSessionServiceFactory from "../factory/UserSessionServiceFactory.js";
|
|
3
|
+
import {AbstractFastifyController} from "@drax/crud-back";
|
|
4
|
+
import UserSessionPermissions from "../permissions/UserSessionPermissions.js";
|
|
5
|
+
import type {IUserSession, IUserSessionBase} from "@drax/identity-share";
|
|
6
|
+
import type {FastifyReply, FastifyRequest} from "fastify";
|
|
7
|
+
import {MethodNotAllowedError} from "@drax/common-back";
|
|
8
|
+
|
|
9
|
+
class UserSessionController extends AbstractFastifyController<IUserSession, IUserSessionBase, IUserSessionBase> {
|
|
10
|
+
|
|
11
|
+
constructor() {
|
|
12
|
+
super(UserSessionServiceFactory(), UserSessionPermissions)
|
|
13
|
+
this.tenantField = "tenant";
|
|
14
|
+
this.userField = "user";
|
|
15
|
+
this.tenantFilter = false;
|
|
16
|
+
this.userFilter = true;
|
|
17
|
+
this.tenantSetter = false;
|
|
18
|
+
this.userSetter = true;
|
|
19
|
+
this.tenantAssert = false;
|
|
20
|
+
this.userAssert = true;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async create(request: FastifyRequest, reply: FastifyReply): Promise<IUserSession> {
|
|
24
|
+
try {
|
|
25
|
+
throw new MethodNotAllowedError()
|
|
26
|
+
} catch (e) {
|
|
27
|
+
this.handleError(e, reply)
|
|
28
|
+
}
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async update(request: FastifyRequest, reply: FastifyReply): Promise<IUserSession> {
|
|
33
|
+
try {
|
|
34
|
+
throw new MethodNotAllowedError()
|
|
35
|
+
} catch (e) {
|
|
36
|
+
this.handleError(e, reply)
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async updatePartial(request: FastifyRequest, reply: FastifyReply): Promise<IUserSession> {
|
|
42
|
+
try {
|
|
43
|
+
throw new MethodNotAllowedError()
|
|
44
|
+
} catch (e) {
|
|
45
|
+
this.handleError(e, reply)
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async delete(request: FastifyRequest, reply: FastifyReply) {
|
|
51
|
+
try {
|
|
52
|
+
throw new MethodNotAllowedError()
|
|
53
|
+
} catch (e) {
|
|
54
|
+
this.handleError(e, reply)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export default UserSessionController;
|
|
61
|
+
export {
|
|
62
|
+
UserSessionController
|
|
63
|
+
}
|
|
64
|
+
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import UserLoginFailMongoRepository from "../repository/mongo/UserLoginFailMongoRepository.js";
|
|
2
|
+
import UserLoginFailService from "../services/UserLoginFailService.js";
|
|
3
|
+
import UserLoginFailSqliteRepository from "../repository/sqlite/UserLoginFailSqliteRepository.js";
|
|
4
|
+
import {IUserLoginFailRepository} from "../interfaces/IUserLoginFailRepository";
|
|
5
|
+
import {COMMON, CommonConfig, DraxConfig} from "@drax/common-back";
|
|
6
|
+
|
|
7
|
+
let userService: UserLoginFailService
|
|
8
|
+
|
|
9
|
+
const UserLoginFailServiceFactory = (verbose:boolean = false) : UserLoginFailService => {
|
|
10
|
+
if(!userService){
|
|
11
|
+
let userRepository: IUserLoginFailRepository
|
|
12
|
+
switch (DraxConfig.getOrLoad(CommonConfig.DbEngine)) {
|
|
13
|
+
case COMMON.DB_ENGINES.MONGODB:
|
|
14
|
+
userRepository = new UserLoginFailMongoRepository()
|
|
15
|
+
break;
|
|
16
|
+
case COMMON.DB_ENGINES.SQLITE:
|
|
17
|
+
const dbFile = DraxConfig.getOrLoad(CommonConfig.SqliteDbFile)
|
|
18
|
+
userRepository = new UserLoginFailSqliteRepository(dbFile,verbose)
|
|
19
|
+
userRepository.build()
|
|
20
|
+
break;
|
|
21
|
+
default:
|
|
22
|
+
throw new Error("DraxConfig.DB_ENGINE must be one of " + Object.values(COMMON.DB_ENGINES).join(", "));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
userService = new UserLoginFailService(userRepository)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return userService
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export default UserLoginFailServiceFactory
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import UserSessionMongoRepository from "../repository/mongo/UserSessionMongoRepository.js";
|
|
2
|
+
import UserSessionService from "../services/UserSessionService.js";
|
|
3
|
+
import UserSessionSqliteRepository from "../repository/sqlite/UserSessionSqliteRepository.js";
|
|
4
|
+
import {IUserSessionRepository} from "../interfaces/IUserSessionRepository";
|
|
5
|
+
import {COMMON, CommonConfig, DraxConfig} from "@drax/common-back";
|
|
6
|
+
|
|
7
|
+
let userService: UserSessionService
|
|
8
|
+
|
|
9
|
+
const UserSessionServiceFactory = (verbose:boolean = false) : UserSessionService => {
|
|
10
|
+
if(!userService){
|
|
11
|
+
let userRepository: IUserSessionRepository
|
|
12
|
+
switch (DraxConfig.getOrLoad(CommonConfig.DbEngine)) {
|
|
13
|
+
case COMMON.DB_ENGINES.MONGODB:
|
|
14
|
+
userRepository = new UserSessionMongoRepository()
|
|
15
|
+
break;
|
|
16
|
+
case COMMON.DB_ENGINES.SQLITE:
|
|
17
|
+
const dbFile = DraxConfig.getOrLoad(CommonConfig.SqliteDbFile)
|
|
18
|
+
userRepository = new UserSessionSqliteRepository(dbFile,verbose)
|
|
19
|
+
userRepository.build()
|
|
20
|
+
break;
|
|
21
|
+
default:
|
|
22
|
+
throw new Error("DraxConfig.DB_ENGINE must be one of " + Object.values(COMMON.DB_ENGINES).join(", "));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
userService = new UserSessionService(userRepository)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return userService
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export default UserSessionServiceFactory
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import RoleServiceFactory from "../../factory/RoleServiceFactory.js";
|
|
2
|
-
import {
|
|
2
|
+
import {RolePermissions} from "../../permissions/RolePermissions.js";
|
|
3
3
|
import {ValidationError, ValidationErrorToGraphQLError, UnauthorizedError} from "@drax/common-back";
|
|
4
4
|
import {GraphQLError} from "graphql";
|
|
5
5
|
import {PermissionService} from "../../services/PermissionService.js";
|
|
@@ -10,7 +10,7 @@ export default {
|
|
|
10
10
|
Query: {
|
|
11
11
|
findRoleById: async (_, {id}, {rbac}) => {
|
|
12
12
|
try {
|
|
13
|
-
rbac.assertPermission(
|
|
13
|
+
rbac.assertPermission(RolePermissions.View)
|
|
14
14
|
const roleService = RoleServiceFactory()
|
|
15
15
|
return await roleService.findById(id)
|
|
16
16
|
} catch (e) {
|
|
@@ -22,7 +22,7 @@ export default {
|
|
|
22
22
|
},
|
|
23
23
|
findRoleByName: async (_, {name}, {rbac}) => {
|
|
24
24
|
try {
|
|
25
|
-
rbac.assertPermission(
|
|
25
|
+
rbac.assertPermission(RolePermissions.View)
|
|
26
26
|
const roleService = RoleServiceFactory()
|
|
27
27
|
return await roleService.findByName(name)
|
|
28
28
|
} catch (e) {
|
|
@@ -34,7 +34,7 @@ export default {
|
|
|
34
34
|
},
|
|
35
35
|
fetchRole: async (_, {}, {rbac}) => {
|
|
36
36
|
try {
|
|
37
|
-
rbac.assertPermission(
|
|
37
|
+
rbac.assertPermission(RolePermissions.View)
|
|
38
38
|
const roleService = RoleServiceFactory()
|
|
39
39
|
const roles = await roleService.fetchAll()
|
|
40
40
|
if(rbac.getRole?.childRoles?.length > 0) {
|
|
@@ -53,7 +53,7 @@ export default {
|
|
|
53
53
|
},
|
|
54
54
|
fetchPermissions: async (_, {}, {rbac}) => {
|
|
55
55
|
try {
|
|
56
|
-
rbac.assertPermission(
|
|
56
|
+
rbac.assertPermission(RolePermissions.Permissions)
|
|
57
57
|
return PermissionService.getPermissions()
|
|
58
58
|
} catch (e) {
|
|
59
59
|
if (e instanceof UnauthorizedError) {
|
|
@@ -64,7 +64,7 @@ export default {
|
|
|
64
64
|
},
|
|
65
65
|
paginateRole: async (_, {options= {page:1, limit:5, orderBy:"", order:"asc", search:"", filters: []} as IDraxPaginateOptions }, {rbac}) => {
|
|
66
66
|
try {
|
|
67
|
-
rbac.assertPermission(
|
|
67
|
+
rbac.assertPermission(RolePermissions.View)
|
|
68
68
|
const roleService = RoleServiceFactory()
|
|
69
69
|
return await roleService.paginate(options)
|
|
70
70
|
} catch (e) {
|
|
@@ -79,7 +79,7 @@ export default {
|
|
|
79
79
|
Mutation: {
|
|
80
80
|
createRole: async (_, {input}, {rbac}) => {
|
|
81
81
|
try {
|
|
82
|
-
rbac.assertPermission(
|
|
82
|
+
rbac.assertPermission(RolePermissions.Create)
|
|
83
83
|
const roleService = RoleServiceFactory()
|
|
84
84
|
return await roleService.create(input)
|
|
85
85
|
} catch (e) {
|
|
@@ -96,7 +96,7 @@ export default {
|
|
|
96
96
|
},
|
|
97
97
|
updateRole: async (_, {id, input}, {rbac}) => {
|
|
98
98
|
try {
|
|
99
|
-
rbac.assertPermission(
|
|
99
|
+
rbac.assertPermission(RolePermissions.Update)
|
|
100
100
|
const roleService = RoleServiceFactory()
|
|
101
101
|
const currentRole = await roleService.findById(id)
|
|
102
102
|
if(currentRole.readonly){
|
|
@@ -117,7 +117,7 @@ export default {
|
|
|
117
117
|
},
|
|
118
118
|
deleteRole: async (_, {id}, {rbac}) => {
|
|
119
119
|
try {
|
|
120
|
-
rbac.assertPermission(
|
|
120
|
+
rbac.assertPermission(RolePermissions.Delete)
|
|
121
121
|
const roleService = RoleServiceFactory()
|
|
122
122
|
const currentRole = await roleService.findById(id)
|
|
123
123
|
if(currentRole.readonly){
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import TenantServiceFactory from "../../factory/TenantServiceFactory.js";
|
|
2
|
-
import {
|
|
2
|
+
import {TenantPermissions} from "../../permissions/TenantPermissions.js";
|
|
3
3
|
import {ValidationError, ValidationErrorToGraphQLError, UnauthorizedError} from "@drax/common-back";
|
|
4
4
|
import {GraphQLError} from "graphql";
|
|
5
5
|
import {IDraxPaginateOptions} from "@drax/crud-share";
|
|
@@ -9,7 +9,7 @@ export default {
|
|
|
9
9
|
Query: {
|
|
10
10
|
findTenantById: async (_, {id}, {rbac}) => {
|
|
11
11
|
try {
|
|
12
|
-
rbac.assertPermission(
|
|
12
|
+
rbac.assertPermission(TenantPermissions.View)
|
|
13
13
|
const tenantService = TenantServiceFactory()
|
|
14
14
|
return await tenantService.findById(id)
|
|
15
15
|
} catch (e) {
|
|
@@ -21,7 +21,7 @@ export default {
|
|
|
21
21
|
},
|
|
22
22
|
findTenantByName: async (_, {name}, {rbac}) => {
|
|
23
23
|
try {
|
|
24
|
-
rbac.assertPermission(
|
|
24
|
+
rbac.assertPermission(TenantPermissions.View)
|
|
25
25
|
const tenantService = TenantServiceFactory()
|
|
26
26
|
return await tenantService.findByName(name)
|
|
27
27
|
} catch (e) {
|
|
@@ -33,7 +33,7 @@ export default {
|
|
|
33
33
|
},
|
|
34
34
|
fetchTenant: async (_, {}, {rbac}) => {
|
|
35
35
|
try {
|
|
36
|
-
rbac.assertPermission(
|
|
36
|
+
rbac.assertPermission(TenantPermissions.View)
|
|
37
37
|
const tenantService = TenantServiceFactory()
|
|
38
38
|
const tenants = await tenantService.fetchAll()
|
|
39
39
|
if(rbac.getAuthUser.tenantId){
|
|
@@ -51,7 +51,7 @@ export default {
|
|
|
51
51
|
},
|
|
52
52
|
paginateTenant: async (_, {options= {page:1, limit:5, orderBy:"", order:"asc", search:"", filters: []} as IDraxPaginateOptions }, {rbac}) => {
|
|
53
53
|
try {
|
|
54
|
-
rbac.assertPermission(
|
|
54
|
+
rbac.assertPermission(TenantPermissions.View)
|
|
55
55
|
const tenantService = TenantServiceFactory()
|
|
56
56
|
return await tenantService.paginate(options)
|
|
57
57
|
} catch (e) {
|
|
@@ -66,7 +66,7 @@ export default {
|
|
|
66
66
|
Mutation: {
|
|
67
67
|
createTenant: async (_, {input}, {rbac}) => {
|
|
68
68
|
try {
|
|
69
|
-
rbac.assertPermission(
|
|
69
|
+
rbac.assertPermission(TenantPermissions.Create)
|
|
70
70
|
const tenantService = TenantServiceFactory()
|
|
71
71
|
return await tenantService.create(input)
|
|
72
72
|
} catch (e) {
|
|
@@ -83,7 +83,7 @@ export default {
|
|
|
83
83
|
},
|
|
84
84
|
updateTenant: async (_, {id, input}, {rbac}) => {
|
|
85
85
|
try {
|
|
86
|
-
rbac.assertPermission(
|
|
86
|
+
rbac.assertPermission(TenantPermissions.Update)
|
|
87
87
|
const tenantService = TenantServiceFactory()
|
|
88
88
|
return await tenantService.update(id, input)
|
|
89
89
|
} catch (e) {
|
|
@@ -99,7 +99,7 @@ export default {
|
|
|
99
99
|
},
|
|
100
100
|
deleteTenant: async (_, {id}, {rbac}) => {
|
|
101
101
|
try {
|
|
102
|
-
rbac.assertPermission(
|
|
102
|
+
rbac.assertPermission(TenantPermissions.Delete)
|
|
103
103
|
const tenantService = TenantServiceFactory()
|
|
104
104
|
return await tenantService.delete(id)
|
|
105
105
|
} catch (e) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import UserApiKeyServiceFactory from "../../factory/UserApiKeyServiceFactory.js";
|
|
2
|
-
import {
|
|
2
|
+
import {UserApiKeyPermissions} from "../../permissions/UserApiKeyPermissions.js";
|
|
3
3
|
import {ValidationError, ValidationErrorToGraphQLError, UnauthorizedError} from "@drax/common-back";
|
|
4
4
|
import {GraphQLError} from "graphql";
|
|
5
5
|
import * as crypto from "node:crypto";
|
|
@@ -14,15 +14,15 @@ export default {
|
|
|
14
14
|
|
|
15
15
|
|
|
16
16
|
rbac.assertOrPermissions([
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
UserApiKeyPermissions.View,
|
|
18
|
+
UserApiKeyPermissions.ViewMy
|
|
19
19
|
])
|
|
20
20
|
|
|
21
21
|
if(!Array.isArray(options.filters)){
|
|
22
22
|
options.filters = []
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
if(!rbac.hasPermission(
|
|
25
|
+
if(!rbac.hasPermission(UserApiKeyPermissions.View)){
|
|
26
26
|
options.filters.push({field: "user", operator: "eq", value: rbac.userId})
|
|
27
27
|
}
|
|
28
28
|
|
|
@@ -40,7 +40,7 @@ export default {
|
|
|
40
40
|
Mutation: {
|
|
41
41
|
createUserApiKey: async (_, {input}, {rbac}) => {
|
|
42
42
|
try {
|
|
43
|
-
rbac.assertPermission(
|
|
43
|
+
rbac.assertPermission(UserApiKeyPermissions.Create)
|
|
44
44
|
input.user = rbac.authUser.id
|
|
45
45
|
input.secret = crypto.randomUUID()
|
|
46
46
|
const userApiKeyService = UserApiKeyServiceFactory(true)
|
|
@@ -59,7 +59,7 @@ export default {
|
|
|
59
59
|
},
|
|
60
60
|
updateUserApiKey: async (_, {id, input}, {rbac}) => {
|
|
61
61
|
try {
|
|
62
|
-
rbac.assertPermission(
|
|
62
|
+
rbac.assertPermission(UserApiKeyPermissions.Update)
|
|
63
63
|
const userApiKeyService = UserApiKeyServiceFactory()
|
|
64
64
|
return await userApiKeyService.update(id, input)
|
|
65
65
|
} catch (e) {
|
|
@@ -75,7 +75,7 @@ export default {
|
|
|
75
75
|
},
|
|
76
76
|
deleteUserApiKey: async (_, {id}, {rbac}) => {
|
|
77
77
|
try {
|
|
78
|
-
rbac.assertPermission(
|
|
78
|
+
rbac.assertPermission(UserApiKeyPermissions.Delete)
|
|
79
79
|
const userApiKeyService = UserApiKeyServiceFactory()
|
|
80
80
|
return await userApiKeyService.delete(id)
|
|
81
81
|
} catch (e) {
|
|
@@ -71,7 +71,9 @@ export default {
|
|
|
71
71
|
auth: async (_, {input}) => {
|
|
72
72
|
try {
|
|
73
73
|
let userService = UserServiceFactory()
|
|
74
|
-
|
|
74
|
+
const userAgent = ''
|
|
75
|
+
const ip = ''
|
|
76
|
+
return await userService.auth(input.username, input.password,{userAgent, ip})
|
|
75
77
|
} catch (e) {
|
|
76
78
|
console.error("auth", e)
|
|
77
79
|
if (e instanceof BadCredentialsError) {
|