@nestjs-kitchen/authz 2.0.2 → 2.0.4
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/authz.provider.d.ts +1 -3
- package/dist/authz.provider.js +29 -48
- package/dist/constants.d.ts +6 -8
- package/dist/constants.js +13 -46
- package/dist/errors.d.ts +3 -5
- package/dist/errors.js +35 -60
- package/dist/index.d.ts +5 -21
- package/dist/index.js +16 -45
- package/dist/jwt/extract-jwt.d.ts +4 -7
- package/dist/jwt/extract-jwt.js +138 -158
- package/dist/jwt/index.d.ts +2 -16
- package/dist/jwt/index.js +7 -30
- package/dist/jwt/jwt-authz-als.middleware.d.ts +6 -16
- package/dist/jwt/jwt-authz-als.middleware.js +44 -81
- package/dist/jwt/jwt-authz.guard.d.ts +8 -21
- package/dist/jwt/jwt-authz.guard.js +130 -175
- package/dist/jwt/jwt-authz.interface.d.ts +16 -25
- package/dist/jwt/jwt-authz.interface.js +69 -92
- package/dist/jwt/jwt-authz.module.d.ts +19 -31
- package/dist/jwt/jwt-authz.module.js +250 -299
- package/dist/jwt/jwt-authz.service.d.ts +6 -17
- package/dist/jwt/jwt-authz.service.js +116 -162
- package/dist/jwt/jwt-authz.strategy.d.ts +5 -17
- package/dist/jwt/jwt-authz.strategy.js +132 -192
- package/dist/session/index.d.ts +1 -12
- package/dist/session/index.js +5 -27
- package/dist/session/session-authz-als.middleware.d.ts +5 -12
- package/dist/session/session-authz-als.middleware.js +98 -130
- package/dist/session/session-authz.guard.d.ts +6 -15
- package/dist/session/session-authz.guard.js +98 -135
- package/dist/session/session-authz.interface.d.ts +5 -12
- package/dist/session/session-authz.interface.js +19 -42
- package/dist/session/session-authz.module.d.ts +18 -26
- package/dist/session/session-authz.module.js +241 -290
- package/dist/session/session-authz.service.d.ts +5 -13
- package/dist/session/session-authz.service.js +69 -105
- package/dist/session/session-authz.strategy.d.ts +4 -12
- package/dist/session/session-authz.strategy.js +68 -105
- package/dist/user.decorator.d.ts +3 -6
- package/dist/user.decorator.js +26 -35
- package/dist/utils/cookie-parsers.d.ts +3 -6
- package/dist/utils/cookie-parsers.js +66 -74
- package/dist/utils/create-authz-decorator-factory.d.ts +3 -9
- package/dist/utils/create-authz-decorator-factory.js +32 -54
- package/dist/utils/create-set-cookie-fn.d.ts +3 -9
- package/dist/utils/create-set-cookie-fn.js +23 -45
- package/dist/utils/generics.d.ts +4 -6
- package/dist/utils/generics.js +37 -63
- package/dist/utils/get-allow-anonymous.d.ts +2 -9
- package/dist/utils/get-allow-anonymous.js +7 -29
- package/dist/utils/get-als-store.d.ts +2 -5
- package/dist/utils/get-als-store.js +11 -34
- package/dist/utils/get-context-authz-meta-params-list.d.ts +2 -9
- package/dist/utils/get-context-authz-meta-params-list.js +16 -35
- package/dist/utils/get-passport-property.d.ts +1 -3
- package/dist/utils/get-passport-property.js +8 -30
- package/dist/utils/index.d.ts +11 -16
- package/dist/utils/index.js +26 -40
- package/dist/utils/merge-dynamic-module-configs.d.ts +2 -5
- package/dist/utils/merge-dynamic-module-configs.js +14 -49
- package/dist/utils/msgpackrs.d.ts +2 -4
- package/dist/utils/msgpackrs.js +11 -35
- package/dist/utils/types.d.ts +23 -26
- package/dist/utils/types.js +2 -15
- package/package.json +7 -7
|
@@ -1,18 +1,9 @@
|
|
|
1
|
-
import * as _nestjs_common from '@nestjs/common';
|
|
2
|
-
import { AuthzProviderClass } from '../authz.provider.js';
|
|
3
|
-
import { CookieOptionsWithSecret, DeepReadonly } from '../utils/types.js';
|
|
4
1
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import '
|
|
8
|
-
import '
|
|
9
|
-
import
|
|
10
|
-
import 'crypto';
|
|
11
|
-
import 'jsonwebtoken';
|
|
12
|
-
import './extract-jwt.js';
|
|
13
|
-
import 'cookie';
|
|
14
|
-
|
|
15
|
-
declare const createJwtAuthzService: <P = unknown, U = unknown>([AUTHZ_PROVIDER, JWT_AUTHZ_OPTIONS, ALS_PROVIDER]: [any, any, any]) => _nestjs_common.Type<Omit<{
|
|
2
|
+
import { AuthzProviderClass } from '../authz.provider';
|
|
3
|
+
import { type DeepReadonly } from '../utils';
|
|
4
|
+
import type { JwtAlsType } from './jwt-authz-als.middleware';
|
|
5
|
+
import type { JwtAuthzOptions } from './jwt-authz.interface';
|
|
6
|
+
export declare const createJwtAuthzService: <P = unknown, U = unknown>([AUTHZ_PROVIDER, JWT_AUTHZ_OPTIONS, ALS_PROVIDER]: [any, any, any]) => import("@nestjs/common").Type<Omit<{
|
|
16
7
|
readonly authzProvider: AuthzProviderClass<P, U>;
|
|
17
8
|
readonly jwtAuthzOptions: JwtAuthzOptions;
|
|
18
9
|
readonly als: AsyncLocalStorage<JwtAlsType<U>>;
|
|
@@ -44,11 +35,9 @@ declare const createJwtAuthzService: <P = unknown, U = unknown>([AUTHZ_PROVIDER,
|
|
|
44
35
|
/**
|
|
45
36
|
* Sets a secure HTTP cookie with the given name, value, and optional cookie options.
|
|
46
37
|
*/
|
|
47
|
-
setCookie(name: string, value: string, options?: CookieOptionsWithSecret | undefined): void;
|
|
38
|
+
setCookie(name: string, value: string, options?: import("../utils").CookieOptionsWithSecret | undefined): void;
|
|
48
39
|
/**
|
|
49
40
|
* Retrieves the current user associated with the request, if available.
|
|
50
41
|
*/
|
|
51
42
|
getUser(): DeepReadonly<U> | undefined;
|
|
52
43
|
}, "als" | "jwtAuthzOptions" | "authzProvider">>;
|
|
53
|
-
|
|
54
|
-
export { createJwtAuthzService };
|
|
@@ -1,165 +1,119 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
9
|
-
var __export = (target, all) => {
|
|
10
|
-
for (var name in all)
|
|
11
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
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;
|
|
12
7
|
};
|
|
13
|
-
var
|
|
14
|
-
|
|
15
|
-
for (let key of __getOwnPropNames(from))
|
|
16
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
-
}
|
|
19
|
-
return to;
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
20
10
|
};
|
|
21
|
-
var
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Refreshes the JWT token for the provided user. If no user is provided, it attempts to retrieve the
|
|
106
|
-
* current user and generate a new token.
|
|
107
|
-
*
|
|
108
|
-
* @param [user] - User entity
|
|
109
|
-
* @returns
|
|
110
|
-
*/
|
|
111
|
-
async refresh(user) {
|
|
112
|
-
if (!this.jwtAuthzOptions.refresh) {
|
|
113
|
-
console.warn(`'refresh' method can only be called when configured in module options.`);
|
|
114
|
-
return void 0;
|
|
115
|
-
}
|
|
116
|
-
let userParams = user;
|
|
117
|
-
if (!user) {
|
|
118
|
-
const store = (0, import_utils.getAlsStore)(this.als);
|
|
119
|
-
if (store.jwtVerifiedBy !== import_constants.JwtValidationType.REFRESH) {
|
|
120
|
-
throw new import_errors.AuthzError(`InvocationError: Calling 'refresh' method without user parameter can only be called under @Refresh().`);
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
15
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
16
|
+
};
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.createJwtAuthzService = void 0;
|
|
19
|
+
const node_async_hooks_1 = require("node:async_hooks");
|
|
20
|
+
const common_1 = require("@nestjs/common");
|
|
21
|
+
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
22
|
+
const authz_provider_1 = require("../authz.provider");
|
|
23
|
+
const constants_1 = require("../constants");
|
|
24
|
+
const errors_1 = require("../errors");
|
|
25
|
+
const utils_1 = require("../utils");
|
|
26
|
+
const createJwtAuthzService = ([AUTHZ_PROVIDER, JWT_AUTHZ_OPTIONS, ALS_PROVIDER]) => {
|
|
27
|
+
let JwtAuthzService = class JwtAuthzService {
|
|
28
|
+
constructor(authzProvider, jwtAuthzOptions, als) {
|
|
29
|
+
this.authzProvider = authzProvider;
|
|
30
|
+
this.jwtAuthzOptions = jwtAuthzOptions;
|
|
31
|
+
this.als = als;
|
|
32
|
+
if (typeof this.authzProvider.createPayload !== 'function') {
|
|
33
|
+
throw new errors_1.AuthzError(`InternalError: Method 'createPayload' from abstract class 'AuthzProvider' must be implemented.`);
|
|
34
|
+
}
|
|
35
|
+
if (!jwtAuthzOptions.jwt?.sign) {
|
|
36
|
+
throw new errors_1.AuthzError(`InternalError: Missing JWT sign options.`);
|
|
37
|
+
}
|
|
38
|
+
if (this.jwtAuthzOptions.refresh && !this.jwtAuthzOptions.refresh.sign) {
|
|
39
|
+
throw new errors_1.AuthzError(`InternalError: Missing Refresh sign options.`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Creates a JWT token with a payload generated by AuthzProviderClass.createPayload(). Optionally, includes a refresh token if configured.
|
|
44
|
+
*
|
|
45
|
+
* @param user - User entity
|
|
46
|
+
* @returns
|
|
47
|
+
* - `token` : The generated JWT access token.
|
|
48
|
+
* - `refresh` (optional): The generated refresh token, if enabled.
|
|
49
|
+
*/
|
|
50
|
+
async logIn(user) {
|
|
51
|
+
const payload = (await this.authzProvider.createPayload(user));
|
|
52
|
+
const token = jsonwebtoken_1.default.sign(payload, this.jwtAuthzOptions.jwt.secretOrPrivateKey, this.jwtAuthzOptions.jwt.sign);
|
|
53
|
+
if (this.jwtAuthzOptions.refresh) {
|
|
54
|
+
const refresh = jsonwebtoken_1.default.sign({
|
|
55
|
+
data: (0, utils_1.encodeMsgpackrString)(payload)
|
|
56
|
+
}, this.jwtAuthzOptions.refresh.secretOrPrivateKey, this.jwtAuthzOptions.refresh.sign);
|
|
57
|
+
return {
|
|
58
|
+
token,
|
|
59
|
+
refresh
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
token
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Refreshes the JWT token for the provided user. If no user is provided, it attempts to retrieve the
|
|
68
|
+
* current user and generate a new token.
|
|
69
|
+
*
|
|
70
|
+
* @param [user] - User entity
|
|
71
|
+
* @returns
|
|
72
|
+
*/
|
|
73
|
+
async refresh(user) {
|
|
74
|
+
if (!this.jwtAuthzOptions.refresh) {
|
|
75
|
+
console.warn(`'refresh' method can only be called when configured in module options.`);
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
let userParams = user;
|
|
79
|
+
if (!user) {
|
|
80
|
+
const store = (0, utils_1.getAlsStore)(this.als);
|
|
81
|
+
if (store.jwtVerifiedBy !== constants_1.JwtValidationType.REFRESH) {
|
|
82
|
+
throw new errors_1.AuthzError(`InvocationError: Calling 'refresh' method without user parameter can only be called under @Refresh().`);
|
|
83
|
+
}
|
|
84
|
+
userParams = store.user;
|
|
85
|
+
}
|
|
86
|
+
if (!userParams) {
|
|
87
|
+
throw new errors_1.AuthzError(`ParameterError: User data is undefined.`);
|
|
88
|
+
}
|
|
89
|
+
const payload = (await this.authzProvider.createPayload(userParams));
|
|
90
|
+
const token = jsonwebtoken_1.default.sign(payload, this.jwtAuthzOptions.jwt.secretOrPrivateKey, this.jwtAuthzOptions.jwt.sign);
|
|
91
|
+
return {
|
|
92
|
+
token
|
|
93
|
+
};
|
|
121
94
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
}, __name(_a, "JwtAuthzService"), _a);
|
|
149
|
-
JwtAuthzService = _ts_decorate([
|
|
150
|
-
_ts_param(0, (0, import_common.Inject)(AUTHZ_PROVIDER)),
|
|
151
|
-
_ts_param(1, (0, import_common.Inject)(JWT_AUTHZ_OPTIONS)),
|
|
152
|
-
_ts_param(2, (0, import_common.Inject)(ALS_PROVIDER)),
|
|
153
|
-
_ts_metadata("design:type", Function),
|
|
154
|
-
_ts_metadata("design:paramtypes", [
|
|
155
|
-
typeof import_authz.AuthzProviderClass === "undefined" ? Object : import_authz.AuthzProviderClass,
|
|
156
|
-
typeof JwtAuthzOptions === "undefined" ? Object : JwtAuthzOptions,
|
|
157
|
-
typeof import_node_async_hooks.AsyncLocalStorage === "undefined" ? Object : import_node_async_hooks.AsyncLocalStorage
|
|
158
|
-
])
|
|
159
|
-
], JwtAuthzService);
|
|
160
|
-
return (0, import_common.mixin)(JwtAuthzService);
|
|
161
|
-
}, "createJwtAuthzService");
|
|
162
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
163
|
-
0 && (module.exports = {
|
|
164
|
-
createJwtAuthzService
|
|
165
|
-
});
|
|
95
|
+
/**
|
|
96
|
+
* Sets a secure HTTP cookie with the given name, value, and optional cookie options.
|
|
97
|
+
*/
|
|
98
|
+
setCookie(...rest) {
|
|
99
|
+
const store = (0, utils_1.getAlsStore)(this.als);
|
|
100
|
+
store.setCookie(...rest);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Retrieves the current user associated with the request, if available.
|
|
104
|
+
*/
|
|
105
|
+
getUser() {
|
|
106
|
+
const store = (0, utils_1.getAlsStore)(this.als);
|
|
107
|
+
const user = store.user;
|
|
108
|
+
return user;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
JwtAuthzService = __decorate([
|
|
112
|
+
__param(0, (0, common_1.Inject)(AUTHZ_PROVIDER)),
|
|
113
|
+
__param(1, (0, common_1.Inject)(JWT_AUTHZ_OPTIONS)),
|
|
114
|
+
__param(2, (0, common_1.Inject)(ALS_PROVIDER)),
|
|
115
|
+
__metadata("design:paramtypes", [authz_provider_1.AuthzProviderClass, Object, node_async_hooks_1.AsyncLocalStorage])
|
|
116
|
+
], JwtAuthzService);
|
|
117
|
+
return (0, common_1.mixin)(JwtAuthzService);
|
|
118
|
+
};
|
|
119
|
+
exports.createJwtAuthzService = createJwtAuthzService;
|
|
@@ -1,28 +1,16 @@
|
|
|
1
|
-
import * as _nestjs_common from '@nestjs/common';
|
|
2
1
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
3
|
-
import { Request } from 'express';
|
|
4
|
-
import { AuthzProviderClass } from '../authz.provider
|
|
5
|
-
import { JwtAlsType } from './jwt-authz-als.middleware
|
|
6
|
-
import
|
|
7
|
-
import '../utils/types.js';
|
|
8
|
-
import '@nestjs/common/interfaces';
|
|
9
|
-
import './jwt-authz.interface.js';
|
|
10
|
-
import 'crypto';
|
|
11
|
-
import 'jsonwebtoken';
|
|
12
|
-
import './extract-jwt.js';
|
|
13
|
-
import 'cookie';
|
|
14
|
-
|
|
15
|
-
declare const createJwtStrategy: ([JWT_STRATEGY, AUTHZ_PROVIDER, ALS_PROVIDER]: [string, any, any]) => _nestjs_common.Type<Omit<{
|
|
2
|
+
import type { Request } from 'express';
|
|
3
|
+
import { AuthzProviderClass } from '../authz.provider';
|
|
4
|
+
import type { JwtAlsType } from './jwt-authz-als.middleware';
|
|
5
|
+
export declare const createJwtStrategy: ([JWT_STRATEGY, AUTHZ_PROVIDER, ALS_PROVIDER]: [string, any, any]) => import("@nestjs/common").Type<Omit<{
|
|
16
6
|
readonly authzProvider: AuthzProviderClass<unknown, unknown>;
|
|
17
7
|
readonly als: AsyncLocalStorage<JwtAlsType<unknown>>;
|
|
18
8
|
validate(req: Request): Promise<{}>;
|
|
19
9
|
authenticate(req: Request, options?: any): any;
|
|
20
10
|
}, "als" | "authzProvider">>;
|
|
21
|
-
declare const createRefreshStrategy: ([JWT_REFRESH_STRATEGY, AUTHZ_PROVIDER, ALS_PROVIDER]: [string, any, any]) =>
|
|
11
|
+
export declare const createRefreshStrategy: ([JWT_REFRESH_STRATEGY, AUTHZ_PROVIDER, ALS_PROVIDER]: [string, any, any]) => import("@nestjs/common").Type<Omit<{
|
|
22
12
|
readonly authzProvider: AuthzProviderClass<unknown, unknown>;
|
|
23
13
|
readonly als: AsyncLocalStorage<JwtAlsType<unknown>>;
|
|
24
14
|
validate(req: Request): Promise<{}>;
|
|
25
15
|
authenticate(req: Request, options?: any): any;
|
|
26
16
|
}, "als" | "authzProvider">>;
|
|
27
|
-
|
|
28
|
-
export { createJwtStrategy, createRefreshStrategy };
|