@my-nestjs-kit/auth 0.0.0-stage → 0.1.0-dev.20261129020438
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/LICENSE +21 -0
- package/README.md +27 -2
- package/dist/access-tokens/access-token.d.ts +13 -0
- package/dist/access-tokens/access-token.js +10 -0
- package/dist/access-tokens/access-tokens.module.d.ts +2 -0
- package/dist/access-tokens/access-tokens.module.js +22 -0
- package/dist/access-tokens/access-tokens.service.d.ts +16 -0
- package/dist/access-tokens/access-tokens.service.js +72 -0
- package/dist/access-tokens/access-tokens.types.d.ts +5 -0
- package/dist/access-tokens/access-tokens.types.js +9 -0
- package/dist/access-tokens/dto/access-token.dto.d.ts +13 -0
- package/dist/access-tokens/dto/access-token.dto.js +33 -0
- package/dist/auth-sessions/devices/dto/create-device.dto.d.ts +6 -0
- package/dist/auth-sessions/devices/dto/create-device.dto.js +33 -0
- package/dist/auth-sessions/dto/create-auth-session.dto.d.ts +8 -0
- package/dist/auth-sessions/dto/create-auth-session.dto.js +36 -0
- package/dist/auth.constants.d.ts +30 -0
- package/dist/auth.constants.js +36 -0
- package/dist/decorators/GetAuthParams.decorator.d.ts +34 -0
- package/dist/decorators/GetAuthParams.decorator.js +64 -0
- package/dist/decorators/GetSession.decorator.d.ts +2 -0
- package/dist/decorators/GetSession.decorator.js +13 -0
- package/dist/decorators/GetUser.decorator.d.ts +2 -0
- package/dist/decorators/GetUser.decorator.js +13 -0
- package/dist/decorators/Public.decorator.d.ts +3 -0
- package/dist/decorators/Public.decorator.js +18 -0
- package/dist/decorators/SetAuthConfig.decorator.d.ts +3 -0
- package/dist/decorators/SetAuthConfig.decorator.js +13 -0
- package/dist/dto/confirm-secure-action.dto.d.ts +8 -0
- package/dist/dto/confirm-secure-action.dto.js +46 -0
- package/dist/dto/log-in-by-email.dto.d.ts +16 -0
- package/dist/dto/log-in-by-email.dto.js +15 -0
- package/dist/dto/login-user-auth.dto.d.ts +4 -0
- package/dist/dto/login-user-auth.dto.js +29 -0
- package/dist/dto/verify-user-registration.dto.d.ts +4 -0
- package/dist/dto/verify-user-registration.dto.js +25 -0
- package/dist/entities/abstract-device-base.entity.d.ts +10 -0
- package/dist/entities/abstract-device-base.entity.js +77 -0
- package/dist/entities/abstract-o-auth-connection.entity.d.ts +28 -0
- package/dist/entities/abstract-o-auth-connection.entity.js +100 -0
- package/dist/entities/abstract-o-auth-profile.entity.d.ts +10 -0
- package/dist/entities/abstract-o-auth-profile.entity.js +38 -0
- package/dist/entities/auth-session-base.entity.d.ts +18 -0
- package/dist/entities/auth-session-base.entity.js +73 -0
- package/dist/guards/AbstractRefreshJwtToken.guard.d.ts +9 -0
- package/dist/guards/AbstractRefreshJwtToken.guard.js +45 -0
- package/dist/guards/abstract/AppAuthGuard-abstract.d.ts +219 -0
- package/dist/guards/abstract/AppAuthGuard-abstract.js +187 -0
- package/dist/guards/index.d.ts +0 -0
- package/dist/guards/index.js +0 -0
- package/dist/helper/base-auth-service.helper.d.ts +60 -0
- package/dist/helper/base-auth-service.helper.js +184 -0
- package/dist/helper/jsw-auth-controller-abstract.d.ts +33 -0
- package/dist/helper/jsw-auth-controller-abstract.js +89 -0
- package/dist/helper/jwt-auth-service-abstract.d.ts +40 -0
- package/dist/helper/jwt-auth-service-abstract.js +181 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +55 -0
- package/dist/jwt/index.d.ts +5 -0
- package/dist/jwt/index.js +21 -0
- package/dist/jwt/jwt-auth.module.d.ts +6 -0
- package/dist/jwt/jwt-auth.module.js +45 -0
- package/dist/jwt/jwt-auth.service.d.ts +33 -0
- package/dist/jwt/jwt-auth.service.js +170 -0
- package/dist/jwt/jwt-keys.helper.d.ts +12 -0
- package/dist/jwt/jwt-keys.helper.js +32 -0
- package/dist/jwt/types/enums.d.ts +19 -0
- package/dist/jwt/types/enums.js +26 -0
- package/dist/jwt/types/index.d.ts +2 -0
- package/dist/jwt/types/index.js +18 -0
- package/dist/jwt/types/jwt-auth.types.d.ts +44 -0
- package/dist/jwt/types/jwt-auth.types.js +2 -0
- package/dist/o-auth/abstract-o-auth.service.d.ts +30 -0
- package/dist/o-auth/abstract-o-auth.service.js +64 -0
- package/dist/o-auth/o-auth.namespace.d.ts +131 -0
- package/dist/o-auth/o-auth.namespace.js +256 -0
- package/dist/o-auth/sdk/base-ext-o-auth-sdk.d.ts +104 -0
- package/dist/o-auth/sdk/base-ext-o-auth-sdk.js +56 -0
- package/dist/route-auth-configs.helper.d.ts +29 -0
- package/dist/route-auth-configs.helper.js +36 -0
- package/dist/services/abstrac-auth-sessions.service.d.ts +28 -0
- package/dist/services/abstrac-auth-sessions.service.js +41 -0
- package/dist/services/abstract-sessions-repo.service.d.ts +28 -0
- package/dist/services/abstract-sessions-repo.service.js +28 -0
- package/dist/types/app-request.inteface.d.ts +14 -0
- package/dist/types/app-request.inteface.js +2 -0
- package/dist/types/auth.types.d.ts +43 -0
- package/dist/types/auth.types.js +12 -0
- package/dist/types/ws-client.types.d.ts +14 -0
- package/dist/types/ws-client.types.js +2 -0
- package/package.json +95 -3
|
@@ -0,0 +1,89 @@
|
|
|
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.JwtAuthControllerAbstract = exports.GetAuthVerificationTokens = void 0;
|
|
16
|
+
const core_1 = require("@my-nestjs-kit/core");
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const SetAuthConfig_decorator_1 = require("../decorators/SetAuthConfig.decorator");
|
|
19
|
+
const core_2 = require("@my-nestjs-kit/core");
|
|
20
|
+
const core_3 = require("@my-nestjs-kit/core");
|
|
21
|
+
class GetAuthVerificationTokens {
|
|
22
|
+
}
|
|
23
|
+
exports.GetAuthVerificationTokens = GetAuthVerificationTokens;
|
|
24
|
+
__decorate([
|
|
25
|
+
(0, core_3.IsEmailProperty)(undefined, { optional: false }),
|
|
26
|
+
__metadata("design:type", String)
|
|
27
|
+
], GetAuthVerificationTokens.prototype, "email", void 0);
|
|
28
|
+
/**
|
|
29
|
+
* Server-to-server verification endpoints (they return verification codes/tokens).
|
|
30
|
+
* `isServerAction` routes are denied by the kit auth guards unless the subclass adds a server guard,
|
|
31
|
+
* e.g. `@UseGuards(ServerActionGuard)` on an override — never expose them to end users.
|
|
32
|
+
*/
|
|
33
|
+
class JwtAuthControllerAbstract extends core_1.AbstractAppController {
|
|
34
|
+
async getVerificationInfo(query) {
|
|
35
|
+
const credentials = await this.authService.getVerificationCredentials(query);
|
|
36
|
+
return new core_2.HttpSuccessRes({
|
|
37
|
+
message: 'Verification tokens',
|
|
38
|
+
data: { ...query, ...credentials },
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
async setVerificationInfo(query) {
|
|
42
|
+
const credentials = await this.authService.setVerificationCredentials(query);
|
|
43
|
+
return new core_2.HttpSuccessRes({
|
|
44
|
+
message: 'Verification tokens',
|
|
45
|
+
data: { ...query, ...credentials },
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
async deleteVerificationInfo(query) {
|
|
49
|
+
const credentials = await this.authService.deleteVerificationCredentials(query);
|
|
50
|
+
return new core_2.HttpSuccessRes({
|
|
51
|
+
message: 'Verification tokens',
|
|
52
|
+
data: { ...query, ...credentials },
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.JwtAuthControllerAbstract = JwtAuthControllerAbstract;
|
|
57
|
+
__decorate([
|
|
58
|
+
(0, common_1.Get)(['verification-tokens', 'verification/credentials']),
|
|
59
|
+
(0, SetAuthConfig_decorator_1.SetAuthConfig)({
|
|
60
|
+
isPublic: true,
|
|
61
|
+
isServerAction: true,
|
|
62
|
+
}),
|
|
63
|
+
__param(0, (0, common_1.Query)()),
|
|
64
|
+
__metadata("design:type", Function),
|
|
65
|
+
__metadata("design:paramtypes", [GetAuthVerificationTokens]),
|
|
66
|
+
__metadata("design:returntype", Promise)
|
|
67
|
+
], JwtAuthControllerAbstract.prototype, "getVerificationInfo", null);
|
|
68
|
+
__decorate([
|
|
69
|
+
(0, common_1.Post)(['verification-tokens', 'verification/credentials']),
|
|
70
|
+
(0, SetAuthConfig_decorator_1.SetAuthConfig)({
|
|
71
|
+
isPublic: true,
|
|
72
|
+
isServerAction: true,
|
|
73
|
+
}),
|
|
74
|
+
__param(0, (0, common_1.Query)()),
|
|
75
|
+
__metadata("design:type", Function),
|
|
76
|
+
__metadata("design:paramtypes", [GetAuthVerificationTokens]),
|
|
77
|
+
__metadata("design:returntype", Promise)
|
|
78
|
+
], JwtAuthControllerAbstract.prototype, "setVerificationInfo", null);
|
|
79
|
+
__decorate([
|
|
80
|
+
(0, common_1.Delete)(['verification-tokens', 'verification/credentials']),
|
|
81
|
+
(0, SetAuthConfig_decorator_1.SetAuthConfig)({
|
|
82
|
+
isPublic: true,
|
|
83
|
+
isServerAction: true,
|
|
84
|
+
}),
|
|
85
|
+
__param(0, (0, common_1.Query)()),
|
|
86
|
+
__metadata("design:type", Function),
|
|
87
|
+
__metadata("design:paramtypes", [GetAuthVerificationTokens]),
|
|
88
|
+
__metadata("design:returntype", Promise)
|
|
89
|
+
], JwtAuthControllerAbstract.prototype, "deleteVerificationInfo", null);
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { AbstractAuthService, JwtTokensPayloadMap } from './base-auth-service.helper';
|
|
2
|
+
import { AccessTokensService } from '../access-tokens/access-tokens.service';
|
|
3
|
+
import { AccessTokenTypeEnum } from '../access-tokens/access-tokens.types';
|
|
4
|
+
import { PartialRecord } from '@my-nestjs-kit/core';
|
|
5
|
+
import { AccessToken } from '../access-tokens/access-token';
|
|
6
|
+
import type { JwtTokenPayloadBase as IUserAccessTokenPayload } from '../services/abstrac-auth-sessions.service';
|
|
7
|
+
import { AbstractAuthSessionRepoService } from '../services/abstract-sessions-repo.service';
|
|
8
|
+
import { AuthSessionBaseEntity } from '../entities/auth-session-base.entity';
|
|
9
|
+
export type VerificationTrackerQuery = {
|
|
10
|
+
_id?: string;
|
|
11
|
+
email?: string;
|
|
12
|
+
code?: string;
|
|
13
|
+
};
|
|
14
|
+
export type ValidateUserByBearerTokenReturn<Session extends AuthSessionBaseEntity> = {
|
|
15
|
+
user: Session['user'];
|
|
16
|
+
session?: Session;
|
|
17
|
+
tokenPayload: IUserAccessTokenPayload;
|
|
18
|
+
};
|
|
19
|
+
export declare abstract class JwtAuthServiceAbstract<Session extends AuthSessionBaseEntity, TokensPayload extends JwtTokensPayloadMap = JwtTokensPayloadMap> extends AbstractAuthService<TokensPayload> {
|
|
20
|
+
protected abstract readonly accessTokensSrv: AccessTokensService;
|
|
21
|
+
protected readonly credentialTypes: AccessTokenTypeEnum[];
|
|
22
|
+
protected abstract VERIFICATION_NSPS_KEY: string;
|
|
23
|
+
protected abstract sessionsRepoSrv: AbstractAuthSessionRepoService<Session>;
|
|
24
|
+
hashPasswordAsync(password: string): Promise<string>;
|
|
25
|
+
comparePasswordsAsync(password: string, passwordHash?: string): Promise<void>;
|
|
26
|
+
verification: import("@my-nestjs-kit/core").FromEntries<(readonly [AccessTokenTypeEnum, {
|
|
27
|
+
readonly get: (query: VerificationTrackerQuery) => Promise<AccessToken>;
|
|
28
|
+
readonly set: (query: VerificationTrackerQuery, value: string) => Promise<AccessToken>;
|
|
29
|
+
readonly del: (query: VerificationTrackerQuery) => Promise<{
|
|
30
|
+
result: boolean;
|
|
31
|
+
}>;
|
|
32
|
+
}])[]>;
|
|
33
|
+
getVerificationCredentials(query: VerificationTrackerQuery): Promise<PartialRecord<AccessToken['type'], AccessToken['value']>>;
|
|
34
|
+
setVerificationCredentials(query: VerificationTrackerQuery): Promise<PartialRecord<AccessToken['type'], AccessToken['value']>>;
|
|
35
|
+
deleteVerificationCredentials(query: VerificationTrackerQuery): Promise<PartialRecord<AccessToken['type'], boolean>>;
|
|
36
|
+
validateUserByToken<Input = any, Return extends ValidateUserByBearerTokenReturn<Session> = ValidateUserByBearerTokenReturn<Session>>(bearerToken: string, sessionInfo?: {
|
|
37
|
+
ip?: string;
|
|
38
|
+
userAgent?: string;
|
|
39
|
+
}): Promise<Return>;
|
|
40
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.JwtAuthServiceAbstract = void 0;
|
|
27
|
+
const base_auth_service_helper_1 = require("./base-auth-service.helper");
|
|
28
|
+
const core_1 = require("@my-nestjs-kit/core");
|
|
29
|
+
const access_tokens_types_1 = require("../access-tokens/access-tokens.types");
|
|
30
|
+
const _ = __importStar(require("lodash"));
|
|
31
|
+
const bcryptjs_1 = require("bcryptjs");
|
|
32
|
+
const common_1 = require("@nestjs/common");
|
|
33
|
+
const auth_constants_1 = require("../auth.constants");
|
|
34
|
+
const core_2 = require("@my-nestjs-kit/core");
|
|
35
|
+
class JwtAuthServiceAbstract extends base_auth_service_helper_1.AbstractAuthService {
|
|
36
|
+
constructor() {
|
|
37
|
+
super(...arguments);
|
|
38
|
+
this.credentialTypes = (0, core_1.ObjectValues)(access_tokens_types_1.AccessTokenTypeEnum);
|
|
39
|
+
this.verification = (0, core_1.ObjectFromEntries)(this.credentialTypes.map((type) => {
|
|
40
|
+
return [
|
|
41
|
+
type,
|
|
42
|
+
{
|
|
43
|
+
get: (query) => this.accessTokensSrv.get({
|
|
44
|
+
namespace: this.VERIFICATION_NSPS_KEY,
|
|
45
|
+
queries: {
|
|
46
|
+
...query,
|
|
47
|
+
type,
|
|
48
|
+
},
|
|
49
|
+
}),
|
|
50
|
+
set: (query, value) => this.accessTokensSrv.set({
|
|
51
|
+
namespace: this.VERIFICATION_NSPS_KEY,
|
|
52
|
+
queries: {
|
|
53
|
+
...query,
|
|
54
|
+
type,
|
|
55
|
+
},
|
|
56
|
+
data: {
|
|
57
|
+
type,
|
|
58
|
+
value,
|
|
59
|
+
},
|
|
60
|
+
}),
|
|
61
|
+
del: (query) => this.accessTokensSrv.delete({
|
|
62
|
+
namespace: this.VERIFICATION_NSPS_KEY,
|
|
63
|
+
queries: {
|
|
64
|
+
...query,
|
|
65
|
+
type,
|
|
66
|
+
},
|
|
67
|
+
}),
|
|
68
|
+
},
|
|
69
|
+
];
|
|
70
|
+
}));
|
|
71
|
+
}
|
|
72
|
+
async hashPasswordAsync(password) {
|
|
73
|
+
const salt = (0, bcryptjs_1.genSaltSync)(10);
|
|
74
|
+
return (0, bcryptjs_1.hash)(password, salt);
|
|
75
|
+
}
|
|
76
|
+
async comparePasswordsAsync(password, passwordHash) {
|
|
77
|
+
if (!passwordHash) {
|
|
78
|
+
this.fail({
|
|
79
|
+
message: 'User credentials not found',
|
|
80
|
+
statusCode: common_1.HttpStatus.UNAUTHORIZED,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
const isCorrectPassword = await (0, bcryptjs_1.compare)(password, passwordHash);
|
|
84
|
+
if (!isCorrectPassword) {
|
|
85
|
+
this.fail({
|
|
86
|
+
message: auth_constants_1.WRONG_CREDENTIALS_ERROR,
|
|
87
|
+
statusCode: common_1.HttpStatus.UNAUTHORIZED,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
async getVerificationCredentials(query) {
|
|
92
|
+
const credentials = await Promise.all(this.credentialTypes.map(async (type) => {
|
|
93
|
+
const control = this.verification[type];
|
|
94
|
+
if (!control)
|
|
95
|
+
return null;
|
|
96
|
+
const token = await control.get(_.pick(query, 'email'));
|
|
97
|
+
return {
|
|
98
|
+
[type]: token.value,
|
|
99
|
+
};
|
|
100
|
+
}));
|
|
101
|
+
return Object.assign({ ...query }, ...credentials);
|
|
102
|
+
}
|
|
103
|
+
async setVerificationCredentials(query) {
|
|
104
|
+
const verification_token = await this.signTokenAsync('VERIFICATION', {
|
|
105
|
+
_id: query._id,
|
|
106
|
+
});
|
|
107
|
+
const values = {
|
|
108
|
+
token: verification_token,
|
|
109
|
+
code: this.accessTokensSrv.generateCode(),
|
|
110
|
+
};
|
|
111
|
+
const credentials = await Promise.all(this.credentialTypes.map(async (type) => {
|
|
112
|
+
const control = this.verification[type];
|
|
113
|
+
if (!control)
|
|
114
|
+
return {};
|
|
115
|
+
const token = await control.set(_.pick(query, 'email'), values[type]);
|
|
116
|
+
return {
|
|
117
|
+
[type]: token.value,
|
|
118
|
+
};
|
|
119
|
+
}));
|
|
120
|
+
return Object.assign({ ...query }, ...credentials);
|
|
121
|
+
}
|
|
122
|
+
async deleteVerificationCredentials(query) {
|
|
123
|
+
const res = await Promise.all(this.credentialTypes.map(async (type) => {
|
|
124
|
+
const res = await this.verification[type].del(_.pick(query, 'email'));
|
|
125
|
+
return { [type]: res.result };
|
|
126
|
+
}));
|
|
127
|
+
return Object.assign({}, ...res);
|
|
128
|
+
}
|
|
129
|
+
async validateUserByToken(bearerToken, sessionInfo) {
|
|
130
|
+
const message = 'Login by bearer token failed';
|
|
131
|
+
try {
|
|
132
|
+
const [bearer, token] = bearerToken.split(' ');
|
|
133
|
+
if (bearer !== 'Bearer') {
|
|
134
|
+
this.fail({
|
|
135
|
+
message,
|
|
136
|
+
reason: 'Token is not valid as "Bearer token"',
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
const tokenPayload = await this.verifyTokenAsync('ACCESS', token);
|
|
140
|
+
const session = await this.sessionsRepoSrv.findOne({
|
|
141
|
+
access_token: token,
|
|
142
|
+
userId: tokenPayload._id,
|
|
143
|
+
}, { getUser: true, credentials: true });
|
|
144
|
+
if (!session) {
|
|
145
|
+
this.fail({ reason: 'Session not found' });
|
|
146
|
+
}
|
|
147
|
+
const checks = {
|
|
148
|
+
'Session not found': () => !session,
|
|
149
|
+
'Token not found': () => !session?.access_token,
|
|
150
|
+
'Invalid token': () => session?.access_token !== token,
|
|
151
|
+
};
|
|
152
|
+
for (const [reason, canFail] of (0, core_1.ObjectEntries)(checks)) {
|
|
153
|
+
if (canFail()) {
|
|
154
|
+
console.log({
|
|
155
|
+
statusCode: common_1.HttpStatus.UNAUTHORIZED,
|
|
156
|
+
// description,
|
|
157
|
+
reason,
|
|
158
|
+
});
|
|
159
|
+
this.fail({
|
|
160
|
+
statusCode: common_1.HttpStatus.UNAUTHORIZED,
|
|
161
|
+
// description,
|
|
162
|
+
reason,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
user: session.user,
|
|
168
|
+
session,
|
|
169
|
+
tokenPayload,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
catch (e) {
|
|
173
|
+
if (e instanceof core_2.AppHttpException) {
|
|
174
|
+
throw e;
|
|
175
|
+
}
|
|
176
|
+
console.log('validateUserByToken', e);
|
|
177
|
+
this.fail({ reason: message });
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
exports.JwtAuthServiceAbstract = JwtAuthServiceAbstract;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export * from './jwt';
|
|
2
|
+
export * from './types/auth.types';
|
|
3
|
+
export * from './types/app-request.inteface';
|
|
4
|
+
export * from './types/ws-client.types';
|
|
5
|
+
export * from './route-auth-configs.helper';
|
|
6
|
+
export * from './decorators/GetAuthParams.decorator';
|
|
7
|
+
export * from './decorators/SetAuthConfig.decorator';
|
|
8
|
+
export * from './decorators/Public.decorator';
|
|
9
|
+
export * from './decorators/GetUser.decorator';
|
|
10
|
+
export * from './decorators/GetSession.decorator';
|
|
11
|
+
export * from './entities/auth-session-base.entity';
|
|
12
|
+
export * from './entities/abstract-device-base.entity';
|
|
13
|
+
export * from './entities/abstract-o-auth-connection.entity';
|
|
14
|
+
export * from './entities/abstract-o-auth-profile.entity';
|
|
15
|
+
export * from './helper/base-auth-service.helper';
|
|
16
|
+
export * from './helper/jwt-auth-service-abstract';
|
|
17
|
+
export * from './helper/jsw-auth-controller-abstract';
|
|
18
|
+
export * from './services/abstract-sessions-repo.service';
|
|
19
|
+
export * from './services/abstrac-auth-sessions.service';
|
|
20
|
+
export * from './access-tokens/access-token';
|
|
21
|
+
export * from './access-tokens/access-tokens.types';
|
|
22
|
+
export * from './access-tokens/access-tokens.service';
|
|
23
|
+
export * from './access-tokens/access-tokens.module';
|
|
24
|
+
export * from './o-auth/o-auth.namespace';
|
|
25
|
+
export * from './o-auth/sdk/base-ext-o-auth-sdk';
|
|
26
|
+
export * from './o-auth/abstract-o-auth.service';
|
|
27
|
+
export * from './guards/abstract/AppAuthGuard-abstract';
|
|
28
|
+
export * from './guards/AbstractRefreshJwtToken.guard';
|
|
29
|
+
export * from './dto/log-in-by-email.dto';
|
|
30
|
+
export * from './dto/verify-user-registration.dto';
|
|
31
|
+
export * from './auth.constants';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @my-nestjs-kit/auth — JWT engine, auth/session service bases, guards, auth decorators and request auth context.
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
15
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
16
|
+
};
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
// JWT engine
|
|
19
|
+
__exportStar(require("./jwt"), exports);
|
|
20
|
+
// request auth context, route auth configs
|
|
21
|
+
__exportStar(require("./types/auth.types"), exports);
|
|
22
|
+
__exportStar(require("./types/app-request.inteface"), exports);
|
|
23
|
+
__exportStar(require("./types/ws-client.types"), exports);
|
|
24
|
+
__exportStar(require("./route-auth-configs.helper"), exports);
|
|
25
|
+
__exportStar(require("./decorators/GetAuthParams.decorator"), exports);
|
|
26
|
+
__exportStar(require("./decorators/SetAuthConfig.decorator"), exports);
|
|
27
|
+
__exportStar(require("./decorators/Public.decorator"), exports);
|
|
28
|
+
__exportStar(require("./decorators/GetUser.decorator"), exports);
|
|
29
|
+
__exportStar(require("./decorators/GetSession.decorator"), exports);
|
|
30
|
+
// entities
|
|
31
|
+
__exportStar(require("./entities/auth-session-base.entity"), exports);
|
|
32
|
+
__exportStar(require("./entities/abstract-device-base.entity"), exports);
|
|
33
|
+
__exportStar(require("./entities/abstract-o-auth-connection.entity"), exports);
|
|
34
|
+
__exportStar(require("./entities/abstract-o-auth-profile.entity"), exports);
|
|
35
|
+
// services
|
|
36
|
+
__exportStar(require("./helper/base-auth-service.helper"), exports);
|
|
37
|
+
__exportStar(require("./helper/jwt-auth-service-abstract"), exports);
|
|
38
|
+
__exportStar(require("./helper/jsw-auth-controller-abstract"), exports);
|
|
39
|
+
__exportStar(require("./services/abstract-sessions-repo.service"), exports);
|
|
40
|
+
__exportStar(require("./services/abstrac-auth-sessions.service"), exports);
|
|
41
|
+
__exportStar(require("./access-tokens/access-token"), exports);
|
|
42
|
+
__exportStar(require("./access-tokens/access-tokens.types"), exports);
|
|
43
|
+
__exportStar(require("./access-tokens/access-tokens.service"), exports);
|
|
44
|
+
__exportStar(require("./access-tokens/access-tokens.module"), exports);
|
|
45
|
+
// oauth
|
|
46
|
+
__exportStar(require("./o-auth/o-auth.namespace"), exports);
|
|
47
|
+
__exportStar(require("./o-auth/sdk/base-ext-o-auth-sdk"), exports);
|
|
48
|
+
__exportStar(require("./o-auth/abstract-o-auth.service"), exports);
|
|
49
|
+
// guards
|
|
50
|
+
__exportStar(require("./guards/abstract/AppAuthGuard-abstract"), exports);
|
|
51
|
+
__exportStar(require("./guards/AbstractRefreshJwtToken.guard"), exports);
|
|
52
|
+
// dto
|
|
53
|
+
__exportStar(require("./dto/log-in-by-email.dto"), exports);
|
|
54
|
+
__exportStar(require("./dto/verify-user-registration.dto"), exports);
|
|
55
|
+
__exportStar(require("./auth.constants"), exports);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./types/enums"), exports);
|
|
18
|
+
__exportStar(require("./types/jwt-auth.types"), exports);
|
|
19
|
+
__exportStar(require("./jwt-keys.helper"), exports);
|
|
20
|
+
__exportStar(require("./jwt-auth.service"), exports);
|
|
21
|
+
__exportStar(require("./jwt-auth.module"), exports);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { JwtAuthModuleAsyncOptions, JwtAuthModuleOptions } from './types/jwt-auth.types';
|
|
3
|
+
export declare class JwtAuthModule {
|
|
4
|
+
static forRoot(options: JwtAuthModuleOptions, isGlobal?: boolean): DynamicModule;
|
|
5
|
+
static forRootAsync(options: JwtAuthModuleAsyncOptions): DynamicModule;
|
|
6
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
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 JwtAuthModule_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.JwtAuthModule = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const jwt_auth_service_1 = require("./jwt-auth.service");
|
|
13
|
+
let JwtAuthModule = JwtAuthModule_1 = class JwtAuthModule {
|
|
14
|
+
static forRoot(options, isGlobal = true) {
|
|
15
|
+
return {
|
|
16
|
+
module: JwtAuthModule_1,
|
|
17
|
+
global: isGlobal,
|
|
18
|
+
providers: [
|
|
19
|
+
{ provide: jwt_auth_service_1.JWT_AUTH_MODULE_OPTIONS, useValue: options },
|
|
20
|
+
jwt_auth_service_1.JwtAuthService,
|
|
21
|
+
],
|
|
22
|
+
exports: [jwt_auth_service_1.JwtAuthService],
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
static forRootAsync(options) {
|
|
26
|
+
return {
|
|
27
|
+
module: JwtAuthModule_1,
|
|
28
|
+
global: options.isGlobal ?? true,
|
|
29
|
+
imports: options.imports,
|
|
30
|
+
providers: [
|
|
31
|
+
{
|
|
32
|
+
provide: jwt_auth_service_1.JWT_AUTH_MODULE_OPTIONS,
|
|
33
|
+
useFactory: options.useFactory,
|
|
34
|
+
inject: options.inject ?? [],
|
|
35
|
+
},
|
|
36
|
+
jwt_auth_service_1.JwtAuthService,
|
|
37
|
+
],
|
|
38
|
+
exports: [jwt_auth_service_1.JwtAuthService],
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
exports.JwtAuthModule = JwtAuthModule;
|
|
43
|
+
exports.JwtAuthModule = JwtAuthModule = JwtAuthModule_1 = __decorate([
|
|
44
|
+
(0, common_1.Module)({})
|
|
45
|
+
], JwtAuthModule);
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import * as jwt from 'jsonwebtoken';
|
|
2
|
+
import { JwtType, JwtTypeEnum } from './types/enums';
|
|
3
|
+
import { JwtAuthModuleOptions, JwtSignParams, JwtStandardClaims, JwtVerifyParams } from './types/jwt-auth.types';
|
|
4
|
+
export declare const JWT_AUTH_MODULE_OPTIONS: unique symbol;
|
|
5
|
+
/** token type as the string key or the enum member (same runtime value) */
|
|
6
|
+
export type JwtTypeInput = JwtType | JwtTypeEnum;
|
|
7
|
+
/**
|
|
8
|
+
* Typed JWT sign/verify per token type (ACCESS / REFRESH / VERIFICATION / ...).
|
|
9
|
+
* - key rotation: tokens carry `kid`; verification picks the key by `kid` (verify-only keys stay valid)
|
|
10
|
+
* - every token carries a `typ` claim, checked on verify (an access token is never accepted as refresh)
|
|
11
|
+
* - `aud` / `iss` are enforced when configured
|
|
12
|
+
* - only HMAC algorithms; any verify failure is a 401 AppHttpException
|
|
13
|
+
*/
|
|
14
|
+
export declare class JwtAuthService {
|
|
15
|
+
private readonly options;
|
|
16
|
+
private readonly _logger;
|
|
17
|
+
private readonly _types;
|
|
18
|
+
constructor(options: JwtAuthModuleOptions);
|
|
19
|
+
private resolveType;
|
|
20
|
+
private getType;
|
|
21
|
+
hasType(type: JwtTypeInput): boolean;
|
|
22
|
+
signAsync<Payload extends object>(type: JwtTypeInput, payload: Payload, params?: JwtSignParams): Promise<string>;
|
|
23
|
+
verify<Payload extends object = Record<string, any>>(type: JwtTypeInput, token: string, params?: JwtVerifyParams): Payload & JwtStandardClaims;
|
|
24
|
+
verifyAsync<Payload extends object = Record<string, any>>(type: JwtTypeInput, token: string, params?: JwtVerifyParams): Promise<Payload & JwtStandardClaims>;
|
|
25
|
+
/** decodes without verifying — never trust the result for auth decisions */
|
|
26
|
+
decode<Payload extends object = Record<string, any>>(token: string): jwt.Jwt & {
|
|
27
|
+
payload: Payload & JwtStandardClaims;
|
|
28
|
+
};
|
|
29
|
+
private verifyOptions;
|
|
30
|
+
private pickKey;
|
|
31
|
+
private checkType;
|
|
32
|
+
private unauthorized;
|
|
33
|
+
}
|