@telperion/nest-pack 1.2.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 +107 -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 +54 -21
- package/auth/create-jwt-auth-guard.cjs.map +1 -1
- package/auth/create-jwt-auth-guard.d.cts +12 -6
- package/auth/create-jwt-auth-guard.d.ts +12 -6
- package/auth/create-jwt-auth-guard.js +55 -22
- package/auth/create-jwt-auth-guard.js.map +1 -1
- package/auth/index.d.cts +2 -1
- package/auth/index.d.ts +2 -1
- package/auth/jwt-auth-guard-options.d.cts +75 -9
- package/auth/jwt-auth-guard-options.d.ts +75 -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
|
@@ -3,21 +3,22 @@
|
|
|
3
3
|
var tslib = require('tslib');
|
|
4
4
|
var common = require('@nestjs/common');
|
|
5
5
|
var core = require('@nestjs/core');
|
|
6
|
-
var jwt = require('@nestjs/jwt');
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
8
|
* #### Create JWT Auth Guard
|
|
10
9
|
*
|
|
11
10
|
* Creates a guard that requires a valid JWT on every route, for use as the global `APP_GUARD`:
|
|
12
11
|
*
|
|
13
|
-
* - routes and controllers marked with `@Guard.public()` pass without a token
|
|
12
|
+
* - routes and controllers marked with `@Guard.public()` pass without a token; with `publicRoutes: 'optional'` a valid
|
|
13
|
+
* token still gives them the user, and a missing, invalid or expired one passes as anonymous
|
|
14
14
|
* - a missing, malformed or invalid token (bad signature, wrong audience, …): **401** `unauthorizedMessage`
|
|
15
|
-
* - an expired token: **401** `expiredMessage` (`'TokenExpired'`), so clients can tell "renew and retry" from "sign in"
|
|
15
|
+
* - an expired token (see `isExpired`): **401** `expiredMessage` (`'TokenExpired'`), so clients can tell "renew and retry" from "sign in"
|
|
16
16
|
* - otherwise the payload is written to `request.user` (see `property`) and is available through `@Guard.user()`
|
|
17
17
|
*
|
|
18
18
|
* Tokens are verified with the `JwtService` of `@nestjs/jwt`: register `JwtModule` (with the secret or keys) in
|
|
19
|
-
* the
|
|
20
|
-
*
|
|
19
|
+
* the app. To verify otherwise (e.g. with a JWKS and `jose`), provide a {@link JwtAuthGuardVerifier} and pass its
|
|
20
|
+
* injection token as `verifier`; then `@nestjs/jwt` is not needed. The token comes from the `Authorization: Bearer`
|
|
21
|
+
* header unless `extract` reads it from elsewhere. HTTP only.
|
|
21
22
|
*
|
|
22
23
|
* * * *
|
|
23
24
|
* Example:
|
|
@@ -46,30 +47,50 @@ var jwt = require('@nestjs/jwt');
|
|
|
46
47
|
* ```
|
|
47
48
|
*
|
|
48
49
|
* To give the guard a class name of its own, extend it: `@Injectable() export class AuthGuard extends createJwtAuthGuard<TokenPayload>() {}`.
|
|
50
|
+
*
|
|
51
|
+
* Other guards and interceptors can skip public routes with `AuthGuard.isPublic(context)`.
|
|
49
52
|
* * * *
|
|
50
|
-
* @param options Messages, where the token comes from,
|
|
51
|
-
* @returns An injectable guard class with `public()`, `user()` and `userOf()`
|
|
53
|
+
* @param options Messages, where the token comes from, how it is verified, public routes and where the payload goes
|
|
54
|
+
* @returns An injectable guard class with `public()`, `isPublic()`, `user()` and `userOf()`
|
|
55
|
+
* @throws When `verifyOptions` come with a `verifier`, as only the `JwtService` reads them
|
|
52
56
|
*/
|
|
53
57
|
function createJwtAuthGuard(options = {}) {
|
|
54
58
|
var JwtAuthGuard_1;
|
|
55
|
-
const { expiredMessage = 'TokenExpired', unauthorizedMessage = 'Unauthorized', extract, verifyOptions, property = 'user', } = options;
|
|
59
|
+
const { expiredMessage = 'TokenExpired', unauthorizedMessage = 'Unauthorized', extract, verifier, verifyOptions, publicRoutes = 'skip', property = 'user', } = options;
|
|
56
60
|
const publicKey = Symbol('jwt-auth-guard:public');
|
|
61
|
+
if (verifier !== undefined && verifyOptions !== undefined) {
|
|
62
|
+
throw new Error('createJwtAuthGuard: `verifyOptions` are options of the JwtService of @nestjs/jwt; pass them to the `verifier` instead');
|
|
63
|
+
}
|
|
57
64
|
let JwtAuthGuard = class JwtAuthGuard {
|
|
58
65
|
static { JwtAuthGuard_1 = this; }
|
|
59
66
|
reflector;
|
|
60
|
-
jwt;
|
|
61
67
|
static user = common.createParamDecorator((_data, context) => JwtAuthGuard_1.userOf(context.switchToHttp().getRequest()));
|
|
68
|
+
/** Reads `public()` markers when `isPublic()` gets no reflector; `Reflector` needs no dependencies */
|
|
69
|
+
static defaultReflector = new core.Reflector();
|
|
70
|
+
/** Whether a verification error means an expired token: jsonwebtoken's `TokenExpiredError` or jose's `JWTExpired` */
|
|
71
|
+
static isExpired = options.isExpired ??
|
|
72
|
+
((error) => typeof error === 'object' &&
|
|
73
|
+
error !== null &&
|
|
74
|
+
(error.name === 'TokenExpiredError' || error.code === 'ERR_JWT_EXPIRED'));
|
|
75
|
+
/** The verifier of the requests */
|
|
76
|
+
verifier;
|
|
62
77
|
/**
|
|
63
78
|
* @param reflector Reads `public()` markers
|
|
64
|
-
* @param
|
|
79
|
+
* @param source The `verifier`, or without one the `ModuleRef` the `JwtService` is resolved from
|
|
65
80
|
*/
|
|
66
|
-
constructor(reflector,
|
|
81
|
+
constructor(reflector, source) {
|
|
67
82
|
this.reflector = reflector;
|
|
68
|
-
this.
|
|
83
|
+
this.verifier =
|
|
84
|
+
verifier === undefined
|
|
85
|
+
? () => JwtAuthGuard_1.jwtServiceVerifier(source)
|
|
86
|
+
: () => Promise.resolve(source);
|
|
69
87
|
}
|
|
70
88
|
static public() {
|
|
71
89
|
return common.SetMetadata(publicKey, true);
|
|
72
90
|
}
|
|
91
|
+
static isPublic(context, reflector = JwtAuthGuard_1.defaultReflector) {
|
|
92
|
+
return !!reflector.getAllAndOverride(publicKey, [context.getHandler(), context.getClass()]);
|
|
93
|
+
}
|
|
73
94
|
static userOf(request) {
|
|
74
95
|
return request[property];
|
|
75
96
|
}
|
|
@@ -80,23 +101,35 @@ function createJwtAuthGuard(options = {}) {
|
|
|
80
101
|
const header = headers['authorization'];
|
|
81
102
|
return (typeof header === 'string' ? /^Bearer +(\S+)$/i.exec(header) : null)?.[1];
|
|
82
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Verifies with the `JwtService` of `@nestjs/jwt`, imported only here so apps with a `verifier` need not install it.
|
|
106
|
+
* Looked up in the whole app, as the guard's module only imports `JwtModule`.
|
|
107
|
+
*/
|
|
108
|
+
static jwtServiceVerifier(moduleRef) {
|
|
109
|
+
return import('@nestjs/jwt')
|
|
110
|
+
.then(({ JwtService }) => moduleRef.get(JwtService, { strict: false }))
|
|
111
|
+
.then(jwt => ({ verify: token => jwt.verifyAsync(token, verifyOptions) }));
|
|
112
|
+
}
|
|
83
113
|
canActivate(context) {
|
|
84
|
-
if (this.reflector.getAllAndOverride(publicKey, [context.getHandler(), context.getClass()])) {
|
|
85
|
-
return true;
|
|
86
|
-
}
|
|
87
114
|
const request = context.switchToHttp().getRequest();
|
|
88
|
-
const
|
|
115
|
+
const optional = JwtAuthGuard_1.isPublic(context, this.reflector);
|
|
116
|
+
const token = optional && publicRoutes === 'skip'
|
|
117
|
+
? undefined
|
|
118
|
+
: extract
|
|
119
|
+
? extract(request)
|
|
120
|
+
: JwtAuthGuard_1.bearerToken(request);
|
|
89
121
|
return token
|
|
90
|
-
? this.
|
|
91
|
-
|
|
122
|
+
? this.verifier().then(verifier => Promise.resolve(token)
|
|
123
|
+
.then(value => verifier.verify(value, request))
|
|
124
|
+
.then(payload => (Object.assign(request, { [property]: payload }), true), (error) => optional || Promise.reject(new common.UnauthorizedException(JwtAuthGuard_1.isExpired(error) ? expiredMessage : unauthorizedMessage))))
|
|
125
|
+
: optional || Promise.reject(new common.UnauthorizedException(unauthorizedMessage));
|
|
92
126
|
}
|
|
93
127
|
};
|
|
94
128
|
JwtAuthGuard = JwtAuthGuard_1 = tslib.__decorate([
|
|
95
129
|
common.Injectable(),
|
|
96
|
-
tslib.__metadata("design:paramtypes", [core.Reflector,
|
|
97
|
-
jwt.JwtService])
|
|
130
|
+
tslib.__metadata("design:paramtypes", [core.Reflector, Object])
|
|
98
131
|
], JwtAuthGuard);
|
|
99
|
-
[core.Reflector,
|
|
132
|
+
[core.Reflector, verifier ?? core.ModuleRef].forEach((token, index) => common.Inject(token)(JwtAuthGuard, undefined, index));
|
|
100
133
|
return JwtAuthGuard;
|
|
101
134
|
}
|
|
102
135
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-jwt-auth-guard.cjs","sources":["../../../../libs/nest-pack/src/auth/create-jwt-auth-guard.ts"],"sourcesContent":[null],"names":["createParamDecorator","
|
|
1
|
+
{"version":3,"file":"create-jwt-auth-guard.cjs","sources":["../../../../libs/nest-pack/src/auth/create-jwt-auth-guard.ts"],"sourcesContent":[null],"names":["createParamDecorator","Reflector","SetMetadata","UnauthorizedException","__decorate","Injectable","ModuleRef","Inject"],"mappings":";;;;;;AAaA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiDG;AACG,SAAU,kBAAkB,CAChC,OAAA,GAAwC,EAAE,EAAA;;IAE1C,MAAM,EACJ,cAAc,GAAG,cAAc,EAC/B,mBAAmB,GAAG,cAAc,EACpC,OAAO,EACP,QAAQ,EACR,aAAa,EACb,YAAY,GAAG,MAAM,EACrB,QAAQ,GAAG,MAAM,GAClB,GAAG,OAAO;AACX,IAAA,MAAM,SAAS,GAAG,MAAM,CAAC,uBAAuB,CAAC;IAEjD,IAAI,QAAQ,KAAK,S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,CAAC,OAAO,EAAE,EAAE,CAAC,QAAQ,GAAG,OAAO,EAAE,CAAC,EAAE,IAAI,CAAC,EAClE,CAAC,KAAc,KACb,QAAQ,IAAI,OAAO,CAAC,MAAM,CAAC,IAAIC,4BAAqB,CAAC,cAAY,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,cAAc,GAAG,mBAAmB,CAAC,CAAC,CAC9H;AAEP,kBAAE,QAAQ,IAAI,OAAO,CAAC,MAAM,CAAC,IAAIA,4BAAqB,CAAC,mBAAmB,CAAC,CAAC;QAChF;;IArFI,YAAY,GAAA,cAAA,GAAAC,gBAAA,CAAA;AADjB,QAAAC,iBAAU,EAAE;+CAyBmBJ,cAAS,EAAA,MAAA,CAAA;AAxBnC,KAAA,EAAA,YAAY,CAsFjB;AAED,IAAA,CAACA,cAAS,EAAE,QAAQ,IAAIK,cAAS,CAAC,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,KAAK,KAAKC,aAAM,CAAC,KAAK,CAAC,CAAC,YAAY,EAAE,SAAS,EAAE,KAAK,CAAC,CAAC;AAE3G,IAAA,OAAO,YAAY;AACrB;;;;"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { JwtAuthGuardRequest, JwtAuthGuardOptions, JwtAuthGuardType } from './jwt-auth-guard-options.cjs';
|
|
2
2
|
import '@nestjs/common';
|
|
3
|
+
import '@nestjs/core';
|
|
3
4
|
import '@nestjs/jwt';
|
|
4
5
|
|
|
5
6
|
/**
|
|
@@ -7,14 +8,16 @@ import '@nestjs/jwt';
|
|
|
7
8
|
*
|
|
8
9
|
* Creates a guard that requires a valid JWT on every route, for use as the global `APP_GUARD`:
|
|
9
10
|
*
|
|
10
|
-
* - routes and controllers marked with `@Guard.public()` pass without a token
|
|
11
|
+
* - routes and controllers marked with `@Guard.public()` pass without a token; with `publicRoutes: 'optional'` a valid
|
|
12
|
+
* token still gives them the user, and a missing, invalid or expired one passes as anonymous
|
|
11
13
|
* - 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"
|
|
14
|
+
* - an expired token (see `isExpired`): **401** `expiredMessage` (`'TokenExpired'`), so clients can tell "renew and retry" from "sign in"
|
|
13
15
|
* - otherwise the payload is written to `request.user` (see `property`) and is available through `@Guard.user()`
|
|
14
16
|
*
|
|
15
17
|
* Tokens are verified with the `JwtService` of `@nestjs/jwt`: register `JwtModule` (with the secret or keys) in
|
|
16
|
-
* the
|
|
17
|
-
*
|
|
18
|
+
* the app. To verify otherwise (e.g. with a JWKS and `jose`), provide a {@link JwtAuthGuardVerifier} and pass its
|
|
19
|
+
* injection token as `verifier`; then `@nestjs/jwt` is not needed. The token comes from the `Authorization: Bearer`
|
|
20
|
+
* header unless `extract` reads it from elsewhere. HTTP only.
|
|
18
21
|
*
|
|
19
22
|
* * * *
|
|
20
23
|
* Example:
|
|
@@ -43,9 +46,12 @@ import '@nestjs/jwt';
|
|
|
43
46
|
* ```
|
|
44
47
|
*
|
|
45
48
|
* To give the guard a class name of its own, extend it: `@Injectable() export class AuthGuard extends createJwtAuthGuard<TokenPayload>() {}`.
|
|
49
|
+
*
|
|
50
|
+
* Other guards and interceptors can skip public routes with `AuthGuard.isPublic(context)`.
|
|
46
51
|
* * * *
|
|
47
|
-
* @param options Messages, where the token comes from,
|
|
48
|
-
* @returns An injectable guard class with `public()`, `user()` and `userOf()`
|
|
52
|
+
* @param options Messages, where the token comes from, how it is verified, public routes and where the payload goes
|
|
53
|
+
* @returns An injectable guard class with `public()`, `isPublic()`, `user()` and `userOf()`
|
|
54
|
+
* @throws When `verifyOptions` come with a `verifier`, as only the `JwtService` reads them
|
|
49
55
|
*/
|
|
50
56
|
declare function createJwtAuthGuard<Payload extends object, Request extends object = JwtAuthGuardRequest>(options?: JwtAuthGuardOptions<Request>): JwtAuthGuardType<Payload>;
|
|
51
57
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { JwtAuthGuardRequest, JwtAuthGuardOptions, JwtAuthGuardType } from './jwt-auth-guard-options.js';
|
|
2
2
|
import '@nestjs/common';
|
|
3
|
+
import '@nestjs/core';
|
|
3
4
|
import '@nestjs/jwt';
|
|
4
5
|
|
|
5
6
|
/**
|
|
@@ -7,14 +8,16 @@ import '@nestjs/jwt';
|
|
|
7
8
|
*
|
|
8
9
|
* Creates a guard that requires a valid JWT on every route, for use as the global `APP_GUARD`:
|
|
9
10
|
*
|
|
10
|
-
* - routes and controllers marked with `@Guard.public()` pass without a token
|
|
11
|
+
* - routes and controllers marked with `@Guard.public()` pass without a token; with `publicRoutes: 'optional'` a valid
|
|
12
|
+
* token still gives them the user, and a missing, invalid or expired one passes as anonymous
|
|
11
13
|
* - 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"
|
|
14
|
+
* - an expired token (see `isExpired`): **401** `expiredMessage` (`'TokenExpired'`), so clients can tell "renew and retry" from "sign in"
|
|
13
15
|
* - otherwise the payload is written to `request.user` (see `property`) and is available through `@Guard.user()`
|
|
14
16
|
*
|
|
15
17
|
* Tokens are verified with the `JwtService` of `@nestjs/jwt`: register `JwtModule` (with the secret or keys) in
|
|
16
|
-
* the
|
|
17
|
-
*
|
|
18
|
+
* the app. To verify otherwise (e.g. with a JWKS and `jose`), provide a {@link JwtAuthGuardVerifier} and pass its
|
|
19
|
+
* injection token as `verifier`; then `@nestjs/jwt` is not needed. The token comes from the `Authorization: Bearer`
|
|
20
|
+
* header unless `extract` reads it from elsewhere. HTTP only.
|
|
18
21
|
*
|
|
19
22
|
* * * *
|
|
20
23
|
* Example:
|
|
@@ -43,9 +46,12 @@ import '@nestjs/jwt';
|
|
|
43
46
|
* ```
|
|
44
47
|
*
|
|
45
48
|
* To give the guard a class name of its own, extend it: `@Injectable() export class AuthGuard extends createJwtAuthGuard<TokenPayload>() {}`.
|
|
49
|
+
*
|
|
50
|
+
* Other guards and interceptors can skip public routes with `AuthGuard.isPublic(context)`.
|
|
46
51
|
* * * *
|
|
47
|
-
* @param options Messages, where the token comes from,
|
|
48
|
-
* @returns An injectable guard class with `public()`, `user()` and `userOf()`
|
|
52
|
+
* @param options Messages, where the token comes from, how it is verified, public routes and where the payload goes
|
|
53
|
+
* @returns An injectable guard class with `public()`, `isPublic()`, `user()` and `userOf()`
|
|
54
|
+
* @throws When `verifyOptions` come with a `verifier`, as only the `JwtService` reads them
|
|
49
55
|
*/
|
|
50
56
|
declare function createJwtAuthGuard<Payload extends object, Request extends object = JwtAuthGuardRequest>(options?: JwtAuthGuardOptions<Request>): JwtAuthGuardType<Payload>;
|
|
51
57
|
|
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
import { __decorate, __metadata } from 'tslib';
|
|
2
2
|
import { createParamDecorator, SetMetadata, UnauthorizedException, Injectable, Inject } from '@nestjs/common';
|
|
3
|
-
import { Reflector } from '@nestjs/core';
|
|
4
|
-
import { TokenExpiredError, JwtService } from '@nestjs/jwt';
|
|
3
|
+
import { Reflector, ModuleRef } from '@nestjs/core';
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* #### Create JWT Auth Guard
|
|
8
7
|
*
|
|
9
8
|
* Creates a guard that requires a valid JWT on every route, for use as the global `APP_GUARD`:
|
|
10
9
|
*
|
|
11
|
-
* - routes and controllers marked with `@Guard.public()` pass without a token
|
|
10
|
+
* - routes and controllers marked with `@Guard.public()` pass without a token; with `publicRoutes: 'optional'` a valid
|
|
11
|
+
* token still gives them the user, and a missing, invalid or expired one passes as anonymous
|
|
12
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"
|
|
13
|
+
* - an expired token (see `isExpired`): **401** `expiredMessage` (`'TokenExpired'`), so clients can tell "renew and retry" from "sign in"
|
|
14
14
|
* - otherwise the payload is written to `request.user` (see `property`) and is available through `@Guard.user()`
|
|
15
15
|
*
|
|
16
16
|
* Tokens are verified with the `JwtService` of `@nestjs/jwt`: register `JwtModule` (with the secret or keys) in
|
|
17
|
-
* the
|
|
18
|
-
*
|
|
17
|
+
* the app. To verify otherwise (e.g. with a JWKS and `jose`), provide a {@link JwtAuthGuardVerifier} and pass its
|
|
18
|
+
* injection token as `verifier`; then `@nestjs/jwt` is not needed. The token comes from the `Authorization: Bearer`
|
|
19
|
+
* header unless `extract` reads it from elsewhere. HTTP only.
|
|
19
20
|
*
|
|
20
21
|
* * * *
|
|
21
22
|
* Example:
|
|
@@ -44,30 +45,50 @@ import { TokenExpiredError, JwtService } from '@nestjs/jwt';
|
|
|
44
45
|
* ```
|
|
45
46
|
*
|
|
46
47
|
* To give the guard a class name of its own, extend it: `@Injectable() export class AuthGuard extends createJwtAuthGuard<TokenPayload>() {}`.
|
|
48
|
+
*
|
|
49
|
+
* Other guards and interceptors can skip public routes with `AuthGuard.isPublic(context)`.
|
|
47
50
|
* * * *
|
|
48
|
-
* @param options Messages, where the token comes from,
|
|
49
|
-
* @returns An injectable guard class with `public()`, `user()` and `userOf()`
|
|
51
|
+
* @param options Messages, where the token comes from, how it is verified, public routes and where the payload goes
|
|
52
|
+
* @returns An injectable guard class with `public()`, `isPublic()`, `user()` and `userOf()`
|
|
53
|
+
* @throws When `verifyOptions` come with a `verifier`, as only the `JwtService` reads them
|
|
50
54
|
*/
|
|
51
55
|
function createJwtAuthGuard(options = {}) {
|
|
52
56
|
var JwtAuthGuard_1;
|
|
53
|
-
const { expiredMessage = 'TokenExpired', unauthorizedMessage = 'Unauthorized', extract, verifyOptions, property = 'user', } = options;
|
|
57
|
+
const { expiredMessage = 'TokenExpired', unauthorizedMessage = 'Unauthorized', extract, verifier, verifyOptions, publicRoutes = 'skip', property = 'user', } = options;
|
|
54
58
|
const publicKey = Symbol('jwt-auth-guard:public');
|
|
59
|
+
if (verifier !== undefined && verifyOptions !== undefined) {
|
|
60
|
+
throw new Error('createJwtAuthGuard: `verifyOptions` are options of the JwtService of @nestjs/jwt; pass them to the `verifier` instead');
|
|
61
|
+
}
|
|
55
62
|
let JwtAuthGuard = class JwtAuthGuard {
|
|
56
63
|
static { JwtAuthGuard_1 = this; }
|
|
57
64
|
reflector;
|
|
58
|
-
jwt;
|
|
59
65
|
static user = createParamDecorator((_data, context) => JwtAuthGuard_1.userOf(context.switchToHttp().getRequest()));
|
|
66
|
+
/** Reads `public()` markers when `isPublic()` gets no reflector; `Reflector` needs no dependencies */
|
|
67
|
+
static defaultReflector = new Reflector();
|
|
68
|
+
/** Whether a verification error means an expired token: jsonwebtoken's `TokenExpiredError` or jose's `JWTExpired` */
|
|
69
|
+
static isExpired = options.isExpired ??
|
|
70
|
+
((error) => typeof error === 'object' &&
|
|
71
|
+
error !== null &&
|
|
72
|
+
(error.name === 'TokenExpiredError' || error.code === 'ERR_JWT_EXPIRED'));
|
|
73
|
+
/** The verifier of the requests */
|
|
74
|
+
verifier;
|
|
60
75
|
/**
|
|
61
76
|
* @param reflector Reads `public()` markers
|
|
62
|
-
* @param
|
|
77
|
+
* @param source The `verifier`, or without one the `ModuleRef` the `JwtService` is resolved from
|
|
63
78
|
*/
|
|
64
|
-
constructor(reflector,
|
|
79
|
+
constructor(reflector, source) {
|
|
65
80
|
this.reflector = reflector;
|
|
66
|
-
this.
|
|
81
|
+
this.verifier =
|
|
82
|
+
verifier === undefined
|
|
83
|
+
? () => JwtAuthGuard_1.jwtServiceVerifier(source)
|
|
84
|
+
: () => Promise.resolve(source);
|
|
67
85
|
}
|
|
68
86
|
static public() {
|
|
69
87
|
return SetMetadata(publicKey, true);
|
|
70
88
|
}
|
|
89
|
+
static isPublic(context, reflector = JwtAuthGuard_1.defaultReflector) {
|
|
90
|
+
return !!reflector.getAllAndOverride(publicKey, [context.getHandler(), context.getClass()]);
|
|
91
|
+
}
|
|
71
92
|
static userOf(request) {
|
|
72
93
|
return request[property];
|
|
73
94
|
}
|
|
@@ -78,23 +99,35 @@ function createJwtAuthGuard(options = {}) {
|
|
|
78
99
|
const header = headers['authorization'];
|
|
79
100
|
return (typeof header === 'string' ? /^Bearer +(\S+)$/i.exec(header) : null)?.[1];
|
|
80
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* Verifies with the `JwtService` of `@nestjs/jwt`, imported only here so apps with a `verifier` need not install it.
|
|
104
|
+
* Looked up in the whole app, as the guard's module only imports `JwtModule`.
|
|
105
|
+
*/
|
|
106
|
+
static jwtServiceVerifier(moduleRef) {
|
|
107
|
+
return import('@nestjs/jwt')
|
|
108
|
+
.then(({ JwtService }) => moduleRef.get(JwtService, { strict: false }))
|
|
109
|
+
.then(jwt => ({ verify: token => jwt.verifyAsync(token, verifyOptions) }));
|
|
110
|
+
}
|
|
81
111
|
canActivate(context) {
|
|
82
|
-
if (this.reflector.getAllAndOverride(publicKey, [context.getHandler(), context.getClass()])) {
|
|
83
|
-
return true;
|
|
84
|
-
}
|
|
85
112
|
const request = context.switchToHttp().getRequest();
|
|
86
|
-
const
|
|
113
|
+
const optional = JwtAuthGuard_1.isPublic(context, this.reflector);
|
|
114
|
+
const token = optional && publicRoutes === 'skip'
|
|
115
|
+
? undefined
|
|
116
|
+
: extract
|
|
117
|
+
? extract(request)
|
|
118
|
+
: JwtAuthGuard_1.bearerToken(request);
|
|
87
119
|
return token
|
|
88
|
-
? this.
|
|
89
|
-
|
|
120
|
+
? this.verifier().then(verifier => Promise.resolve(token)
|
|
121
|
+
.then(value => verifier.verify(value, request))
|
|
122
|
+
.then(payload => (Object.assign(request, { [property]: payload }), true), (error) => optional || Promise.reject(new UnauthorizedException(JwtAuthGuard_1.isExpired(error) ? expiredMessage : unauthorizedMessage))))
|
|
123
|
+
: optional || Promise.reject(new UnauthorizedException(unauthorizedMessage));
|
|
90
124
|
}
|
|
91
125
|
};
|
|
92
126
|
JwtAuthGuard = JwtAuthGuard_1 = __decorate([
|
|
93
127
|
Injectable(),
|
|
94
|
-
__metadata("design:paramtypes", [Reflector,
|
|
95
|
-
JwtService])
|
|
128
|
+
__metadata("design:paramtypes", [Reflector, Object])
|
|
96
129
|
], JwtAuthGuard);
|
|
97
|
-
[Reflector,
|
|
130
|
+
[Reflector, verifier ?? ModuleRef].forEach((token, index) => Inject(token)(JwtAuthGuard, undefined, index));
|
|
98
131
|
return JwtAuthGuard;
|
|
99
132
|
}
|
|
100
133
|
|
|
@@ -1 +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":"
|
|
1
|
+
{"version":3,"file":"create-jwt-auth-guard.js","sources":["../../../../libs/nest-pack/src/auth/create-jwt-auth-guard.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;AAaA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiDG;AACG,SAAU,kBAAkB,CAChC,OAAA,GAAwC,EAAE,EAAA;;IAE1C,MAAM,EACJ,cAAc,GAAG,cAAc,EAC/B,mBAAmB,GAAG,cAAc,EACpC,OAAO,EACP,QAAQ,EACR,aAAa,EACb,YAAY,GAAG,MAAM,EACrB,QAAQ,GAAG,MAAM,GAClB,GAAG,OAAO;AACX,IAAA,MAAM,SAAS,GAAG,MAAM,CAAC,uBAAuB,CAAC;IAEjD,IAAI,QAAQ,KAAK,SAAS,IAAI,aAAa,KAAK,SAAS,EAAE;AACzD,QAAA,MAAM,IAAI,KAAK,CAAC,uHAAuH,CAAC;IAC1I;IAEA,IACM,YAAY,GADlB,MACM,YAAY,CAAA;;AAwBG,QAAA,SAAA;QAvBnB,OAAgB,IAAI,GAAG,oBAAoB,CACzC,CAAC,KAAc,EAAE,OAAyB,KAA0B,cAAY,CAAC,MAAM,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC,UAAU,EAAU,CAAC,CACrI;;AAGO,QAAA,OAAgB,g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C;QAChF;;IArFI,YAAY,GAAA,cAAA,GAAA,UAAA,CAAA;AADjB,QAAA,UAAU,EAAE;yCAyBmB,SAAS,EAAA,MAAA,CAAA;AAxBnC,KAAA,EAAA,YAAY,CAsFjB;AAED,IAAA,CAAC,SAAS,EAAE,QAAQ,IAAI,SAAS,CAAC,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,KAAK,KAAK,MAAM,CAAC,KAAK,CAAC,CAAC,YAAY,EAAE,SAAS,EAAE,KAAK,CAAC,CAAC;AAE3G,IAAA,OAAO,YAAY;AACrB;;;;"}
|
package/auth/index.d.cts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export { JwtAuthGuardOptions, JwtAuthGuardRequest, JwtAuthGuardType } from './jwt-auth-guard-options.cjs';
|
|
1
|
+
export { JwtAuthGuardBaseOptions, JwtAuthGuardCustomVerifierOptions, JwtAuthGuardJwtServiceOptions, JwtAuthGuardOptions, JwtAuthGuardPublicRoutes, JwtAuthGuardRequest, JwtAuthGuardType, JwtAuthGuardVerifier } from './jwt-auth-guard-options.cjs';
|
|
2
2
|
export { createJwtAuthGuard } from './create-jwt-auth-guard.cjs';
|
|
3
3
|
import '@nestjs/common';
|
|
4
|
+
import '@nestjs/core';
|
|
4
5
|
import '@nestjs/jwt';
|
package/auth/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export { JwtAuthGuardOptions, JwtAuthGuardRequest, JwtAuthGuardType } from './jwt-auth-guard-options.js';
|
|
1
|
+
export { JwtAuthGuardBaseOptions, JwtAuthGuardCustomVerifierOptions, JwtAuthGuardJwtServiceOptions, JwtAuthGuardOptions, JwtAuthGuardPublicRoutes, JwtAuthGuardRequest, JwtAuthGuardType, JwtAuthGuardVerifier } from './jwt-auth-guard-options.js';
|
|
2
2
|
export { createJwtAuthGuard } from './create-jwt-auth-guard.js';
|
|
3
3
|
import '@nestjs/common';
|
|
4
|
+
import '@nestjs/core';
|
|
4
5
|
import '@nestjs/jwt';
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { Type, CanActivate } from '@nestjs/common';
|
|
1
|
+
import { InjectionToken, Type, CanActivate, ExecutionContext } from '@nestjs/common';
|
|
2
|
+
import { Reflector } from '@nestjs/core';
|
|
2
3
|
import { JwtVerifyOptions } from '@nestjs/jwt';
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -8,9 +9,29 @@ interface JwtAuthGuardRequest {
|
|
|
8
9
|
headers: Record<string, string | string[] | undefined>;
|
|
9
10
|
}
|
|
10
11
|
/**
|
|
11
|
-
*
|
|
12
|
+
* Verifies tokens for a {@link createJwtAuthGuard} in place of the `JwtService` of `@nestjs/jwt`,
|
|
13
|
+
* e.g. with `jwtVerify` and a JWKS of `jose`. Provide it under the token given as `verifier`.
|
|
12
14
|
*/
|
|
13
|
-
interface
|
|
15
|
+
interface JwtAuthGuardVerifier<Payload extends object = object, Request extends object = JwtAuthGuardRequest> {
|
|
16
|
+
/**
|
|
17
|
+
* The payload of a valid token; throws (or rejects) for an invalid or expired one
|
|
18
|
+
*
|
|
19
|
+
* @param token The token `extract` read
|
|
20
|
+
* @param request The HTTP request the token came with
|
|
21
|
+
*/
|
|
22
|
+
verify(token: string, request: Request): Payload | Promise<Payload>;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* How a {@link createJwtAuthGuard} treats routes marked with `public()`:
|
|
26
|
+
*
|
|
27
|
+
* - `'skip'`: the token is not read, the route has no user
|
|
28
|
+
* - `'optional'`: a valid token gives the route its user; a missing, invalid or expired token passes as anonymous
|
|
29
|
+
*/
|
|
30
|
+
type JwtAuthGuardPublicRoutes = 'skip' | 'optional';
|
|
31
|
+
/**
|
|
32
|
+
* The options of {@link createJwtAuthGuard} besides the verification
|
|
33
|
+
*/
|
|
34
|
+
interface JwtAuthGuardBaseOptions<Request extends object = JwtAuthGuardRequest> {
|
|
14
35
|
/**
|
|
15
36
|
* The message of the 401 answer for an expired token, so clients can renew the token and retry
|
|
16
37
|
* (e.g. `ApiAuth.expired` of `@telperion/ng-pack/http`)
|
|
@@ -31,9 +52,17 @@ interface JwtAuthGuardOptions<Request extends object = JwtAuthGuardRequest> {
|
|
|
31
52
|
*/
|
|
32
53
|
extract?: (request: Request) => string | null | undefined;
|
|
33
54
|
/**
|
|
34
|
-
*
|
|
55
|
+
* Whether a verification error means an expired token, which answers `expiredMessage`
|
|
56
|
+
*
|
|
57
|
+
* @default recognizes the errors of jsonwebtoken (`@nestjs/jwt`, `name === 'TokenExpiredError'`) and jose (`code === 'ERR_JWT_EXPIRED'`)
|
|
35
58
|
*/
|
|
36
|
-
|
|
59
|
+
isExpired?: (error: unknown) => boolean;
|
|
60
|
+
/**
|
|
61
|
+
* How routes marked with `public()` treat tokens, see {@link JwtAuthGuardPublicRoutes}
|
|
62
|
+
*
|
|
63
|
+
* @default 'skip'
|
|
64
|
+
*/
|
|
65
|
+
publicRoutes?: JwtAuthGuardPublicRoutes;
|
|
37
66
|
/**
|
|
38
67
|
* The request property the verified payload is written to
|
|
39
68
|
*
|
|
@@ -41,22 +70,59 @@ interface JwtAuthGuardOptions<Request extends object = JwtAuthGuardRequest> {
|
|
|
41
70
|
*/
|
|
42
71
|
property?: string;
|
|
43
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* Verification with the `JwtService` of `@nestjs/jwt`
|
|
75
|
+
*/
|
|
76
|
+
interface JwtAuthGuardJwtServiceOptions {
|
|
77
|
+
verifier?: undefined;
|
|
78
|
+
/**
|
|
79
|
+
* Options of `JwtService.verifyAsync`, e.g. the `audience`, `issuer` or a `secret` other than the `JwtModule`'s
|
|
80
|
+
*/
|
|
81
|
+
verifyOptions?: JwtVerifyOptions;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Verification with a verifier of its own
|
|
85
|
+
*/
|
|
86
|
+
interface JwtAuthGuardCustomVerifierOptions {
|
|
87
|
+
/**
|
|
88
|
+
* The injection token of a {@link JwtAuthGuardVerifier}, e.g. one verifying with a JWKS. With it, the guard does not use `@nestjs/jwt`.
|
|
89
|
+
*/
|
|
90
|
+
verifier: InjectionToken;
|
|
91
|
+
/**
|
|
92
|
+
* Only for the `JwtService`; pass these options to the verifier itself
|
|
93
|
+
*/
|
|
94
|
+
verifyOptions?: never;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Options of {@link createJwtAuthGuard}: the `JwtService` of `@nestjs/jwt` verifies the tokens, or the `verifier`
|
|
98
|
+
*/
|
|
99
|
+
type JwtAuthGuardOptions<Request extends object = JwtAuthGuardRequest> = JwtAuthGuardBaseOptions<Request> & (JwtAuthGuardJwtServiceOptions | JwtAuthGuardCustomVerifierOptions);
|
|
44
100
|
/**
|
|
45
101
|
* The guard class {@link createJwtAuthGuard} returns, with its decorators
|
|
46
102
|
*/
|
|
47
103
|
interface JwtAuthGuardType<Payload extends object> extends Type<CanActivate> {
|
|
48
104
|
/**
|
|
49
|
-
* Route or controller decorator: the route needs no token
|
|
105
|
+
* Route or controller decorator: the route needs no token (see `publicRoutes` for reading one anyway).
|
|
106
|
+
* A method's setting replaces its controller's.
|
|
50
107
|
*/
|
|
51
108
|
public(): MethodDecorator & ClassDecorator;
|
|
52
109
|
/**
|
|
53
|
-
*
|
|
110
|
+
* Whether the route of a context is marked with `public()`, for other guards and interceptors that skip public
|
|
111
|
+
* routes (throttling, auditing, resolving the user, …). A method's setting replaces its controller's.
|
|
112
|
+
*
|
|
113
|
+
* @param context The context of the route, e.g. of `canActivate` or `intercept`
|
|
114
|
+
* @param reflector Reads the markers; a `Reflector` of its own by default
|
|
115
|
+
*/
|
|
116
|
+
isPublic(context: ExecutionContext, reflector?: Reflector): boolean;
|
|
117
|
+
/**
|
|
118
|
+
* Parameter decorator: the verified token payload; on public routes `undefined`, or with `publicRoutes: 'optional'`
|
|
119
|
+
* the payload of a valid token
|
|
54
120
|
*/
|
|
55
121
|
user(...pipes: unknown[]): ParameterDecorator;
|
|
56
122
|
/**
|
|
57
|
-
* The verified payload of a request, `undefined` before the guard ran or on public routes
|
|
123
|
+
* The verified payload of a request, `undefined` before the guard ran or on public routes without a valid token
|
|
58
124
|
*/
|
|
59
125
|
userOf(request: object): Payload | undefined;
|
|
60
126
|
}
|
|
61
127
|
|
|
62
|
-
export type { JwtAuthGuardOptions, JwtAuthGuardRequest, JwtAuthGuardType };
|
|
128
|
+
export type { JwtAuthGuardBaseOptions, JwtAuthGuardCustomVerifierOptions, JwtAuthGuardJwtServiceOptions, JwtAuthGuardOptions, JwtAuthGuardPublicRoutes, JwtAuthGuardRequest, JwtAuthGuardType, JwtAuthGuardVerifier };
|