@lyxa.ai/core 1.0.189 → 1.0.191
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/libraries/trpc/middlewares/createRoleProtectedProcedure.d.ts +15 -0
- package/dist/libraries/trpc/middlewares/createRoleProtectedProcedure.js +21 -0
- package/dist/libraries/trpc/middlewares/createRoleProtectedProcedure.js.map +1 -0
- package/dist/types/README.md +1 -1
- package/dist/types/package.json +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AuthEntityType } from '../../auth';
|
|
2
|
+
interface RoleProtectedOptions {
|
|
3
|
+
entityTypes: AuthEntityType[];
|
|
4
|
+
allowedRoles: string[];
|
|
5
|
+
}
|
|
6
|
+
export declare function createRoleProtectedProcedure(options: RoleProtectedOptions): import("@trpc/server").TRPCProcedureBuilder<import("../context").LyxaHTTPContext, object, {
|
|
7
|
+
res: import("http").ServerResponse<import("http").IncomingMessage>;
|
|
8
|
+
tokenType: import("../../auth").TokenType | undefined;
|
|
9
|
+
entity: import("../context").EntityContext | undefined;
|
|
10
|
+
req: import("http").IncomingMessage;
|
|
11
|
+
requestId: string | undefined;
|
|
12
|
+
usedRefreshToken: boolean | undefined;
|
|
13
|
+
tokenRenewed: boolean | undefined;
|
|
14
|
+
}, import("@trpc/server").TRPCUnsetMarker, import("@trpc/server").TRPCUnsetMarker, import("@trpc/server").TRPCUnsetMarker, import("@trpc/server").TRPCUnsetMarker, false>;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createRoleProtectedProcedure = createRoleProtectedProcedure;
|
|
4
|
+
const server_1 = require("@trpc/server");
|
|
5
|
+
const auth_1 = require("./auth");
|
|
6
|
+
function createRoleProtectedProcedure(options) {
|
|
7
|
+
return (0, auth_1.createAuthenticatedProcedure)({ entityTypes: options.entityTypes }).use(async ({ ctx, next }) => {
|
|
8
|
+
if (!ctx.entity) {
|
|
9
|
+
throw new server_1.TRPCError({ code: 'UNAUTHORIZED', message: 'User not authorized' });
|
|
10
|
+
}
|
|
11
|
+
const role = ctx.entity.parameters?.role;
|
|
12
|
+
if (typeof role !== 'string' || !options.allowedRoles.includes(role)) {
|
|
13
|
+
throw new server_1.TRPCError({
|
|
14
|
+
code: 'FORBIDDEN',
|
|
15
|
+
message: 'You do not have the required role to access this resource',
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
return next({ ctx });
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=createRoleProtectedProcedure.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createRoleProtectedProcedure.js","sourceRoot":"/","sources":["libraries/trpc/middlewares/createRoleProtectedProcedure.ts"],"names":[],"mappings":";;AAYA,oEAgBC;AA5BD,yCAAyC;AACzC,iCAAsD;AAWtD,SAAgB,4BAA4B,CAAC,OAA6B;IACzE,OAAO,IAAA,mCAA4B,EAAC,EAAE,WAAW,EAAE,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE,EAAE;QACrG,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC;YACjB,MAAM,IAAI,kBAAS,CAAC,EAAE,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,qBAAqB,EAAE,CAAC,CAAC;QAC/E,CAAC;QAED,MAAM,IAAI,GAAG,GAAG,CAAC,MAAM,CAAC,UAAU,EAAE,IAAI,CAAC;QACzC,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;YACtE,MAAM,IAAI,kBAAS,CAAC;gBACnB,IAAI,EAAE,WAAW;gBACjB,OAAO,EAAE,2DAA2D;aACpE,CAAC,CAAC;QACJ,CAAC;QAED,OAAO,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC;IACtB,CAAC,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import { TRPCError } from '@trpc/server';\nimport { createAuthenticatedProcedure } from './auth';\nimport { AuthEntityType } from '../../auth';\n\ninterface RoleProtectedOptions {\n\tentityTypes: AuthEntityType[];\n\tallowedRoles: string[];\n}\n\n/**\n * Middleware that ensures the user has one of the allowed roles from ctx.entity.parameters.role\n */\nexport function createRoleProtectedProcedure(options: RoleProtectedOptions) {\n\treturn createAuthenticatedProcedure({ entityTypes: options.entityTypes }).use(async ({ ctx, next }) => {\n\t\tif (!ctx.entity) {\n\t\t\tthrow new TRPCError({ code: 'UNAUTHORIZED', message: 'User not authorized' });\n\t\t}\n\n\t\tconst role = ctx.entity.parameters?.role;\n\t\tif (typeof role !== 'string' || !options.allowedRoles.includes(role)) {\n\t\t\tthrow new TRPCError({\n\t\t\t\tcode: 'FORBIDDEN',\n\t\t\t\tmessage: 'You do not have the required role to access this resource',\n\t\t\t});\n\t\t}\n\n\t\treturn next({ ctx });\n\t});\n}\n"]}
|
package/dist/types/README.md
CHANGED
package/dist/types/package.json
CHANGED