@tmlmobilidade/fastify 20260222.2136.56 → 20260223.1747.6
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.
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* * */
|
|
2
|
-
import { HttpException,
|
|
2
|
+
import { HttpException, HTTP_STATUS } from '@tmlmobilidade/consts';
|
|
3
3
|
import { AUTH_SESSION_COOKIE_NAME, authProvider } from '@tmlmobilidade/interfaces';
|
|
4
4
|
import { PermissionCatalog } from '@tmlmobilidade/types';
|
|
5
5
|
/**
|
|
@@ -18,7 +18,7 @@ export function authorizationMiddleware(scope, actions, requireAll = false) {
|
|
|
18
18
|
if (!sessionToken) {
|
|
19
19
|
return reply
|
|
20
20
|
.setCookie(AUTH_SESSION_COOKIE_NAME, '', { httpOnly: true, maxAge: 0, path: '/', sameSite: 'lax', secure: true })
|
|
21
|
-
.send({ data: 'Session token is missing', error: null, statusCode:
|
|
21
|
+
.send({ data: 'Session token is missing', error: null, statusCode: HTTP_STATUS.UNAUTHORIZED });
|
|
22
22
|
}
|
|
23
23
|
//
|
|
24
24
|
// Get user and permissions from cache or auth provider.
|
|
@@ -31,7 +31,7 @@ export function authorizationMiddleware(scope, actions, requireAll = false) {
|
|
|
31
31
|
if (!userData || !permissionsData || !organizationData) {
|
|
32
32
|
return reply
|
|
33
33
|
.setCookie(AUTH_SESSION_COOKIE_NAME, '', { httpOnly: true, maxAge: 0, path: '/', sameSite: 'lax', secure: true })
|
|
34
|
-
.send({ data: 'Session token is missing', error: null, statusCode:
|
|
34
|
+
.send({ data: 'Session token is missing', error: null, statusCode: HTTP_STATUS.UNAUTHORIZED });
|
|
35
35
|
}
|
|
36
36
|
request.me = userData;
|
|
37
37
|
request.permissions = permissionsData;
|
|
@@ -41,7 +41,7 @@ export function authorizationMiddleware(scope, actions, requireAll = false) {
|
|
|
41
41
|
console.error('Authorization Middleware Error:', error);
|
|
42
42
|
return reply
|
|
43
43
|
.setCookie(AUTH_SESSION_COOKIE_NAME, '', { httpOnly: true, maxAge: 0, path: '/', sameSite: 'lax', secure: true })
|
|
44
|
-
.send({ data: 'Session token is missing', error: null, statusCode:
|
|
44
|
+
.send({ data: 'Session token is missing', error: null, statusCode: HTTP_STATUS.UNAUTHORIZED });
|
|
45
45
|
}
|
|
46
46
|
//
|
|
47
47
|
// Evaluate the retrieved permissions,
|
|
@@ -53,7 +53,7 @@ export function authorizationMiddleware(scope, actions, requireAll = false) {
|
|
|
53
53
|
? permissionChecks.every(Boolean) // all must be true
|
|
54
54
|
: permissionChecks.some(Boolean); // at least one must be true
|
|
55
55
|
if (!isAllowed)
|
|
56
|
-
throw new HttpException(
|
|
56
|
+
throw new HttpException(HTTP_STATUS.FORBIDDEN, `Insufficient permissions | User: ${request.me._id} | Scope: "${scope}" | Actions: [${actions.join(',')}]`);
|
|
57
57
|
//
|
|
58
58
|
};
|
|
59
59
|
}
|
package/dist/fastify-service.js
CHANGED
|
@@ -6,7 +6,7 @@ import '@fastify/multipart';
|
|
|
6
6
|
import fastifyCookie from '@fastify/cookie';
|
|
7
7
|
import fastifyCors from '@fastify/cors';
|
|
8
8
|
import oneLineLogger from '@fastify/one-line-logger';
|
|
9
|
-
import { HttpException,
|
|
9
|
+
import { HttpException, HTTP_STATUS } from '@tmlmobilidade/consts';
|
|
10
10
|
import fastify from 'fastify';
|
|
11
11
|
const defaultFastifyServiceOptions = {
|
|
12
12
|
bodyLimit: 1024 * 1024 * 10, // 10MB
|
|
@@ -203,11 +203,11 @@ export class FastifyService {
|
|
|
203
203
|
}
|
|
204
204
|
else {
|
|
205
205
|
reply
|
|
206
|
-
.status(
|
|
206
|
+
.status(HTTP_STATUS.INTERNAL_SERVER_ERROR)
|
|
207
207
|
.send({
|
|
208
208
|
data: undefined,
|
|
209
209
|
error: 'Internal server error',
|
|
210
|
-
statusCode:
|
|
210
|
+
statusCode: HTTP_STATUS.INTERNAL_SERVER_ERROR,
|
|
211
211
|
});
|
|
212
212
|
}
|
|
213
213
|
});
|
|
@@ -216,14 +216,14 @@ export class FastifyService {
|
|
|
216
216
|
* This hook intercepts every outgoing response before it is sent.
|
|
217
217
|
* It parses the payload as a JSON object (assuming it matches the HttpResponse<T> structure),
|
|
218
218
|
* and sets the HTTP status code of the reply to the value of 'statusCode' in the payload,
|
|
219
|
-
* defaulting to
|
|
219
|
+
* defaulting to HTTP_STATUS.OK if not present.
|
|
220
220
|
* This ensures that the HTTP status code in the response matches the statusCode property
|
|
221
221
|
* in the application's response payload, providing consistent status handling.
|
|
222
222
|
*/
|
|
223
223
|
this.server.addHook('onSend', (_, reply, payload, done) => {
|
|
224
224
|
try {
|
|
225
225
|
const payloadJson = JSON.parse(payload);
|
|
226
|
-
reply.code(payloadJson.statusCode ??
|
|
226
|
+
reply.code(payloadJson.statusCode ?? HTTP_STATUS.OK);
|
|
227
227
|
}
|
|
228
228
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
229
229
|
catch (error) {
|