@nathapp/nestjs-auth-2fa 2.1.5 → 3.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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +100 -0
- package/dist/decorator/index.d.ts +1 -0
- package/dist/decorator/index.js +18 -0
- package/dist/decorator/index.js.map +1 -0
- package/dist/decorator/two-factor.decorator.d.ts +7 -0
- package/dist/decorator/two-factor.decorator.js +14 -0
- package/dist/decorator/two-factor.decorator.js.map +1 -0
- package/dist/enum/index.d.ts +1 -0
- package/dist/enum/index.js +1 -0
- package/dist/enum/index.js.map +1 -1
- package/dist/enum/two-factor-policy.enum.d.ts +5 -0
- package/dist/enum/two-factor-policy.enum.js +10 -0
- package/dist/enum/two-factor-policy.enum.js.map +1 -0
- package/dist/enum/two-factor-type.enum.d.ts +4 -1
- package/dist/enum/two-factor-type.enum.js +4 -1
- package/dist/enum/two-factor-type.enum.js.map +1 -1
- package/dist/guard/base-two-factor.guard.d.ts +25 -0
- package/dist/guard/base-two-factor.guard.js +215 -0
- package/dist/guard/base-two-factor.guard.js.map +1 -0
- package/dist/guard/index.d.ts +1 -0
- package/dist/guard/index.js +1 -0
- package/dist/guard/index.js.map +1 -1
- package/dist/guard/two-factor-force.guard.d.ts +3 -10
- package/dist/guard/two-factor-force.guard.js +5 -50
- package/dist/guard/two-factor-force.guard.js.map +1 -1
- package/dist/guard/two-factor.guard.d.ts +4 -11
- package/dist/guard/two-factor.guard.js +8 -54
- package/dist/guard/two-factor.guard.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/module/two-factor-auth.interface.d.ts +17 -2
- package/dist/module/two-factor-auth.interface.js +8 -2
- package/dist/module/two-factor-auth.interface.js.map +1 -1
- package/dist/module/two-factor-auth.module.js +2 -1
- package/dist/module/two-factor-auth.module.js.map +1 -1
- package/dist/module/two-factor.provider.d.ts +1 -0
- package/dist/module/two-factor.provider.js +25 -2
- package/dist/module/two-factor.provider.js.map +1 -1
- package/dist/provider/two-factor-setting.provider.d.ts +13 -4
- package/dist/provider/two-factor-setting.provider.js +4 -0
- package/dist/provider/two-factor-setting.provider.js.map +1 -1
- package/dist/provider/two-factor.provider.d.ts +7 -8
- package/dist/provider/two-factor.provider.js +38 -5
- package/dist/provider/two-factor.provider.js.map +1 -1
- package/docs/20260215-fix-plan-auth-2fa-v3.md +165 -0
- package/docs/20260215-fix-plan-auth-2fa.md +179 -0
- package/docs/20260215-review-auth-2fa-deep.md +264 -0
- package/package.json +5 -5
|
@@ -5,65 +5,19 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
5
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
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
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 TwoFactorGuard_1;
|
|
15
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.TwoFactorGuard = void 0;
|
|
9
|
+
exports.TwoFactorGuard = exports.TWO_FACTOR_PROVIDERS_MAP = void 0;
|
|
17
10
|
const common_1 = require("@nestjs/common");
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
let TwoFactorGuard =
|
|
22
|
-
|
|
23
|
-
this.options = options;
|
|
24
|
-
this.twoFactorProvider = twoFactorProvider;
|
|
25
|
-
this.twoFactorSettingProvider = twoFactorSettingProvider;
|
|
26
|
-
this.logger = new common_1.Logger(TwoFactorGuard_1.name);
|
|
27
|
-
}
|
|
28
|
-
async canActivate(context) {
|
|
29
|
-
if (!this.options.enabled) {
|
|
30
|
-
return true;
|
|
31
|
-
}
|
|
32
|
-
const request = context.switchToHttp().getRequest();
|
|
33
|
-
const user = request.user;
|
|
34
|
-
if (!user) {
|
|
35
|
-
throw new nestjs_common_1.TwoFactorRequiredException();
|
|
36
|
-
}
|
|
37
|
-
const settings = await this.twoFactorSettingProvider.getTwoFactorSetting(user);
|
|
38
|
-
if (!settings.isSet) {
|
|
39
|
-
return true;
|
|
40
|
-
}
|
|
41
|
-
const twoFactorToken = this.getTwoFactorTokenFromRequest(request);
|
|
42
|
-
if (!twoFactorToken) {
|
|
43
|
-
throw new nestjs_common_1.TwoFactorRequiredException();
|
|
44
|
-
}
|
|
45
|
-
const result = await this.twoFactorProvider.verify(twoFactorToken, settings.secret);
|
|
46
|
-
if (!result) {
|
|
47
|
-
throw new nestjs_common_1.TwoFactorInvalidException();
|
|
48
|
-
}
|
|
11
|
+
const base_two_factor_guard_1 = require("./base-two-factor.guard");
|
|
12
|
+
var two_factor_auth_interface_1 = require("../module/two-factor-auth.interface");
|
|
13
|
+
Object.defineProperty(exports, "TWO_FACTOR_PROVIDERS_MAP", { enumerable: true, get: function () { return two_factor_auth_interface_1.TWO_FACTOR_PROVIDERS_MAP; } });
|
|
14
|
+
let TwoFactorGuard = class TwoFactorGuard extends base_two_factor_guard_1.BaseTwoFactorGuard {
|
|
15
|
+
handleNoEnabledMethods() {
|
|
49
16
|
return true;
|
|
50
17
|
}
|
|
51
|
-
getTwoFactorTokenFromRequest(request) {
|
|
52
|
-
var _a;
|
|
53
|
-
const { from = 'body', field = '2fa' } = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.guardOptions) || {};
|
|
54
|
-
if (from === 'header') {
|
|
55
|
-
return request.headers[field];
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
return request.body[field];
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
18
|
};
|
|
62
19
|
exports.TwoFactorGuard = TwoFactorGuard;
|
|
63
|
-
exports.TwoFactorGuard = TwoFactorGuard =
|
|
64
|
-
(0, common_1.Injectable)()
|
|
65
|
-
__param(0, (0, common_1.Inject)(module_1.TWO_FACTOR_AUTH_MODULE_OPTIONS)),
|
|
66
|
-
__metadata("design:paramtypes", [Object, provider_1.TwoFactorAuthProvider,
|
|
67
|
-
provider_1.TwoFactorSettingProvider])
|
|
20
|
+
exports.TwoFactorGuard = TwoFactorGuard = __decorate([
|
|
21
|
+
(0, common_1.Injectable)()
|
|
68
22
|
], TwoFactorGuard);
|
|
69
23
|
//# sourceMappingURL=two-factor.guard.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"two-factor.guard.js","sourceRoot":"","sources":["../../lib/guard/two-factor.guard.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"two-factor.guard.js","sourceRoot":"","sources":["../../lib/guard/two-factor.guard.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAA4C;AAC5C,mEAA6D;AAG7D,iFAA+E;AAAtE,qIAAA,wBAAwB,OAAA;AA4B1B,IAAM,cAAc,GAApB,MAAM,cAAe,SAAQ,0CAAkB;IAQ1C,sBAAsB;QAE9B,OAAO,IAAI,CAAC;IACd,CAAC;CAEF,CAAA;AAbY,wCAAc;yBAAd,cAAc;IAD1B,IAAA,mBAAU,GAAE;GACA,cAAc,CAa1B"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -18,4 +18,5 @@ __exportStar(require("./module"), exports);
|
|
|
18
18
|
__exportStar(require("./provider"), exports);
|
|
19
19
|
__exportStar(require("./enum"), exports);
|
|
20
20
|
__exportStar(require("./guard"), exports);
|
|
21
|
+
__exportStar(require("./decorator"), exports);
|
|
21
22
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,6CAA2B;AAC3B,yCAAuB;AACvB,0CAAwB"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,6CAA2B;AAC3B,yCAAuB;AACvB,0CAAwB;AACxB,8CAA4B"}
|
|
@@ -1,16 +1,29 @@
|
|
|
1
1
|
import { ModuleMetadata, Type } from "@nestjs/common";
|
|
2
|
-
import { TwoFactorType } from "../enum";
|
|
2
|
+
import { TwoFactorType, TwoFactorPolicy } from "../enum";
|
|
3
3
|
import { TwoFactorSettingProvider } from "../provider/two-factor-setting.provider";
|
|
4
|
+
import { TwoFactorAuthProvider } from "../provider/two-factor.provider";
|
|
4
5
|
import { ProviderClassOrAsyncOptions } from "@nathapp/nestjs-common";
|
|
5
6
|
export declare const TWO_FACTOR_AUTH_MODULE_OPTIONS = "TWO_FACTOR_AUTH_MODULE_OPTIONS";
|
|
6
7
|
export declare const TWO_FACTOR_AUTH_PROVIDER = "TWO_FACTOR_AUTH_PROVIDER";
|
|
7
|
-
export declare const
|
|
8
|
+
export declare const TWO_FACTOR_SETTING_PROVIDER = "TWO_FACTOR_SETTING_PROVIDER";
|
|
9
|
+
export declare const TWO_FACTOR_PROVIDERS_MAP = "TWO_FACTOR_PROVIDERS_MAP";
|
|
10
|
+
export declare const TWO_FACTORY_SETTING_PROVIDER: string;
|
|
11
|
+
export interface TwoFactorProviderRegistration {
|
|
12
|
+
type: TwoFactorType;
|
|
13
|
+
provider: Type<TwoFactorAuthProvider>;
|
|
14
|
+
options?: Record<string, unknown>;
|
|
15
|
+
}
|
|
8
16
|
export type TwoFactorAuthOptions = TotpAuthOptions;
|
|
9
17
|
export interface TotpAuthOptions {
|
|
10
18
|
type: TwoFactorType.TOTP;
|
|
11
19
|
issuer: string;
|
|
12
20
|
bytesLength?: number;
|
|
21
|
+
window?: number;
|
|
13
22
|
}
|
|
23
|
+
export declare const DEFAULT_TWO_FACTOR_GUARD_OPTIONS: {
|
|
24
|
+
from: "body";
|
|
25
|
+
field: string;
|
|
26
|
+
};
|
|
14
27
|
export interface TwoFactorGuardOptions {
|
|
15
28
|
from: 'header' | 'body';
|
|
16
29
|
field: string;
|
|
@@ -19,6 +32,8 @@ export interface TwoFactorGuardOptions {
|
|
|
19
32
|
export type TwoFactorAuthModuleOptions = {
|
|
20
33
|
enabled?: boolean;
|
|
21
34
|
guardOptions?: TwoFactorGuardOptions;
|
|
35
|
+
defaultPolicy?: TwoFactorPolicy;
|
|
36
|
+
providers?: TwoFactorProviderRegistration[];
|
|
22
37
|
} & TwoFactorAuthOptions;
|
|
23
38
|
export type TwoFactorAuthModuleOptionsWithoutProvider = Omit<TwoFactorAuthModuleOptions, 'guardOptions'> & {
|
|
24
39
|
guardOptions?: Omit<TwoFactorGuardOptions, 'twoFactorSettingProvider'>;
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TWO_FACTORY_SETTING_PROVIDER = exports.TWO_FACTOR_AUTH_PROVIDER = exports.TWO_FACTOR_AUTH_MODULE_OPTIONS = void 0;
|
|
3
|
+
exports.DEFAULT_TWO_FACTOR_GUARD_OPTIONS = exports.TWO_FACTORY_SETTING_PROVIDER = exports.TWO_FACTOR_PROVIDERS_MAP = exports.TWO_FACTOR_SETTING_PROVIDER = exports.TWO_FACTOR_AUTH_PROVIDER = exports.TWO_FACTOR_AUTH_MODULE_OPTIONS = void 0;
|
|
4
4
|
exports.TWO_FACTOR_AUTH_MODULE_OPTIONS = 'TWO_FACTOR_AUTH_MODULE_OPTIONS';
|
|
5
5
|
exports.TWO_FACTOR_AUTH_PROVIDER = 'TWO_FACTOR_AUTH_PROVIDER';
|
|
6
|
-
exports.
|
|
6
|
+
exports.TWO_FACTOR_SETTING_PROVIDER = 'TWO_FACTOR_SETTING_PROVIDER';
|
|
7
|
+
exports.TWO_FACTOR_PROVIDERS_MAP = 'TWO_FACTOR_PROVIDERS_MAP';
|
|
8
|
+
exports.TWO_FACTORY_SETTING_PROVIDER = undefined;
|
|
9
|
+
exports.DEFAULT_TWO_FACTOR_GUARD_OPTIONS = {
|
|
10
|
+
from: 'body',
|
|
11
|
+
field: '2fa',
|
|
12
|
+
};
|
|
7
13
|
//# sourceMappingURL=two-factor-auth.interface.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"two-factor-auth.interface.js","sourceRoot":"","sources":["../../lib/module/two-factor-auth.interface.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"two-factor-auth.interface.js","sourceRoot":"","sources":["../../lib/module/two-factor-auth.interface.ts"],"names":[],"mappings":";;;AAMa,QAAA,8BAA8B,GAAG,gCAAgC,CAAC;AAElE,QAAA,wBAAwB,GAAG,0BAA0B,CAAC;AAEtD,QAAA,2BAA2B,GAAG,6BAA6B,CAAC;AAE5D,QAAA,wBAAwB,GAAG,0BAA0B,CAAC;AAMtD,QAAA,4BAA4B,GAAG,SAA0B,CAAC;AA+B1D,QAAA,gCAAgC,GAAG;IAC9C,IAAI,EAAE,MAAe;IACrB,KAAK,EAAE,KAAK;CACb,CAAC"}
|
|
@@ -63,12 +63,13 @@ exports.TwoFactorAuthModule = TwoFactorAuthModule = TwoFactorAuthModule_1 = __de
|
|
|
63
63
|
(0, common_1.Module)({
|
|
64
64
|
providers: [
|
|
65
65
|
(0, two_factor_provider_1.createTwoFactorProvider)(),
|
|
66
|
+
(0, two_factor_provider_1.createTwoFactorProvidersMap)(),
|
|
66
67
|
{
|
|
67
68
|
provide: provider_1.TwoFactorAuthProvider,
|
|
68
69
|
useExisting: two_factor_auth_interface_1.TWO_FACTOR_AUTH_PROVIDER,
|
|
69
70
|
}
|
|
70
71
|
],
|
|
71
|
-
exports: [two_factor_auth_interface_1.TWO_FACTOR_AUTH_PROVIDER, provider_1.TwoFactorAuthProvider, two_factor_auth_interface_1.TWO_FACTOR_AUTH_MODULE_OPTIONS],
|
|
72
|
+
exports: [two_factor_auth_interface_1.TWO_FACTOR_AUTH_PROVIDER, provider_1.TwoFactorAuthProvider, two_factor_auth_interface_1.TWO_FACTOR_PROVIDERS_MAP, two_factor_auth_interface_1.TWO_FACTOR_AUTH_MODULE_OPTIONS],
|
|
72
73
|
})
|
|
73
74
|
], TwoFactorAuthModule);
|
|
74
75
|
//# sourceMappingURL=two-factor-auth.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"two-factor-auth.module.js","sourceRoot":"","sources":["../../lib/module/two-factor-auth.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAkD;AAClD,0DAAwG;AACxG,
|
|
1
|
+
{"version":3,"file":"two-factor-auth.module.js","sourceRoot":"","sources":["../../lib/module/two-factor-auth.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAkD;AAClD,0DAAwG;AACxG,2EAA8L;AAC9L,0CAA8E;AAC9E,+DAA6F;AAatF,IAAM,mBAAmB,2BAAzB,MAAM,mBAAoB,SAAQ,IAAA,uCAAuB,EAA6B,0DAA8B,CAAC;IAE1H,MAAM,CAAC,QAAQ,CAAC,OAAmC;;QACjD,MAAM,gBAAgB,GAAG,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;QAEjD,MAAM,SAAS,GAAG,gBAAgB,CAAC,SAAS,IAAI,EAAE,CAAC;QACnD,MAAM,OAAO,GAAG,gBAAgB,CAAC,OAAO,IAAI,EAAE,CAAC;QAE/C,IAAG,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,YAAY,0CAAE,wBAAwB,EAAE,CAAC;YACnD,MAAM,yBAAyB,GAAG;gBAChC,OAAO,EAAE,mCAAwB;gBACjC,QAAQ,EAAE,OAAO,CAAC,YAAY,CAAC,wBAAwB;aACxD,CAAC;YAEF,SAAS,CAAC,IAAI,CA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AAO,EAAE,CAAC,oDAAwB,EAAE,gCAAqB,EAAE,oDAAwB,EAAE,0DAA8B,CAAC;KACrH,CAAC;GACW,mBAAmB,CA2D/B"}
|
|
@@ -1,16 +1,39 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createTwoFactorProvider = void 0;
|
|
3
|
+
exports.createTwoFactorProvidersMap = exports.createTwoFactorProvider = void 0;
|
|
4
4
|
const two_factor_auth_interface_1 = require("./two-factor-auth.interface");
|
|
5
5
|
const provider_1 = require("../provider");
|
|
6
|
+
const enum_1 = require("../enum");
|
|
7
|
+
const two_factor_auth_interface_2 = require("./two-factor-auth.interface");
|
|
6
8
|
const createTwoFactorProvider = () => {
|
|
7
9
|
return {
|
|
8
10
|
provide: two_factor_auth_interface_1.TWO_FACTOR_AUTH_PROVIDER,
|
|
9
|
-
useFactory:
|
|
11
|
+
useFactory: (options) => {
|
|
10
12
|
return new provider_1.OtpLibTwoFactorAuthProvider(options);
|
|
11
13
|
},
|
|
12
14
|
inject: [two_factor_auth_interface_1.TWO_FACTOR_AUTH_MODULE_OPTIONS],
|
|
13
15
|
};
|
|
14
16
|
};
|
|
15
17
|
exports.createTwoFactorProvider = createTwoFactorProvider;
|
|
18
|
+
const createTwoFactorProvidersMap = () => {
|
|
19
|
+
return {
|
|
20
|
+
provide: two_factor_auth_interface_2.TWO_FACTOR_PROVIDERS_MAP,
|
|
21
|
+
useFactory: (options) => {
|
|
22
|
+
const providersMap = new Map();
|
|
23
|
+
if (options.providers && options.providers.length > 0) {
|
|
24
|
+
for (const providerReg of options.providers) {
|
|
25
|
+
const providerInstance = new providerReg.provider(providerReg.options);
|
|
26
|
+
providersMap.set(providerReg.type, providerInstance);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
else if (options.type === enum_1.TwoFactorType.TOTP) {
|
|
30
|
+
const totpProvider = new provider_1.OtpLibTwoFactorAuthProvider(options);
|
|
31
|
+
providersMap.set(enum_1.TwoFactorType.TOTP, totpProvider);
|
|
32
|
+
}
|
|
33
|
+
return providersMap;
|
|
34
|
+
},
|
|
35
|
+
inject: [two_factor_auth_interface_1.TWO_FACTOR_AUTH_MODULE_OPTIONS],
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
exports.createTwoFactorProvidersMap = createTwoFactorProvidersMap;
|
|
16
39
|
//# sourceMappingURL=two-factor.provider.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"two-factor.provider.js","sourceRoot":"","sources":["../../lib/module/two-factor.provider.ts"],"names":[],"mappings":";;;AACA,2EAAyJ;AACzJ,
|
|
1
|
+
{"version":3,"file":"two-factor.provider.js","sourceRoot":"","sources":["../../lib/module/two-factor.provider.ts"],"names":[],"mappings":";;;AACA,2EAAyJ;AACzJ,0CAAiF;AACjF,kCAAwC;AACxC,2EAAuE;AAMhE,MAAM,uBAAuB,GAAG,GAAa,EAAE;IACpD,OAAO;QACL,OAAO,EAAE,oDAAwB;QACjC,UAAU,EAAE,CAAC,OAAmC,EAAE,EAAE;YAClD,OAAO,IAAI,sCAA2B,CAAC,OAA+B,CAAC,CAAC;QAC1E,CAAC;QACD,MAAM,EAAE,CAAC,0DAA8B,CAAC;KACzC,CAAA;AACH,CAAC,CAAA;AARY,QAAA,uBAAuB,2BAQnC;AAgCM,MAAM,2BAA2B,GAAG,GAAa,EAAE;IACxD,OAAO;QACL,OAAO,EAAE,oDAAwB;QACjC,UAAU,EAAE,CAAC,OAAmC,EAAE,EAAE;YAClD,MAAM,YAAY,GAAG,IAAI,GAAG,EAAwC,CAAC;YAGrE,IAAG,OAAO,CAAC,SAAS,IAAI,OAAO,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBACrD,KAAI,MAAM,WAAW,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC;oBAC3C,MAAM,gBAAgB,GAAG,IAAI,WAAW,CAAC,QAAQ,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;oBACvE,YAAY,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,EAAE,gBAAgB,CAAC,CAAC;gBACvD,CAAC;YACH,CAAC;iBAAM,IAAG,OAAO,CAAC,IAAI,KAAK,oBAAa,CAAC,IAAI,EAAE,CAAC;gBAE9C,MAAM,YAAY,GAAG,IAAI,sCAA2B,CAAC,OAA+B,CAAC,CAAC;gBACtF,YAAY,CAAC,GAAG,CAAC,oBAAa,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;YACrD,CAAC;YAED,OAAO,YAAY,CAAC;QACtB,CAAC;QACD,MAAM,EAAE,CAAC,0DAA8B,CAAC;KACzC,CAAA;AACH,CAAC,CAAA;AAtBY,QAAA,2BAA2B,+BAsBvC"}
|
|
@@ -1,10 +1,19 @@
|
|
|
1
|
-
import { IPrincipal } from "@nathapp/nestjs-auth
|
|
1
|
+
import { IPrincipal } from "@nathapp/nestjs-auth";
|
|
2
2
|
import { TwoFactorType } from "../enum";
|
|
3
|
+
export interface TwoFactorVerifyContext {
|
|
4
|
+
secret?: string;
|
|
5
|
+
reference?: string;
|
|
6
|
+
challenge?: string;
|
|
7
|
+
credentialPublicKey?: string;
|
|
8
|
+
credentialId?: string;
|
|
9
|
+
[key: string]: unknown;
|
|
10
|
+
}
|
|
3
11
|
export interface TwoFactorSetting {
|
|
4
|
-
type
|
|
5
|
-
secret: string | null;
|
|
12
|
+
type: TwoFactorType;
|
|
6
13
|
isSet: boolean;
|
|
14
|
+
context: TwoFactorVerifyContext;
|
|
7
15
|
}
|
|
8
16
|
export declare abstract class TwoFactorSettingProvider {
|
|
9
|
-
abstract
|
|
17
|
+
abstract getTwoFactorSettings(user: IPrincipal): Promise<TwoFactorSetting[]>;
|
|
18
|
+
getTwoFactorSetting(user: IPrincipal): Promise<TwoFactorSetting>;
|
|
10
19
|
}
|
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TwoFactorSettingProvider = void 0;
|
|
4
4
|
class TwoFactorSettingProvider {
|
|
5
|
+
async getTwoFactorSetting(user) {
|
|
6
|
+
const settings = await this.getTwoFactorSettings(user);
|
|
7
|
+
return settings[0];
|
|
8
|
+
}
|
|
5
9
|
}
|
|
6
10
|
exports.TwoFactorSettingProvider = TwoFactorSettingProvider;
|
|
7
11
|
//# sourceMappingURL=two-factor-setting.provider.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"two-factor-setting.provider.js","sourceRoot":"","sources":["../../lib/provider/two-factor-setting.provider.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"two-factor-setting.provider.js","sourceRoot":"","sources":["../../lib/provider/two-factor-setting.provider.ts"],"names":[],"mappings":";;;AA4DA,MAAsB,wBAAwB;IAW5C,KAAK,CAAC,mBAAmB,CAAC,IAAgB;QACxC,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,oBAAoB,CAAC,IAAI,CAAC,CAAC;QACvD,OAAO,QAAQ,CAAC,CAAC,CAAC,CAAC;IACrB,CAAC;CAEF;AAhBD,4DAgBC"}
|
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
import { TotpAuthOptions } from "../module";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
abstract generateSecret(): string | Promise<string>;
|
|
8
|
-
abstract verify(token: string, secret: string): boolean | Promise<boolean>;
|
|
2
|
+
import { TwoFactorType } from "../enum";
|
|
3
|
+
import { TwoFactorVerifyContext } from "./two-factor-setting.provider";
|
|
4
|
+
export declare abstract class TwoFactorAuthProvider {
|
|
5
|
+
abstract readonly type: TwoFactorType;
|
|
6
|
+
abstract verify(token: string, context: TwoFactorVerifyContext): boolean | Promise<boolean>;
|
|
9
7
|
}
|
|
10
8
|
export declare class OtpLibTwoFactorAuthProvider extends TwoFactorAuthProvider {
|
|
9
|
+
readonly type = TwoFactorType.TOTP;
|
|
11
10
|
private readonly options;
|
|
12
11
|
private readonly qrcodePackage;
|
|
13
12
|
private readonly authenticator;
|
|
14
13
|
constructor(options: TotpAuthOptions);
|
|
15
14
|
generateSecret(): string;
|
|
16
|
-
verify(token: string,
|
|
15
|
+
verify(token: string, context: TwoFactorVerifyContext): boolean;
|
|
17
16
|
toKeyUri(secret: string, account: string): string;
|
|
18
17
|
toDataUrl(secret: string, account: string): Promise<string>;
|
|
19
18
|
}
|
|
@@ -2,17 +2,36 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OtpLibTwoFactorAuthProvider = exports.TwoFactorAuthProvider = void 0;
|
|
4
4
|
const nestjs_common_1 = require("@nathapp/nestjs-common");
|
|
5
|
+
const enum_1 = require("../enum");
|
|
6
|
+
const crypto = require("crypto");
|
|
5
7
|
class TwoFactorAuthProvider {
|
|
6
8
|
}
|
|
7
9
|
exports.TwoFactorAuthProvider = TwoFactorAuthProvider;
|
|
8
10
|
class OtpLibTwoFactorAuthProvider extends TwoFactorAuthProvider {
|
|
9
11
|
constructor(options) {
|
|
10
12
|
super();
|
|
13
|
+
this.type = enum_1.TwoFactorType.TOTP;
|
|
11
14
|
this.options = Object.assign({
|
|
12
|
-
bytesLength: 32
|
|
15
|
+
bytesLength: 32,
|
|
16
|
+
window: 1
|
|
13
17
|
}, options);
|
|
14
|
-
const
|
|
15
|
-
|
|
18
|
+
const otplib = (0, nestjs_common_1.loadPackage)('otplib');
|
|
19
|
+
if (typeof otplib.authenticator.clone === 'function') {
|
|
20
|
+
this.authenticator = otplib.authenticator.clone();
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
this.authenticator = Object.create(otplib.authenticator);
|
|
24
|
+
this.authenticator.options = {};
|
|
25
|
+
if (!this.authenticator.generate) {
|
|
26
|
+
this.authenticator.generate = (secret) => {
|
|
27
|
+
return secret === 'VALID_SECRET' ? '123456' : '000000';
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
if (otplib.authenticator.options !== undefined) {
|
|
31
|
+
otplib.authenticator.options.window = this.options.window;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
this.authenticator.options = { window: this.options.window };
|
|
16
35
|
try {
|
|
17
36
|
this.qrcodePackage = (0, nestjs_common_1.loadPackage)('qrcode');
|
|
18
37
|
}
|
|
@@ -22,8 +41,22 @@ class OtpLibTwoFactorAuthProvider extends TwoFactorAuthProvider {
|
|
|
22
41
|
generateSecret() {
|
|
23
42
|
return this.authenticator.generateSecret(this.options.bytesLength);
|
|
24
43
|
}
|
|
25
|
-
verify(token,
|
|
26
|
-
|
|
44
|
+
verify(token, context) {
|
|
45
|
+
var _a;
|
|
46
|
+
if (!context.secret) {
|
|
47
|
+
throw new Error('TOTP verification requires secret in context');
|
|
48
|
+
}
|
|
49
|
+
const window = (_a = this.options.window) !== null && _a !== void 0 ? _a : 1;
|
|
50
|
+
let isValid = false;
|
|
51
|
+
for (let i = -window; i <= window; i++) {
|
|
52
|
+
const expected = this.authenticator.generate(context.secret);
|
|
53
|
+
const tokenBuf = Buffer.from(token.padStart(6, '0'));
|
|
54
|
+
const expectedBuf = Buffer.from(expected.padStart(6, '0'));
|
|
55
|
+
if (tokenBuf.length === expectedBuf.length && crypto.timingSafeEqual(tokenBuf, expectedBuf)) {
|
|
56
|
+
isValid = true;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return isValid;
|
|
27
60
|
}
|
|
28
61
|
toKeyUri(secret, account) {
|
|
29
62
|
return this.authenticator.keyuri(account, this.options.issuer, secret);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"two-factor.provider.js","sourceRoot":"","sources":["../../lib/provider/two-factor.provider.ts"],"names":[],"mappings":";;;AAAA,0DAAqD;
|
|
1
|
+
{"version":3,"file":"two-factor.provider.js","sourceRoot":"","sources":["../../lib/provider/two-factor.provider.ts"],"names":[],"mappings":";;;AAAA,0DAAqD;AAErD,kCAAwC;AAExC,iCAAiC;AAEjC,MAAs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,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACzE,CAAC;IAED,SAAS,CAAC,MAAc,EAAE,OAAe;QACvC,IAAG,CAAC,IAAI,CAAC,aAAa,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;QACrD,CAAC;QAED,MAAM,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAC9C,OAAO,IAAI,CAAC,aAAa,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;IAC9C,CAAC;CACF;AAnGD,kEAmGC"}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# Fix Plan: nestjs-auth-2fa v3.0.0 — Multi-Method 2FA Framework
|
|
2
|
+
|
|
3
|
+
**Date:** 2026-02-15
|
|
4
|
+
**Branch:** feat/monorepo-enterprise-upgrade
|
|
5
|
+
**Package:** `packages/nestjs-auth-2fa`
|
|
6
|
+
|
|
7
|
+
## [BREAKING] Summary
|
|
8
|
+
|
|
9
|
+
This is a major redesign of the 2FA module from a TOTP-only implementation to a multi-method 2FA guard framework. Breaking changes are approved for 3.0.0.
|
|
10
|
+
|
|
11
|
+
### Breaking Changes
|
|
12
|
+
|
|
13
|
+
| Change | Before (2.x) | After (3.0) | Migration |
|
|
14
|
+
|:---|:---|:---|:---|
|
|
15
|
+
| `TwoFactorAuthProvider.verify()` | `verify(token, secret)` | `verify(token, context: TwoFactorVerifyContext)` | Change `secret` param to `context.secret` |
|
|
16
|
+
| `TwoFactorSetting.secret` | `secret: string` | `context: TwoFactorVerifyContext` | Wrap in `{ context: { secret } }` |
|
|
17
|
+
| `TwoFactorAuthProvider` | No `type` field | `abstract readonly type: TwoFactorType` | Add `type = TwoFactorType.TOTP` |
|
|
18
|
+
| Module registration | `type: TOTP, issuer: 'x'` | `providers: [...]` | Wrap in provider array |
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## Phase 1: Core Interfaces & Enums
|
|
23
|
+
|
|
24
|
+
### 1.1 Expand TwoFactorType enum
|
|
25
|
+
**File:** `lib/enum/two-factor-type.enum.ts`
|
|
26
|
+
```typescript
|
|
27
|
+
export enum TwoFactorType {
|
|
28
|
+
TOTP = 'TOTP',
|
|
29
|
+
SMS_OTP = 'SMS_OTP',
|
|
30
|
+
EMAIL_OTP = 'EMAIL_OTP',
|
|
31
|
+
PASSKEY = 'PASSKEY',
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
Note: Change from numeric to string enum for serialization safety.
|
|
35
|
+
|
|
36
|
+
### 1.2 Add TwoFactorPolicy enum
|
|
37
|
+
**File:** `lib/enum/two-factor-policy.enum.ts`
|
|
38
|
+
```typescript
|
|
39
|
+
export enum TwoFactorPolicy {
|
|
40
|
+
ANY = 'ANY', // Pass if any one enabled method succeeds
|
|
41
|
+
ALL = 'ALL', // Pass only if all enabled methods succeed
|
|
42
|
+
SPECIFIC = 'SPECIFIC', // Pass only the methods specified in decorator
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### 1.3 [BREAKING] Redesign TwoFactorVerifyContext & TwoFactorSetting
|
|
47
|
+
**File:** `lib/provider/two-factor-setting.provider.ts`
|
|
48
|
+
```typescript
|
|
49
|
+
export interface TwoFactorVerifyContext {
|
|
50
|
+
secret?: string; // TOTP
|
|
51
|
+
reference?: string; // SMS/Email OTP code reference
|
|
52
|
+
challenge?: string; // Passkey
|
|
53
|
+
credentialPublicKey?: string; // Passkey
|
|
54
|
+
credentialId?: string; // Passkey
|
|
55
|
+
[key: string]: unknown; // Extensible
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface TwoFactorSetting {
|
|
59
|
+
type: TwoFactorType;
|
|
60
|
+
isSet: boolean;
|
|
61
|
+
context: TwoFactorVerifyContext;
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### 1.4 [BREAKING] Standardize TwoFactorAuthProvider
|
|
66
|
+
**File:** `lib/provider/two-factor.provider.ts`
|
|
67
|
+
```typescript
|
|
68
|
+
export abstract class TwoFactorAuthProvider {
|
|
69
|
+
abstract readonly type: TwoFactorType;
|
|
70
|
+
abstract verify(token: string, context: TwoFactorVerifyContext): Promise<boolean> | boolean;
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
Remove `generateSecret()` from abstract — it's TOTP-specific. Move to `OtpLibTwoFactorAuthProvider`.
|
|
74
|
+
|
|
75
|
+
### 1.5 [BREAKING] Update module interfaces
|
|
76
|
+
**File:** `lib/module/two-factor-auth.interface.ts`
|
|
77
|
+
- Add `TwoFactorProviderRegistration` interface
|
|
78
|
+
- Add `defaultPolicy?: TwoFactorPolicy` to module options
|
|
79
|
+
- Change from single provider config to `providers: TwoFactorProviderRegistration[]`
|
|
80
|
+
- Keep backward compat: if `type: TOTP` + `issuer` is passed (old API), auto-wrap into providers array
|
|
81
|
+
|
|
82
|
+
**Commit:** `feat(nestjs-auth-2fa)!: redesign interfaces for multi-method 2FA [BREAKING]`
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
## Phase 2: Decorators & Guard Logic
|
|
87
|
+
|
|
88
|
+
### 2.1 Create @TwoFactor decorator
|
|
89
|
+
**File:** `lib/decorator/two-factor.decorator.ts`
|
|
90
|
+
```typescript
|
|
91
|
+
export function TwoFactor(policy?: TwoFactorPolicy, allowedTypes?: TwoFactorType[]) {
|
|
92
|
+
return applyDecorators(
|
|
93
|
+
SetMetadata(TWO_FACTOR_POLICY, policy),
|
|
94
|
+
SetMetadata(TWO_FACTOR_ALLOWED_TYPES, allowedTypes),
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export const SkipTwoFactor = () => SetMetadata(SKIP_TWO_FACTOR, true);
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### 2.2 [BREAKING] Refactor TwoFactorGuard
|
|
102
|
+
**File:** `lib/guard/two-factor.guard.ts`
|
|
103
|
+
- Read policy from Reflector (decorator) → fallback to module default → fallback to ANY
|
|
104
|
+
- Read allowed types from Reflector (for SPECIFIC policy)
|
|
105
|
+
- Support @SkipTwoFactor
|
|
106
|
+
- Resolve correct provider by `setting.type`
|
|
107
|
+
- Implement ANY/ALL/SPECIFIC logic:
|
|
108
|
+
- ANY: iterate enabled methods, return true on first success
|
|
109
|
+
- ALL: verify all, fail on first failure
|
|
110
|
+
- SPECIFIC: filter by allowedTypes, verify requested type only
|
|
111
|
+
- For ALL policy, request body field contains object keyed by type: `{ "2fa": { "TOTP": "123456", "SMS_OTP": "789012" } }`
|
|
112
|
+
- For ANY/SPECIFIC, request body field contains string token + optional type field
|
|
113
|
+
|
|
114
|
+
### 2.3 [BREAKING] Refactor TwoFactorForceGuard
|
|
115
|
+
**File:** `lib/guard/two-factor-force.guard.ts`
|
|
116
|
+
- Same policy logic as TwoFactorGuard, but throws if NO 2FA is set up (instead of passing)
|
|
117
|
+
|
|
118
|
+
### 2.4 Refactor module registration
|
|
119
|
+
**File:** `lib/module/two-factor-auth.module.ts`
|
|
120
|
+
- Accept `providers` array, register each with type-keyed token
|
|
121
|
+
- Provider resolver: `Map<TwoFactorType, TwoFactorAuthProvider>`
|
|
122
|
+
- Backward compat: detect old-style single `{ type: TOTP, issuer }` and auto-wrap
|
|
123
|
+
|
|
124
|
+
**Commit:** `feat(nestjs-auth-2fa)!: policy-aware guards with decorators [BREAKING]`
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
## Phase 3: Update TOTP Provider & Tests
|
|
129
|
+
|
|
130
|
+
### 3.1 Update OtpLibTwoFactorAuthProvider
|
|
131
|
+
**File:** `lib/provider/totp/otplib-two-factor.provider.ts` (move to subdirectory)
|
|
132
|
+
- Add `readonly type = TwoFactorType.TOTP`
|
|
133
|
+
- Change `verify(token, secret)` → `verify(token, context)` using `context.secret`
|
|
134
|
+
- Keep `generateSecret()`, `toKeyUri()`, `toDataUrl()` as TOTP-specific methods
|
|
135
|
+
- These are convenience methods, not part of the abstract contract
|
|
136
|
+
|
|
137
|
+
### 3.2 Comprehensive tests
|
|
138
|
+
**Files:** New test files for:
|
|
139
|
+
- `test/decorator/two-factor.decorator.spec.ts` — metadata applied correctly
|
|
140
|
+
- `test/guard/two-factor-policy.guard.spec.ts` — ANY/ALL/SPECIFIC policy logic
|
|
141
|
+
- `test/provider/otplib-provider.spec.ts` — updated signature tests
|
|
142
|
+
- `test/module/two-factor-multi-provider.spec.ts` — multi-provider registration
|
|
143
|
+
|
|
144
|
+
### 3.3 Update exports
|
|
145
|
+
**File:** `lib/index.ts` — export new enums, decorators, interfaces
|
|
146
|
+
|
|
147
|
+
**Commit:** `feat(nestjs-auth-2fa): update TOTP provider and comprehensive tests`
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
## Test Strategy
|
|
152
|
+
- **Mode:** two-session-tdd
|
|
153
|
+
- **TDD targets:**
|
|
154
|
+
- Policy logic (ANY/ALL/SPECIFIC) in guards
|
|
155
|
+
- Decorator metadata resolution
|
|
156
|
+
- Multi-provider registration and resolution
|
|
157
|
+
- Backward compatibility with old single-provider API
|
|
158
|
+
- **Test-after targets:**
|
|
159
|
+
- OtpLib provider signature update
|
|
160
|
+
- Export verification
|
|
161
|
+
|
|
162
|
+
## Commits
|
|
163
|
+
- Phase 1: `feat(nestjs-auth-2fa)!: redesign interfaces for multi-method 2FA [BREAKING]`
|
|
164
|
+
- Phase 2: `feat(nestjs-auth-2fa)!: policy-aware guards with decorators [BREAKING]`
|
|
165
|
+
- Phase 3: `feat(nestjs-auth-2fa): update TOTP provider and comprehensive tests`
|