@decaf-ts/for-nest 0.1.6 → 0.1.8
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/for-nest.cjs +1 -1
- package/dist/for-nest.cjs.map +1 -1
- package/dist/for-nest.js +1 -1
- package/dist/for-nest.js.map +1 -1
- package/lib/constants.cjs +4 -1
- package/lib/constants.d.ts +3 -0
- package/lib/constants.js.map +1 -1
- package/lib/decaf-model/FromModelController.cjs +2 -0
- package/lib/decaf-model/FromModelController.js.map +1 -1
- package/lib/decaf-model/decorators/decorators.cjs +60 -0
- package/lib/decaf-model/decorators/decorators.d.ts +46 -0
- package/lib/decaf-model/decorators/decorators.js.map +1 -0
- package/lib/decaf-model/decorators/index.cjs +1 -0
- package/lib/decaf-model/decorators/index.d.ts +1 -0
- package/lib/decaf-model/decorators/index.js.map +1 -1
- package/lib/esm/constants.d.ts +3 -0
- package/lib/esm/constants.js +3 -0
- package/lib/esm/constants.js.map +1 -1
- package/lib/esm/decaf-model/FromModelController.js +2 -0
- package/lib/esm/decaf-model/FromModelController.js.map +1 -1
- package/lib/esm/decaf-model/decorators/decorators.d.ts +46 -0
- package/lib/esm/decaf-model/decorators/decorators.js +55 -0
- package/lib/esm/decaf-model/decorators/decorators.js.map +1 -0
- package/lib/esm/decaf-model/decorators/index.d.ts +1 -0
- package/lib/esm/decaf-model/decorators/index.js +1 -0
- package/lib/esm/decaf-model/decorators/index.js.map +1 -1
- package/lib/esm/index.d.ts +1 -1
- package/lib/esm/index.js +1 -1
- package/lib/esm/interceptors/AuthInterceptor.d.ts +10 -0
- package/lib/esm/interceptors/AuthInterceptor.js +37 -0
- package/lib/esm/interceptors/AuthInterceptor.js.map +1 -0
- package/lib/esm/interceptors/index.d.ts +1 -0
- package/lib/esm/interceptors/index.js +1 -0
- package/lib/esm/interceptors/index.js.map +1 -1
- package/lib/esm/types.d.ts +45 -1
- package/lib/index.cjs +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/interceptors/AuthInterceptor.cjs +40 -0
- package/lib/interceptors/AuthInterceptor.d.ts +10 -0
- package/lib/interceptors/AuthInterceptor.js.map +1 -0
- package/lib/interceptors/index.cjs +1 -0
- package/lib/interceptors/index.d.ts +1 -0
- package/lib/interceptors/index.js.map +1 -1
- package/lib/types.d.ts +45 -1
- package/package.json +1 -1
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Constructor } from "@decaf-ts/decoration";
|
|
2
|
+
/**
|
|
3
|
+
* A decorator function that applies authentication and authorization metadata to a NestJS controller or method.
|
|
4
|
+
*
|
|
5
|
+
* @param model - The model name or constructor function for the resource being accessed.
|
|
6
|
+
* If a string is provided, it is used as the resource name.
|
|
7
|
+
* If a constructor function is provided, its name is used as the resource name.
|
|
8
|
+
*
|
|
9
|
+
* @returns - A function that applies the authentication and authorization decorators to the target.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* @Controller('users')
|
|
14
|
+
* @Auth('User')
|
|
15
|
+
* export class UsersController {
|
|
16
|
+
* // ...
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* @Controller('users')
|
|
23
|
+
* @Auth(User)
|
|
24
|
+
* export class UsersController {
|
|
25
|
+
* // ...
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function Auth(model: string | Constructor): <TFunction extends Function, Y>(target: TFunction | object, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
|
|
30
|
+
/**
|
|
31
|
+
* A decorator function that sets the roles required for authentication and authorization to the model in NestJS.
|
|
32
|
+
*
|
|
33
|
+
* @param roles - An array of role names required for access.
|
|
34
|
+
*
|
|
35
|
+
* @returns - A function that applies the role decorators to the target.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* @model('users')
|
|
40
|
+
* @Roles(['admin'])
|
|
41
|
+
* export class UserModel {
|
|
42
|
+
* //...
|
|
43
|
+
* }
|
|
44
|
+
*
|
|
45
|
+
*/
|
|
46
|
+
export declare const Roles: (roles: string[]) => (model: any, prop?: any, descriptor?: PropertyDescriptor | number) => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorators.js","sourceRoot":"","sources":["../../../src/decaf-model/decorators/decorators.ts"],"names":[],"mappings":";;;AAiCA,oBAOC;AAxCD,2CAA+E;AAE/E,qDAA0D;AAC1D,8EAAqE;AACrE,qDAAgD;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,SAAgB,IAAI,CAAC,KAA2B;IAC9C,MAAM,QAAQ,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC;IAEhE,OAAO,IAAA,wBAAe,EACpB,IAAA,oBAAW,EAAC,yBAAa,EAAE,QAAQ,CAAC,EACpC,IAAA,wBAAe,EAAC,iCAAe,CAAC,CACjC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACI,MAAM,KAAK,GAAG,CAAC,KAAe,EAAE,EAAE;IACvC,OAAO,IAAA,qBAAQ,EAAC,oBAAQ,EAAE,KAAK,CAAC,CAAC;AACnC,CAAC,CAAC;AAFW,QAAA,KAAK,SAEhB"}
|
|
@@ -18,4 +18,5 @@ __exportStar(require("./types.cjs"), exports);
|
|
|
18
18
|
__exportStar(require("./ApiOperationFromModel.cjs"), exports);
|
|
19
19
|
__exportStar(require("./ApiParamsFromModel.cjs"), exports);
|
|
20
20
|
__exportStar(require("./DecafParams.cjs"), exports);
|
|
21
|
+
__exportStar(require("./decorators.cjs"), exports);
|
|
21
22
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/decaf-model/decorators/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,8CAAwB;AACxB,8DAAwC;AACxC,2DAAqC;AACrC,oDAA8B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/decaf-model/decorators/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,8CAAwB;AACxB,8DAAwC;AACxC,2DAAqC;AACrC,oDAA8B;AAC9B,mDAA6B"}
|
package/lib/esm/constants.d.ts
CHANGED
|
@@ -2,3 +2,6 @@ export declare const DECAF_MODULE_OPTIONS = "DecafModuleOptions";
|
|
|
2
2
|
export declare const DECAF_ADAPTER_ID = "DecafAdapter";
|
|
3
3
|
export declare const DECAF_HANDLERS: unique symbol;
|
|
4
4
|
export declare const DECAF_ADAPTER_OPTIONS: unique symbol;
|
|
5
|
+
export declare const AUTH_HANDLER: unique symbol;
|
|
6
|
+
export declare const AUTH_META_KEY = "auth:meta";
|
|
7
|
+
export declare const AuthRole = "AuthRole";
|
package/lib/esm/constants.js
CHANGED
|
@@ -2,4 +2,7 @@ export const DECAF_MODULE_OPTIONS = "DecafModuleOptions";
|
|
|
2
2
|
export const DECAF_ADAPTER_ID = "DecafAdapter";
|
|
3
3
|
export const DECAF_HANDLERS = Symbol("DecafHandlers");
|
|
4
4
|
export const DECAF_ADAPTER_OPTIONS = Symbol("DecafAdapterForOptions");
|
|
5
|
+
export const AUTH_HANDLER = Symbol("AUTH_HANDLER");
|
|
6
|
+
export const AUTH_META_KEY = "auth:meta";
|
|
7
|
+
export const AuthRole = "AuthRole";
|
|
5
8
|
//# sourceMappingURL=constants.js.map
|
package/lib/esm/constants.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,oBAAoB,GAAG,oBAAoB,CAAC;AACzD,MAAM,CAAC,MAAM,gBAAgB,GAAG,cAAc,CAAC;AAE/C,MAAM,CAAC,MAAM,cAAc,GAAG,MAAM,CAAC,eAAe,CAAC,CAAC;AACtD,MAAM,CAAC,MAAM,qBAAqB,GAAG,MAAM,CAAC,wBAAwB,CAAC,CAAC"}
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,oBAAoB,GAAG,oBAAoB,CAAC;AACzD,MAAM,CAAC,MAAM,gBAAgB,GAAG,cAAc,CAAC;AAE/C,MAAM,CAAC,MAAM,cAAc,GAAG,MAAM,CAAC,eAAe,CAAC,CAAC;AACtD,MAAM,CAAC,MAAM,qBAAqB,GAAG,MAAM,CAAC,wBAAwB,CAAC,CAAC;AAEtE,MAAM,CAAC,MAAM,YAAY,GAAG,MAAM,CAAC,cAAc,CAAC,CAAC;AACnD,MAAM,CAAC,MAAM,aAAa,GAAG,WAAW,CAAC;AAEzC,MAAM,CAAC,MAAM,QAAQ,GAAG,UAAU,CAAC"}
|
|
@@ -20,6 +20,7 @@ import { Metadata } from "@decaf-ts/decoration";
|
|
|
20
20
|
import { ApiOperationFromModel, ApiParamsFromModel, DecafParams, } from "./decorators/index.js";
|
|
21
21
|
import { DecafRequestContext } from "./../request/index.js";
|
|
22
22
|
import { DECAF_ADAPTER_OPTIONS } from "./../constants.js";
|
|
23
|
+
import { Auth } from "./decorators/decorators.js";
|
|
23
24
|
/**
|
|
24
25
|
* @description
|
|
25
26
|
* Factory and utilities for generating dynamic NestJS controllers from Decaf {@link Model} definitions.
|
|
@@ -270,6 +271,7 @@ export class FromModelController {
|
|
|
270
271
|
Controller(routePath),
|
|
271
272
|
ApiTags(modelClazzName),
|
|
272
273
|
ApiExtraModels(ModelClazz),
|
|
274
|
+
Auth(ModelClazz),
|
|
273
275
|
__metadata("design:paramtypes", [DecafRequestContext])
|
|
274
276
|
], DynamicModelController);
|
|
275
277
|
return DynamicModelController;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FromModelController.js","sourceRoot":"","sources":["../../../src/decaf-model/FromModelController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EACL,qBAAqB,EACrB,OAAO,EACP,kBAAkB,EAClB,cAAc,EACd,mBAAmB,EACnB,aAAa,EACb,YAAY,EACZ,QAAQ,EACR,OAAO,EACP,8BAA8B,EAC9B,aAAa,GACd,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,YAAY,EAAQ,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAChE,OAAO,EAAE,KAAK,EAAoB,MAAM,gCAAgC,CAAC;AACzE,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACtE,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAClE,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAMhD,OAAO,EACL,qBAAqB,EACrB,kBAAkB,EAClB,WAAW,GACZ,8BAAqB;AACtB,OAAO,EAAE,mBAAmB,EAAE,8BAAmB;AACjD,OAAO,EAAE,qBAAqB,EAAE,0BAAqB;
|
|
1
|
+
{"version":3,"file":"FromModelController.js","sourceRoot":"","sources":["../../../src/decaf-model/FromModelController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EACL,qBAAqB,EACrB,OAAO,EACP,kBAAkB,EAClB,cAAc,EACd,mBAAmB,EACnB,aAAa,EACb,YAAY,EACZ,QAAQ,EACR,OAAO,EACP,8BAA8B,EAC9B,aAAa,GACd,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,YAAY,EAAQ,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAChE,OAAO,EAAE,KAAK,EAAoB,MAAM,g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AAC;QAErD,MAAM,IAAI,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,GAAG,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC1D,MAAM,aAAa,GAAyB,UAAU,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE;YACjE,OAAO;gBACL,IAAI,EAAE,GAAG;gBACT,WAAW,EAAE,QAAQ,CAAC,WAAW,CAAC,UAAU,EAAE,GAAG,CAAC;gBAClD,QAAQ,EAAE,IAAI;gBACd,IAAI,EAAE,MAAM;aACb,CAAC;QACJ,CAAC,CAAC,CAAC;QAEH,OAAO;YACL,IAAI;YACJ,WAAW;YACX,aAAa;YACb,KAAK,EAAE,CAAC,GAAG,MAA8B,EAAE,EAAE,CAC3C,QAAQ,EAAE,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;SAC1E,CAAC;IACJ,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Constructor } from "@decaf-ts/decoration";
|
|
2
|
+
/**
|
|
3
|
+
* A decorator function that applies authentication and authorization metadata to a NestJS controller or method.
|
|
4
|
+
*
|
|
5
|
+
* @param model - The model name or constructor function for the resource being accessed.
|
|
6
|
+
* If a string is provided, it is used as the resource name.
|
|
7
|
+
* If a constructor function is provided, its name is used as the resource name.
|
|
8
|
+
*
|
|
9
|
+
* @returns - A function that applies the authentication and authorization decorators to the target.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* @Controller('users')
|
|
14
|
+
* @Auth('User')
|
|
15
|
+
* export class UsersController {
|
|
16
|
+
* // ...
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* @Controller('users')
|
|
23
|
+
* @Auth(User)
|
|
24
|
+
* export class UsersController {
|
|
25
|
+
* // ...
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function Auth(model: string | Constructor): <TFunction extends Function, Y>(target: TFunction | object, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
|
|
30
|
+
/**
|
|
31
|
+
* A decorator function that sets the roles required for authentication and authorization to the model in NestJS.
|
|
32
|
+
*
|
|
33
|
+
* @param roles - An array of role names required for access.
|
|
34
|
+
*
|
|
35
|
+
* @returns - A function that applies the role decorators to the target.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* @model('users')
|
|
40
|
+
* @Roles(['admin'])
|
|
41
|
+
* export class UserModel {
|
|
42
|
+
* //...
|
|
43
|
+
* }
|
|
44
|
+
*
|
|
45
|
+
*/
|
|
46
|
+
export declare const Roles: (roles: string[]) => (model: any, prop?: any, descriptor?: PropertyDescriptor | number) => void;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { applyDecorators, SetMetadata, UseInterceptors } from "@nestjs/common";
|
|
2
|
+
import { AUTH_META_KEY, AuthRole } from "./../../constants.js";
|
|
3
|
+
import { AuthInterceptor } from "./../../interceptors/AuthInterceptor.js";
|
|
4
|
+
import { metadata } from "@decaf-ts/decoration";
|
|
5
|
+
/**
|
|
6
|
+
* A decorator function that applies authentication and authorization metadata to a NestJS controller or method.
|
|
7
|
+
*
|
|
8
|
+
* @param model - The model name or constructor function for the resource being accessed.
|
|
9
|
+
* If a string is provided, it is used as the resource name.
|
|
10
|
+
* If a constructor function is provided, its name is used as the resource name.
|
|
11
|
+
*
|
|
12
|
+
* @returns - A function that applies the authentication and authorization decorators to the target.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* @Controller('users')
|
|
17
|
+
* @Auth('User')
|
|
18
|
+
* export class UsersController {
|
|
19
|
+
* // ...
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* @Controller('users')
|
|
26
|
+
* @Auth(User)
|
|
27
|
+
* export class UsersController {
|
|
28
|
+
* // ...
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export function Auth(model) {
|
|
33
|
+
const resource = typeof model === "string" ? model : model.name;
|
|
34
|
+
return applyDecorators(SetMetadata(AUTH_META_KEY, resource), UseInterceptors(AuthInterceptor));
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* A decorator function that sets the roles required for authentication and authorization to the model in NestJS.
|
|
38
|
+
*
|
|
39
|
+
* @param roles - An array of role names required for access.
|
|
40
|
+
*
|
|
41
|
+
* @returns - A function that applies the role decorators to the target.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* @model('users')
|
|
46
|
+
* @Roles(['admin'])
|
|
47
|
+
* export class UserModel {
|
|
48
|
+
* //...
|
|
49
|
+
* }
|
|
50
|
+
*
|
|
51
|
+
*/
|
|
52
|
+
export const Roles = (roles) => {
|
|
53
|
+
return metadata(AuthRole, roles);
|
|
54
|
+
};
|
|
55
|
+
//# sourceMappingURL=decorators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorators.js","sourceRoot":"","sources":["../../../../src/decaf-model/decorators/decorators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAE/E,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,6BAAwB;AAC1D,OAAO,EAAE,eAAe,EAAE,gDAA2C;AACrE,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,UAAU,IAAI,CAAC,KAA2B;IAC9C,MAAM,QAAQ,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC;IAEhE,OAAO,eAAe,CACpB,WAAW,CAAC,aAAa,EAAE,QAAQ,CAAC,EACpC,eAAe,CAAC,eAAe,CAAC,CACjC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,KAAe,EAAE,EAAE;IACvC,OAAO,QAAQ,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;AACnC,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/decaf-model/decorators/index.ts"],"names":[],"mappings":"AAAA,2BAAwB;AACxB,2CAAwC;AACxC,wCAAqC;AACrC,iCAA8B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/decaf-model/decorators/index.ts"],"names":[],"mappings":"AAAA,2BAAwB;AACxB,2CAAwC;AACxC,wCAAqC;AACrC,iCAA8B;AAC9B,gCAA6B"}
|
package/lib/esm/index.d.ts
CHANGED
package/lib/esm/index.js
CHANGED
|
@@ -36,7 +36,7 @@ export * from "./request/index.js";
|
|
|
36
36
|
* @constant
|
|
37
37
|
* @type {string}
|
|
38
38
|
*/
|
|
39
|
-
export const VERSION = "0.1.
|
|
39
|
+
export const VERSION = "0.1.8";
|
|
40
40
|
export const PACKAGE_NAME = "@decaf-ts/for-nest";
|
|
41
41
|
Metadata.registerLibrary(PACKAGE_NAME, VERSION);
|
|
42
42
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { NestInterceptor, ExecutionContext, CallHandler } from "@nestjs/common";
|
|
2
|
+
import { Reflector } from "@nestjs/core";
|
|
3
|
+
import { Observable } from "rxjs";
|
|
4
|
+
import type { AuthHandler } from "../types";
|
|
5
|
+
export declare class AuthInterceptor implements NestInterceptor {
|
|
6
|
+
private readonly reflector;
|
|
7
|
+
private readonly authHandler?;
|
|
8
|
+
constructor(reflector: Reflector, authHandler?: AuthHandler | undefined);
|
|
9
|
+
intercept(ctx: ExecutionContext, next: CallHandler): Promise<Observable<any>>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
|
+
};
|
|
13
|
+
import { Injectable, Inject, Optional, } from "@nestjs/common";
|
|
14
|
+
import { Reflector } from "@nestjs/core";
|
|
15
|
+
import { AUTH_HANDLER, AUTH_META_KEY } from "./../constants.js";
|
|
16
|
+
let AuthInterceptor = class AuthInterceptor {
|
|
17
|
+
constructor(reflector, authHandler) {
|
|
18
|
+
this.reflector = reflector;
|
|
19
|
+
this.authHandler = authHandler;
|
|
20
|
+
}
|
|
21
|
+
async intercept(ctx, next) {
|
|
22
|
+
const modelName = this.reflector.get(AUTH_META_KEY, ctx.getHandler()) ??
|
|
23
|
+
this.reflector.get(AUTH_META_KEY, ctx.getClass());
|
|
24
|
+
if (modelName && this.authHandler) {
|
|
25
|
+
await this.authHandler.authorize(ctx, modelName);
|
|
26
|
+
}
|
|
27
|
+
return next.handle();
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
AuthInterceptor = __decorate([
|
|
31
|
+
Injectable(),
|
|
32
|
+
__param(1, Optional()),
|
|
33
|
+
__param(1, Inject(AUTH_HANDLER)),
|
|
34
|
+
__metadata("design:paramtypes", [Reflector, Object])
|
|
35
|
+
], AuthInterceptor);
|
|
36
|
+
export { AuthInterceptor };
|
|
37
|
+
//# sourceMappingURL=AuthInterceptor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AuthInterceptor.js","sourceRoot":"","sources":["../../../src/interceptors/AuthInterceptor.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EACL,UAAU,EAIV,MAAM,EACN,QAAQ,GACT,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAIzC,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,0BAAqB;AAIpD,IAAM,eAAe,GAArB,MAAM,eAAe;IAC1B,YACmB,SAAoB,EACc,WAAyB;QAD3D,cAAS,GAAT,SAAS,CAAW;QACc,gBAAW,GAAX,WAAW,CAAc;IAC3E,CAAC;IAEJ,KAAK,CAAC,SAAS,CACb,GAAqB,EACrB,IAAiB;QAEjB,MAAM,SAAS,GACb,IAAI,CAAC,SAAS,CAAC,GAAG,CAChB,aAAa,EACb,GAAG,CAAC,UAAU,EAAE,CACjB;YACD,IAAI,CAAC,SAAS,CAAC,GAAG,CAAuB,aAAa,EAAE,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC;QAE1E,IAAI,SAAS,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;YAClC,MAAM,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;QACnD,CAAC;QAED,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC;CACF,CAAA;AAvBY,eAAe;IAD3B,UAAU,EAAE;IAIR,WAAA,QAAQ,EAAE,CAAA;IAAE,WAAA,MAAM,CAAC,YAAY,CAAC,CAAA;qCADL,SAAS;GAF5B,eAAe,CAuB3B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/interceptors/index.ts"],"names":[],"mappings":"AAAA,oDAAiD"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/interceptors/index.ts"],"names":[],"mappings":"AAAA,oDAAiD;AACjD,qCAAkC"}
|
package/lib/esm/types.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Adapter } from "@decaf-ts/core";
|
|
2
2
|
import { Constructor } from "@decaf-ts/decoration";
|
|
3
|
-
import { Type } from "@nestjs/common";
|
|
3
|
+
import { ExecutionContext, Type } from "@nestjs/common";
|
|
4
4
|
export interface RequestContextAccessor {
|
|
5
5
|
set(key: string | symbol, value: any): void;
|
|
6
6
|
get<T = any>(key: string | symbol): T | undefined;
|
|
@@ -18,3 +18,47 @@ export type DecafModuleOptions<CONF = any, A extends Adapter<CONF, any, any, any
|
|
|
18
18
|
autoControllers: boolean;
|
|
19
19
|
handlers?: Type<DecafRequestHandler>[];
|
|
20
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* Abstraction used by the {@link AuthInterceptor} to authorize decaf models.
|
|
23
|
+
*
|
|
24
|
+
* // costumauthHandler.ts
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* export class CustomAuthHandler implements AuthHandler {
|
|
28
|
+
* async authorize(ctx: ExecutionContext, resource: string) {
|
|
29
|
+
* const req = ctx.switchToHttp().getRequest();
|
|
30
|
+
* const userRole = req.headers.authorization?.split(" ")[1] as string;
|
|
31
|
+
* if (!userRole) throw new AuthorizationError("Unauthenticated");
|
|
32
|
+
* const roles = Metadata.get(Model.get(resource)!, AuthRole);
|
|
33
|
+
* if (!roles.includes(userRole)) {
|
|
34
|
+
* throw new AuthorizationError("Unauthorized");
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
* }
|
|
38
|
+
*
|
|
39
|
+
* // auth.module.ts
|
|
40
|
+
*
|
|
41
|
+
* @Global()
|
|
42
|
+
* @Module({
|
|
43
|
+
* providers: [
|
|
44
|
+
* AuthInterceptor,
|
|
45
|
+
* CustomAuthHandler,
|
|
46
|
+
* {
|
|
47
|
+
* provide: AUTH_HANDLER,
|
|
48
|
+
* useClass: CustomAuthHandler, //swap this to use another provider
|
|
49
|
+
* },
|
|
50
|
+
* ],
|
|
51
|
+
* exports: [AUTH_HANDLER, AuthInterceptor],
|
|
52
|
+
* })
|
|
53
|
+
* export class AuthModule {}
|
|
54
|
+
*/
|
|
55
|
+
export interface AuthHandler {
|
|
56
|
+
/**
|
|
57
|
+
* Inspect the request context and ensure the caller can access the model.
|
|
58
|
+
* Implementations should throw an {@link AuthorizationError} on denial.
|
|
59
|
+
*
|
|
60
|
+
* @param ctx - Nest execution context that exposes the request/response.
|
|
61
|
+
* @param model - Model name or constructor being accessed.
|
|
62
|
+
*/
|
|
63
|
+
authorize(ctx: ExecutionContext, model: string | Constructor): Promise<void> | void;
|
|
64
|
+
}
|
package/lib/index.cjs
CHANGED
|
@@ -53,7 +53,7 @@ __exportStar(require("./request/index.cjs"), exports);
|
|
|
53
53
|
* @constant
|
|
54
54
|
* @type {string}
|
|
55
55
|
*/
|
|
56
|
-
exports.VERSION = "0.1.
|
|
56
|
+
exports.VERSION = "0.1.8";
|
|
57
57
|
exports.PACKAGE_NAME = "@decaf-ts/for-nest";
|
|
58
58
|
decoration_1.Metadata.registerLibrary(exports.PACKAGE_NAME, exports.VERSION);
|
|
59
59
|
//# sourceMappingURL=index.js.map
|
package/lib/index.d.ts
CHANGED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.AuthInterceptor = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const core_1 = require("@nestjs/core");
|
|
18
|
+
const constants_1 = require("./../constants.cjs");
|
|
19
|
+
let AuthInterceptor = class AuthInterceptor {
|
|
20
|
+
constructor(reflector, authHandler) {
|
|
21
|
+
this.reflector = reflector;
|
|
22
|
+
this.authHandler = authHandler;
|
|
23
|
+
}
|
|
24
|
+
async intercept(ctx, next) {
|
|
25
|
+
const modelName = this.reflector.get(constants_1.AUTH_META_KEY, ctx.getHandler()) ??
|
|
26
|
+
this.reflector.get(constants_1.AUTH_META_KEY, ctx.getClass());
|
|
27
|
+
if (modelName && this.authHandler) {
|
|
28
|
+
await this.authHandler.authorize(ctx, modelName);
|
|
29
|
+
}
|
|
30
|
+
return next.handle();
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
exports.AuthInterceptor = AuthInterceptor;
|
|
34
|
+
exports.AuthInterceptor = AuthInterceptor = __decorate([
|
|
35
|
+
(0, common_1.Injectable)(),
|
|
36
|
+
__param(1, (0, common_1.Optional)()),
|
|
37
|
+
__param(1, (0, common_1.Inject)(constants_1.AUTH_HANDLER)),
|
|
38
|
+
__metadata("design:paramtypes", [core_1.Reflector, Object])
|
|
39
|
+
], AuthInterceptor);
|
|
40
|
+
//# sourceMappingURL=AuthInterceptor.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { NestInterceptor, ExecutionContext, CallHandler } from "@nestjs/common";
|
|
2
|
+
import { Reflector } from "@nestjs/core";
|
|
3
|
+
import { Observable } from "rxjs";
|
|
4
|
+
import type { AuthHandler } from "../types";
|
|
5
|
+
export declare class AuthInterceptor implements NestInterceptor {
|
|
6
|
+
private readonly reflector;
|
|
7
|
+
private readonly authHandler?;
|
|
8
|
+
constructor(reflector: Reflector, authHandler?: AuthHandler | undefined);
|
|
9
|
+
intercept(ctx: ExecutionContext, next: CallHandler): Promise<Observable<any>>;
|
|
10
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AuthInterceptor.js","sourceRoot":"","sources":["../../src/interceptors/AuthInterceptor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAOwB;AACxB,uCAAyC;AAIzC,kDAA2D;AAIpD,IAAM,eAAe,GAArB,MAAM,eAAe;IAC1B,YACmB,SAAoB,EACc,WAAyB;QAD3D,cAAS,GAAT,SAAS,CAAW;QACc,gBAAW,GAAX,WAAW,CAAc;IAC3E,CAAC;IAEJ,KAAK,CAAC,SAAS,CACb,GAAqB,EACrB,IAAiB;QAEjB,MAAM,SAAS,GACb,IAAI,CAAC,SAAS,CAAC,GAAG,CAChB,yBAAa,EACb,GAAG,CAAC,UAAU,EAAE,CACjB;YACD,IAAI,CAAC,SAAS,CAAC,GAAG,CAAuB,yBAAa,EAAE,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC;QAE1E,IAAI,SAAS,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;YAClC,MAAM,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;QACnD,CAAC;QAED,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC;CACF,CAAA;AAvBY,0CAAe;0BAAf,eAAe;IAD3B,IAAA,mBAAU,GAAE;IAIR,WAAA,IAAA,iBAAQ,GAAE,CAAA;IAAE,WAAA,IAAA,eAAM,EAAC,wBAAY,CAAC,CAAA;qCADL,gBAAS;GAF5B,eAAe,CAuB3B"}
|
|
@@ -15,4 +15,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./DecafRequestHandlerInterceptor.cjs"), exports);
|
|
18
|
+
__exportStar(require("./AuthInterceptor.cjs"), exports);
|
|
18
19
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/interceptors/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,uEAAiD"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/interceptors/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,uEAAiD;AACjD,wDAAkC"}
|
package/lib/types.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Adapter } from "@decaf-ts/core";
|
|
2
2
|
import { Constructor } from "@decaf-ts/decoration";
|
|
3
|
-
import { Type } from "@nestjs/common";
|
|
3
|
+
import { ExecutionContext, Type } from "@nestjs/common";
|
|
4
4
|
export interface RequestContextAccessor {
|
|
5
5
|
set(key: string | symbol, value: any): void;
|
|
6
6
|
get<T = any>(key: string | symbol): T | undefined;
|
|
@@ -18,3 +18,47 @@ export type DecafModuleOptions<CONF = any, A extends Adapter<CONF, any, any, any
|
|
|
18
18
|
autoControllers: boolean;
|
|
19
19
|
handlers?: Type<DecafRequestHandler>[];
|
|
20
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* Abstraction used by the {@link AuthInterceptor} to authorize decaf models.
|
|
23
|
+
*
|
|
24
|
+
* // costumauthHandler.ts
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* export class CustomAuthHandler implements AuthHandler {
|
|
28
|
+
* async authorize(ctx: ExecutionContext, resource: string) {
|
|
29
|
+
* const req = ctx.switchToHttp().getRequest();
|
|
30
|
+
* const userRole = req.headers.authorization?.split(" ")[1] as string;
|
|
31
|
+
* if (!userRole) throw new AuthorizationError("Unauthenticated");
|
|
32
|
+
* const roles = Metadata.get(Model.get(resource)!, AuthRole);
|
|
33
|
+
* if (!roles.includes(userRole)) {
|
|
34
|
+
* throw new AuthorizationError("Unauthorized");
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
* }
|
|
38
|
+
*
|
|
39
|
+
* // auth.module.ts
|
|
40
|
+
*
|
|
41
|
+
* @Global()
|
|
42
|
+
* @Module({
|
|
43
|
+
* providers: [
|
|
44
|
+
* AuthInterceptor,
|
|
45
|
+
* CustomAuthHandler,
|
|
46
|
+
* {
|
|
47
|
+
* provide: AUTH_HANDLER,
|
|
48
|
+
* useClass: CustomAuthHandler, //swap this to use another provider
|
|
49
|
+
* },
|
|
50
|
+
* ],
|
|
51
|
+
* exports: [AUTH_HANDLER, AuthInterceptor],
|
|
52
|
+
* })
|
|
53
|
+
* export class AuthModule {}
|
|
54
|
+
*/
|
|
55
|
+
export interface AuthHandler {
|
|
56
|
+
/**
|
|
57
|
+
* Inspect the request context and ensure the caller can access the model.
|
|
58
|
+
* Implementations should throw an {@link AuthorizationError} on denial.
|
|
59
|
+
*
|
|
60
|
+
* @param ctx - Nest execution context that exposes the request/response.
|
|
61
|
+
* @param model - Model name or constructor being accessed.
|
|
62
|
+
*/
|
|
63
|
+
authorize(ctx: ExecutionContext, model: string | Constructor): Promise<void> | void;
|
|
64
|
+
}
|