@telperion/nest-pack 1.0.0 → 1.2.0
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 +61 -1
- package/README.md +124 -3
- package/access/access-guard-options.d.cts +19 -4
- package/access/access-guard-options.d.ts +19 -4
- package/access/create-access-guard.cjs +15 -4
- package/access/create-access-guard.cjs.map +1 -1
- package/access/create-access-guard.d.cts +3 -2
- package/access/create-access-guard.d.ts +3 -2
- package/access/create-access-guard.js +15 -4
- 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 +104 -0
- package/auth/create-jwt-auth-guard.cjs.map +1 -0
- package/auth/create-jwt-auth-guard.d.cts +52 -0
- package/auth/create-jwt-auth-guard.d.ts +52 -0
- package/auth/create-jwt-auth-guard.js +102 -0
- package/auth/create-jwt-auth-guard.js.map +1 -0
- package/auth/index.cjs +8 -0
- package/auth/index.cjs.map +1 -0
- package/auth/index.d.cts +4 -0
- package/auth/index.d.ts +4 -0
- package/auth/index.js +2 -0
- package/auth/index.js.map +1 -0
- package/auth/jwt-auth-guard-options.cjs +3 -0
- package/auth/jwt-auth-guard-options.cjs.map +1 -0
- package/auth/jwt-auth-guard-options.d.cts +62 -0
- package/auth/jwt-auth-guard-options.d.ts +62 -0
- package/auth/jwt-auth-guard-options.js +2 -0
- package/auth/jwt-auth-guard-options.js.map +1 -0
- package/fesm/nest-pack.mjs +15 -4
- package/fesm/nest-pack.mjs.map +1 -1
- package/index.d.cts +1 -1
- package/index.d.ts +1 -1
- package/package.json +96 -4
- package/pagination/index.cjs +10 -0
- package/pagination/index.cjs.map +1 -0
- package/pagination/index.d.cts +2 -0
- package/pagination/index.d.ts +2 -0
- package/pagination/index.js +3 -0
- package/pagination/index.js.map +1 -0
- package/pagination/page-query.cjs +92 -0
- package/pagination/page-query.cjs.map +1 -0
- package/pagination/page-query.d.cts +63 -0
- package/pagination/page-query.d.ts +63 -0
- package/pagination/page-query.js +90 -0
- package/pagination/page-query.js.map +1 -0
- package/pagination/page.cjs +48 -0
- package/pagination/page.cjs.map +1 -0
- package/pagination/page.d.cts +40 -0
- package/pagination/page.d.ts +40 -0
- package/pagination/page.js +46 -0
- package/pagination/page.js.map +1 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { JwtAuthGuardRequest, JwtAuthGuardOptions, JwtAuthGuardType } from './jwt-auth-guard-options.cjs';
|
|
2
|
+
import '@nestjs/common';
|
|
3
|
+
import '@nestjs/jwt';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* #### Create JWT Auth Guard
|
|
7
|
+
*
|
|
8
|
+
* Creates a guard that requires a valid JWT on every route, for use as the global `APP_GUARD`:
|
|
9
|
+
*
|
|
10
|
+
* - routes and controllers marked with `@Guard.public()` pass without a token
|
|
11
|
+
* - a missing, malformed or invalid token (bad signature, wrong audience, …): **401** `unauthorizedMessage`
|
|
12
|
+
* - an expired token: **401** `expiredMessage` (`'TokenExpired'`), so clients can tell "renew and retry" from "sign in"
|
|
13
|
+
* - otherwise the payload is written to `request.user` (see `property`) and is available through `@Guard.user()`
|
|
14
|
+
*
|
|
15
|
+
* Tokens are verified with the `JwtService` of `@nestjs/jwt`: register `JwtModule` (with the secret or keys) in
|
|
16
|
+
* the module that provides the guard. The token comes from the `Authorization: Bearer` header unless `extract`
|
|
17
|
+
* reads it from elsewhere. HTTP only.
|
|
18
|
+
*
|
|
19
|
+
* * * *
|
|
20
|
+
* Example:
|
|
21
|
+
* ```typescript
|
|
22
|
+
* import { createJwtAuthGuard } from '@telperion/nest-pack/auth';
|
|
23
|
+
* import { APP_GUARD } from '@nestjs/core';
|
|
24
|
+
* import { JwtModule } from '@nestjs/jwt';
|
|
25
|
+
*
|
|
26
|
+
* export const AuthGuard = createJwtAuthGuard<TokenPayload>();
|
|
27
|
+
*
|
|
28
|
+
* @Module({
|
|
29
|
+
* imports: [JwtModule.register({ secret: process.env.JWT_SECRET })],
|
|
30
|
+
* providers: [{ provide: APP_GUARD, useClass: AuthGuard }],
|
|
31
|
+
* })
|
|
32
|
+
* export class AppModule {}
|
|
33
|
+
*
|
|
34
|
+
* @Controller('huddles')
|
|
35
|
+
* export class HuddlesController {
|
|
36
|
+
* @Get()
|
|
37
|
+
* list(@AuthGuard.user() user: TokenPayload) {}
|
|
38
|
+
*
|
|
39
|
+
* @Get('health')
|
|
40
|
+
* @AuthGuard.public()
|
|
41
|
+
* health() {}
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* To give the guard a class name of its own, extend it: `@Injectable() export class AuthGuard extends createJwtAuthGuard<TokenPayload>() {}`.
|
|
46
|
+
* * * *
|
|
47
|
+
* @param options Messages, where the token comes from, verification options and where the payload goes
|
|
48
|
+
* @returns An injectable guard class with `public()`, `user()` and `userOf()`
|
|
49
|
+
*/
|
|
50
|
+
declare function createJwtAuthGuard<Payload extends object, Request extends object = JwtAuthGuardRequest>(options?: JwtAuthGuardOptions<Request>): JwtAuthGuardType<Payload>;
|
|
51
|
+
|
|
52
|
+
export { createJwtAuthGuard };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { JwtAuthGuardRequest, JwtAuthGuardOptions, JwtAuthGuardType } from './jwt-auth-guard-options.js';
|
|
2
|
+
import '@nestjs/common';
|
|
3
|
+
import '@nestjs/jwt';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* #### Create JWT Auth Guard
|
|
7
|
+
*
|
|
8
|
+
* Creates a guard that requires a valid JWT on every route, for use as the global `APP_GUARD`:
|
|
9
|
+
*
|
|
10
|
+
* - routes and controllers marked with `@Guard.public()` pass without a token
|
|
11
|
+
* - a missing, malformed or invalid token (bad signature, wrong audience, …): **401** `unauthorizedMessage`
|
|
12
|
+
* - an expired token: **401** `expiredMessage` (`'TokenExpired'`), so clients can tell "renew and retry" from "sign in"
|
|
13
|
+
* - otherwise the payload is written to `request.user` (see `property`) and is available through `@Guard.user()`
|
|
14
|
+
*
|
|
15
|
+
* Tokens are verified with the `JwtService` of `@nestjs/jwt`: register `JwtModule` (with the secret or keys) in
|
|
16
|
+
* the module that provides the guard. The token comes from the `Authorization: Bearer` header unless `extract`
|
|
17
|
+
* reads it from elsewhere. HTTP only.
|
|
18
|
+
*
|
|
19
|
+
* * * *
|
|
20
|
+
* Example:
|
|
21
|
+
* ```typescript
|
|
22
|
+
* import { createJwtAuthGuard } from '@telperion/nest-pack/auth';
|
|
23
|
+
* import { APP_GUARD } from '@nestjs/core';
|
|
24
|
+
* import { JwtModule } from '@nestjs/jwt';
|
|
25
|
+
*
|
|
26
|
+
* export const AuthGuard = createJwtAuthGuard<TokenPayload>();
|
|
27
|
+
*
|
|
28
|
+
* @Module({
|
|
29
|
+
* imports: [JwtModule.register({ secret: process.env.JWT_SECRET })],
|
|
30
|
+
* providers: [{ provide: APP_GUARD, useClass: AuthGuard }],
|
|
31
|
+
* })
|
|
32
|
+
* export class AppModule {}
|
|
33
|
+
*
|
|
34
|
+
* @Controller('huddles')
|
|
35
|
+
* export class HuddlesController {
|
|
36
|
+
* @Get()
|
|
37
|
+
* list(@AuthGuard.user() user: TokenPayload) {}
|
|
38
|
+
*
|
|
39
|
+
* @Get('health')
|
|
40
|
+
* @AuthGuard.public()
|
|
41
|
+
* health() {}
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* To give the guard a class name of its own, extend it: `@Injectable() export class AuthGuard extends createJwtAuthGuard<TokenPayload>() {}`.
|
|
46
|
+
* * * *
|
|
47
|
+
* @param options Messages, where the token comes from, verification options and where the payload goes
|
|
48
|
+
* @returns An injectable guard class with `public()`, `user()` and `userOf()`
|
|
49
|
+
*/
|
|
50
|
+
declare function createJwtAuthGuard<Payload extends object, Request extends object = JwtAuthGuardRequest>(options?: JwtAuthGuardOptions<Request>): JwtAuthGuardType<Payload>;
|
|
51
|
+
|
|
52
|
+
export { createJwtAuthGuard };
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { __decorate, __metadata } from 'tslib';
|
|
2
|
+
import { createParamDecorator, SetMetadata, UnauthorizedException, Injectable, Inject } from '@nestjs/common';
|
|
3
|
+
import { Reflector } from '@nestjs/core';
|
|
4
|
+
import { TokenExpiredError, JwtService } from '@nestjs/jwt';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* #### Create JWT Auth Guard
|
|
8
|
+
*
|
|
9
|
+
* Creates a guard that requires a valid JWT on every route, for use as the global `APP_GUARD`:
|
|
10
|
+
*
|
|
11
|
+
* - routes and controllers marked with `@Guard.public()` pass without a token
|
|
12
|
+
* - a missing, malformed or invalid token (bad signature, wrong audience, …): **401** `unauthorizedMessage`
|
|
13
|
+
* - an expired token: **401** `expiredMessage` (`'TokenExpired'`), so clients can tell "renew and retry" from "sign in"
|
|
14
|
+
* - otherwise the payload is written to `request.user` (see `property`) and is available through `@Guard.user()`
|
|
15
|
+
*
|
|
16
|
+
* Tokens are verified with the `JwtService` of `@nestjs/jwt`: register `JwtModule` (with the secret or keys) in
|
|
17
|
+
* the module that provides the guard. The token comes from the `Authorization: Bearer` header unless `extract`
|
|
18
|
+
* reads it from elsewhere. HTTP only.
|
|
19
|
+
*
|
|
20
|
+
* * * *
|
|
21
|
+
* Example:
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import { createJwtAuthGuard } from '@telperion/nest-pack/auth';
|
|
24
|
+
* import { APP_GUARD } from '@nestjs/core';
|
|
25
|
+
* import { JwtModule } from '@nestjs/jwt';
|
|
26
|
+
*
|
|
27
|
+
* export const AuthGuard = createJwtAuthGuard<TokenPayload>();
|
|
28
|
+
*
|
|
29
|
+
* @Module({
|
|
30
|
+
* imports: [JwtModule.register({ secret: process.env.JWT_SECRET })],
|
|
31
|
+
* providers: [{ provide: APP_GUARD, useClass: AuthGuard }],
|
|
32
|
+
* })
|
|
33
|
+
* export class AppModule {}
|
|
34
|
+
*
|
|
35
|
+
* @Controller('huddles')
|
|
36
|
+
* export class HuddlesController {
|
|
37
|
+
* @Get()
|
|
38
|
+
* list(@AuthGuard.user() user: TokenPayload) {}
|
|
39
|
+
*
|
|
40
|
+
* @Get('health')
|
|
41
|
+
* @AuthGuard.public()
|
|
42
|
+
* health() {}
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* To give the guard a class name of its own, extend it: `@Injectable() export class AuthGuard extends createJwtAuthGuard<TokenPayload>() {}`.
|
|
47
|
+
* * * *
|
|
48
|
+
* @param options Messages, where the token comes from, verification options and where the payload goes
|
|
49
|
+
* @returns An injectable guard class with `public()`, `user()` and `userOf()`
|
|
50
|
+
*/
|
|
51
|
+
function createJwtAuthGuard(options = {}) {
|
|
52
|
+
var JwtAuthGuard_1;
|
|
53
|
+
const { expiredMessage = 'TokenExpired', unauthorizedMessage = 'Unauthorized', extract, verifyOptions, property = 'user', } = options;
|
|
54
|
+
const publicKey = Symbol('jwt-auth-guard:public');
|
|
55
|
+
let JwtAuthGuard = class JwtAuthGuard {
|
|
56
|
+
static { JwtAuthGuard_1 = this; }
|
|
57
|
+
reflector;
|
|
58
|
+
jwt;
|
|
59
|
+
static user = createParamDecorator((_data, context) => JwtAuthGuard_1.userOf(context.switchToHttp().getRequest()));
|
|
60
|
+
/**
|
|
61
|
+
* @param reflector Reads `public()` markers
|
|
62
|
+
* @param jwt Verifies tokens
|
|
63
|
+
*/
|
|
64
|
+
constructor(reflector, jwt) {
|
|
65
|
+
this.reflector = reflector;
|
|
66
|
+
this.jwt = jwt;
|
|
67
|
+
}
|
|
68
|
+
static public() {
|
|
69
|
+
return SetMetadata(publicKey, true);
|
|
70
|
+
}
|
|
71
|
+
static userOf(request) {
|
|
72
|
+
return request[property];
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The token of an `Authorization: Bearer <token>` header (the scheme is case-insensitive)
|
|
76
|
+
*/
|
|
77
|
+
static bearerToken({ headers }) {
|
|
78
|
+
const header = headers['authorization'];
|
|
79
|
+
return (typeof header === 'string' ? /^Bearer +(\S+)$/i.exec(header) : null)?.[1];
|
|
80
|
+
}
|
|
81
|
+
canActivate(context) {
|
|
82
|
+
if (this.reflector.getAllAndOverride(publicKey, [context.getHandler(), context.getClass()])) {
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
const request = context.switchToHttp().getRequest();
|
|
86
|
+
const token = extract ? extract(request) : JwtAuthGuard_1.bearerToken(request);
|
|
87
|
+
return token
|
|
88
|
+
? this.jwt.verifyAsync(token, verifyOptions).then(payload => (Object.assign(request, { [property]: payload }), true), (error) => Promise.reject(new UnauthorizedException(error instanceof TokenExpiredError ? expiredMessage : unauthorizedMessage)))
|
|
89
|
+
: Promise.reject(new UnauthorizedException(unauthorizedMessage));
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
JwtAuthGuard = JwtAuthGuard_1 = __decorate([
|
|
93
|
+
Injectable(),
|
|
94
|
+
__metadata("design:paramtypes", [Reflector,
|
|
95
|
+
JwtService])
|
|
96
|
+
], JwtAuthGuard);
|
|
97
|
+
[Reflector, JwtService].forEach((token, index) => Inject(token)(JwtAuthGuard, undefined, index));
|
|
98
|
+
return JwtAuthGuard;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export { createJwtAuthGuard };
|
|
102
|
+
//# sourceMappingURL=create-jwt-auth-guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-jwt-auth-guard.js","sources":["../../../../libs/nest-pack/src/auth/create-jwt-auth-guard.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;;AAcA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CG;AACG,SAAU,kBAAkB,CAChC,OAAA,GAAwC,EAAE,EAAA;;AAE1C,IAAA,MAAM,EACJ,cAAc,GAAG,cAAc,EAC/B,mBAAmB,GAAG,cAAc,EACpC,OAAO,EACP,aAAa,EACb,QAAQ,GAAG,MAAM,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OAAyC,CAAC;AAE9G,YAAA,OAAO;AACL,kBAAE,IAAI,CAAC,GAAG,CAAC,WAAW,CAAU,KAAK,EAAE,aAAa,CAAC,CAAC,IAAI,CACtD,OAAO,KAAK,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,EAAE,CAAC,QAAQ,GAAG,OAAO,EAAE,CAAC,EAAE,IAAI,CAAC,EAClE,CAAC,KAAc,KAAK,OAAO,CAAC,MAAM,CAAC,IAAI,qBAAqB,CAAC,KAAK,YAAY,iBAAiB,GAAG,cAAc,GAAG,mBAAmB,CAAC,CAAC;kBAE1I,OAAO,CAAC,MAAM,CAAC,IAAI,qBAAqB,CAAC,mBAAmB,CAAC,CAAC;QACpE;;IA7CI,YAAY,GAAA,cAAA,GAAA,UAAA,CAAA;AADjB,QAAA,UAAU,EAAE;yCAWmB,SAAS;YACf,UAAU,CAAA;AAX9B,KAAA,EAAA,YAAY,CA8CjB;IAED,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,KAAK,KAAK,MAAM,CAAC,KAAK,CAAC,CAAC,YAAY,EAAE,SAAS,EAAE,KAAK,CAAC,CAAC;AAEhG,IAAA,OAAO,YAAY;AACrB;;;;"}
|
package/auth/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;"}
|
package/auth/index.d.cts
ADDED
package/auth/index.d.ts
ADDED
package/auth/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jwt-auth-guard-options.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Type, CanActivate } from '@nestjs/common';
|
|
2
|
+
import { JwtVerifyOptions } from '@nestjs/jwt';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What a {@link createJwtAuthGuard} reads from a request
|
|
6
|
+
*/
|
|
7
|
+
interface JwtAuthGuardRequest {
|
|
8
|
+
headers: Record<string, string | string[] | undefined>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Options of {@link createJwtAuthGuard}
|
|
12
|
+
*/
|
|
13
|
+
interface JwtAuthGuardOptions<Request extends object = JwtAuthGuardRequest> {
|
|
14
|
+
/**
|
|
15
|
+
* The message of the 401 answer for an expired token, so clients can renew the token and retry
|
|
16
|
+
* (e.g. `ApiAuth.expired` of `@telperion/ng-pack/http`)
|
|
17
|
+
*
|
|
18
|
+
* @default 'TokenExpired'
|
|
19
|
+
*/
|
|
20
|
+
expiredMessage?: string;
|
|
21
|
+
/**
|
|
22
|
+
* The message of the 401 answer for a missing or invalid token
|
|
23
|
+
*
|
|
24
|
+
* @default 'Unauthorized'
|
|
25
|
+
*/
|
|
26
|
+
unauthorizedMessage?: string;
|
|
27
|
+
/**
|
|
28
|
+
* Reads the token from a request
|
|
29
|
+
*
|
|
30
|
+
* @default the token of an `Authorization: Bearer <token>` header
|
|
31
|
+
*/
|
|
32
|
+
extract?: (request: Request) => string | null | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Options of `JwtService.verifyAsync`, e.g. the `audience`, `issuer` or a `secret` other than the `JwtModule`'s
|
|
35
|
+
*/
|
|
36
|
+
verifyOptions?: JwtVerifyOptions;
|
|
37
|
+
/**
|
|
38
|
+
* The request property the verified payload is written to
|
|
39
|
+
*
|
|
40
|
+
* @default 'user'
|
|
41
|
+
*/
|
|
42
|
+
property?: string;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The guard class {@link createJwtAuthGuard} returns, with its decorators
|
|
46
|
+
*/
|
|
47
|
+
interface JwtAuthGuardType<Payload extends object> extends Type<CanActivate> {
|
|
48
|
+
/**
|
|
49
|
+
* Route or controller decorator: the route needs no token. A method's setting replaces its controller's.
|
|
50
|
+
*/
|
|
51
|
+
public(): MethodDecorator & ClassDecorator;
|
|
52
|
+
/**
|
|
53
|
+
* Parameter decorator: the verified token payload, `undefined` on public routes
|
|
54
|
+
*/
|
|
55
|
+
user(...pipes: unknown[]): ParameterDecorator;
|
|
56
|
+
/**
|
|
57
|
+
* The verified payload of a request, `undefined` before the guard ran or on public routes
|
|
58
|
+
*/
|
|
59
|
+
userOf(request: object): Payload | undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export type { JwtAuthGuardOptions, JwtAuthGuardRequest, JwtAuthGuardType };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Type, CanActivate } from '@nestjs/common';
|
|
2
|
+
import { JwtVerifyOptions } from '@nestjs/jwt';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What a {@link createJwtAuthGuard} reads from a request
|
|
6
|
+
*/
|
|
7
|
+
interface JwtAuthGuardRequest {
|
|
8
|
+
headers: Record<string, string | string[] | undefined>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Options of {@link createJwtAuthGuard}
|
|
12
|
+
*/
|
|
13
|
+
interface JwtAuthGuardOptions<Request extends object = JwtAuthGuardRequest> {
|
|
14
|
+
/**
|
|
15
|
+
* The message of the 401 answer for an expired token, so clients can renew the token and retry
|
|
16
|
+
* (e.g. `ApiAuth.expired` of `@telperion/ng-pack/http`)
|
|
17
|
+
*
|
|
18
|
+
* @default 'TokenExpired'
|
|
19
|
+
*/
|
|
20
|
+
expiredMessage?: string;
|
|
21
|
+
/**
|
|
22
|
+
* The message of the 401 answer for a missing or invalid token
|
|
23
|
+
*
|
|
24
|
+
* @default 'Unauthorized'
|
|
25
|
+
*/
|
|
26
|
+
unauthorizedMessage?: string;
|
|
27
|
+
/**
|
|
28
|
+
* Reads the token from a request
|
|
29
|
+
*
|
|
30
|
+
* @default the token of an `Authorization: Bearer <token>` header
|
|
31
|
+
*/
|
|
32
|
+
extract?: (request: Request) => string | null | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Options of `JwtService.verifyAsync`, e.g. the `audience`, `issuer` or a `secret` other than the `JwtModule`'s
|
|
35
|
+
*/
|
|
36
|
+
verifyOptions?: JwtVerifyOptions;
|
|
37
|
+
/**
|
|
38
|
+
* The request property the verified payload is written to
|
|
39
|
+
*
|
|
40
|
+
* @default 'user'
|
|
41
|
+
*/
|
|
42
|
+
property?: string;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The guard class {@link createJwtAuthGuard} returns, with its decorators
|
|
46
|
+
*/
|
|
47
|
+
interface JwtAuthGuardType<Payload extends object> extends Type<CanActivate> {
|
|
48
|
+
/**
|
|
49
|
+
* Route or controller decorator: the route needs no token. A method's setting replaces its controller's.
|
|
50
|
+
*/
|
|
51
|
+
public(): MethodDecorator & ClassDecorator;
|
|
52
|
+
/**
|
|
53
|
+
* Parameter decorator: the verified token payload, `undefined` on public routes
|
|
54
|
+
*/
|
|
55
|
+
user(...pipes: unknown[]): ParameterDecorator;
|
|
56
|
+
/**
|
|
57
|
+
* The verified payload of a request, `undefined` before the guard ran or on public routes
|
|
58
|
+
*/
|
|
59
|
+
userOf(request: object): Payload | undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export type { JwtAuthGuardOptions, JwtAuthGuardRequest, JwtAuthGuardType };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jwt-auth-guard-options.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|
package/fesm/nest-pack.mjs
CHANGED
|
@@ -6,9 +6,10 @@ import { Reflector } from '@nestjs/core';
|
|
|
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
|
|
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:
|
|
10
11
|
*
|
|
11
|
-
* - malformed parameter (see `validate`), or no access (`resolve` gives `null`/`undefined`): **404**,
|
|
12
|
+
* - missing or malformed parameter (see `validate`), or no access (`resolve` gives `null`/`undefined`): **404**,
|
|
12
13
|
* so callers can't probe which ids exist
|
|
13
14
|
* - access that does not meet every requirement of the route (see `allows`): **403**
|
|
14
15
|
* - otherwise the request passes, and the resolved access is kept on the request for `@Guard.access()`
|
|
@@ -48,7 +49,8 @@ import { Reflector } from '@nestjs/core';
|
|
|
48
49
|
*/
|
|
49
50
|
function createAccessGuard(options) {
|
|
50
51
|
var AccessGuard_1;
|
|
51
|
-
const { param, validate = () => true, inject = [], resolve, allows, notFound = 'Not Found', forbidden = 'Forbidden' } = options;
|
|
52
|
+
const { param, from = 'params', validate = () => true, inject = [], resolve, allows, notFound = 'Not Found', forbidden = 'Forbidden' } = options;
|
|
53
|
+
const sources = typeof from === 'string' ? [from] : from;
|
|
52
54
|
const requirementsKey = Symbol(`access-guard:${param}:requirements`);
|
|
53
55
|
const accessKey = Symbol(`access-guard:${param}:access`);
|
|
54
56
|
let AccessGuard = class AccessGuard {
|
|
@@ -70,12 +72,21 @@ function createAccessGuard(options) {
|
|
|
70
72
|
static accessOf(request) {
|
|
71
73
|
return request[accessKey];
|
|
72
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* The parameter in the first source holding a string value
|
|
77
|
+
*/
|
|
78
|
+
static idOf(request) {
|
|
79
|
+
return sources
|
|
80
|
+
.map(source => request[source])
|
|
81
|
+
.map(holder => (typeof holder === 'object' && holder !== null ? holder[param] : undefined))
|
|
82
|
+
.find((value) => typeof value === 'string');
|
|
83
|
+
}
|
|
73
84
|
static notGuarded() {
|
|
74
85
|
throw new Error(`The access of :${param} is read on a route without its access guard; add \`.require()\` to the route or controller`);
|
|
75
86
|
}
|
|
76
87
|
canActivate(context) {
|
|
77
88
|
const request = context.switchToHttp().getRequest();
|
|
78
|
-
const value = request
|
|
89
|
+
const value = AccessGuard_1.idOf(request);
|
|
79
90
|
const required = this.reflector.getAllAndOverride(requirementsKey, [context.getHandler(), context.getClass()]) ?? [];
|
|
80
91
|
return Promise.resolve(typeof value === 'string' && validate(value) ? resolve(value, request, ...this.dependencies) : null).then(access => {
|
|
81
92
|
if (access === null || access === undefined) {
|
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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CG;AACG,SAAU,iBAAiB,CAK/B,OAAiE,EAAA;;AACjE,IAAA,MAAM,EAAE,KAAK,EAAE,IAAI,GAAG,QAAQ,EAAE,QAAQ,GAAG,MAAM,IAAI,EAAE,MAAM,GAAG,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,GAAG,WAAW,EAAE,SAAS,GAAG,WAAW,EAAE,GAAG,OAAO;AAChJ,IAAA,MAAM,OAAO,GAAiC,OAAO,IAAI,KAAK,QAAQ,GAAG,CAAC,IAAI,CAAC,GAAG,IAAI;IACtF,MAAM,eAAe,GAAG,MAAM,CAAC,gBAAgB,KAAK,CAAA,aAAA,CAAe,CAAC;IACpE,MAAM,SAAS,GAAG,MAAM,CAAC,gBAAgB,KAAK,CAAA,OAAA,CAAS,CAAC;IAExD,IACM,WAAW,GADjB,MACM,WAAW,CAAA;;AACf,QAAA,OAAgB,MAAM,GAAG,oBAAoB,CAC3C,CAAC,KAAc,EAAE,OAAyB,KACxC,aAAW,CAAC,QAAQ,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC,UAAU,EAAU,CAAC,IAAI,aAAW,CAAC,UAAU,EAAE,CAChG;AAEgB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EAAE,CAAC,CAAC,IAAI,EAAE;AAE/I,YAAA,OAAO,OAAO,CAAC,OAAO,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,QAAQ,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC,YAAY,CAAC,GAAG,IAAI,CAAC,CAAC,IAAI,CAAC,MAAM,IAAG;gBACxI,IAAI,MAAM,KAAK,IAAI,IAAI,MAAM,KAAK,SAAS,EAAE;AAC3C,oBAAA,MAAM,IAAI,iBAAiB,CAAC,QAAQ,CAAC;gBACvC;AAEA,gBAAA,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,WAAW,IAAI,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,EAAE;AAC/D,oBAAA,MAAM,IAAI,kBAAkB,CAAC,SAAS,CAAC;gBACzC;AAEA,gBAAA,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,EAAE,CAAC,SAAS,GAAG,MAAM,EAAE,CAAC;AAE/C,gBAAA,OAAO,IAAI;AACb,YAAA,CAAC,CAAC;QACJ;;IA3DI,WAAW,GAAA,aAAA,GAAA,UAAA,CAAA;AADhB,QAAA,UAAU,EAAE;;AACP,KAAA,EAAA,WAAW,CA4DhB;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, AccessGuardType, InjectedDependencies } from './access/access-guard-options.cjs';
|
|
1
|
+
export { AccessGuardOptions, 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, AccessGuardType, InjectedDependencies } from './access/access-guard-options.js';
|
|
1
|
+
export { AccessGuardOptions, 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';
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@telperion/nest-pack",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "NestJS utilities: access guards
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"description": "NestJS utilities: JWT auth and access guards, validation helpers and pagination",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"nestjs",
|
|
7
7
|
"nest",
|
|
@@ -13,7 +13,15 @@
|
|
|
13
13
|
"class-transformer",
|
|
14
14
|
"class-validator",
|
|
15
15
|
"validation",
|
|
16
|
-
"trim"
|
|
16
|
+
"trim",
|
|
17
|
+
"pagination",
|
|
18
|
+
"paging",
|
|
19
|
+
"dto",
|
|
20
|
+
"jwt",
|
|
21
|
+
"authentication",
|
|
22
|
+
"auth guard",
|
|
23
|
+
"bearer token",
|
|
24
|
+
"public route"
|
|
17
25
|
],
|
|
18
26
|
"homepage": "https://github.com/telperiontech/telperion/tree/main/libs/nest-pack#readme",
|
|
19
27
|
"repository": {
|
|
@@ -32,9 +40,13 @@
|
|
|
32
40
|
"@nestjs/common": "^11.0.0 || ^12.0.0",
|
|
33
41
|
"@nestjs/core": "^11.0.0 || ^12.0.0",
|
|
34
42
|
"class-transformer": "^0.5.0",
|
|
35
|
-
"class-validator": "^0.14.0 || ^0.15.0"
|
|
43
|
+
"class-validator": "^0.14.0 || ^0.15.0",
|
|
44
|
+
"@nestjs/jwt": "^11.0.0 || ^12.0.0"
|
|
36
45
|
},
|
|
37
46
|
"peerDependenciesMeta": {
|
|
47
|
+
"@nestjs/jwt": {
|
|
48
|
+
"optional": true
|
|
49
|
+
},
|
|
38
50
|
"class-transformer": {
|
|
39
51
|
"optional": true
|
|
40
52
|
},
|
|
@@ -105,6 +117,86 @@
|
|
|
105
117
|
"node": "./access/index.cjs",
|
|
106
118
|
"default": "./access/index.js"
|
|
107
119
|
},
|
|
120
|
+
"./auth/create-jwt-auth-guard": {
|
|
121
|
+
"types": {
|
|
122
|
+
"import": "./auth/create-jwt-auth-guard.d.ts",
|
|
123
|
+
"require": "./auth/create-jwt-auth-guard.d.cts"
|
|
124
|
+
},
|
|
125
|
+
"import": "./auth/create-jwt-auth-guard.js",
|
|
126
|
+
"require": "./auth/create-jwt-auth-guard.cjs",
|
|
127
|
+
"node": "./auth/create-jwt-auth-guard.cjs",
|
|
128
|
+
"default": "./auth/create-jwt-auth-guard.js"
|
|
129
|
+
},
|
|
130
|
+
"./auth/index": {
|
|
131
|
+
"types": {
|
|
132
|
+
"import": "./auth/index.d.ts",
|
|
133
|
+
"require": "./auth/index.d.cts"
|
|
134
|
+
},
|
|
135
|
+
"import": "./auth/index.js",
|
|
136
|
+
"require": "./auth/index.cjs",
|
|
137
|
+
"node": "./auth/index.cjs",
|
|
138
|
+
"default": "./auth/index.js"
|
|
139
|
+
},
|
|
140
|
+
"./auth": {
|
|
141
|
+
"types": {
|
|
142
|
+
"import": "./auth/index.d.ts",
|
|
143
|
+
"require": "./auth/index.d.cts"
|
|
144
|
+
},
|
|
145
|
+
"import": "./auth/index.js",
|
|
146
|
+
"require": "./auth/index.cjs",
|
|
147
|
+
"node": "./auth/index.cjs",
|
|
148
|
+
"default": "./auth/index.js"
|
|
149
|
+
},
|
|
150
|
+
"./auth/jwt-auth-guard-options": {
|
|
151
|
+
"types": {
|
|
152
|
+
"import": "./auth/jwt-auth-guard-options.d.ts",
|
|
153
|
+
"require": "./auth/jwt-auth-guard-options.d.cts"
|
|
154
|
+
},
|
|
155
|
+
"import": "./auth/jwt-auth-guard-options.js",
|
|
156
|
+
"require": "./auth/jwt-auth-guard-options.cjs",
|
|
157
|
+
"node": "./auth/jwt-auth-guard-options.cjs",
|
|
158
|
+
"default": "./auth/jwt-auth-guard-options.js"
|
|
159
|
+
},
|
|
160
|
+
"./pagination/index": {
|
|
161
|
+
"types": {
|
|
162
|
+
"import": "./pagination/index.d.ts",
|
|
163
|
+
"require": "./pagination/index.d.cts"
|
|
164
|
+
},
|
|
165
|
+
"import": "./pagination/index.js",
|
|
166
|
+
"require": "./pagination/index.cjs",
|
|
167
|
+
"node": "./pagination/index.cjs",
|
|
168
|
+
"default": "./pagination/index.js"
|
|
169
|
+
},
|
|
170
|
+
"./pagination": {
|
|
171
|
+
"types": {
|
|
172
|
+
"import": "./pagination/index.d.ts",
|
|
173
|
+
"require": "./pagination/index.d.cts"
|
|
174
|
+
},
|
|
175
|
+
"import": "./pagination/index.js",
|
|
176
|
+
"require": "./pagination/index.cjs",
|
|
177
|
+
"node": "./pagination/index.cjs",
|
|
178
|
+
"default": "./pagination/index.js"
|
|
179
|
+
},
|
|
180
|
+
"./pagination/page-query": {
|
|
181
|
+
"types": {
|
|
182
|
+
"import": "./pagination/page-query.d.ts",
|
|
183
|
+
"require": "./pagination/page-query.d.cts"
|
|
184
|
+
},
|
|
185
|
+
"import": "./pagination/page-query.js",
|
|
186
|
+
"require": "./pagination/page-query.cjs",
|
|
187
|
+
"node": "./pagination/page-query.cjs",
|
|
188
|
+
"default": "./pagination/page-query.js"
|
|
189
|
+
},
|
|
190
|
+
"./pagination/page": {
|
|
191
|
+
"types": {
|
|
192
|
+
"import": "./pagination/page.d.ts",
|
|
193
|
+
"require": "./pagination/page.d.cts"
|
|
194
|
+
},
|
|
195
|
+
"import": "./pagination/page.js",
|
|
196
|
+
"require": "./pagination/page.cjs",
|
|
197
|
+
"node": "./pagination/page.cjs",
|
|
198
|
+
"default": "./pagination/page.js"
|
|
199
|
+
},
|
|
108
200
|
"./validation/index": {
|
|
109
201
|
"types": {
|
|
110
202
|
"import": "./validation/index.d.ts",
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
|