|
| 1 | +import { ClsPluginBase, ClsService } from 'nestjs-cls'; |
| 2 | +import { AuthHost } from './auth-host'; |
| 3 | +import { AUTH_HOST_OPTIONS, getAuthClsKey } from './authorization.symbols'; |
| 4 | +import { |
| 5 | + AuthHostOptions, |
| 6 | + RequirePermissionOptions, |
| 7 | +} from './authorization.interfaces'; |
| 8 | +//import { UnauthorizedException } from '@nestjs/common'; |
| 9 | + |
| 10 | +const CLS_AUTHORIZATION_OPTIONS = Symbol('CLS_AUTHORIZATION_OPTIONS'); |
| 11 | + |
| 12 | +interface ClsAuthCallbacks<T> { |
| 13 | + authObjectFactory: (cls: ClsService) => T; |
| 14 | + permissionResolutionStrategy: (authObject: T, value: any) => boolean; |
| 15 | + exceptionFactory?: ( |
| 16 | + options: RequirePermissionOptions, |
| 17 | + value?: any, |
| 18 | + ) => Error | string; |
| 19 | +} |
| 20 | + |
| 21 | +export interface ClsPluginAuthorizationOptions<T> { |
| 22 | + imports?: any[]; |
| 23 | + inject?: any[]; |
| 24 | + useFactory?: (...args: any[]) => ClsAuthCallbacks<T>; |
| 25 | +} |
| 26 | + |
| 27 | +export class ClsPluginAuthorization extends ClsPluginBase { |
| 28 | + constructor(options: ClsPluginAuthorizationOptions<any>) { |
| 29 | + super('cls-plugin-authorization'); |
| 30 | + this.imports.push(...(options.imports ?? [])); |
| 31 | + this.providers = [ |
| 32 | + { |
| 33 | + provide: CLS_AUTHORIZATION_OPTIONS, |
| 34 | + inject: options.inject, |
| 35 | + useFactory: options.useFactory ?? (() => ({})), |
| 36 | + }, |
| 37 | + { |
| 38 | + provide: AUTH_HOST_OPTIONS, |
| 39 | + inject: [CLS_AUTHORIZATION_OPTIONS], |
| 40 | + useFactory: (callbacks: ClsAuthCallbacks<any>) => |
| 41 | + ({ |
| 42 | + permissionResolutionStrategy: |
| 43 | + callbacks.permissionResolutionStrategy, |
| 44 | + exceptionFactory: ( |
| 45 | + options: RequirePermissionOptions, |
| 46 | + value?: any, |
| 47 | + ) => { |
| 48 | + const factory = |
| 49 | + callbacks.exceptionFactory ?? |
| 50 | + this.defaultExceptionFactory; |
| 51 | + |
| 52 | + const exception = factory(options, value); |
| 53 | + if (typeof exception === 'string') { |
| 54 | + return new Error(exception); |
| 55 | + } |
| 56 | + return exception; |
| 57 | + }, |
| 58 | + }) satisfies AuthHostOptions, |
| 59 | + }, |
| 60 | + { |
| 61 | + provide: AuthHost, |
| 62 | + useClass: AuthHost, |
| 63 | + }, |
| 64 | + ]; |
| 65 | + |
| 66 | + this.registerHooks({ |
| 67 | + inject: [CLS_AUTHORIZATION_OPTIONS], |
| 68 | + useFactory: (options: ClsAuthCallbacks<any>) => ({ |
| 69 | + afterSetup: (cls: ClsService) => { |
| 70 | + const authObject = options.authObjectFactory(cls); |
| 71 | + cls.setIfUndefined(getAuthClsKey(), authObject); |
| 72 | + }, |
| 73 | + }), |
| 74 | + }); |
| 75 | + |
| 76 | + this.exports = [AuthHost]; |
| 77 | + } |
| 78 | + |
| 79 | + private defaultExceptionFactory( |
| 80 | + options: RequirePermissionOptions, |
| 81 | + value?: any, |
| 82 | + ): Error | string { |
| 83 | + let message = options.exceptionMessage ?? 'Permission denied'; |
| 84 | + if (value !== undefined) { |
| 85 | + message += ` (${JSON.stringify(value)})`; |
| 86 | + } |
| 87 | + return message; |
| 88 | + } |
| 89 | +} |
0 commit comments