UNPKG

denwa-nest-shared

Version:

Shared NestJS module for DTO validation, RBAC, TypeORM query building, and OpenAPI client

156 lines (114 loc) • 3.58 kB
--- name: auth-security description: > Implementing security and RBAC using Roles, Public, and identity decorators. Handles GraphQL and REST contexts uniformly. Includes field-level role checking. type: core library: denwa-nest-shared library_version: '1.0.80' sources: - 'Denwa799/nest-shared:src/decorators.ts' - 'Denwa799/nest-shared:src/interceptors/fields-roles.interceptor.ts' --- # RBAC & Identity Decorators Use this skill to protect routes and mutations, and to extract user information regardless of whether the request came from a GraphQL resolver or a standard REST controller. ## Setup Protect a resolver with roles and extract the current user ID. ```typescript import { Resolver, Mutation, Args } from '@nestjs/graphql'; import { Roles, GetCurrentUserId } from 'denwa-nest-shared'; @Resolver() export class UserResolver { @Roles(['admin', 'manager']) @Mutation(() => Boolean) async deleteUser(@Args('id') id: string, @GetCurrentUserId() userId: string) { // userId is extracted correctly for both GQL and REST return true; } } ``` ## Core Patterns ### Public Routes Bypass global authentication guards for specific handlers. ```typescript import { Public } from 'denwa-nest-shared'; @Public() @Get('health') getHealth() { return 'ok'; } ``` ### Full User Object Extraction Extract the complete user object from the request context. ```typescript import { GetCurrentUser } from 'denwa-nest-shared'; @Get('profile') getProfile(@GetCurrentUser() user: any) { return user; } ``` ### Field-Level Role Protection Apply restrictions to specific GraphQL fields within a request. ```typescript import { Resolver, ResolveField, Parent } from '@nestjs/graphql'; import { CheckFieldsRoles, FieldsRolesInterceptor } from 'denwa-nest-shared'; import { UseInterceptors } from '@nestjs/common'; @Resolver(() => User) @UseInterceptors(FieldsRolesInterceptor) export class UserResolver { @CheckFieldsRoles({ email: ['admin'] }) @ResolveField(() => String) async email(@Parent() user: User) { return user.email; } } ``` ## Common Mistakes ### HIGH Direct Access to req.user Wrong: ```typescript @Mutation() async update(@Context() ctx) { const user = ctx.req.user; // Potential failure if context not normalized } ``` Correct: ```typescript @Mutation() async update(@GetCurrentUser() user: User) { // Safe extraction across GQL and REST } ``` Directly accessing the request object can fail in mixed environments. The identity decorators handle the underlying context normalization automatically. Source: Denwa799/nest-shared:src/decorators.ts:121 ### MEDIUM Listing super-admin in Roles Wrong: ```typescript @Roles(['admin', 'super-admin']) @Mutation() async delete() {} ``` Correct: ```typescript @Roles(['admin']) @Mutation() async delete() {} ``` The library's `FieldsRolesInterceptor` and standard role guards typically bypass all restrictions for `super-admin` or `super_admin`. You do not need to specify them explicitly in projects. Source: maintainer interview ### LOW Forgetting FieldsRolesInterceptor Wrong: ```typescript @CheckFieldsRoles({ salary: ['admin'] }) @ResolveField() async salary() {} ``` Correct: ```typescript @UseInterceptors(FieldsRolesInterceptor) @CheckFieldsRoles({ salary: ['admin'] }) @ResolveField() async salary() {} ``` The `@CheckFieldsRoles` decorator only stores metadata; it requires the `FieldsRolesInterceptor` to be active on the class or handler to actually enforce the restriction. Source: Denwa799/nest-shared:src/interceptors/fields-roles.interceptor.ts