@tmlmobilidade/fastify 20260706.931.16 → 20260706.1756.42

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,10 @@
1
1
  import { FastifyReply, type FastifyRequest } from './fastify-service.js';
2
2
  import { type ActionsOf, type Organization, type Permission, type User } from '@tmlmobilidade/types';
3
+ interface AuthorizationPermissionCheck<S extends Permission['scope'] = Permission['scope']> {
4
+ actions: ActionsOf<S>[];
5
+ requireAll?: boolean;
6
+ scope: S;
7
+ }
3
8
  declare module 'fastify' {
4
9
  interface FastifyRequest {
5
10
  me: User;
@@ -14,4 +19,6 @@ declare module 'fastify' {
14
19
  * @param requireAll Whether all actions must be true or at least one must be true.
15
20
  * @returns Fastify middleware function.
16
21
  */
22
+ export declare function authorizationMiddleware(checks: AuthorizationPermissionCheck[]): (request: FastifyRequest, reply: FastifyReply<string>) => Promise<void>;
17
23
  export declare function authorizationMiddleware<S extends Permission['scope']>(scope?: S, actions?: ActionsOf<S>[], requireAll?: boolean): (request: FastifyRequest, reply: FastifyReply<string>) => Promise<void>;
24
+ export {};
@@ -2,14 +2,13 @@
2
2
  import { HTTP_STATUS, HttpException } from '@tmlmobilidade/consts';
3
3
  import { AUTH_SESSION_COOKIE_NAME, authProvider } from '@tmlmobilidade/interfaces';
4
4
  import { PermissionCatalog } from '@tmlmobilidade/types';
5
- /**
6
- * Creates an authorization middleware that validates user authentication and permissions.
7
- * @param scope The permission scope to check (optional).
8
- * @param action The permission action(s) to check (optional).
9
- * @param requireAll Whether all actions must be true or at least one must be true.
10
- * @returns Fastify middleware function.
11
- */
12
- export function authorizationMiddleware(scope, actions, requireAll = false) {
5
+ function isPermissionCheckAllowed(permissionEntries, check) {
6
+ const permissionChecks = check.actions.map(action => PermissionCatalog.hasPermission(permissionEntries, check.scope, action));
7
+ return check.requireAll
8
+ ? permissionChecks.every(Boolean)
9
+ : permissionChecks.some(Boolean);
10
+ }
11
+ export function authorizationMiddleware(scopeOrChecks, actions = [], requireAll = false) {
13
12
  return async (request, reply) => {
14
13
  //
15
14
  //
@@ -46,14 +45,21 @@ export function authorizationMiddleware(scope, actions, requireAll = false) {
46
45
  //
47
46
  // Evaluate the retrieved permissions,
48
47
  // if scope and actions are provided.
49
- if (!scope)
48
+ if (Array.isArray(scopeOrChecks)) {
49
+ const isAllowed = scopeOrChecks.some(check => isPermissionCheckAllowed(request.permissions, check));
50
+ if (!isAllowed) {
51
+ throw new HttpException(HTTP_STATUS.FORBIDDEN, `Insufficient permissions | User: ${request.me._id}`);
52
+ }
53
+ return;
54
+ }
55
+ if (!scopeOrChecks)
50
56
  return;
51
- const permissionChecks = actions.map(action => PermissionCatalog.hasPermission(request.permissions, scope, action));
57
+ const permissionChecks = actions.map(action => PermissionCatalog.hasPermission(request.permissions, scopeOrChecks, action));
52
58
  const isAllowed = requireAll
53
59
  ? permissionChecks.every(Boolean) // all must be true
54
60
  : permissionChecks.some(Boolean); // at least one must be true
55
61
  if (!isAllowed) {
56
- throw new HttpException(HTTP_STATUS.FORBIDDEN, `Insufficient permissions | User: ${request.me._id} | Scope: "${scope}" | Actions: [${actions.join(',')}]`);
62
+ throw new HttpException(HTTP_STATUS.FORBIDDEN, `Insufficient permissions | User: ${request.me._id} | Scope: "${scopeOrChecks}" | Actions: [${actions.join(',')}]`);
57
63
  }
58
64
  //
59
65
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/fastify",
3
- "version": "20260706.931.16",
3
+ "version": "20260706.1756.42",
4
4
  "author": {
5
5
  "email": "iso@tmlmobilidade.pt",
6
6
  "name": "TML-ISO"
@@ -44,7 +44,7 @@
44
44
  "@tmlmobilidade/interfaces": "*",
45
45
  "@tmlmobilidade/logger": "*",
46
46
  "@tmlmobilidade/utils": "*",
47
- "fastify": "5.9.0"
47
+ "fastify": "5.10.0"
48
48
  },
49
49
  "devDependencies": {
50
50
  "@tmlmobilidade/tsconfig": "*",