@tmlmobilidade/interfaces 20250828.1517.15 → 20250828.1816.30
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/src/interfaces/auth/authorization.middleware.d.ts +9 -0
- package/dist/src/interfaces/auth/authorization.middleware.js +18 -0
- package/dist/src/interfaces/auth/users.interface.d.ts +2 -2
- package/dist/src/interfaces/index.d.ts +1 -0
- package/dist/src/interfaces/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type FastifyRequest } from '@tmlmobilidade/connectors';
|
|
2
|
+
import { type Permission, type User } from '@tmlmobilidade/types';
|
|
3
|
+
declare module 'fastify' {
|
|
4
|
+
interface FastifyRequest {
|
|
5
|
+
me: null | User;
|
|
6
|
+
permissions: Permission<unknown>[];
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export declare function authorizationMiddleware<T = unknown>(scope: string, action: string): (request: FastifyRequest) => Promise<void>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/* * */
|
|
2
|
+
import { authProvider } from '../../providers/auth/auth.js';
|
|
3
|
+
import { HttpException, HttpStatus } from '@tmlmobilidade/lib';
|
|
4
|
+
import { hasPermission } from '@tmlmobilidade/utils';
|
|
5
|
+
export function authorizationMiddleware(scope, action) {
|
|
6
|
+
return async (request) => {
|
|
7
|
+
const token = request.cookies.session_token;
|
|
8
|
+
if (!token) {
|
|
9
|
+
throw new HttpException(HttpStatus.UNAUTHORIZED, 'Invalid authorization token');
|
|
10
|
+
}
|
|
11
|
+
const permissions = await authProvider.getPermissions(token);
|
|
12
|
+
if (!hasPermission(permissions, scope, action)) {
|
|
13
|
+
throw new HttpException(HttpStatus.FORBIDDEN, 'User does not have permissions');
|
|
14
|
+
}
|
|
15
|
+
const user = await authProvider.getUser(token);
|
|
16
|
+
request.me = user;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -37,8 +37,8 @@ declare class UsersClass extends MongoCollectionClass<User, CreateUserDto, Updat
|
|
|
37
37
|
updated_at: import("@tmlmobilidade/types").UnixTimestamp;
|
|
38
38
|
phone?: string | null | undefined | undefined;
|
|
39
39
|
email: string;
|
|
40
|
-
permissions: import("@tmlmobilidade/types").Permission<unknown>[];
|
|
41
40
|
email_verified?: (null | import("@tmlmobilidade/types").UnixTimestamp) | undefined;
|
|
41
|
+
permissions: import("@tmlmobilidade/types").Permission<unknown>[];
|
|
42
42
|
first_name: string;
|
|
43
43
|
last_name: string;
|
|
44
44
|
organization_ids: string[];
|
|
@@ -62,8 +62,8 @@ declare class UsersClass extends MongoCollectionClass<User, CreateUserDto, Updat
|
|
|
62
62
|
updated_at: import("@tmlmobilidade/types").UnixTimestamp;
|
|
63
63
|
phone?: string | null | undefined | undefined;
|
|
64
64
|
email: string;
|
|
65
|
-
permissions: import("@tmlmobilidade/types").Permission<unknown>[];
|
|
66
65
|
email_verified?: (null | import("@tmlmobilidade/types").UnixTimestamp) | undefined;
|
|
66
|
+
permissions: import("@tmlmobilidade/types").Permission<unknown>[];
|
|
67
67
|
first_name: string;
|
|
68
68
|
last_name: string;
|
|
69
69
|
organization_ids: string[];
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from './agencies/agencies.interface.js';
|
|
2
2
|
export * from './alerts/alerts-realtime.interface.js';
|
|
3
3
|
export * from './alerts/alerts.interface.js';
|
|
4
|
+
export * from './auth/authorization.middleware.js';
|
|
4
5
|
export * from './auth/roles.interface.js';
|
|
5
6
|
export * from './auth/sessions.interface.js';
|
|
6
7
|
export * from './auth/users.interface.js';
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
export * from './agencies/agencies.interface.js';
|
|
3
3
|
export * from './alerts/alerts-realtime.interface.js';
|
|
4
4
|
export * from './alerts/alerts.interface.js';
|
|
5
|
+
export * from './auth/authorization.middleware.js';
|
|
5
6
|
export * from './auth/roles.interface.js';
|
|
6
7
|
export * from './auth/sessions.interface.js';
|
|
7
8
|
export * from './auth/users.interface.js';
|
package/package.json
CHANGED