@wabot-dev/framework 0.1.0-beta.5 → 0.1.0-beta.51

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.
Files changed (177) hide show
  1. package/dist/src/addon/async/pg/PgJobRepository.js +26 -0
  2. package/dist/src/addon/auth/api-key/@apiKeyGuard.js +17 -0
  3. package/dist/src/addon/auth/api-key/ApiKey.js +53 -0
  4. package/dist/src/addon/auth/api-key/ApiKeyGuardMiddleware.js +45 -0
  5. package/dist/src/addon/auth/api-key/ApiKeyRepository.js +13 -0
  6. package/dist/src/addon/auth/api-key/PgApiKeyRepository.js +21 -0
  7. package/dist/src/addon/auth/jwt/@jwtConnectionGuard.js +16 -0
  8. package/dist/src/addon/auth/jwt/@jwtGuard.js +17 -0
  9. package/dist/src/addon/auth/jwt/Jwt.js +53 -0
  10. package/dist/src/addon/auth/jwt/JwtAccessAndRefreshTokenDto.js +20 -0
  11. package/dist/src/addon/auth/jwt/JwtConfig.js +28 -0
  12. package/dist/src/addon/auth/jwt/JwtConnectionGuardMiddleware.js +57 -0
  13. package/dist/src/addon/auth/jwt/JwtGuardMiddleware.js +45 -0
  14. package/dist/src/addon/auth/jwt/JwtRefreshToken.js +56 -0
  15. package/dist/src/addon/auth/jwt/JwtRefreshTokenRepository.js +25 -0
  16. package/dist/src/addon/auth/jwt/JwtSigner.js +36 -0
  17. package/dist/src/addon/auth/jwt/JwtTokenDto.js +22 -0
  18. package/dist/src/addon/auth/jwt/PgJwtRefreshTokenRepository.js +21 -0
  19. package/dist/src/addon/chat-bot/anthropic/AnthropicChatAdapter.js +135 -0
  20. package/dist/src/addon/chat-bot/deepseek/DeepSeekChatAdapter.js +137 -0
  21. package/dist/src/addon/chat-bot/google/GoogleChatAdapter.js +145 -0
  22. package/dist/src/addon/chat-bot/openia/OpenaiChatAdapter.js +117 -0
  23. package/dist/src/{pre-made/repository/chat → addon/chat-bot}/pg/PgChatMemory.js +6 -4
  24. package/dist/src/{pre-made/repository/chat → addon/chat-bot}/pg/PgChatRepository.js +6 -5
  25. package/dist/src/{pre-made/repository/chat → addon/chat-bot}/ram/RamChatRepository.js +2 -2
  26. package/dist/src/addon/chat-bot/wabot/WabotChatAdapter.js +41 -0
  27. package/dist/src/addon/chat-controller/cmd/@cmd.js +23 -0
  28. package/dist/src/{channels → addon/chat-controller}/cmd/CmdChannel.js +4 -26
  29. package/dist/src/{channels → addon/chat-controller}/socket/@socket.js +9 -4
  30. package/dist/src/{channels → addon/chat-controller}/socket/SocketChannel.js +5 -23
  31. package/dist/src/{channels → addon/chat-controller}/socket/SocketChannelConfig.js +1 -1
  32. package/dist/src/{channels → addon/chat-controller}/telegram/@telegram.js +9 -4
  33. package/dist/src/{channels → addon/chat-controller}/telegram/TelegramChannel.js +4 -22
  34. package/dist/src/addon/chat-controller/whatsapp/@whatsApp.js +25 -0
  35. package/dist/src/{channels → addon/chat-controller}/whatsapp/EnvWhatsAppRepository.js +3 -3
  36. package/dist/src/{channels → addon/chat-controller}/whatsapp/PgWhatsAppRepository.js +2 -2
  37. package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsApp.js +2 -4
  38. package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppChannel.js +6 -18
  39. package/dist/src/addon/chat-controller/whatsapp/WhatsAppReceiver.js +10 -0
  40. package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppSender.js +20 -41
  41. package/dist/src/addon/chat-controller/whatsapp/cloud-api/WhatsAppReceiverByCloudApi.js +97 -0
  42. package/dist/src/{channels/whatsapp → addon/chat-controller/whatsapp/cloud-api}/WhatsAppSenderByCloudApi.js +26 -20
  43. package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppProxyContracts.js +5 -0
  44. package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppReceiverByWabotProxy.js +65 -0
  45. package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppSenderByWabotProxy.js +60 -0
  46. package/dist/src/addon/chat-controller/whatsapp/proxy/WhatsAppWabotProxyConnection.js +45 -0
  47. package/dist/src/addon/mindset/html/HtmlModule.js +70 -0
  48. package/dist/src/core/auth/Auth.js +24 -0
  49. package/dist/src/core/{Persistent.js → entity/Entity.js} +24 -9
  50. package/dist/src/core/env/Env.js +39 -0
  51. package/dist/src/core/error/CustomError.js +15 -0
  52. package/dist/src/core/injection/index.js +4 -0
  53. package/dist/src/core/mapper/Mapper.js +42 -0
  54. package/dist/src/core/password/Password.js +30 -0
  55. package/dist/src/core/random/Random.js +47 -0
  56. package/dist/src/core/storable/Storable.js +8 -0
  57. package/dist/src/core/validation/core/validateArray.js +51 -0
  58. package/dist/src/core/validation/core/validateIsOptional.js +5 -0
  59. package/dist/src/core/validation/core/validateModel.js +36 -0
  60. package/dist/src/core/validation/metadata/@isArray.js +18 -0
  61. package/dist/src/core/validation/metadata/@isModel.js +18 -0
  62. package/dist/src/core/validation/metadata/@isOptional.js +17 -0
  63. package/dist/src/core/validation/metadata/ValidationMetadataStore.js +98 -0
  64. package/dist/src/core/validation/modelInfo.js +9 -0
  65. package/dist/src/core/validation/validate.js +11 -0
  66. package/dist/src/core/validation/validators/is-boolean/@isBoolean.js +17 -0
  67. package/dist/src/core/validation/validators/is-boolean/validateIsBoolean.js +12 -0
  68. package/dist/src/core/validation/validators/is-date/@isDate.js +17 -0
  69. package/dist/src/core/validation/validators/is-date/validateIsDate.js +16 -0
  70. package/dist/src/core/validation/validators/is-not-empty/@isNotEmpty.js +17 -0
  71. package/dist/src/core/validation/validators/is-not-empty/validateIsNotEmpty.js +12 -0
  72. package/dist/src/core/validation/validators/is-number/@isNumber.js +17 -0
  73. package/dist/src/core/validation/validators/is-number/validateIsNumber.js +12 -0
  74. package/dist/src/core/validation/validators/is-present/@isPresent.js +17 -0
  75. package/dist/src/core/validation/validators/is-present/validateIsPresent.js +12 -0
  76. package/dist/src/core/validation/validators/is-string/@isString.js +17 -0
  77. package/dist/src/core/validation/validators/is-string/validateIsString.js +12 -0
  78. package/dist/src/core/validation/validators/max/@max.js +18 -0
  79. package/dist/src/core/validation/validators/max/validateMax.js +17 -0
  80. package/dist/src/core/validation/validators/min/@min.js +18 -0
  81. package/dist/src/core/validation/validators/min/validateMin.js +17 -0
  82. package/dist/src/feature/async/@command.js +11 -0
  83. package/dist/src/feature/async/@commandHandler.js +12 -0
  84. package/dist/src/feature/async/Async.js +38 -0
  85. package/dist/src/feature/async/Command.js +9 -0
  86. package/dist/src/feature/async/CommandMetadataStore.js +38 -0
  87. package/dist/src/feature/async/Job.js +24 -0
  88. package/dist/src/feature/async/JobRepository.js +31 -0
  89. package/dist/src/feature/async/JobRunner.js +45 -0
  90. package/dist/src/feature/async/JobsEventsHub.js +24 -0
  91. package/dist/src/feature/async/runCommandHandlers.js +29 -0
  92. package/dist/src/{core/chat → feature/chat-bot}/Chat.js +2 -2
  93. package/dist/src/feature/chat-bot/ChatAdapter.js +7 -0
  94. package/dist/src/feature/chat-bot/ChatBot.js +73 -0
  95. package/dist/src/feature/chat-bot/ChatItem.js +24 -0
  96. package/dist/src/feature/chat-bot/ChatMemory.js +10 -0
  97. package/dist/src/{core/chat/repository/IChatRepository.js → feature/chat-bot/ChatRepository.js} +2 -8
  98. package/dist/src/feature/chat-bot/IChatItem.js +3 -0
  99. package/dist/src/{chatbot → feature/chat-bot}/metadata/@chatBot.js +1 -1
  100. package/dist/src/{chatbot → feature/chat-bot}/metadata/ChatBotMetadataStore.js +1 -1
  101. package/dist/src/{controller/channel → feature/chat-controller}/ChatResolver.js +6 -4
  102. package/dist/src/{controller → feature/chat-controller}/metadata/ControllerMetadataStore.js +1 -1
  103. package/dist/src/{controller → feature/chat-controller}/metadata/controller/@chatController.js +1 -1
  104. package/dist/src/feature/chat-controller/runChatControllers.js +69 -0
  105. package/dist/src/{channels → feature}/express/ExpressProvider.js +2 -4
  106. package/dist/src/{channels → feature}/http/HttpServerProvider.js +2 -2
  107. package/dist/src/{mindset → feature/mindset}/IMindset.js +3 -0
  108. package/dist/src/{mindset → feature/mindset}/MindsetOperator.js +69 -42
  109. package/dist/src/{mindset → feature/mindset}/metadata/MindsetMetadataStore.js +1 -1
  110. package/dist/src/{mindset → feature/mindset}/metadata/functions/@mindsetFunction.js +1 -1
  111. package/dist/src/{mindset → feature/mindset}/metadata/mindsets/@mindset.js +1 -1
  112. package/dist/src/{mindset → feature/mindset}/metadata/modules/@mindsetModule.js +1 -2
  113. package/dist/src/{mindset → feature/mindset}/metadata/params/@param.js +1 -1
  114. package/dist/src/feature/money/Money.js +61 -0
  115. package/dist/src/feature/money/MoneyDto.js +22 -0
  116. package/dist/src/{repository → feature}/pg/PgCrudRepository.js +25 -2
  117. package/dist/src/{repository → feature}/pg/PgRepositoryBase.js +2 -2
  118. package/dist/src/feature/rest-controller/injection-tokens.js +4 -0
  119. package/dist/src/feature/rest-controller/metadata/@get.js +19 -0
  120. package/dist/src/feature/rest-controller/metadata/@middleware.js +16 -0
  121. package/dist/src/feature/rest-controller/metadata/@post.js +19 -0
  122. package/dist/src/feature/rest-controller/metadata/@restController.js +15 -0
  123. package/dist/src/feature/rest-controller/metadata/RestControllerMetadataStore.js +50 -0
  124. package/dist/src/feature/rest-controller/runRestControllers.js +102 -0
  125. package/dist/src/{channels → feature}/socket/SocketServerProvider.js +2 -2
  126. package/dist/src/feature/socket-controller/metadata/@connectionMiddleware.js +16 -0
  127. package/dist/src/feature/socket-controller/metadata/@socketConnection.js +18 -0
  128. package/dist/src/feature/socket-controller/metadata/@socketController.js +15 -0
  129. package/dist/src/feature/socket-controller/metadata/@socketEvent.js +18 -0
  130. package/dist/src/feature/socket-controller/metadata/SocketControllerMetadataStore.js +65 -0
  131. package/dist/src/feature/socket-controller/runSocketControllers.js +98 -0
  132. package/dist/src/index.d.ts +1151 -575
  133. package/dist/src/index.js +140 -76
  134. package/package.json +10 -4
  135. package/dist/src/ai/deepseek/DeepSeekChatBotAdapter.js +0 -106
  136. package/dist/src/ai/openia/OpenaiChatBotAdapter.js +0 -84
  137. package/dist/src/channels/cmd/@cmd.js +0 -18
  138. package/dist/src/channels/wabot/WabotDevConnection.js +0 -57
  139. package/dist/src/channels/wabot/WabotDevSocketContracts.js +0 -10
  140. package/dist/src/channels/whatsapp/@whatsapp.js +0 -20
  141. package/dist/src/channels/whatsapp/WhatsAppReceiver.js +0 -59
  142. package/dist/src/channels/whatsapp/WhatsAppReceiverByDevConnection.js +0 -32
  143. package/dist/src/channels/whatsapp/WhatsAppReceiverByWebHook.js +0 -63
  144. package/dist/src/channels/whatsapp/WhatsAppSenderByDevConnection.js +0 -61
  145. package/dist/src/chatbot/ChatBot.js +0 -51
  146. package/dist/src/chatbot/ChatBotAdapter.js +0 -72
  147. package/dist/src/controller/channel/UserResolver.js +0 -21
  148. package/dist/src/core/IMessageContext.js +0 -12
  149. package/dist/src/core/chat/ChatItem.js +0 -15
  150. package/dist/src/core/chat/repository/IChatMemory.js +0 -10
  151. package/dist/src/core/user/IUserRepository.js +0 -19
  152. package/dist/src/core/user/User.js +0 -26
  153. package/dist/src/env/WabotEnv.js +0 -27
  154. package/dist/src/injection/index.js +0 -4
  155. package/dist/src/mindset/metadata/params/@isOptional.js +0 -21
  156. package/dist/src/pre-made/module/authentication/AuthenticationModule.js +0 -97
  157. package/dist/src/pre-made/module/authentication/requests/SendOneTimePasswordRequest.js +0 -25
  158. package/dist/src/pre-made/module/authentication/requests/ValidateOneTimePasswordRequest.js +0 -25
  159. package/dist/src/pre-made/module/register-user/RegisterUserModule.js +0 -56
  160. package/dist/src/pre-made/module/register-user/requests/RegisterUserWithEmailRequest.js +0 -25
  161. package/dist/src/pre-made/repository/user/pg/PgUserRepository.js +0 -33
  162. package/dist/src/pre-made/repository/user/ram/RamUserRepository.js +0 -27
  163. package/dist/src/pre-made/service/EmailService.js +0 -13
  164. package/dist/src/pre-made/service/OtpService.js +0 -14
  165. package/dist/src/server/prepareChatContainer.js +0 -43
  166. package/dist/src/server/runChannel.js +0 -27
  167. package/dist/src/server/runServer.js +0 -40
  168. /package/dist/src/{pre-made/repository/chat → addon/chat-bot}/ram/RamChatMemory.js +0 -0
  169. /package/dist/src/{channels → addon/chat-controller}/telegram/TelegramChannelConfig.js +0 -0
  170. /package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppChannelConfig.js +0 -0
  171. /package/dist/src/{channels → addon/chat-controller}/whatsapp/WhatsAppRepository.js +0 -0
  172. /package/dist/src/{injection → core/injection}/Container.js +0 -0
  173. /package/dist/src/{logger → core/logger}/Logger.js +0 -0
  174. /package/dist/src/{mindset → feature/mindset}/metadata/functions/decoratorNames.js +0 -0
  175. /package/dist/src/{mindset → feature/mindset}/metadata/mindsets/decoratorNames.js +0 -0
  176. /package/dist/src/{mindset → feature/mindset}/metadata/modules/decoratorNames.js +0 -0
  177. /package/dist/src/{mindset → feature/mindset}/metadata/params/decoratorNames.js +0 -0
@@ -0,0 +1,26 @@
1
+ import { __decorate, __metadata } from 'tslib';
2
+ import { Pool } from 'pg';
3
+ import { singleton } from '../../../core/injection/index.js';
4
+ import { PgCrudRepository } from '../../../feature/pg/PgCrudRepository.js';
5
+ import '../../../feature/async/CommandMetadataStore.js';
6
+ import '../../../feature/async/Async.js';
7
+ import { Job } from '../../../feature/async/Job.js';
8
+ import '../../../feature/async/JobRepository.js';
9
+ import '../../../feature/async/JobRunner.js';
10
+ import '../../../feature/async/JobsEventsHub.js';
11
+
12
+ let PgJobRepository = class PgJobRepository extends PgCrudRepository {
13
+ constructor(pool) {
14
+ super(pool, {
15
+ schema: 'wabot',
16
+ table: 'job',
17
+ constructor: Job,
18
+ });
19
+ }
20
+ };
21
+ PgJobRepository = __decorate([
22
+ singleton(),
23
+ __metadata("design:paramtypes", [Pool])
24
+ ], PgJobRepository);
25
+
26
+ export { PgJobRepository };
@@ -0,0 +1,17 @@
1
+ import '../../../core/injection/index.js';
2
+ import '../../../feature/rest-controller/metadata/RestControllerMetadataStore.js';
3
+ import { middleware } from '../../../feature/rest-controller/metadata/@middleware.js';
4
+ import 'debug';
5
+ import '../../../core/validation/metadata/ValidationMetadataStore.js';
6
+ import '../../../feature/express/ExpressProvider.js';
7
+ import 'express';
8
+ import 'path';
9
+ import { ApiKeyGuardMiddleware } from './ApiKeyGuardMiddleware.js';
10
+
11
+ function apiKeyGuard() {
12
+ return function (target, propertyKey) {
13
+ middleware(ApiKeyGuardMiddleware)(target, propertyKey);
14
+ };
15
+ }
16
+
17
+ export { apiKeyGuard };
@@ -0,0 +1,53 @@
1
+ import { Entity } from '../../../core/entity/Entity.js';
2
+ import { CustomError } from '../../../core/error/CustomError.js';
3
+ import { Password } from '../../../core/password/Password.js';
4
+
5
+ class ApiKey extends Entity {
6
+ get authInfo() {
7
+ return this.data.authInfo;
8
+ }
9
+ setAuthInfo(authInfo) {
10
+ this.data.authInfo = authInfo;
11
+ }
12
+ generatePassword() {
13
+ if (this.data.passwordHash) {
14
+ throw new Error('This api key, already has a secret');
15
+ }
16
+ const password = Password.generate(64);
17
+ this.data.passwordHash = Password.hash({ password: password });
18
+ return password;
19
+ }
20
+ isValidPassword(password) {
21
+ if (!this.data.passwordHash)
22
+ return false;
23
+ return Password.isValid({ password: password, hash: this.data.passwordHash });
24
+ }
25
+ validatePassword(password) {
26
+ if (!this.isValidPassword(password)) {
27
+ throw new CustomError({ message: 'invalid api key', httpCode: 401 });
28
+ }
29
+ }
30
+ static inflate(secret) {
31
+ try {
32
+ const json = Buffer.from(secret, 'base64').toString('utf-8');
33
+ const data = JSON.parse(json);
34
+ if (!data.id || !data.pass) {
35
+ throw new Error('invalid secret structure');
36
+ }
37
+ return data;
38
+ }
39
+ catch (err) {
40
+ throw new Error('fail to inflate secret: ' + err.message);
41
+ }
42
+ }
43
+ static deflate(data) {
44
+ const { id, pass } = data;
45
+ if (!id || !pass) {
46
+ throw new Error('id and pass required');
47
+ }
48
+ const json = JSON.stringify({ id, pass });
49
+ return Buffer.from(json, 'utf-8').toString('base64');
50
+ }
51
+ }
52
+
53
+ export { ApiKey };
@@ -0,0 +1,45 @@
1
+ import { __decorate, __metadata } from 'tslib';
2
+ import { injectable } from '../../../core/injection/index.js';
3
+ import { Auth } from '../../../core/auth/Auth.js';
4
+ import { CustomError } from '../../../core/error/CustomError.js';
5
+ import { ApiKeyRepository } from './ApiKeyRepository.js';
6
+ import { ApiKey } from './ApiKey.js';
7
+
8
+ let ApiKeyGuardMiddleware = class ApiKeyGuardMiddleware {
9
+ apiKeyRepository;
10
+ auth;
11
+ constructor(apiKeyRepository, auth) {
12
+ this.apiKeyRepository = apiKeyRepository;
13
+ this.auth = auth;
14
+ }
15
+ async handle(req, res, container) {
16
+ const authorization = req.header('Authorization');
17
+ if (!authorization) {
18
+ throw new CustomError({ httpCode: 401, message: 'Authorization header not available' });
19
+ }
20
+ const [keyPrefix, keySecret] = authorization.split(' ');
21
+ if (keyPrefix.toLowerCase() !== 'api-key' || !keySecret) {
22
+ throw new CustomError({ httpCode: 401, message: 'Authorization should be an Api-Key' });
23
+ }
24
+ try {
25
+ const keyData = ApiKey.inflate(keySecret);
26
+ const apiKey = await this.apiKeyRepository.findOrThrow(keyData.id);
27
+ apiKey.validatePassword(keyData.pass);
28
+ this.auth.assign(apiKey.authInfo);
29
+ }
30
+ catch (err) {
31
+ throw new CustomError({
32
+ httpCode: 401,
33
+ message: err instanceof Error ? `Invalid token: ${err.message}` : 'Invalid token',
34
+ cause: err instanceof Error ? err : undefined,
35
+ });
36
+ }
37
+ }
38
+ };
39
+ ApiKeyGuardMiddleware = __decorate([
40
+ injectable(),
41
+ __metadata("design:paramtypes", [ApiKeyRepository,
42
+ Auth])
43
+ ], ApiKeyGuardMiddleware);
44
+
45
+ export { ApiKeyGuardMiddleware };
@@ -0,0 +1,13 @@
1
+ class ApiKeyRepository {
2
+ find(id) {
3
+ throw new Error('Method not implemented.');
4
+ }
5
+ findOrThrow(id) {
6
+ throw new Error('Method not implemented.');
7
+ }
8
+ create(item) {
9
+ throw new Error('Method not implemented.');
10
+ }
11
+ }
12
+
13
+ export { ApiKeyRepository };
@@ -0,0 +1,21 @@
1
+ import { __decorate, __metadata } from 'tslib';
2
+ import { ApiKey } from './ApiKey.js';
3
+ import { Pool } from 'pg';
4
+ import { PgCrudRepository } from '../../../feature/pg/PgCrudRepository.js';
5
+ import { singleton } from 'tsyringe';
6
+
7
+ let PgApiKeyRepository = class PgApiKeyRepository extends PgCrudRepository {
8
+ constructor(pool) {
9
+ super(pool, {
10
+ schema: 'wabot',
11
+ table: 'api_key',
12
+ constructor: ApiKey,
13
+ });
14
+ }
15
+ };
16
+ PgApiKeyRepository = __decorate([
17
+ singleton(),
18
+ __metadata("design:paramtypes", [Pool])
19
+ ], PgApiKeyRepository);
20
+
21
+ export { PgApiKeyRepository };
@@ -0,0 +1,16 @@
1
+ import { connectionMiddleware } from '../../../feature/socket-controller/metadata/@connectionMiddleware.js';
2
+ import '../../../core/injection/index.js';
3
+ import '../../../feature/socket-controller/metadata/SocketControllerMetadataStore.js';
4
+ import 'path';
5
+ import 'debug';
6
+ import '../../../feature/socket/SocketServerProvider.js';
7
+ import '../../../core/validation/metadata/ValidationMetadataStore.js';
8
+ import { JwtConnectionGuardMiddleware } from './JwtConnectionGuardMiddleware.js';
9
+
10
+ function jwtConnectionGuard() {
11
+ return function (target, propertyKey) {
12
+ connectionMiddleware(JwtConnectionGuardMiddleware)(target, propertyKey);
13
+ };
14
+ }
15
+
16
+ export { jwtConnectionGuard };
@@ -0,0 +1,17 @@
1
+ import '../../../core/injection/index.js';
2
+ import '../../../feature/rest-controller/metadata/RestControllerMetadataStore.js';
3
+ import { middleware } from '../../../feature/rest-controller/metadata/@middleware.js';
4
+ import 'debug';
5
+ import '../../../core/validation/metadata/ValidationMetadataStore.js';
6
+ import '../../../feature/express/ExpressProvider.js';
7
+ import 'express';
8
+ import 'path';
9
+ import { JwtGuardMiddleware } from './JwtGuardMiddleware.js';
10
+
11
+ function jwtGuard() {
12
+ return function (target, propertyKey) {
13
+ middleware(JwtGuardMiddleware)(target, propertyKey);
14
+ };
15
+ }
16
+
17
+ export { jwtGuard };
@@ -0,0 +1,53 @@
1
+ import { __decorate, __metadata } from 'tslib';
2
+ import { JwtSigner } from './JwtSigner.js';
3
+ import { JwtRefreshTokenRepository } from './JwtRefreshTokenRepository.js';
4
+ import { JwtRefreshToken } from './JwtRefreshToken.js';
5
+ import { injectable } from '../../../core/injection/index.js';
6
+ import { Auth } from '../../../core/auth/Auth.js';
7
+ import { JwtConfig } from './JwtConfig.js';
8
+
9
+ let Jwt = class Jwt {
10
+ auth;
11
+ jwtSigner;
12
+ jwtRefreshTokenRepository;
13
+ config;
14
+ constructor(auth, jwtSigner, jwtRefreshTokenRepository, config) {
15
+ this.auth = auth;
16
+ this.jwtSigner = jwtSigner;
17
+ this.jwtRefreshTokenRepository = jwtRefreshTokenRepository;
18
+ this.config = config;
19
+ }
20
+ async createToken() {
21
+ const authInfo = this.auth.require();
22
+ const refreshToken = new JwtRefreshToken({
23
+ authInfo,
24
+ expirationTime: new Date().getTime() + this.config.refreshExpirationSeconds * 1000,
25
+ });
26
+ const refreshPassword = refreshToken.generatePassword();
27
+ await this.jwtRefreshTokenRepository.create(refreshToken);
28
+ const access = await this.jwtSigner.signAccessToken(refreshToken);
29
+ const refresh = {
30
+ token: JwtRefreshToken.deflate({ id: refreshToken.id, pass: refreshPassword }),
31
+ expiration: new Date(refreshToken.expirationTime),
32
+ };
33
+ return {
34
+ access,
35
+ refresh,
36
+ };
37
+ }
38
+ async refreshToken(refreshSecret) {
39
+ const { id, pass } = JwtRefreshToken.inflate(refreshSecret);
40
+ const refreshToken = await this.jwtRefreshTokenRepository.findOrThrow(id);
41
+ refreshToken.validatePassword(pass);
42
+ return this.jwtSigner.signAccessToken(refreshToken);
43
+ }
44
+ };
45
+ Jwt = __decorate([
46
+ injectable(),
47
+ __metadata("design:paramtypes", [Auth,
48
+ JwtSigner,
49
+ JwtRefreshTokenRepository,
50
+ JwtConfig])
51
+ ], Jwt);
52
+
53
+ export { Jwt };
@@ -0,0 +1,20 @@
1
+ import { __decorate, __metadata } from 'tslib';
2
+ import { isModel } from '../../../core/validation/metadata/@isModel.js';
3
+ import '../../../core/injection/index.js';
4
+ import '../../../core/validation/metadata/ValidationMetadataStore.js';
5
+ import { JwtTokenDto } from './JwtTokenDto.js';
6
+
7
+ class JwtAccessAndRefreshTokenDto {
8
+ access;
9
+ refresh;
10
+ }
11
+ __decorate([
12
+ isModel(JwtTokenDto),
13
+ __metadata("design:type", JwtTokenDto)
14
+ ], JwtAccessAndRefreshTokenDto.prototype, "access", void 0);
15
+ __decorate([
16
+ isModel(JwtTokenDto),
17
+ __metadata("design:type", JwtTokenDto)
18
+ ], JwtAccessAndRefreshTokenDto.prototype, "refresh", void 0);
19
+
20
+ export { JwtAccessAndRefreshTokenDto };
@@ -0,0 +1,28 @@
1
+ import { __decorate, __metadata } from 'tslib';
2
+ import { Env } from '../../../core/env/Env.js';
3
+ import { singleton } from '../../../core/injection/index.js';
4
+
5
+ let JwtConfig = class JwtConfig {
6
+ secretOrPublicKey;
7
+ secretOrPrivateKey;
8
+ algorithm;
9
+ accessExpirationSeconds;
10
+ refreshExpirationSeconds;
11
+ constructor(env) {
12
+ this.algorithm = env.requireString('JWT_ALGORITHM', { default: 'HS256' });
13
+ this.secretOrPublicKey = env.requireString('JWT_SECRET');
14
+ this.secretOrPrivateKey = env.requireString('JWT_SECRET');
15
+ this.accessExpirationSeconds = env.requireNumber('JWT_ACCESS_EXPIRATION_SECONDS', {
16
+ default: 10 * 60,
17
+ });
18
+ this.refreshExpirationSeconds = env.requireNumber('JWT_REFRESH_EXPIRATION_SECONDS', {
19
+ default: 365 * 24 * 3600,
20
+ });
21
+ }
22
+ };
23
+ JwtConfig = __decorate([
24
+ singleton(),
25
+ __metadata("design:paramtypes", [Env])
26
+ ], JwtConfig);
27
+
28
+ export { JwtConfig };
@@ -0,0 +1,57 @@
1
+ import { __decorate, __metadata } from 'tslib';
2
+ import jwt from 'jsonwebtoken';
3
+ import { injectable } from '../../../core/injection/index.js';
4
+ import { Auth } from '../../../core/auth/Auth.js';
5
+ import { CustomError } from '../../../core/error/CustomError.js';
6
+ import { JwtConfig } from './JwtConfig.js';
7
+
8
+ let JwtConnectionGuardMiddleware = class JwtConnectionGuardMiddleware {
9
+ config;
10
+ auth;
11
+ constructor(config, auth) {
12
+ this.config = config;
13
+ this.auth = auth;
14
+ }
15
+ async handle(socket, container) {
16
+ if (!socket.handshake.auth.token) {
17
+ let authorization = socket.handshake.headers['Authorization'] ?? socket.handshake.headers['authorization'];
18
+ if (Array.isArray(authorization)) {
19
+ authorization = authorization[0];
20
+ }
21
+ if (authorization) {
22
+ const [bearer, token] = authorization.split(' ');
23
+ if (bearer.toLowerCase() !== 'bearer' || !token) {
24
+ throw new CustomError({
25
+ httpCode: 401,
26
+ message: 'Authorization should be a bearer token',
27
+ });
28
+ }
29
+ socket.handshake.auth.token = token;
30
+ }
31
+ }
32
+ let token = socket.handshake.auth.token;
33
+ if (!token) {
34
+ throw new CustomError({ httpCode: 401, message: 'Token not available' });
35
+ }
36
+ try {
37
+ const jwtPayload = jwt.verify(token, this.config.secretOrPublicKey, {
38
+ algorithms: [this.config.algorithm],
39
+ });
40
+ this.auth.assign(jwtPayload);
41
+ }
42
+ catch (err) {
43
+ throw new CustomError({
44
+ httpCode: 401,
45
+ message: err instanceof Error ? `Invalid token: ${err.message}` : 'Invalid token',
46
+ cause: err instanceof Error ? err : undefined,
47
+ });
48
+ }
49
+ }
50
+ };
51
+ JwtConnectionGuardMiddleware = __decorate([
52
+ injectable(),
53
+ __metadata("design:paramtypes", [JwtConfig,
54
+ Auth])
55
+ ], JwtConnectionGuardMiddleware);
56
+
57
+ export { JwtConnectionGuardMiddleware };
@@ -0,0 +1,45 @@
1
+ import { __decorate, __metadata } from 'tslib';
2
+ import { CustomError } from '../../../core/error/CustomError.js';
3
+ import { injectable } from '../../../core/injection/index.js';
4
+ import jwt from 'jsonwebtoken';
5
+ import { Auth } from '../../../core/auth/Auth.js';
6
+ import { JwtConfig } from './JwtConfig.js';
7
+
8
+ let JwtGuardMiddleware = class JwtGuardMiddleware {
9
+ config;
10
+ auth;
11
+ constructor(config, auth) {
12
+ this.config = config;
13
+ this.auth = auth;
14
+ }
15
+ async handle(req, res, container) {
16
+ const authorization = req.header('Authorization');
17
+ if (!authorization) {
18
+ throw new CustomError({ httpCode: 401, message: 'Authorization header not available' });
19
+ }
20
+ const [bearer, token] = authorization.split(' ');
21
+ if (bearer.toLowerCase() !== 'bearer' || !token) {
22
+ throw new CustomError({ httpCode: 401, message: 'Authorization should be a bearer token' });
23
+ }
24
+ try {
25
+ const jwtPayload = jwt.verify(token, this.config.secretOrPublicKey, {
26
+ algorithms: [this.config.algorithm],
27
+ });
28
+ this.auth.assign(jwtPayload);
29
+ }
30
+ catch (err) {
31
+ throw new CustomError({
32
+ httpCode: 401,
33
+ message: err instanceof Error ? `Invalid token: ${err.message}` : 'Invalid token',
34
+ cause: err instanceof Error ? err : undefined,
35
+ });
36
+ }
37
+ }
38
+ };
39
+ JwtGuardMiddleware = __decorate([
40
+ injectable(),
41
+ __metadata("design:paramtypes", [JwtConfig,
42
+ Auth])
43
+ ], JwtGuardMiddleware);
44
+
45
+ export { JwtGuardMiddleware };
@@ -0,0 +1,56 @@
1
+ import { Entity } from '../../../core/entity/Entity.js';
2
+ import { CustomError } from '../../../core/error/CustomError.js';
3
+ import { Password } from '../../../core/password/Password.js';
4
+
5
+ class JwtRefreshToken extends Entity {
6
+ get authInfo() {
7
+ return this.data.authInfo;
8
+ }
9
+ get expirationTime() {
10
+ return new Date(this.data.expirationTime);
11
+ }
12
+ generatePassword() {
13
+ if (this.data.passwordHash) {
14
+ throw new Error('This api key, already has a secret');
15
+ }
16
+ const password = Password.generate(64);
17
+ this.data.passwordHash = Password.hash({ password: password });
18
+ return password;
19
+ }
20
+ isValidPassword(password) {
21
+ if (new Date().getTime() > this.data.expirationTime) {
22
+ return false;
23
+ }
24
+ if (!this.data.passwordHash)
25
+ return false;
26
+ return Password.isValid({ password: password, hash: this.data.passwordHash });
27
+ }
28
+ validatePassword(password) {
29
+ if (!this.isValidPassword(password)) {
30
+ throw new CustomError({ message: 'Invalid Api key', httpCode: 401 });
31
+ }
32
+ }
33
+ static inflate(secret) {
34
+ try {
35
+ const json = Buffer.from(secret, 'base64').toString('utf-8');
36
+ const data = JSON.parse(json);
37
+ if (!data.id || !data.pass) {
38
+ throw new Error('invalid secret structure');
39
+ }
40
+ return data;
41
+ }
42
+ catch (err) {
43
+ throw new Error('fail to inflate secret: ' + err.message);
44
+ }
45
+ }
46
+ static deflate(data) {
47
+ const { id, pass } = data;
48
+ if (!id || !pass) {
49
+ throw new Error('id and pass required');
50
+ }
51
+ const json = JSON.stringify({ id, pass });
52
+ return Buffer.from(json, 'utf-8').toString('base64');
53
+ }
54
+ }
55
+
56
+ export { JwtRefreshToken };
@@ -0,0 +1,25 @@
1
+ class JwtRefreshTokenRepository {
2
+ find(id) {
3
+ throw new Error('Method not implemented.');
4
+ }
5
+ findOrThrow(id) {
6
+ throw new Error('Method not implemented.');
7
+ }
8
+ findByIds(ids) {
9
+ throw new Error('Method not implemented.');
10
+ }
11
+ findAll(id) {
12
+ throw new Error('Method not implemented.');
13
+ }
14
+ create(item) {
15
+ throw new Error('Method not implemented.');
16
+ }
17
+ update(item) {
18
+ throw new Error('Method not implemented.');
19
+ }
20
+ discard(item) {
21
+ throw new Error('Method not implemented.');
22
+ }
23
+ }
24
+
25
+ export { JwtRefreshTokenRepository };
@@ -0,0 +1,36 @@
1
+ import { __decorate, __metadata } from 'tslib';
2
+ import jwt from 'jsonwebtoken';
3
+ import { JwtConfig } from './JwtConfig.js';
4
+ import { JwtTokenDto } from './JwtTokenDto.js';
5
+ import { injectable } from '../../../core/injection/index.js';
6
+ import { Mapper } from '../../../core/mapper/Mapper.js';
7
+ import { JwtRefreshToken } from './JwtRefreshToken.js';
8
+
9
+ let JwtSigner = class JwtSigner {
10
+ config;
11
+ mapper;
12
+ constructor(config, mapper) {
13
+ this.config = config;
14
+ this.mapper = mapper;
15
+ }
16
+ async signAccessToken(info) {
17
+ const _authInfo = info instanceof JwtRefreshToken
18
+ ? {
19
+ ...info.authInfo,
20
+ refreshTokenId: info.id,
21
+ }
22
+ : info;
23
+ const token = jwt.sign(_authInfo, this.config.secretOrPrivateKey, {
24
+ expiresIn: this.config.accessExpirationSeconds,
25
+ });
26
+ const expiration = new Date().getTime() + this.config.accessExpirationSeconds * 1000;
27
+ return this.mapper.map({ token, expiration }, JwtTokenDto);
28
+ }
29
+ };
30
+ JwtSigner = __decorate([
31
+ injectable(),
32
+ __metadata("design:paramtypes", [JwtConfig,
33
+ Mapper])
34
+ ], JwtSigner);
35
+
36
+ export { JwtSigner };
@@ -0,0 +1,22 @@
1
+ import { __decorate, __metadata } from 'tslib';
2
+ import '../../../core/injection/index.js';
3
+ import '../../../core/validation/metadata/ValidationMetadataStore.js';
4
+ import { isDate } from '../../../core/validation/validators/is-date/@isDate.js';
5
+ import { isNotEmpty } from '../../../core/validation/validators/is-not-empty/@isNotEmpty.js';
6
+ import { isString } from '../../../core/validation/validators/is-string/@isString.js';
7
+
8
+ class JwtTokenDto {
9
+ token;
10
+ expiration;
11
+ }
12
+ __decorate([
13
+ isString(),
14
+ isNotEmpty(),
15
+ __metadata("design:type", String)
16
+ ], JwtTokenDto.prototype, "token", void 0);
17
+ __decorate([
18
+ isDate(),
19
+ __metadata("design:type", Date)
20
+ ], JwtTokenDto.prototype, "expiration", void 0);
21
+
22
+ export { JwtTokenDto };
@@ -0,0 +1,21 @@
1
+ import { __decorate, __metadata } from 'tslib';
2
+ import { singleton } from '../../../core/injection/index.js';
3
+ import { Pool } from 'pg';
4
+ import { PgCrudRepository } from '../../../feature/pg/PgCrudRepository.js';
5
+ import { JwtRefreshToken } from './JwtRefreshToken.js';
6
+
7
+ let PgJwtRefreshTokenRepository = class PgJwtRefreshTokenRepository extends PgCrudRepository {
8
+ constructor(pool) {
9
+ super(pool, {
10
+ schema: 'wabot',
11
+ table: 'jwt_refresh_token',
12
+ constructor: JwtRefreshToken,
13
+ });
14
+ }
15
+ };
16
+ PgJwtRefreshTokenRepository = __decorate([
17
+ singleton(),
18
+ __metadata("design:paramtypes", [Pool])
19
+ ], PgJwtRefreshTokenRepository);
20
+
21
+ export { PgJwtRefreshTokenRepository };