@nestjs-kitchen/authz 3.0.1 → 4.0.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/README.md +27 -8
- package/dist/authz.provider.d.ts +1 -2
- package/dist/jwt/jwt-authz-als.middleware.d.ts +5 -9
- package/dist/jwt/jwt-authz-als.middleware.js +6 -9
- package/dist/jwt/jwt-authz.guard.d.ts +10 -23
- package/dist/jwt/jwt-authz.guard.js +93 -22
- package/dist/jwt/jwt-authz.module.d.ts +6 -13
- package/dist/jwt/jwt-authz.module.js +3 -17
- package/dist/jwt/jwt-authz.service.d.ts +3 -3
- package/dist/session/session-authz-als.middleware.d.ts +3 -5
- package/dist/session/session-authz-als.middleware.js +29 -50
- package/dist/session/session-authz.guard.d.ts +3 -10
- package/dist/session/session-authz.guard.js +33 -11
- package/dist/session/session-authz.module.d.ts +4 -11
- package/dist/session/session-authz.module.js +1 -13
- package/dist/session/session-authz.service.d.ts +2 -2
- package/dist/utils/adapter-shim.d.ts +28 -0
- package/dist/utils/adapter-shim.js +174 -0
- package/dist/utils/cookie-parsers.d.ts +6 -7
- package/dist/utils/cookie-parsers.js +10 -0
- package/dist/utils/create-set-cookie-fn.d.ts +2 -3
- package/dist/utils/create-set-cookie-fn.js +2 -1
- package/dist/utils/generics.d.ts +0 -1
- package/dist/utils/generics.js +1 -12
- package/dist/utils/get-passport-property.d.ts +1 -1
- package/dist/utils/get-passport-property.js +0 -1
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +2 -0
- package/dist/utils/safe-clone.d.ts +1 -0
- package/dist/utils/safe-clone.js +15 -0
- package/dist/utils/types.d.ts +0 -7
- package/package.json +24 -10
- package/dist/jwt/jwt-authz.strategy.d.ts +0 -17
- package/dist/jwt/jwt-authz.strategy.js +0 -134
- package/dist/session/session-authz.strategy.d.ts +0 -11
- package/dist/session/session-authz.strategy.js +0 -70
|
@@ -1,134 +0,0 @@
|
|
|
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;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
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.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");
|
|
29
|
-
const createJwtStrategy = ([JWT_STRATEGY, AUTHZ_PROVIDER, ALS_PROVIDER]) => {
|
|
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);
|
|
79
|
-
};
|
|
80
|
-
exports.createJwtStrategy = createJwtStrategy;
|
|
81
|
-
const createRefreshStrategy = ([JWT_REFRESH_STRATEGY, AUTHZ_PROVIDER, ALS_PROVIDER]) => {
|
|
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);
|
|
133
|
-
};
|
|
134
|
-
exports.createRefreshStrategy = createRefreshStrategy;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
-
import { type Type } from '@nestjs/common';
|
|
3
|
-
import type { Request } from 'express';
|
|
4
|
-
import { AuthzProviderClass } from '../authz.provider';
|
|
5
|
-
import type { SessionAlsType } from './session-authz-als.middleware';
|
|
6
|
-
export declare const createSessionAuthzStrategy: ([SESSION_STRATEGY, AUTHZ_PROVIDER, ALS_PROVIDER]: [string, any, any]) => Type<Omit<{
|
|
7
|
-
readonly authzProvider: AuthzProviderClass<unknown, unknown>;
|
|
8
|
-
readonly als: AsyncLocalStorage<SessionAlsType<unknown, unknown>>;
|
|
9
|
-
validate(req: Request): Promise<{}>;
|
|
10
|
-
authenticate(req: Request, options?: any): any;
|
|
11
|
-
}, "als" | "authzProvider">>;
|
|
@@ -1,70 +0,0 @@
|
|
|
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;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
-
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.createSessionAuthzStrategy = void 0;
|
|
16
|
-
const node_async_hooks_1 = require("node:async_hooks");
|
|
17
|
-
const common_1 = require("@nestjs/common");
|
|
18
|
-
const passport_1 = require("@nestjs/passport");
|
|
19
|
-
const passport_custom_1 = require("passport-custom");
|
|
20
|
-
const authz_provider_1 = require("../authz.provider");
|
|
21
|
-
const constants_1 = require("../constants");
|
|
22
|
-
const errors_1 = require("../errors");
|
|
23
|
-
const utils_1 = require("../utils");
|
|
24
|
-
const createSessionAuthzStrategy = ([SESSION_STRATEGY, AUTHZ_PROVIDER, ALS_PROVIDER]) => {
|
|
25
|
-
let SessionAuthzStrategy = class SessionAuthzStrategy extends (0, passport_1.PassportStrategy)(passport_custom_1.Strategy, SESSION_STRATEGY) {
|
|
26
|
-
constructor(authzProvider, als) {
|
|
27
|
-
super();
|
|
28
|
-
this.authzProvider = authzProvider;
|
|
29
|
-
this.als = als;
|
|
30
|
-
if (typeof this.authzProvider.authenticate !== 'function') {
|
|
31
|
-
throw new errors_1.AuthzError(`InternalError: Method 'authenticate' from abstract class 'AuthzProvider' must be implemented.`);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
async validate(req) {
|
|
35
|
-
const store = (0, utils_1.getAlsStore)(this.als);
|
|
36
|
-
const authOptions = store.authOptions;
|
|
37
|
-
// @ts-ignore
|
|
38
|
-
req[constants_1.PASSPORT_PROPERTY] = authOptions.passportProperty;
|
|
39
|
-
const payload = req?.session?.[constants_1.SESSION_PASSPORT_KEY]?.user;
|
|
40
|
-
if (!payload) {
|
|
41
|
-
return [null, new errors_1.AuthzAnonymousError('AnonymousError: Cannnot find session.')];
|
|
42
|
-
}
|
|
43
|
-
let user = undefined;
|
|
44
|
-
try {
|
|
45
|
-
user = await this.authzProvider.authenticate(payload, req);
|
|
46
|
-
}
|
|
47
|
-
catch (error) {
|
|
48
|
-
return [
|
|
49
|
-
null,
|
|
50
|
-
error instanceof Error
|
|
51
|
-
? new errors_1.AuthzVerificationError(`${error.name}: ${error.message}`, error)
|
|
52
|
-
: new errors_1.AuthzVerificationError(`${error}`)
|
|
53
|
-
];
|
|
54
|
-
}
|
|
55
|
-
store.user = user;
|
|
56
|
-
if (!user) {
|
|
57
|
-
return [null, new errors_1.AuthzAnonymousError('AnonymousError: Cannnot find user.')];
|
|
58
|
-
}
|
|
59
|
-
return user;
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
SessionAuthzStrategy = __decorate([
|
|
63
|
-
__param(0, (0, common_1.Inject)(AUTHZ_PROVIDER)),
|
|
64
|
-
__param(1, (0, common_1.Inject)(ALS_PROVIDER)),
|
|
65
|
-
__metadata("design:paramtypes", [authz_provider_1.AuthzProviderClass,
|
|
66
|
-
node_async_hooks_1.AsyncLocalStorage])
|
|
67
|
-
], SessionAuthzStrategy);
|
|
68
|
-
return (0, common_1.mixin)(SessionAuthzStrategy);
|
|
69
|
-
};
|
|
70
|
-
exports.createSessionAuthzStrategy = createSessionAuthzStrategy;
|