@appweaver/core 1.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.
Files changed (271) hide show
  1. package/LICENSE +1 -0
  2. package/README.md +7 -0
  3. package/app/application.d.ts +43 -0
  4. package/app/application.js +112 -0
  5. package/app/create-app.d.ts +32 -0
  6. package/app/create-app.js +38 -0
  7. package/app/index.d.ts +2 -0
  8. package/app/index.js +18 -0
  9. package/app/lifecycle-manager.d.ts +20 -0
  10. package/app/lifecycle-manager.js +42 -0
  11. package/app/load-modules.d.ts +10 -0
  12. package/app/load-modules.js +50 -0
  13. package/app/load-providers.d.ts +8 -0
  14. package/app/load-providers.js +33 -0
  15. package/cache/cache-service.d.ts +72 -0
  16. package/cache/cache-service.js +202 -0
  17. package/cache/cache.d.ts +12 -0
  18. package/cache/cache.js +184 -0
  19. package/cache/caching.d.ts +8 -0
  20. package/cache/caching.js +72 -0
  21. package/cache/eviction/eviction-index.d.ts +35 -0
  22. package/cache/eviction/eviction-index.js +2 -0
  23. package/cache/eviction/fifo-eviction-index.d.ts +7 -0
  24. package/cache/eviction/fifo-eviction-index.js +28 -0
  25. package/cache/eviction/index.d.ts +4 -0
  26. package/cache/eviction/index.js +20 -0
  27. package/cache/eviction/lfu-eviction-index.d.ts +8 -0
  28. package/cache/eviction/lfu-eviction-index.js +82 -0
  29. package/cache/eviction/lru-eviction-index.d.ts +8 -0
  30. package/cache/eviction/lru-eviction-index.js +81 -0
  31. package/cache/index.d.ts +2 -0
  32. package/cache/index.js +18 -0
  33. package/cache/memory-cache.d.ts +4 -0
  34. package/cache/memory-cache.js +12 -0
  35. package/cache/redis-cache.d.ts +4 -0
  36. package/cache/redis-cache.js +12 -0
  37. package/context/context.d.ts +2 -0
  38. package/context/context.js +13 -0
  39. package/context/dependency-injection.d.ts +98 -0
  40. package/context/dependency-injection.js +276 -0
  41. package/context/index.d.ts +2 -0
  42. package/context/index.js +18 -0
  43. package/database/create-client.d.ts +11 -0
  44. package/database/create-client.js +142 -0
  45. package/database/index.d.ts +1 -0
  46. package/database/index.js +17 -0
  47. package/database/prisma-database.d.ts +15 -0
  48. package/database/prisma-database.js +53 -0
  49. package/errors/error-handler.d.ts +3 -0
  50. package/errors/error-handler.js +24 -0
  51. package/errors/error-schema.d.ts +32 -0
  52. package/errors/error-schema.js +31 -0
  53. package/errors/http-error.d.ts +21 -0
  54. package/errors/http-error.js +33 -0
  55. package/errors/index.d.ts +3 -0
  56. package/errors/index.js +19 -0
  57. package/events/node-events.d.ts +7 -0
  58. package/events/node-events.js +39 -0
  59. package/export/export-service.d.ts +19 -0
  60. package/export/export-service.js +154 -0
  61. package/export/index.d.ts +1 -0
  62. package/export/index.js +17 -0
  63. package/factory/create-model.d.ts +2 -0
  64. package/factory/create-model.js +366 -0
  65. package/factory/create-policy.d.ts +2 -0
  66. package/factory/create-policy.js +16 -0
  67. package/factory/create-routes.d.ts +2 -0
  68. package/factory/create-routes.js +21 -0
  69. package/factory/create-service.d.ts +3 -0
  70. package/factory/create-service.js +97 -0
  71. package/factory/index.d.ts +4 -0
  72. package/factory/index.js +20 -0
  73. package/health/health-routes.d.ts +2 -0
  74. package/health/health-routes.js +36 -0
  75. package/health/health-schema.d.ts +45 -0
  76. package/health/health-schema.js +52 -0
  77. package/health/health-service.d.ts +38 -0
  78. package/health/health-service.js +82 -0
  79. package/health/index.d.ts +2 -0
  80. package/health/index.js +18 -0
  81. package/index.d.ts +15 -0
  82. package/index.js +31 -0
  83. package/mailer/email-service.d.ts +36 -0
  84. package/mailer/email-service.js +89 -0
  85. package/mailer/index.d.ts +1 -0
  86. package/mailer/index.js +17 -0
  87. package/mailer/json-mailer.d.ts +7 -0
  88. package/mailer/json-mailer.js +32 -0
  89. package/mailer/smtp-mailer.d.ts +7 -0
  90. package/mailer/smtp-mailer.js +43 -0
  91. package/memory/in-memory.d.ts +24 -0
  92. package/memory/in-memory.js +164 -0
  93. package/memory/redis.d.ts +26 -0
  94. package/memory/redis.js +158 -0
  95. package/package.json +68 -0
  96. package/prisma/client/browser.d.ts +34 -0
  97. package/prisma/client/browser.js +56 -0
  98. package/prisma/client/client.d.ts +53 -0
  99. package/prisma/client/client.js +70 -0
  100. package/prisma/client/commonInputTypes.d.ts +395 -0
  101. package/prisma/client/commonInputTypes.js +11 -0
  102. package/prisma/client/enums.d.ts +1 -0
  103. package/prisma/client/enums.js +11 -0
  104. package/prisma/client/internal/class.d.ts +187 -0
  105. package/prisma/client/internal/class.js +85 -0
  106. package/prisma/client/internal/prismaNamespace.d.ts +987 -0
  107. package/prisma/client/internal/prismaNamespace.js +193 -0
  108. package/prisma/client/internal/prismaNamespaceBrowser.d.ts +125 -0
  109. package/prisma/client/internal/prismaNamespaceBrowser.js +164 -0
  110. package/prisma/client/models/ApiKey.d.ts +1167 -0
  111. package/prisma/client/models/ApiKey.js +2 -0
  112. package/prisma/client/models/File.d.ts +1248 -0
  113. package/prisma/client/models/File.js +2 -0
  114. package/prisma/client/models/OneTimeToken.d.ts +1083 -0
  115. package/prisma/client/models/OneTimeToken.js +2 -0
  116. package/prisma/client/models/Permission.d.ts +1209 -0
  117. package/prisma/client/models/Permission.js +2 -0
  118. package/prisma/client/models/Role.d.ts +1209 -0
  119. package/prisma/client/models/Role.js +2 -0
  120. package/prisma/client/models/Seeder.d.ts +1023 -0
  121. package/prisma/client/models/Seeder.js +2 -0
  122. package/prisma/client/models.d.ts +7 -0
  123. package/prisma/client/models.js +2 -0
  124. package/queue/bull-queue.d.ts +37 -0
  125. package/queue/bull-queue.js +207 -0
  126. package/queue/memory-queue.d.ts +54 -0
  127. package/queue/memory-queue.js +283 -0
  128. package/resource/index.d.ts +4 -0
  129. package/resource/index.js +20 -0
  130. package/resource/resource-loader.d.ts +26 -0
  131. package/resource/resource-loader.js +161 -0
  132. package/resource/resource-routes.d.ts +3 -0
  133. package/resource/resource-routes.js +174 -0
  134. package/resource/resource-schema.d.ts +33 -0
  135. package/resource/resource-schema.js +186 -0
  136. package/resource/resource-service.d.ts +89 -0
  137. package/resource/resource-service.js +772 -0
  138. package/resources.d.ts +9 -0
  139. package/resources.js +24 -0
  140. package/scheduler/cron-scheduler.d.ts +12 -0
  141. package/scheduler/cron-scheduler.js +73 -0
  142. package/security/account/account-routes.d.ts +2 -0
  143. package/security/account/account-routes.js +104 -0
  144. package/security/account/account-schema.d.ts +290 -0
  145. package/security/account/account-schema.js +166 -0
  146. package/security/account/account-service.d.ts +88 -0
  147. package/security/account/account-service.js +218 -0
  148. package/security/account/index.d.ts +3 -0
  149. package/security/account/index.js +19 -0
  150. package/security/api-key/api-key-auth.d.ts +4 -0
  151. package/security/api-key/api-key-auth.js +66 -0
  152. package/security/api-key/index.d.ts +1 -0
  153. package/security/api-key/index.js +17 -0
  154. package/security/auth-routes.d.ts +2 -0
  155. package/security/auth-routes.js +80 -0
  156. package/security/auth-schema.d.ts +215 -0
  157. package/security/auth-schema.js +129 -0
  158. package/security/auth-service.d.ts +117 -0
  159. package/security/auth-service.js +272 -0
  160. package/security/auth.d.ts +3 -0
  161. package/security/auth.js +127 -0
  162. package/security/basic/basic-auth.d.ts +4 -0
  163. package/security/basic/basic-auth.js +32 -0
  164. package/security/basic/index.d.ts +1 -0
  165. package/security/basic/index.js +17 -0
  166. package/security/create-auth-resources.d.ts +6 -0
  167. package/security/create-auth-resources.js +129 -0
  168. package/security/helper.d.ts +127 -0
  169. package/security/helper.js +329 -0
  170. package/security/index.d.ts +4 -0
  171. package/security/index.js +20 -0
  172. package/security/jwt/index.d.ts +2 -0
  173. package/security/jwt/index.js +18 -0
  174. package/security/jwt/jwt-auth.d.ts +4 -0
  175. package/security/jwt/jwt-auth.js +57 -0
  176. package/security/jwt/jwt-keys.d.ts +7 -0
  177. package/security/jwt/jwt-keys.js +43 -0
  178. package/security/oauth2/create-oauth2-plugin.d.ts +17 -0
  179. package/security/oauth2/create-oauth2-plugin.js +85 -0
  180. package/security/oauth2/index.d.ts +3 -0
  181. package/security/oauth2/index.js +19 -0
  182. package/security/oauth2/oauth2-custom.d.ts +7 -0
  183. package/security/oauth2/oauth2-custom.js +40 -0
  184. package/security/oauth2/oauth2-facebook.d.ts +1 -0
  185. package/security/oauth2/oauth2-facebook.js +30 -0
  186. package/security/oauth2/oauth2-google.d.ts +1 -0
  187. package/security/oauth2/oauth2-google.js +28 -0
  188. package/security/oauth2/oauth2-schema.d.ts +10 -0
  189. package/security/oauth2/oauth2-schema.js +53 -0
  190. package/security/recaptcha/index.d.ts +2 -0
  191. package/security/recaptcha/index.js +18 -0
  192. package/security/recaptcha/recaptcha-verify.d.ts +1 -0
  193. package/security/recaptcha/recaptcha-verify.js +33 -0
  194. package/security/recaptcha/recaptcha.d.ts +2 -0
  195. package/security/recaptcha/recaptcha.js +22 -0
  196. package/security/resources/api-key/model.d.ts +2 -0
  197. package/security/resources/api-key/model.js +63 -0
  198. package/security/resources/api-key/policy.d.ts +2 -0
  199. package/security/resources/api-key/policy.js +26 -0
  200. package/security/resources/api-key/routes.d.ts +2 -0
  201. package/security/resources/api-key/routes.js +15 -0
  202. package/security/resources/api-key/service.d.ts +3 -0
  203. package/security/resources/api-key/service.js +33 -0
  204. package/security/resources/one-time-token/model.d.ts +2 -0
  205. package/security/resources/one-time-token/model.js +32 -0
  206. package/security/resources/permission/model.d.ts +2 -0
  207. package/security/resources/permission/model.js +16 -0
  208. package/security/resources/role/model.d.ts +2 -0
  209. package/security/resources/role/model.js +22 -0
  210. package/security/store/database-security-store.d.ts +5 -0
  211. package/security/store/database-security-store.js +51 -0
  212. package/security/store/redis-security-store.d.ts +5 -0
  213. package/security/store/redis-security-store.js +32 -0
  214. package/seeder/create-seeder.d.ts +21 -0
  215. package/seeder/create-seeder.js +17 -0
  216. package/seeder/index.d.ts +2 -0
  217. package/seeder/index.js +18 -0
  218. package/seeder/resources/seeder/model.d.ts +2 -0
  219. package/seeder/resources/seeder/model.js +38 -0
  220. package/seeder/seeder.d.ts +20 -0
  221. package/seeder/seeder.js +175 -0
  222. package/server/create-server.d.ts +20 -0
  223. package/server/create-server.js +160 -0
  224. package/server/index.d.ts +4 -0
  225. package/server/index.js +20 -0
  226. package/server/info-route.d.ts +2 -0
  227. package/server/info-route.js +18 -0
  228. package/server/info-schema.d.ts +37 -0
  229. package/server/info-schema.js +19 -0
  230. package/server/register-model.d.ts +2 -0
  231. package/server/register-model.js +12 -0
  232. package/server/register-plugin.d.ts +2 -0
  233. package/server/register-plugin.js +13 -0
  234. package/server/register-route.d.ts +3 -0
  235. package/server/register-route.js +74 -0
  236. package/server/schemas.d.ts +3 -0
  237. package/server/schemas.js +30 -0
  238. package/server/swagger.d.ts +3 -0
  239. package/server/swagger.js +122 -0
  240. package/storage/file-route.d.ts +2 -0
  241. package/storage/file-route.js +39 -0
  242. package/storage/file-schema.d.ts +9 -0
  243. package/storage/file-schema.js +42 -0
  244. package/storage/file-service.d.ts +81 -0
  245. package/storage/file-service.js +384 -0
  246. package/storage/filesystem-storage.d.ts +10 -0
  247. package/storage/filesystem-storage.js +128 -0
  248. package/storage/index.d.ts +2 -0
  249. package/storage/index.js +18 -0
  250. package/storage/resources/file/model.d.ts +2 -0
  251. package/storage/resources/file/model.js +75 -0
  252. package/types/application-context.d.ts +20 -0
  253. package/types/application-context.js +2 -0
  254. package/types/auth.d.ts +31 -0
  255. package/types/auth.js +2 -0
  256. package/types/fastify-extensions.d.ts +24 -0
  257. package/types/fastify-extensions.js +4 -0
  258. package/types/generated.d.ts +193 -0
  259. package/types/generated.js +3 -0
  260. package/types/index.d.ts +4 -0
  261. package/types/index.js +20 -0
  262. package/types/server.d.ts +4 -0
  263. package/types/server.js +2 -0
  264. package/utils/export-util.d.ts +8 -0
  265. package/utils/export-util.js +25 -0
  266. package/utils/file-util.d.ts +64 -0
  267. package/utils/file-util.js +141 -0
  268. package/utils/index.d.ts +3 -0
  269. package/utils/index.js +19 -0
  270. package/utils/schema-util.d.ts +20 -0
  271. package/utils/schema-util.js +37 -0
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.authRoutes = authRoutes;
4
+ const auth_schema_1 = require("./auth-schema");
5
+ const common_1 = require("@appweaver/common");
6
+ const auth_service_1 = require("./auth-service");
7
+ const helper_1 = require("./helper");
8
+ const context_1 = require("../context");
9
+ function authRoutes(server) {
10
+ const { authenticate, currentUser } = server;
11
+ const authService = (0, context_1.inject)(auth_service_1.AuthService);
12
+ server.post('/login', {
13
+ schema: auth_schema_1.loginSchema,
14
+ config: {
15
+ rateLimit: {
16
+ max: 12
17
+ }
18
+ }
19
+ }, async (request, reply) => {
20
+ const { username, password } = request.body;
21
+ const authResponse = await authService.login(username, password);
22
+ return reply.send(authResponse);
23
+ });
24
+ server.post('/logout', {
25
+ schema: auth_schema_1.logoutSchema,
26
+ onRequest: authenticate([common_1.AuthType.Jwt])
27
+ }, async (_, reply) => {
28
+ const authUser = currentUser();
29
+ const success = await authService.logout(authUser.id);
30
+ return reply.send({ success });
31
+ });
32
+ server.post('/refresh', {
33
+ schema: auth_schema_1.refreshSchema,
34
+ onRequest: authenticate([common_1.AuthType.Jwt]),
35
+ config: {
36
+ rateLimit: {
37
+ max: 12
38
+ }
39
+ }
40
+ }, async (_, reply) => {
41
+ const authUser = currentUser();
42
+ const authResponse = await authService.generateAuthTokens(authUser, common_1.AuthScope.Auth, (0, helper_1.currentAuthSource)() ?? common_1.AuthSource.Password);
43
+ return reply.send(authResponse);
44
+ });
45
+ server.post('/change-password', {
46
+ schema: auth_schema_1.changePasswordSchema,
47
+ onRequest: authenticate(),
48
+ config: {
49
+ rateLimit: {
50
+ max: 12
51
+ }
52
+ }
53
+ }, async (request, reply) => {
54
+ const authUser = currentUser();
55
+ const authResponse = await authService.changePassword(authUser, request.body.currentPassword, request.body.newPassword);
56
+ return reply.send(authResponse);
57
+ });
58
+ server.post('/exchange-token', {
59
+ schema: auth_schema_1.exchangeTokenSchema,
60
+ config: {
61
+ rateLimit: {
62
+ max: 12
63
+ }
64
+ }
65
+ }, async (request, reply) => {
66
+ const { token } = request.body;
67
+ const authResponse = await authService.exchangeToken(token);
68
+ return reply.send(authResponse);
69
+ });
70
+ const authUserModel = (0, helper_1.resourceAuthModel)();
71
+ if (authUserModel) {
72
+ server.get('/me', {
73
+ schema: (0, auth_schema_1.createCurrentAuthUserSchema)(authUserModel.readOneModel),
74
+ onRequest: authenticate()
75
+ }, async (_, reply) => {
76
+ const authUser = currentUser();
77
+ return reply.send(authUser);
78
+ });
79
+ }
80
+ }
@@ -0,0 +1,215 @@
1
+ import { TObject, TSchema } from '@sinclair/typebox';
2
+ import { AuthType, RecaptchaConfig, RouteSchema } from '@appweaver/common';
3
+ export declare const LoginRequest: TObject<{
4
+ username: import("@sinclair/typebox").TString;
5
+ password: import("@sinclair/typebox").TString;
6
+ }>;
7
+ export declare const AuthenticationResponse: TObject<{
8
+ accessToken: import("@sinclair/typebox").TString;
9
+ refreshToken: import("@sinclair/typebox").TString;
10
+ expiresIn: import("@sinclair/typebox").TNumber;
11
+ refreshExpiresIn: import("@sinclair/typebox").TNumber;
12
+ }>;
13
+ export declare const ChangePasswordRequest: TObject<{
14
+ currentPassword: import("@sinclair/typebox").TString;
15
+ newPassword: import("@sinclair/typebox").TString;
16
+ }>;
17
+ export declare const LogoutResponse: TObject<{
18
+ success: import("@sinclair/typebox").TBoolean;
19
+ }>;
20
+ export declare const ExchangeTokenRequest: TObject<{
21
+ token: import("@sinclair/typebox").TString;
22
+ }>;
23
+ export declare const loginSchema: {
24
+ tags: string[];
25
+ summary: string;
26
+ description: string;
27
+ response: {
28
+ '4xx': {
29
+ description: string;
30
+ content: {
31
+ 'application/json': {
32
+ schema: TObject<{
33
+ errorCode: import("@sinclair/typebox").TInteger;
34
+ message: import("@sinclair/typebox").TString;
35
+ }>;
36
+ };
37
+ };
38
+ };
39
+ '5xx': {
40
+ description: string;
41
+ content: {
42
+ 'application/json': {
43
+ schema: TObject<{
44
+ errorCode: import("@sinclair/typebox").TInteger;
45
+ message: import("@sinclair/typebox").TString;
46
+ }>;
47
+ };
48
+ };
49
+ };
50
+ 200: TObject<{
51
+ accessToken: import("@sinclair/typebox").TString;
52
+ refreshToken: import("@sinclair/typebox").TString;
53
+ expiresIn: import("@sinclair/typebox").TNumber;
54
+ refreshExpiresIn: import("@sinclair/typebox").TNumber;
55
+ }>;
56
+ };
57
+ body: TObject<{
58
+ username: import("@sinclair/typebox").TString;
59
+ password: import("@sinclair/typebox").TString;
60
+ }>;
61
+ };
62
+ export declare const refreshSchema: {
63
+ tags: string[];
64
+ security: {
65
+ bearer: never[];
66
+ }[];
67
+ summary: string;
68
+ description: string;
69
+ response: {
70
+ '4xx': {
71
+ description: string;
72
+ content: {
73
+ 'application/json': {
74
+ schema: TObject<{
75
+ errorCode: import("@sinclair/typebox").TInteger;
76
+ message: import("@sinclair/typebox").TString;
77
+ }>;
78
+ };
79
+ };
80
+ };
81
+ '5xx': {
82
+ description: string;
83
+ content: {
84
+ 'application/json': {
85
+ schema: TObject<{
86
+ errorCode: import("@sinclair/typebox").TInteger;
87
+ message: import("@sinclair/typebox").TString;
88
+ }>;
89
+ };
90
+ };
91
+ };
92
+ 200: TObject<{
93
+ accessToken: import("@sinclair/typebox").TString;
94
+ refreshToken: import("@sinclair/typebox").TString;
95
+ expiresIn: import("@sinclair/typebox").TNumber;
96
+ refreshExpiresIn: import("@sinclair/typebox").TNumber;
97
+ }>;
98
+ };
99
+ };
100
+ export declare const logoutSchema: {
101
+ tags: string[];
102
+ security: {
103
+ bearer: never[];
104
+ }[];
105
+ summary: string;
106
+ description: string;
107
+ response: {
108
+ '4xx': {
109
+ description: string;
110
+ content: {
111
+ 'application/json': {
112
+ schema: TObject<{
113
+ errorCode: import("@sinclair/typebox").TInteger;
114
+ message: import("@sinclair/typebox").TString;
115
+ }>;
116
+ };
117
+ };
118
+ };
119
+ '5xx': {
120
+ description: string;
121
+ content: {
122
+ 'application/json': {
123
+ schema: TObject<{
124
+ errorCode: import("@sinclair/typebox").TInteger;
125
+ message: import("@sinclair/typebox").TString;
126
+ }>;
127
+ };
128
+ };
129
+ };
130
+ 200: TObject<{
131
+ success: import("@sinclair/typebox").TBoolean;
132
+ }>;
133
+ };
134
+ };
135
+ export declare const changePasswordSchema: {
136
+ tags: string[];
137
+ security: any[];
138
+ summary: string;
139
+ description: string;
140
+ response: {
141
+ '4xx': {
142
+ description: string;
143
+ content: {
144
+ 'application/json': {
145
+ schema: TObject<{
146
+ errorCode: import("@sinclair/typebox").TInteger;
147
+ message: import("@sinclair/typebox").TString;
148
+ }>;
149
+ };
150
+ };
151
+ };
152
+ '5xx': {
153
+ description: string;
154
+ content: {
155
+ 'application/json': {
156
+ schema: TObject<{
157
+ errorCode: import("@sinclair/typebox").TInteger;
158
+ message: import("@sinclair/typebox").TString;
159
+ }>;
160
+ };
161
+ };
162
+ };
163
+ 200: TObject<{
164
+ accessToken: import("@sinclair/typebox").TString;
165
+ refreshToken: import("@sinclair/typebox").TString;
166
+ expiresIn: import("@sinclair/typebox").TNumber;
167
+ refreshExpiresIn: import("@sinclair/typebox").TNumber;
168
+ }>;
169
+ };
170
+ body: TObject<{
171
+ currentPassword: import("@sinclair/typebox").TString;
172
+ newPassword: import("@sinclair/typebox").TString;
173
+ }>;
174
+ };
175
+ export declare const exchangeTokenSchema: {
176
+ tags: string[];
177
+ summary: string;
178
+ description: string;
179
+ response: {
180
+ '4xx': {
181
+ description: string;
182
+ content: {
183
+ 'application/json': {
184
+ schema: TObject<{
185
+ errorCode: import("@sinclair/typebox").TInteger;
186
+ message: import("@sinclair/typebox").TString;
187
+ }>;
188
+ };
189
+ };
190
+ };
191
+ '5xx': {
192
+ description: string;
193
+ content: {
194
+ 'application/json': {
195
+ schema: TObject<{
196
+ errorCode: import("@sinclair/typebox").TInteger;
197
+ message: import("@sinclair/typebox").TString;
198
+ }>;
199
+ };
200
+ };
201
+ };
202
+ 200: TObject<{
203
+ accessToken: import("@sinclair/typebox").TString;
204
+ refreshToken: import("@sinclair/typebox").TString;
205
+ expiresIn: import("@sinclair/typebox").TNumber;
206
+ refreshExpiresIn: import("@sinclair/typebox").TNumber;
207
+ }>;
208
+ };
209
+ body: TObject<{
210
+ token: import("@sinclair/typebox").TString;
211
+ }>;
212
+ };
213
+ export declare function authSchema(authTypes?: AuthType[]): any[];
214
+ export declare function recaptchaHeaderSchema(recaptchaConfig: RecaptchaConfig): TObject;
215
+ export declare function createCurrentAuthUserSchema(modelSchema: TSchema): RouteSchema;
@@ -0,0 +1,129 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.exchangeTokenSchema = exports.changePasswordSchema = exports.logoutSchema = exports.refreshSchema = exports.loginSchema = exports.ExchangeTokenRequest = exports.LogoutResponse = exports.ChangePasswordRequest = exports.AuthenticationResponse = exports.LoginRequest = void 0;
4
+ exports.authSchema = authSchema;
5
+ exports.recaptchaHeaderSchema = recaptchaHeaderSchema;
6
+ exports.createCurrentAuthUserSchema = createCurrentAuthUserSchema;
7
+ const typebox_1 = require("@sinclair/typebox");
8
+ const common_1 = require("@appweaver/common");
9
+ const errors_1 = require("../errors");
10
+ const utils_1 = require("../utils");
11
+ exports.LoginRequest = typebox_1.Type.Object({
12
+ username: typebox_1.Type.String({ example: 'john.doe@example.com' }),
13
+ password: typebox_1.Type.String({ example: 'yourPassword123!' })
14
+ }, { $id: 'LoginRequest' });
15
+ exports.AuthenticationResponse = typebox_1.Type.Object({
16
+ accessToken: typebox_1.Type.String({
17
+ example: 'eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJuYk92'
18
+ }),
19
+ refreshToken: typebox_1.Type.String({
20
+ example: 'eyJhbGciOiJIUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJkNTBl'
21
+ }),
22
+ expiresIn: typebox_1.Type.Number({ example: 2592000 }),
23
+ refreshExpiresIn: typebox_1.Type.Number({ example: 5184000 })
24
+ }, { $id: 'AuthenticationResponse' });
25
+ exports.ChangePasswordRequest = typebox_1.Type.Object({
26
+ currentPassword: typebox_1.Type.String({ example: 'oldPassword123!' }),
27
+ newPassword: typebox_1.Type.String({ example: 'newPassword321!' })
28
+ }, { $id: 'ChangePasswordRequest' });
29
+ exports.LogoutResponse = typebox_1.Type.Object({
30
+ success: typebox_1.Type.Boolean({ example: true })
31
+ }, { $id: 'LogoutResponse' });
32
+ exports.ExchangeTokenRequest = typebox_1.Type.Object({
33
+ token: typebox_1.Type.String({ example: 'aBcDeFgHijkLMnO123456789' })
34
+ }, { $id: 'ExchangeTokenRequest' });
35
+ exports.loginSchema = {
36
+ tags: ['Auth'],
37
+ summary: 'Login identity',
38
+ description: 'Login identity',
39
+ response: {
40
+ 200: (0, utils_1.createSchemaModel)(exports.AuthenticationResponse),
41
+ ...errors_1.AllErrorResponses
42
+ },
43
+ body: (0, utils_1.createSchemaModel)(exports.LoginRequest)
44
+ };
45
+ exports.refreshSchema = {
46
+ tags: ['Auth'],
47
+ security: [{ bearer: [] }],
48
+ summary: 'Refresh identity token',
49
+ description: 'Refresh identity token',
50
+ response: {
51
+ 200: (0, utils_1.createSchemaModel)(exports.AuthenticationResponse),
52
+ ...errors_1.AllErrorResponses
53
+ }
54
+ };
55
+ exports.logoutSchema = {
56
+ tags: ['Auth'],
57
+ security: [{ bearer: [] }],
58
+ summary: 'Logout identity',
59
+ description: 'Logout identity',
60
+ response: {
61
+ 200: (0, utils_1.createSchemaModel)(exports.LogoutResponse),
62
+ ...errors_1.AllErrorResponses
63
+ }
64
+ };
65
+ exports.changePasswordSchema = {
66
+ tags: ['Auth'],
67
+ security: authSchema(),
68
+ summary: 'Change identity password',
69
+ description: 'Change identity password',
70
+ response: {
71
+ 200: (0, utils_1.createSchemaModel)(exports.AuthenticationResponse),
72
+ ...errors_1.AllErrorResponses
73
+ },
74
+ body: (0, utils_1.createSchemaModel)(exports.ChangePasswordRequest)
75
+ };
76
+ exports.exchangeTokenSchema = {
77
+ tags: ['Auth'],
78
+ summary: 'Exchange one time token for access token',
79
+ description: 'Exchange one time token for access token',
80
+ response: {
81
+ 200: (0, utils_1.createSchemaModel)(exports.AuthenticationResponse),
82
+ ...errors_1.AllErrorResponses
83
+ },
84
+ body: (0, utils_1.createSchemaModel)(exports.ExchangeTokenRequest)
85
+ };
86
+ function authSchema(authTypes) {
87
+ const authSchemas = [];
88
+ for (const authType of authTypes ?? Object.values(common_1.AuthType)) {
89
+ switch (authType) {
90
+ case common_1.AuthType.Basic:
91
+ if (common_1.config.SECURITY_BASIC_ENABLED) {
92
+ authSchemas.push({ basicAuth: [] });
93
+ }
94
+ break;
95
+ case common_1.AuthType.ApiKey:
96
+ if (common_1.config.SECURITY_API_KEY_ENABLED) {
97
+ authSchemas.push({ apiKeyAuth: [] });
98
+ }
99
+ break;
100
+ case common_1.AuthType.Jwt:
101
+ authSchemas.push({ bearer: [] });
102
+ break;
103
+ }
104
+ }
105
+ return authSchemas;
106
+ }
107
+ function recaptchaHeaderSchema(recaptchaConfig) {
108
+ return typebox_1.Type.Object(common_1.config.SECURITY_RECAPTCHA_ENABLED &&
109
+ (recaptchaConfig.recaptcha || recaptchaConfig.recaptchaAction)
110
+ ? {
111
+ [common_1.config.SECURITY_RECAPTCHA_HEADER_NAME.toLowerCase()]: typebox_1.Type.String({
112
+ minLength: 1,
113
+ description: `reCAPTCHA token for ${recaptchaConfig.recaptchaAction ?? 'any'} action`
114
+ })
115
+ }
116
+ : {}, { additionalProperties: true });
117
+ }
118
+ function createCurrentAuthUserSchema(modelSchema) {
119
+ return {
120
+ tags: ['Auth'],
121
+ security: authSchema(),
122
+ summary: 'Return currently authorized identity',
123
+ description: 'Return currently authorized identity',
124
+ response: {
125
+ 200: modelSchema,
126
+ ...errors_1.AllErrorResponses
127
+ }
128
+ };
129
+ }
@@ -0,0 +1,117 @@
1
+ import { AuthScope, AuthSource, AuthUser, RouteConfig } from '@appweaver/common';
2
+ import { AuthTokens, JwtPayload, UserAdditionalData } from '../types';
3
+ export declare class AuthService {
4
+ /**
5
+ * Finds an authenticated user by their unique identifier.
6
+ *
7
+ * @param {number} id - The unique identifier of the authenticated user to find.
8
+ * @return {Promise<AuthUser | null>} A promise that resolves to the authenticated user object if found, otherwise
9
+ * null.
10
+ */
11
+ findById(id: number): Promise<AuthUser | null>;
12
+ /**
13
+ * Retrieves a user by their username.
14
+ *
15
+ * @param {string} username - The username to search for.
16
+ * @return {Promise<AuthUser | null>} A promise that resolves to the user object if found, or null if not found.
17
+ */
18
+ findByUsername(username: string): Promise<AuthUser | null>;
19
+ /**
20
+ * Updates an authenticated user's information in the system.
21
+ *
22
+ * @param {number} id - The unique identifier of the authenticated user to be updated.
23
+ * @param {Partial<AuthUser> & { password?: string }} data - The partial user data to update, optionally including a
24
+ * password.
25
+ * @return {Promise<AuthUser>} A promise that resolves to the updated authenticated user object.
26
+ */
27
+ updateAuthUser(id: number, data: Partial<AuthUser> & {
28
+ password?: string;
29
+ }): Promise<AuthUser>;
30
+ /**
31
+ * Registers a new authenticated user based on the provided authentication source, email, and optional password and
32
+ * additional data.
33
+ *
34
+ * @param {AuthSource} source - The authentication source to register the user, e.g., password, google, custom.
35
+ * @param {string} email - The email address of the user to be registered.
36
+ * @param {string} [password] - The optional password for the user, used when the authentication type requires it.
37
+ * @param {Partial<UserAdditionalData>} [data] - Optional additional data related to the user to be stored during
38
+ * registration.
39
+ * @return {Promise<AuthUser>} A promise resolving to the registered authentication user object.
40
+ * @throws {HttpError} Throws an error if the registration process fails.
41
+ */
42
+ registerAuthUser(source: AuthSource, email: string, password?: string, data?: Partial<UserAdditionalData>): Promise<AuthUser>;
43
+ /**
44
+ * Changes the password for the authenticated user.
45
+ *
46
+ * @param {AuthUser} authUser - The authenticated user object containing user details.
47
+ * @param {string} currentPassword - The current password of the authenticated user.
48
+ * @param {string} newPassword - The new password to be set for the authenticated user.
49
+ * @return {Promise<AuthTokens>} A promise that resolves to the new authentication tokens after the password is
50
+ * successfully changed.
51
+ * @throws {HttpError} If the current password does not match the stored password hash.
52
+ */
53
+ changePassword(authUser: AuthUser, currentPassword: string, newPassword: string): Promise<AuthTokens>;
54
+ /**
55
+ * Authenticates a user by verifying their username and password.
56
+ *
57
+ * @param {string} username - The username of the user attempting to authenticate.
58
+ * @param {string} password - The password of the user attempting to authenticate.
59
+ * @return {Promise<AuthUser>} A promise that resolves to an authenticated user object if the credentials are valid.
60
+ * @throws {HttpError} If the user does not exist, is disabled, or if the provided credentials are invalid.
61
+ */
62
+ authenticate(username: string, password: string): Promise<AuthUser>;
63
+ /**
64
+ * Authorizes a user based on provided authentication details, route configuration, and JWT payload
65
+ * (if JWT auth type is used).
66
+ *
67
+ * @param {AuthUser | null} authUser - The authenticated user object. Can be null if no user is authenticated.
68
+ * @param {string} url - The URL of the resource being accessed.
69
+ * @param {RouteConfig} [routeConfig={}] - The route configuration object specifying required roles and permissions.
70
+ * @param {JwtPayload} [jwtPayload] - The JWT payload containing additional authentication information.
71
+ * @throws {HttpError} If the user is not authorized to access url based on route config and JWT payload.
72
+ */
73
+ authorize(authUser: AuthUser | null, url: string, routeConfig?: RouteConfig, jwtPayload?: JwtPayload): void;
74
+ /**
75
+ * Authenticates a user based on the provided username and password and generate access tokens.
76
+ * If 2FA is forced at configuration level or auth user has enabled 2FA, then generate JWT with 2FA scope,
77
+ * otherwise return regular JWT with authentication scope.
78
+ *
79
+ * @param {string} username - The username of the user attempting to log in.
80
+ * @param {string} password - The password of the user attempting to log in.
81
+ * @return {Promise<AuthTokens>} A promise that resolves to an object containing authentication tokens if login is
82
+ * successful.
83
+ * @throws {HttpError} If the user does not exist, is disabled, or provides invalid credentials.
84
+ */
85
+ login(username: string, password: string): Promise<AuthTokens>;
86
+ /**
87
+ * Exchanges an existing token for new authentication tokens.
88
+ *
89
+ * @param {string} token - The token to be exchanged for new authentication tokens.
90
+ * @return {Promise<AuthTokens>} A promise that resolves to a set of new authentication tokens.
91
+ * @throws {HttpError} If the token is invalid, expired, or associated with a non-existent or disabled user.
92
+ */
93
+ exchangeToken(token: string): Promise<AuthTokens>;
94
+ /**
95
+ * Generates authentication tokens (access and refresh tokens) for a given authenticated user.
96
+ *
97
+ * @param {AuthUser} authUser - The authenticated user for whom the tokens are to be generated.
98
+ * Contains user identification and authentication details.
99
+ * @param {AuthScope} [scope=AuthScope.Auth] - The scope for which the tokens are generated.
100
+ * @param {AuthSource} [source=AuthSource.Password] - The authentication source used to generate access tokens.
101
+ * @return {Promise<AuthTokens>} A promise that resolves to an object containing the generated authentication tokens:
102
+ * - `accessToken` (string): The access token for API access.
103
+ * - `refreshToken` (string): The refresh token for acquiring new access tokens.
104
+ * - `expiresIn` (number): The expiration time (in seconds) of the access token.
105
+ * - `refreshExpiresIn` (number): The expiration time (in seconds) of the refresh token.
106
+ * @throws {HttpError} If the server instance is not initialized.
107
+ */
108
+ generateAuthTokens(authUser: AuthUser, scope?: AuthScope, source?: AuthSource): Promise<AuthTokens>;
109
+ /**
110
+ * Logs out a user by updating their authentication information with a logout timestamp.
111
+ *
112
+ * @param {number} id - The unique identifier of the user to be logged out.
113
+ * @return {Promise<boolean>} A promise that resolves to a boolean indicating whether the logout operation was
114
+ * successful.
115
+ */
116
+ logout(id: number): Promise<boolean>;
117
+ }