@nestjs-kitchen/authz 2.0.3 → 2.0.5
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 +3 -4
- package/dist/authz.provider.js +27 -43
- package/dist/constants.d.ts +6 -8
- package/dist/constants.js +13 -46
- package/dist/errors.d.ts +3 -5
- package/dist/errors.js +29 -44
- 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 +136 -152
- 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 +40 -60
- package/dist/jwt/jwt-authz.guard.d.ts +8 -21
- package/dist/jwt/jwt-authz.guard.js +126 -148
- package/dist/jwt/jwt-authz.interface.d.ts +16 -25
- package/dist/jwt/jwt-authz.interface.js +66 -114
- package/dist/jwt/jwt-authz.module.d.ts +19 -31
- package/dist/jwt/jwt-authz.module.js +240 -242
- package/dist/jwt/jwt-authz.service.d.ts +6 -17
- package/dist/jwt/jwt-authz.service.js +113 -144
- package/dist/jwt/jwt-authz.strategy.d.ts +5 -17
- package/dist/jwt/jwt-authz.strategy.js +127 -150
- 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 +95 -109
- package/dist/session/session-authz.guard.d.ts +6 -15
- package/dist/session/session-authz.guard.js +96 -116
- package/dist/session/session-authz.interface.d.ts +5 -12
- package/dist/session/session-authz.interface.js +18 -40
- package/dist/session/session-authz.module.d.ts +18 -26
- package/dist/session/session-authz.module.js +231 -237
- package/dist/session/session-authz.service.d.ts +5 -13
- package/dist/session/session-authz.service.js +66 -83
- package/dist/session/session-authz.strategy.d.ts +4 -12
- package/dist/session/session-authz.strategy.js +65 -78
- package/dist/user.decorator.d.ts +3 -6
- package/dist/user.decorator.js +25 -33
- package/dist/utils/cookie-parsers.d.ts +3 -6
- package/dist/utils/cookie-parsers.js +62 -63
- package/dist/utils/create-authz-decorator-factory.d.ts +3 -9
- package/dist/utils/create-authz-decorator-factory.js +29 -50
- package/dist/utils/create-set-cookie-fn.d.ts +3 -9
- package/dist/utils/create-set-cookie-fn.js +21 -42
- package/dist/utils/generics.d.ts +4 -6
- package/dist/utils/generics.js +31 -54
- package/dist/utils/get-allow-anonymous.d.ts +2 -9
- package/dist/utils/get-allow-anonymous.js +6 -27
- package/dist/utils/get-als-store.d.ts +2 -5
- package/dist/utils/get-als-store.js +10 -32
- package/dist/utils/get-context-authz-meta-params-list.d.ts +2 -9
- package/dist/utils/get-context-authz-meta-params-list.js +15 -33
- package/dist/utils/get-passport-property.d.ts +1 -3
- package/dist/utils/get-passport-property.js +7 -28
- 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 +13 -38
- package/dist/utils/msgpackrs.d.ts +2 -4
- package/dist/utils/msgpackrs.js +9 -32
- package/dist/utils/types.d.ts +23 -26
- package/dist/utils/types.js +2 -15
- package/package.json +4 -4
|
@@ -1,150 +1,119 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__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;
|
|
10
7
|
};
|
|
11
|
-
var
|
|
12
|
-
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
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);
|
|
18
10
|
};
|
|
19
|
-
var
|
|
20
|
-
|
|
21
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
-
mod
|
|
26
|
-
));
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
var __decorateClass = (decorators, target, key, kind) => {
|
|
29
|
-
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
30
|
-
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
31
|
-
if (decorator = decorators[i])
|
|
32
|
-
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
33
|
-
if (kind && result) __defProp(target, key, result);
|
|
34
|
-
return result;
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
35
13
|
};
|
|
36
|
-
var
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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");
|
|
47
26
|
const createJwtAuthzService = ([AUTHZ_PROVIDER, JWT_AUTHZ_OPTIONS, ALS_PROVIDER]) => {
|
|
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
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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
|
+
};
|
|
94
|
+
}
|
|
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;
|
|
112
109
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
token
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Sets a secure HTTP cookie with the given name, value, and optional cookie options.
|
|
126
|
-
*/
|
|
127
|
-
setCookie(...rest) {
|
|
128
|
-
const store = (0, import_utils.getAlsStore)(this.als);
|
|
129
|
-
store.setCookie(...rest);
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Retrieves the current user associated with the request, if available.
|
|
133
|
-
*/
|
|
134
|
-
getUser() {
|
|
135
|
-
const store = (0, import_utils.getAlsStore)(this.als);
|
|
136
|
-
const user = store.user;
|
|
137
|
-
return user;
|
|
138
|
-
}
|
|
139
|
-
};
|
|
140
|
-
JwtAuthzService = __decorateClass([
|
|
141
|
-
__decorateParam(0, (0, import_common.Inject)(AUTHZ_PROVIDER)),
|
|
142
|
-
__decorateParam(1, (0, import_common.Inject)(JWT_AUTHZ_OPTIONS)),
|
|
143
|
-
__decorateParam(2, (0, import_common.Inject)(ALS_PROVIDER))
|
|
144
|
-
], JwtAuthzService);
|
|
145
|
-
return (0, import_common.mixin)(JwtAuthzService);
|
|
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);
|
|
146
118
|
};
|
|
147
|
-
|
|
148
|
-
0 && (module.exports = {
|
|
149
|
-
createJwtAuthzService
|
|
150
|
-
});
|
|
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 };
|
|
@@ -1,157 +1,134 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__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;
|
|
10
7
|
};
|
|
11
|
-
var
|
|
12
|
-
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
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);
|
|
18
10
|
};
|
|
19
|
-
var
|
|
20
|
-
|
|
21
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
-
mod
|
|
26
|
-
));
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
var __decorateClass = (decorators, target, key, kind) => {
|
|
29
|
-
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
30
|
-
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
31
|
-
if (decorator = decorators[i])
|
|
32
|
-
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
33
|
-
if (kind && result) __defProp(target, key, result);
|
|
34
|
-
return result;
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
35
13
|
};
|
|
36
|
-
var
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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.createRefreshStrategy = exports.createJwtStrategy = void 0;
|
|
19
|
+
const node_async_hooks_1 = require("node:async_hooks");
|
|
20
|
+
const common_1 = require("@nestjs/common");
|
|
21
|
+
const passport_1 = require("@nestjs/passport");
|
|
22
|
+
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
23
|
+
const passport_custom_1 = require("passport-custom");
|
|
24
|
+
const authz_provider_1 = require("../authz.provider");
|
|
25
|
+
const constants_1 = require("../constants");
|
|
26
|
+
const errors_1 = require("../errors");
|
|
27
|
+
const utils_1 = require("../utils");
|
|
28
|
+
const extract_jwt_1 = require("./extract-jwt");
|
|
51
29
|
const createJwtStrategy = ([JWT_STRATEGY, AUTHZ_PROVIDER, ALS_PROVIDER]) => {
|
|
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
|
-
|
|
30
|
+
let JwtStrategy = class JwtStrategy extends (0, passport_1.PassportStrategy)(passport_custom_1.Strategy, JWT_STRATEGY) {
|
|
31
|
+
constructor(authzProvider, als) {
|
|
32
|
+
super();
|
|
33
|
+
this.authzProvider = authzProvider;
|
|
34
|
+
this.als = als;
|
|
35
|
+
if (typeof this.authzProvider.authenticate !== 'function') {
|
|
36
|
+
throw new errors_1.AuthzError(`InternalError: Method 'authenticate' from abstract class 'AuthzProvider' must be implemented.`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async validate(req) {
|
|
40
|
+
const store = (0, utils_1.getAlsStore)(this.als);
|
|
41
|
+
const authOptions = store.authOptions;
|
|
42
|
+
if (!authOptions.jwt.verify) {
|
|
43
|
+
return [null, new errors_1.AuthzError(`InternalError: Refresh verify options must be implemented.`)];
|
|
44
|
+
}
|
|
45
|
+
const extractor = extract_jwt_1.ExtractJwt.fromExtractors(authOptions.jwt.jwtFromRequest);
|
|
46
|
+
req[constants_1.PASSPORT_PROPERTY] = authOptions.passportProperty;
|
|
47
|
+
const token = extractor(req);
|
|
48
|
+
if (!token) {
|
|
49
|
+
return [null, new errors_1.AuthzAnonymousError('AnonymousError: Cannnot find token.')];
|
|
50
|
+
}
|
|
51
|
+
let user = undefined;
|
|
52
|
+
try {
|
|
53
|
+
const payload = jsonwebtoken_1.default.verify(token, authOptions.jwt.secretOrPublicKey, authOptions.jwt.verify);
|
|
54
|
+
user = await this.authzProvider.authenticate(payload, req);
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
return [
|
|
58
|
+
null,
|
|
59
|
+
error instanceof Error
|
|
60
|
+
? new errors_1.AuthzVerificationError(`${error.name}: ${error.message}`, error)
|
|
61
|
+
: new errors_1.AuthzVerificationError(`${error}`)
|
|
62
|
+
];
|
|
63
|
+
}
|
|
64
|
+
store.user = user;
|
|
65
|
+
store.jwtVerifiedBy = constants_1.JwtValidationType.JWT;
|
|
66
|
+
if (!user) {
|
|
67
|
+
return [null, new errors_1.AuthzAnonymousError('AnonymousError: Cannnot find user.')];
|
|
68
|
+
}
|
|
69
|
+
return user;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
JwtStrategy = __decorate([
|
|
73
|
+
__param(0, (0, common_1.Inject)(AUTHZ_PROVIDER)),
|
|
74
|
+
__param(1, (0, common_1.Inject)(ALS_PROVIDER)),
|
|
75
|
+
__metadata("design:paramtypes", [authz_provider_1.AuthzProviderClass,
|
|
76
|
+
node_async_hooks_1.AsyncLocalStorage])
|
|
77
|
+
], JwtStrategy);
|
|
78
|
+
return (0, common_1.mixin)(JwtStrategy);
|
|
98
79
|
};
|
|
80
|
+
exports.createJwtStrategy = createJwtStrategy;
|
|
99
81
|
const createRefreshStrategy = ([JWT_REFRESH_STRATEGY, AUTHZ_PROVIDER, ALS_PROVIDER]) => {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
return (0, import_common.mixin)(RefreshStrategy);
|
|
82
|
+
let RefreshStrategy = class RefreshStrategy extends (0, passport_1.PassportStrategy)(passport_custom_1.Strategy, JWT_REFRESH_STRATEGY) {
|
|
83
|
+
constructor(authzProvider, als) {
|
|
84
|
+
super();
|
|
85
|
+
this.authzProvider = authzProvider;
|
|
86
|
+
this.als = als;
|
|
87
|
+
if (typeof this.authzProvider.authenticate !== 'function') {
|
|
88
|
+
throw new errors_1.AuthzError(`InternalError: Method 'authenticate' from abstract class 'AuthzProvider' must be implemented.`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
async validate(req) {
|
|
92
|
+
const store = (0, utils_1.getAlsStore)(this.als);
|
|
93
|
+
const authOptions = store.authOptions;
|
|
94
|
+
if (!authOptions.refresh.verify) {
|
|
95
|
+
return [null, new errors_1.AuthzError(`InternalError: Refresh verify options must be implemented.`)];
|
|
96
|
+
}
|
|
97
|
+
const extractor = extract_jwt_1.ExtractJwt.fromExtractors(authOptions.refresh.jwtFromRequest);
|
|
98
|
+
req[constants_1.PASSPORT_PROPERTY] = authOptions.passportProperty;
|
|
99
|
+
const token = extractor(req);
|
|
100
|
+
if (!token) {
|
|
101
|
+
return [null, new errors_1.AuthzAnonymousError('AnonymousError: Cannnot find token.')];
|
|
102
|
+
}
|
|
103
|
+
let user = undefined;
|
|
104
|
+
try {
|
|
105
|
+
const payload = jsonwebtoken_1.default.verify(token, authOptions.refresh.secretOrPublicKey, authOptions.refresh.verify);
|
|
106
|
+
const decodePayload = (0, utils_1.decodeMsgpackrString)(payload.data);
|
|
107
|
+
user = await this.authzProvider.authenticate(decodePayload, req);
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
return [
|
|
111
|
+
null,
|
|
112
|
+
error instanceof Error
|
|
113
|
+
? new errors_1.AuthzVerificationError(`${error.name}: ${error.message}`, error)
|
|
114
|
+
: new errors_1.AuthzVerificationError(`${error}`)
|
|
115
|
+
];
|
|
116
|
+
}
|
|
117
|
+
store.user = user;
|
|
118
|
+
store.jwtVerifiedBy = constants_1.JwtValidationType.REFRESH;
|
|
119
|
+
if (!user) {
|
|
120
|
+
return [null, new errors_1.AuthzAnonymousError('AnonymousError: Cannnot find user.')];
|
|
121
|
+
}
|
|
122
|
+
return user;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
RefreshStrategy = __decorate([
|
|
126
|
+
(0, common_1.Injectable)(),
|
|
127
|
+
__param(0, (0, common_1.Inject)(AUTHZ_PROVIDER)),
|
|
128
|
+
__param(1, (0, common_1.Inject)(ALS_PROVIDER)),
|
|
129
|
+
__metadata("design:paramtypes", [authz_provider_1.AuthzProviderClass,
|
|
130
|
+
node_async_hooks_1.AsyncLocalStorage])
|
|
131
|
+
], RefreshStrategy);
|
|
132
|
+
return (0, common_1.mixin)(RefreshStrategy);
|
|
152
133
|
};
|
|
153
|
-
|
|
154
|
-
0 && (module.exports = {
|
|
155
|
-
createJwtStrategy,
|
|
156
|
-
createRefreshStrategy
|
|
157
|
-
});
|
|
134
|
+
exports.createRefreshStrategy = createRefreshStrategy;
|
package/dist/session/index.d.ts
CHANGED
|
@@ -1,12 +1 @@
|
|
|
1
|
-
export { cereateSessionAuthzModule } from './session-authz.module
|
|
2
|
-
import '@nestjs/core';
|
|
3
|
-
import './session-authz.interface.js';
|
|
4
|
-
import 'express-session';
|
|
5
|
-
import '../utils/types.js';
|
|
6
|
-
import '@nestjs/common';
|
|
7
|
-
import '@nestjs/common/interfaces';
|
|
8
|
-
import 'express';
|
|
9
|
-
import '../authz.provider.js';
|
|
10
|
-
import 'node:async_hooks';
|
|
11
|
-
import '../errors.js';
|
|
12
|
-
import './session-authz-als.middleware.js';
|
|
1
|
+
export { cereateSessionAuthzModule } from './session-authz.module';
|