@telperion/nest-pack 1.3.0 → 1.4.1
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/.build-manifest.json +1 -1
- package/README.md +94 -7
- package/access/access-guard-options.d.cts +40 -7
- package/access/access-guard-options.d.ts +40 -7
- package/access/create-access-guard.cjs +31 -18
- package/access/create-access-guard.cjs.map +1 -1
- package/access/create-access-guard.d.cts +7 -5
- package/access/create-access-guard.d.ts +7 -5
- package/access/create-access-guard.js +32 -19
- package/access/create-access-guard.js.map +1 -1
- package/access/index.d.cts +1 -1
- package/access/index.d.ts +1 -1
- package/auth/create-jwt-auth-guard.cjs +48 -22
- package/auth/create-jwt-auth-guard.cjs.map +1 -1
- package/auth/create-jwt-auth-guard.d.cts +8 -5
- package/auth/create-jwt-auth-guard.d.ts +8 -5
- package/auth/create-jwt-auth-guard.js +49 -23
- package/auth/create-jwt-auth-guard.js.map +1 -1
- package/auth/index.d.cts +1 -1
- package/auth/index.d.ts +1 -1
- package/auth/jwt-auth-guard-options.d.cts +66 -9
- package/auth/jwt-auth-guard-options.d.ts +66 -9
- package/fesm/nest-pack.mjs +32 -19
- package/fesm/nest-pack.mjs.map +1 -1
- package/index.d.cts +1 -1
- package/index.d.ts +1 -1
- package/package.json +1 -1
package/fesm/nest-pack.mjs
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { __decorate, __metadata } from 'tslib';
|
|
2
|
-
import { createParamDecorator, applyDecorators, SetMetadata, UseGuards,
|
|
2
|
+
import { createParamDecorator, applyDecorators, SetMetadata, UseGuards, ForbiddenException, NotFoundException, Injectable, Inject } from '@nestjs/common';
|
|
3
3
|
import { Reflector } from '@nestjs/core';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* #### Create Access Guard
|
|
7
7
|
*
|
|
8
8
|
* Creates a guard for routes about one entity, e.g. a project in `/projects/:projectId/...`.
|
|
9
|
-
* The guard resolves the caller's access to the entity in a route parameter (or a body field or query parameter, see `from
|
|
10
|
-
* and checks the route's requirements:
|
|
9
|
+
* The guard resolves the caller's access to the entity in a route parameter (or a body field or query parameter, see `from`;
|
|
10
|
+
* or a fixed or computed `id`) and checks the route's requirements:
|
|
11
11
|
*
|
|
12
|
-
* - missing or malformed
|
|
13
|
-
*
|
|
12
|
+
* - missing or malformed id (see `validate`): **404**
|
|
13
|
+
* - no access (`resolve` gives `null`/`undefined`): **404**, so callers can't probe which ids exist
|
|
14
|
+
* (or **403** with `noAccess: 'forbidden'`)
|
|
14
15
|
* - access that does not meet every requirement of the route (see `allows`): **403**
|
|
15
16
|
* - otherwise the request passes, and the resolved access is kept on the request for `@Guard.access()`
|
|
16
17
|
*
|
|
@@ -44,15 +45,21 @@ import { Reflector } from '@nestjs/core';
|
|
|
44
45
|
* To give the guard a class name of its own (in stack traces, or to inject it), extend it:
|
|
45
46
|
* `@Injectable() export class ProjectAccessGuard extends createAccessGuard({ ... }) {}`.
|
|
46
47
|
* * * *
|
|
47
|
-
* @param options Which parameter to read, how to resolve the access, and how to check a requirement
|
|
48
|
+
* @param options Which parameter to read (or the `id`), how to resolve the access, and how to check a requirement
|
|
48
49
|
* @returns An injectable guard class with `require()`, `access()` and `accessOf()`
|
|
50
|
+
* @throws When neither or both of `param` and `id` are given
|
|
49
51
|
*/
|
|
50
52
|
function createAccessGuard(options) {
|
|
51
53
|
var AccessGuard_1;
|
|
52
|
-
const { param, from = 'params', validate = () => true, inject = [], resolve, allows, notFound = 'Not Found', forbidden = 'Forbidden' } = options;
|
|
54
|
+
const { param, from = 'params', id, validate = () => true, inject = [], resolve, allows, notFound = 'Not Found', forbidden = 'Forbidden', noAccess = 'not-found', } = options;
|
|
55
|
+
if ((param === undefined) === (id === undefined)) {
|
|
56
|
+
throw new Error('createAccessGuard: give exactly one of `param` (a request parameter) and `id` (a fixed or computed id)');
|
|
57
|
+
}
|
|
53
58
|
const sources = typeof from === 'string' ? [from] : from;
|
|
54
|
-
|
|
55
|
-
const
|
|
59
|
+
/** Names the id in metadata keys and errors */
|
|
60
|
+
const label = param !== undefined ? `:${param}` : typeof id === 'string' ? id : 'the computed id';
|
|
61
|
+
const requirementsKey = Symbol(`access-guard:${label}:requirements`);
|
|
62
|
+
const accessKey = Symbol(`access-guard:${label}:access`);
|
|
56
63
|
let AccessGuard = class AccessGuard {
|
|
57
64
|
static { AccessGuard_1 = this; }
|
|
58
65
|
static access = createParamDecorator((_data, context) => AccessGuard_1.accessOf(context.switchToHttp().getRequest()) ?? AccessGuard_1.notGuarded());
|
|
@@ -73,25 +80,31 @@ function createAccessGuard(options) {
|
|
|
73
80
|
return request[accessKey];
|
|
74
81
|
}
|
|
75
82
|
/**
|
|
76
|
-
* The parameter in the first source holding a string value
|
|
83
|
+
* The fixed or computed `id`, or else the parameter in the first source holding a string value
|
|
77
84
|
*/
|
|
78
85
|
static idOf(request) {
|
|
79
|
-
return
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
86
|
+
return typeof id === 'function'
|
|
87
|
+
? id(request)
|
|
88
|
+
: (id ??
|
|
89
|
+
sources
|
|
90
|
+
.map(source => request[source])
|
|
91
|
+
.map(holder => (typeof holder === 'object' && holder !== null ? holder[param] : undefined))
|
|
92
|
+
.find((value) => typeof value === 'string'));
|
|
93
|
+
}
|
|
94
|
+
/** The answer when `resolve` finds no access */
|
|
95
|
+
static noAccess() {
|
|
96
|
+
throw noAccess === 'forbidden' ? new ForbiddenException(forbidden) : new NotFoundException(notFound);
|
|
83
97
|
}
|
|
84
98
|
static notGuarded() {
|
|
85
|
-
throw new Error(`The access of
|
|
99
|
+
throw new Error(`The access of ${label} is read on a route without its access guard; add \`.require()\` to the route or controller`);
|
|
86
100
|
}
|
|
87
101
|
canActivate(context) {
|
|
88
102
|
const request = context.switchToHttp().getRequest();
|
|
89
103
|
const value = AccessGuard_1.idOf(request);
|
|
90
104
|
const required = this.reflector.getAllAndOverride(requirementsKey, [context.getHandler(), context.getClass()]) ?? [];
|
|
91
|
-
return
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
105
|
+
return (typeof value === 'string' && validate(value)
|
|
106
|
+
? Promise.resolve(resolve(value, request, ...this.dependencies)).then(access => access ?? AccessGuard_1.noAccess())
|
|
107
|
+
: Promise.reject(new NotFoundException(notFound))).then(access => {
|
|
95
108
|
if (!required.every(requirement => allows(access, requirement))) {
|
|
96
109
|
throw new ForbiddenException(forbidden);
|
|
97
110
|
}
|
package/fesm/nest-pack.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nest-pack.mjs","sources":["../../../../libs/nest-pack/src/access/create-access-guard.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;AAkBA
|
|
1
|
+
{"version":3,"file":"nest-pack.mjs","sources":["../../../../libs/nest-pack/src/access/create-access-guard.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;AAkBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8CG;AACG,SAAU,iBAAiB,CAK/B,OAAiE,EAAA;;AACjE,IAAA,MAAM,EACJ,KAAK,EACL,IAAI,GAAG,QAAQ,EACf,EAAE,EACF,QAAQ,GAAG,MAAM,IAAI,EACrB,MAAM,GAAG,EAAE,EACX,OAAO,EACP,MAAM,EACN,QAAQ,GAAG,WAAW,EACtB,SAAS,GAAG,WAAW,EACvB,QAAQ,GAAG,WAAW,GACvB,GAAG,OAAO;AAEX,IAAA,IAAI,CAAC,KAAK,KAAK,SAAS,OAAO,EAAE,KAAK,SAAS,CAAC,EAAE;AAChD,QAAA,MAAM,IAAI,KAAK,CAAC,wGAAwG,CAAC;IAC3H;AAEA,IAAA,MAAM,OAAO,GAAiC,OAAO,IAAI,KAAK,QAAQ,GAAG,CAAC,IAAI,CAAC,GAAG,IAAI;;IAEtF,MAAM,KAAK,GAAG,KAAK,KAAK,SAAS,GAAG,CAAA,CAAA,EAAI,KAAK,CAAA,CAAE,GAAG,OAAO,EAAE,KAAK,QAAQ,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AAE,EAAE,CAAC,SAAS,GAAG,MAAM,EAAE,CAAC;AAE/C,gBAAA,OAAO,IAAI;AACb,YAAA,CAAC,CAAC;QACJ;;IAnEI,WAAW,GAAA,aAAA,GAAA,UAAA,CAAA;AADhB,QAAA,UAAU,EAAE;;AACP,KAAA,EAAA,WAAW,CAoEhB;IAED,CAAC,SAAS,EAAE,GAAG,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,KAAK,KAAK,MAAM,CAAC,KAAK,CAAC,CAAC,WAAW,EAAE,SAAS,EAAE,KAAK,CAAC,CAAC;AAE9F,IAAA,OAAO,WAAW;AACpB;;;;"}
|
package/index.d.cts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { AccessGuardOptions, AccessGuardRequest, AccessGuardSource, AccessGuardType, InjectedDependencies } from './access/access-guard-options.cjs';
|
|
1
|
+
export { AccessGuardBaseOptions, AccessGuardIdOptions, AccessGuardNoAccess, AccessGuardOptions, AccessGuardParamOptions, AccessGuardRequest, AccessGuardSource, AccessGuardType, InjectedDependencies } from './access/access-guard-options.cjs';
|
|
2
2
|
export { createAccessGuard } from './access/create-access-guard.cjs';
|
|
3
3
|
import '@nestjs/common';
|
package/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { AccessGuardOptions, AccessGuardRequest, AccessGuardSource, AccessGuardType, InjectedDependencies } from './access/access-guard-options.js';
|
|
1
|
+
export { AccessGuardBaseOptions, AccessGuardIdOptions, AccessGuardNoAccess, AccessGuardOptions, AccessGuardParamOptions, AccessGuardRequest, AccessGuardSource, AccessGuardType, InjectedDependencies } from './access/access-guard-options.js';
|
|
2
2
|
export { createAccessGuard } from './access/create-access-guard.js';
|
|
3
3
|
import '@nestjs/common';
|