@colearn/microservices-kit
Advanced tools
Comparing version 0.0.29 to 0.0.30
import type { Request, Response, NextFunction } from 'express'; | ||
import { SystemRole } from './types'; | ||
import { AuthToken } from './token'; | ||
@@ -7,2 +8,3 @@ export interface AuthorizedRequest extends Request { | ||
export declare function authorize(req: AuthorizedRequest, _: Response, next: NextFunction): void; | ||
export declare function authorizeRoles(...roles: SystemRole[]): (req: Request, _: Response, next: NextFunction) => void; | ||
export declare function authorizeAdmin(req: Request, _: Response, next: NextFunction): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.authorizeAdmin = exports.authorize = void 0; | ||
exports.authorizeAdmin = exports.authorizeRoles = exports.authorize = void 0; | ||
const types_1 = require("./types"); | ||
@@ -16,2 +16,15 @@ const token_1 = require("./token"); | ||
exports.authorize = authorize; | ||
function authorizeRoles(...roles) { | ||
return (req, _, next) => { | ||
const user = req.user; | ||
const role = (0, token_1.parseSystemRole)(user === null || user === void 0 ? void 0 : user.scopes); | ||
if (!role || !roles.includes(role)) { | ||
next(new errors_1.ForbiddenError('Access Forbidden')); | ||
} | ||
else { | ||
next(); | ||
} | ||
}; | ||
} | ||
exports.authorizeRoles = authorizeRoles; | ||
function authorizeAdmin(req, _, next) { | ||
@@ -18,0 +31,0 @@ const user = req.user; |
@@ -10,3 +10,3 @@ import { SystemRole, User, Person, Maybe, PersonMembership } from './types'; | ||
export declare function createMembershipScopes(memberships?: Maybe<Maybe<PersonMembership>[]>): string[]; | ||
export default function createAuthToken(user: User, person?: Person): AuthToken; | ||
export declare function createAuthToken(user: User, person?: Person): AuthToken; | ||
export declare function parseSystemRole(scopes?: string[]): SystemRole | undefined; | ||
@@ -13,0 +13,0 @@ export declare type RoleRecord = { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.parseEntityRoleRecord = exports.parseSystemRole = exports.createMembershipScopes = void 0; | ||
exports.parseEntityRoleRecord = exports.parseSystemRole = exports.createAuthToken = exports.createMembershipScopes = void 0; | ||
const types_1 = require("./types"); | ||
@@ -39,3 +39,3 @@ function createMembershipScopes(memberships) { | ||
} | ||
exports.default = createAuthToken; | ||
exports.createAuthToken = createAuthToken; | ||
function parseSystemRole(scopes) { | ||
@@ -42,0 +42,0 @@ const system = scopes === null || scopes === void 0 ? void 0 : scopes.find((s) => s.startsWith('role:')); |
{ | ||
"name": "@colearn/microservices-kit", | ||
"version": "0.0.29", | ||
"version": "0.0.30", | ||
"description": "colearn microservice kit", | ||
@@ -5,0 +5,0 @@ "author": "Colearn", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49725
816