@vandenberghinc/volt 1.1.26 → 1.1.28

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 (502) hide show
  1. package/backend/dist/cjs/{blacklist.d.ts → backend/src/blacklist.d.ts} +5 -3
  2. package/backend/dist/cjs/{blacklist.js → backend/src/blacklist.js} +8 -5
  3. package/backend/dist/cjs/{cli.js → backend/src/cli.js} +29 -47
  4. package/backend/dist/cjs/backend/src/database/collection.d.ts +1543 -0
  5. package/backend/dist/cjs/backend/src/database/collection.js +3042 -0
  6. package/backend/dist/cjs/backend/src/database/database.d.ts +66 -0
  7. package/backend/dist/cjs/{database → backend/src/database}/database.js +48 -43
  8. package/backend/dist/cjs/backend/src/database/filters/filters.d.ts +6 -0
  9. package/backend/dist/cjs/backend/src/database/filters/filters.js +15 -0
  10. package/backend/dist/cjs/backend/src/database/filters/strict_filter.d.ts +223 -0
  11. package/backend/dist/cjs/backend/src/database/filters/strict_filter.js +15 -0
  12. package/backend/dist/cjs/backend/src/database/filters/strict_filter_test.js +443 -0
  13. package/backend/dist/cjs/backend/src/database/filters/strict_filter_test_v0.js +15 -0
  14. package/backend/dist/cjs/backend/src/database/filters/strict_filter_v0.d.ts +50 -0
  15. package/backend/dist/cjs/backend/src/database/filters/strict_filter_v0.js +15 -0
  16. package/backend/dist/cjs/backend/src/database/filters/strict_filter_v1.d.ts +76 -0
  17. package/backend/dist/cjs/backend/src/database/filters/strict_filter_v1.js +15 -0
  18. package/backend/dist/cjs/backend/src/database/filters/strict_filter_v2.d.ts +75 -0
  19. package/backend/dist/cjs/backend/src/database/filters/strict_filter_v2.js +15 -0
  20. package/backend/dist/cjs/backend/src/database/filters/strict_filter_v3.d.ts +219 -0
  21. package/backend/dist/cjs/backend/src/database/filters/strict_filter_v3.js +15 -0
  22. package/backend/dist/cjs/backend/src/database/filters/strict_update_filter.d.ts +165 -0
  23. package/backend/dist/cjs/backend/src/database/filters/strict_update_filter.js +15 -0
  24. package/backend/dist/cjs/backend/src/database/filters/strict_update_filter_test.d.ts +5 -0
  25. package/backend/dist/cjs/backend/src/database/filters/strict_update_filter_test.js +355 -0
  26. package/backend/dist/cjs/backend/src/database/flatten.d.ts +75 -0
  27. package/backend/dist/cjs/{logger.js → backend/src/database/flatten.js} +18 -7
  28. package/backend/dist/cjs/backend/src/database/flatten_test.js +175 -0
  29. package/backend/dist/cjs/backend/src/database/quota/quota.d.ts +461 -0
  30. package/backend/dist/cjs/backend/src/database/quota/quota.js +1014 -0
  31. package/backend/dist/cjs/backend/src/database/quota/quota_v1.d.ts +534 -0
  32. package/backend/dist/cjs/backend/src/database/quota/quota_v1.js +1087 -0
  33. package/backend/dist/cjs/backend/src/database/quota/safe_int.d.ts +293 -0
  34. package/backend/dist/cjs/backend/src/database/quota/safe_int.js +573 -0
  35. package/backend/dist/{esm → cjs/backend/src}/endpoint.d.ts +69 -46
  36. package/backend/dist/cjs/{endpoint.js → backend/src/endpoint.js} +87 -101
  37. package/backend/dist/cjs/backend/src/errors/index.d.ts +7 -0
  38. package/backend/dist/cjs/backend/src/errors/index.js +25 -0
  39. package/backend/dist/{esm/utils.d.ts → cjs/backend/src/errors/internal_external.d.ts} +14 -22
  40. package/backend/dist/cjs/backend/src/errors/internal_external.js +85 -0
  41. package/backend/dist/cjs/backend/src/errors/invalid_usage_error.d.ts +38 -0
  42. package/backend/dist/cjs/{mutex.js → backend/src/errors/invalid_usage_error.js} +20 -37
  43. package/backend/dist/cjs/backend/src/errors/system_error.d.ts +230 -0
  44. package/backend/dist/cjs/backend/src/errors/system_error.js +393 -0
  45. package/backend/dist/cjs/backend/src/events.d.ts +54 -0
  46. package/backend/dist/cjs/backend/src/events.js +15 -0
  47. package/backend/dist/cjs/{frontend.js → backend/src/frontend.js} +1 -1
  48. package/backend/dist/cjs/{image_endpoint.d.ts → backend/src/image_endpoint.d.ts} +16 -1
  49. package/backend/dist/cjs/{image_endpoint.js → backend/src/image_endpoint.js} +3 -5
  50. package/backend/dist/cjs/backend/src/logger.d.ts +5 -0
  51. package/backend/dist/cjs/backend/src/logger.js +15 -0
  52. package/backend/dist/cjs/backend/src/meta.d.ts +64 -0
  53. package/backend/dist/cjs/{meta.js → backend/src/meta.js} +9 -12
  54. package/backend/dist/cjs/backend/src/payments/paddle.d.ts +326 -0
  55. package/backend/dist/cjs/{payments → backend/src/payments}/paddle.js +377 -327
  56. package/backend/dist/cjs/backend/src/plugins/browser.d.ts +1 -0
  57. package/backend/dist/cjs/backend/src/plugins/browser.js +15 -0
  58. package/backend/dist/cjs/backend/src/plugins/mail/mail.d.ts +248 -0
  59. package/backend/dist/cjs/backend/src/plugins/mail/mail.js +379 -0
  60. package/backend/dist/{esm → cjs/backend/src}/plugins/mail/ui.d.ts +23 -0
  61. package/backend/dist/cjs/backend/src/plugins/pdf.d.ts +1 -0
  62. package/backend/dist/cjs/backend/src/rate_limit.d.ts +145 -0
  63. package/backend/dist/cjs/backend/src/rate_limit.js +549 -0
  64. package/backend/dist/cjs/{route.d.ts → backend/src/route.d.ts} +3 -10
  65. package/backend/dist/cjs/{route.js → backend/src/route.js} +23 -21
  66. package/backend/dist/cjs/backend/src/server.d.ts +485 -0
  67. package/backend/dist/cjs/{server.js → backend/src/server.js} +688 -873
  68. package/backend/dist/cjs/backend/src/splash_screen.d.ts +80 -0
  69. package/backend/dist/cjs/{splash_screen.js → backend/src/splash_screen.js} +24 -3
  70. package/backend/dist/cjs/backend/src/status.d.ts +74 -0
  71. package/backend/dist/cjs/{status.js → backend/src/status.js} +64 -64
  72. package/backend/dist/cjs/backend/src/stream.d.ts +376 -0
  73. package/backend/dist/cjs/{stream.js → backend/src/stream.js} +299 -276
  74. package/backend/dist/cjs/backend/src/users.d.ts +807 -0
  75. package/backend/dist/cjs/backend/src/users.js +1971 -0
  76. package/backend/dist/cjs/backend/src/utils.d.ts +16 -0
  77. package/backend/dist/cjs/{utils.js → backend/src/utils.js} +14 -77
  78. package/backend/dist/{esm → cjs/backend/src}/view.d.ts +33 -11
  79. package/backend/dist/cjs/backend/src/view.js +508 -0
  80. package/backend/dist/{esm → cjs/backend/src}/volt.d.ts +10 -1
  81. package/backend/dist/cjs/{volt.js → backend/src/volt.js} +8 -5
  82. package/backend/dist/cjs/frontend/src/modules/request.d.ts +70 -0
  83. package/backend/dist/cjs/frontend/src/modules/request.js +99 -0
  84. package/backend/dist/esm/{blacklist.d.ts → backend/src/blacklist.d.ts} +5 -3
  85. package/backend/dist/esm/{blacklist.js → backend/src/blacklist.js} +9 -6
  86. package/backend/dist/esm/{cli.js → backend/src/cli.js} +43 -60
  87. package/backend/dist/esm/backend/src/database/collection.d.ts +1543 -0
  88. package/backend/dist/esm/backend/src/database/collection.js +3510 -0
  89. package/backend/dist/esm/backend/src/database/database.d.ts +66 -0
  90. package/backend/dist/esm/{database → backend/src/database}/database.js +62 -103
  91. package/backend/dist/esm/backend/src/database/document.d.ts +1 -0
  92. package/backend/dist/esm/backend/src/database/document.js +558 -0
  93. package/backend/dist/esm/backend/src/database/filters/filters.d.ts +6 -0
  94. package/backend/dist/esm/backend/src/database/filters/filters.js +1 -0
  95. package/backend/dist/esm/backend/src/database/filters/strict_filter.d.ts +223 -0
  96. package/backend/dist/esm/backend/src/database/filters/strict_filter.js +3 -0
  97. package/backend/dist/esm/backend/src/database/filters/strict_filter_test.d.ts +1 -0
  98. package/backend/dist/esm/backend/src/database/filters/strict_filter_test.js +505 -0
  99. package/backend/dist/esm/backend/src/database/filters/strict_filter_test_v0.d.ts +1 -0
  100. package/backend/dist/esm/backend/src/database/filters/strict_filter_test_v0.js +712 -0
  101. package/backend/dist/esm/backend/src/database/filters/strict_filter_v0.d.ts +50 -0
  102. package/backend/dist/esm/backend/src/database/filters/strict_filter_v0.js +5 -0
  103. package/backend/dist/esm/backend/src/database/filters/strict_filter_v1.d.ts +76 -0
  104. package/backend/dist/esm/backend/src/database/filters/strict_filter_v1.js +44 -0
  105. package/backend/dist/esm/backend/src/database/filters/strict_filter_v2.d.ts +75 -0
  106. package/backend/dist/esm/backend/src/database/filters/strict_filter_v2.js +5 -0
  107. package/backend/dist/esm/backend/src/database/filters/strict_filter_v3.d.ts +219 -0
  108. package/backend/dist/esm/backend/src/database/filters/strict_filter_v3.js +1 -0
  109. package/backend/dist/esm/backend/src/database/filters/strict_update_filter.d.ts +165 -0
  110. package/backend/dist/esm/backend/src/database/filters/strict_update_filter.js +5 -0
  111. package/backend/dist/esm/backend/src/database/filters/strict_update_filter_test.d.ts +5 -0
  112. package/backend/dist/esm/backend/src/database/filters/strict_update_filter_test.js +405 -0
  113. package/backend/dist/esm/backend/src/database/flatten.d.ts +75 -0
  114. package/backend/dist/esm/backend/src/database/flatten.js +22 -0
  115. package/backend/dist/esm/backend/src/database/flatten_test.d.ts +1 -0
  116. package/backend/dist/esm/backend/src/database/flatten_test.js +174 -0
  117. package/backend/dist/esm/backend/src/database/quota/quota.d.ts +461 -0
  118. package/backend/dist/esm/backend/src/database/quota/quota.js +1118 -0
  119. package/backend/dist/esm/backend/src/database/quota/quota_v1.d.ts +534 -0
  120. package/backend/dist/esm/backend/src/database/quota/quota_v1.js +1242 -0
  121. package/backend/dist/esm/backend/src/database/quota/safe_int.d.ts +293 -0
  122. package/backend/dist/esm/backend/src/database/quota/safe_int.js +602 -0
  123. package/backend/dist/{cjs → esm/backend/src}/endpoint.d.ts +69 -46
  124. package/backend/dist/esm/{endpoint.js → backend/src/endpoint.js} +136 -127
  125. package/backend/dist/esm/backend/src/errors/index.d.ts +7 -0
  126. package/backend/dist/esm/backend/src/errors/index.js +7 -0
  127. package/backend/dist/{cjs/utils.d.ts → esm/backend/src/errors/internal_external.d.ts} +14 -22
  128. package/backend/dist/esm/backend/src/errors/internal_external.js +70 -0
  129. package/backend/dist/esm/backend/src/errors/invalid_usage_error.d.ts +38 -0
  130. package/backend/dist/esm/backend/src/errors/invalid_usage_error.js +30 -0
  131. package/backend/dist/esm/backend/src/errors/system_error.d.ts +230 -0
  132. package/backend/dist/esm/backend/src/errors/system_error.js +402 -0
  133. package/backend/dist/esm/backend/src/events.d.ts +54 -0
  134. package/backend/dist/esm/backend/src/events.js +5 -0
  135. package/backend/dist/esm/{frontend.js → backend/src/frontend.js} +1 -1
  136. package/backend/dist/esm/{image_endpoint.d.ts → backend/src/image_endpoint.d.ts} +16 -1
  137. package/backend/dist/esm/{image_endpoint.js → backend/src/image_endpoint.js} +16 -20
  138. package/backend/dist/esm/backend/src/logger.d.ts +5 -0
  139. package/backend/dist/esm/backend/src/logger.js +8 -0
  140. package/backend/dist/esm/backend/src/meta.d.ts +64 -0
  141. package/backend/dist/esm/{meta.js → backend/src/meta.js} +15 -54
  142. package/backend/dist/esm/backend/src/payments/paddle.d.ts +326 -0
  143. package/backend/dist/esm/{payments → backend/src/payments}/paddle.js +417 -452
  144. package/backend/dist/esm/backend/src/plugins/browser.d.ts +1 -0
  145. package/backend/dist/esm/backend/src/plugins/browser.js +170 -0
  146. package/backend/dist/esm/backend/src/plugins/mail/mail.d.ts +248 -0
  147. package/backend/dist/esm/backend/src/plugins/mail/mail.js +389 -0
  148. package/backend/dist/{cjs → esm/backend/src}/plugins/mail/ui.d.ts +23 -0
  149. package/backend/dist/esm/{plugins → backend/src/plugins}/mail/ui.js +3 -6
  150. package/backend/dist/esm/backend/src/plugins/pdf.d.ts +1 -0
  151. package/backend/dist/esm/{plugins → backend/src/plugins}/pdf.js +3 -3
  152. package/backend/dist/esm/backend/src/rate_limit.d.ts +145 -0
  153. package/backend/dist/esm/backend/src/rate_limit.js +667 -0
  154. package/backend/dist/esm/{route.d.ts → backend/src/route.d.ts} +3 -10
  155. package/backend/dist/esm/{route.js → backend/src/route.js} +26 -21
  156. package/backend/dist/esm/backend/src/server.d.ts +485 -0
  157. package/backend/dist/esm/{server.js → backend/src/server.js} +891 -1441
  158. package/backend/dist/esm/backend/src/splash_screen.d.ts +80 -0
  159. package/backend/dist/esm/{splash_screen.js → backend/src/splash_screen.js} +42 -55
  160. package/backend/dist/esm/backend/src/status.d.ts +74 -0
  161. package/backend/dist/esm/backend/src/status.js +199 -0
  162. package/backend/dist/esm/backend/src/stream.d.ts +376 -0
  163. package/backend/dist/esm/{stream.js → backend/src/stream.js} +327 -292
  164. package/backend/dist/esm/backend/src/users.d.ts +809 -0
  165. package/backend/dist/esm/backend/src/users.js +2140 -0
  166. package/backend/dist/esm/backend/src/utils.d.ts +16 -0
  167. package/backend/dist/esm/{utils.js → backend/src/utils.js} +20 -81
  168. package/backend/dist/{cjs → esm/backend/src}/view.d.ts +33 -11
  169. package/backend/dist/esm/{view.js → backend/src/view.js} +266 -86
  170. package/backend/dist/{cjs → esm/backend/src}/volt.d.ts +10 -1
  171. package/backend/dist/esm/{volt.js → backend/src/volt.js} +7 -4
  172. package/backend/dist/esm/frontend/src/modules/request.d.ts +70 -0
  173. package/backend/dist/esm/frontend/src/modules/request.js +117 -0
  174. package/frontend/dist/backend/src/database/collection.d.ts +1543 -0
  175. package/frontend/dist/backend/src/database/collection.js +3510 -0
  176. package/frontend/dist/backend/src/database/database.d.ts +66 -0
  177. package/frontend/dist/backend/src/database/database.js +196 -0
  178. package/frontend/dist/backend/src/database/filters/filters.d.ts +6 -0
  179. package/frontend/dist/backend/src/database/filters/filters.js +1 -0
  180. package/frontend/dist/backend/src/database/filters/strict_filter.d.ts +223 -0
  181. package/frontend/dist/backend/src/database/filters/strict_filter.js +3 -0
  182. package/frontend/dist/backend/src/database/filters/strict_update_filter.d.ts +165 -0
  183. package/frontend/dist/backend/src/database/filters/strict_update_filter.js +5 -0
  184. package/frontend/dist/backend/src/database/flatten.d.ts +75 -0
  185. package/frontend/dist/backend/src/database/flatten.js +22 -0
  186. package/frontend/dist/backend/src/endpoint.d.ts +204 -0
  187. package/frontend/dist/backend/src/endpoint.js +570 -0
  188. package/frontend/dist/backend/src/errors/index.d.ts +7 -0
  189. package/frontend/dist/backend/src/errors/index.js +7 -0
  190. package/frontend/dist/backend/src/errors/internal_external.d.ts +38 -0
  191. package/frontend/dist/backend/src/errors/internal_external.js +70 -0
  192. package/frontend/dist/backend/src/errors/invalid_usage_error.d.ts +38 -0
  193. package/frontend/dist/backend/src/errors/invalid_usage_error.js +30 -0
  194. package/frontend/dist/backend/src/errors/system_error.d.ts +230 -0
  195. package/frontend/dist/backend/src/errors/system_error.js +402 -0
  196. package/frontend/dist/backend/src/events.d.ts +54 -0
  197. package/frontend/dist/backend/src/events.js +5 -0
  198. package/frontend/dist/backend/src/frontend.d.ts +11 -0
  199. package/frontend/dist/backend/src/frontend.js +12 -0
  200. package/frontend/dist/backend/src/image_endpoint.d.ts +39 -0
  201. package/frontend/dist/backend/src/image_endpoint.js +202 -0
  202. package/frontend/dist/backend/src/meta.d.ts +64 -0
  203. package/frontend/dist/backend/src/meta.js +110 -0
  204. package/frontend/dist/backend/src/payments/paddle.d.ts +326 -0
  205. package/frontend/dist/backend/src/payments/paddle.js +2256 -0
  206. package/frontend/dist/backend/src/plugins/mail/mail.d.ts +248 -0
  207. package/frontend/dist/backend/src/plugins/mail/mail.js +389 -0
  208. package/{backend/dist/esm/plugins/mail.d.ts → frontend/dist/backend/src/plugins/mail/ui.d.ts} +23 -0
  209. package/{backend/dist/esm/plugins/mail.js → frontend/dist/backend/src/plugins/mail/ui.js} +3 -6
  210. package/frontend/dist/backend/src/rate_limit.d.ts +145 -0
  211. package/frontend/dist/backend/src/rate_limit.js +673 -0
  212. package/frontend/dist/backend/src/route.d.ts +35 -0
  213. package/frontend/dist/backend/src/route.js +212 -0
  214. package/frontend/dist/backend/src/server.d.ts +485 -0
  215. package/frontend/dist/backend/src/server.js +2670 -0
  216. package/frontend/dist/backend/src/splash_screen.d.ts +80 -0
  217. package/frontend/dist/backend/src/splash_screen.js +135 -0
  218. package/frontend/dist/backend/src/status.d.ts +74 -0
  219. package/frontend/dist/backend/src/status.js +199 -0
  220. package/frontend/dist/backend/src/stream.d.ts +376 -0
  221. package/frontend/dist/backend/src/stream.js +1007 -0
  222. package/frontend/dist/backend/src/users.d.ts +807 -0
  223. package/frontend/dist/backend/src/users.js +2118 -0
  224. package/frontend/dist/backend/src/utils.d.ts +16 -0
  225. package/frontend/dist/backend/src/utils.js +241 -0
  226. package/frontend/dist/backend/src/view.d.ts +162 -0
  227. package/frontend/dist/backend/src/view.js +720 -0
  228. package/frontend/dist/frontend/src/elements/base.d.ts +4414 -0
  229. package/frontend/dist/{elements → frontend/src/elements}/base.js +3624 -260
  230. package/frontend/dist/frontend/src/elements/module.d.ts +95 -0
  231. package/frontend/dist/{elements → frontend/src/elements}/module.js +53 -52
  232. package/frontend/dist/frontend/src/elements/types.d.ts +52 -0
  233. package/frontend/dist/frontend/src/elements/types.js +5 -0
  234. package/frontend/dist/frontend/src/modules/attachment.d.ts +126 -0
  235. package/frontend/dist/frontend/src/modules/attachment.js +306 -0
  236. package/frontend/dist/frontend/src/modules/auth.d.ts +44 -0
  237. package/frontend/dist/frontend/src/modules/auth.js +80 -0
  238. package/frontend/dist/{modules → frontend/src/modules}/color.js +2 -2
  239. package/frontend/dist/frontend/src/modules/compression.d.ts +39 -0
  240. package/frontend/dist/frontend/src/modules/compression.js +102 -0
  241. package/frontend/dist/frontend/src/modules/cookies.d.ts +44 -0
  242. package/frontend/dist/frontend/src/modules/cookies.js +143 -0
  243. package/frontend/dist/frontend/src/modules/events.d.ts +31 -0
  244. package/frontend/dist/frontend/src/modules/events.js +74 -0
  245. package/frontend/dist/frontend/src/modules/google.d.ts +23 -0
  246. package/frontend/dist/frontend/src/modules/google.js +52 -0
  247. package/frontend/dist/frontend/src/modules/meta.d.ts +14 -0
  248. package/frontend/dist/{modules → frontend/src/modules}/meta.js +9 -7
  249. package/frontend/dist/{modules → frontend/src/modules}/paddle.d.ts +37 -134
  250. package/frontend/dist/{modules → frontend/src/modules}/paddle.js +620 -568
  251. package/frontend/dist/frontend/src/modules/request.d.ts +70 -0
  252. package/frontend/dist/frontend/src/modules/request.js +117 -0
  253. package/frontend/dist/frontend/src/modules/settings.d.ts +3 -0
  254. package/frontend/dist/frontend/src/modules/settings.js +5 -0
  255. package/frontend/dist/frontend/src/modules/statics.d.ts +21 -0
  256. package/frontend/dist/{modules → frontend/src/modules}/statics.js +15 -18
  257. package/frontend/dist/frontend/src/modules/support.d.ts +30 -0
  258. package/frontend/dist/frontend/src/modules/support.js +53 -0
  259. package/frontend/dist/{modules → frontend/src/modules}/theme.d.ts +67 -0
  260. package/frontend/dist/{modules → frontend/src/modules}/theme.js +68 -38
  261. package/frontend/dist/frontend/src/modules/themes.d.ts +12 -0
  262. package/frontend/dist/frontend/src/modules/themes.js +22 -0
  263. package/frontend/dist/frontend/src/modules/user.d.ts +164 -0
  264. package/frontend/dist/frontend/src/modules/user.js +268 -0
  265. package/frontend/dist/frontend/src/modules/utils.d.ts +176 -0
  266. package/frontend/dist/frontend/src/modules/utils.js +569 -0
  267. package/frontend/dist/frontend/src/types/gradient.d.ts +29 -0
  268. package/frontend/dist/{types → frontend/src/types}/gradient.js +14 -18
  269. package/frontend/dist/frontend/src/ui/border_button.d.ts +94 -0
  270. package/frontend/dist/{ui → frontend/src/ui}/border_button.js +7 -13
  271. package/frontend/dist/frontend/src/ui/button.d.ts +28 -0
  272. package/frontend/dist/{ui → frontend/src/ui}/button.js +21 -12
  273. package/frontend/dist/frontend/src/ui/canvas.d.ts +138 -0
  274. package/frontend/dist/{ui → frontend/src/ui}/canvas.js +88 -55
  275. package/frontend/dist/frontend/src/ui/checkbox.d.ts +74 -0
  276. package/frontend/dist/{ui → frontend/src/ui}/checkbox.js +80 -41
  277. package/frontend/dist/{ui → frontend/src/ui}/code.d.ts +73 -6
  278. package/frontend/dist/{ui → frontend/src/ui}/code.js +55 -52
  279. package/frontend/dist/{ui → frontend/src/ui}/context_menu.d.ts +4 -0
  280. package/frontend/dist/{ui → frontend/src/ui}/context_menu.js +12 -17
  281. package/frontend/dist/{ui → frontend/src/ui}/css.d.ts +4 -0
  282. package/frontend/dist/{ui → frontend/src/ui}/css.js +3 -3
  283. package/frontend/dist/{ui → frontend/src/ui}/divider.d.ts +4 -0
  284. package/frontend/dist/{ui → frontend/src/ui}/divider.js +3 -3
  285. package/frontend/dist/{ui → frontend/src/ui}/dropdown.d.ts +57 -2
  286. package/frontend/dist/{ui → frontend/src/ui}/dropdown.js +87 -94
  287. package/frontend/dist/{ui → frontend/src/ui}/for_each.d.ts +4 -0
  288. package/frontend/dist/{ui → frontend/src/ui}/for_each.js +3 -3
  289. package/frontend/dist/{ui → frontend/src/ui}/form.d.ts +6 -2
  290. package/frontend/dist/{ui → frontend/src/ui}/form.js +10 -7
  291. package/frontend/dist/frontend/src/ui/frame_modes.d.ts +37 -0
  292. package/frontend/dist/{ui → frontend/src/ui}/frame_modes.js +16 -22
  293. package/frontend/dist/{ui → frontend/src/ui}/google_map.d.ts +4 -0
  294. package/frontend/dist/{ui → frontend/src/ui}/google_map.js +4 -4
  295. package/frontend/dist/{ui → frontend/src/ui}/gradient.d.ts +4 -0
  296. package/frontend/dist/{ui → frontend/src/ui}/gradient.js +3 -3
  297. package/frontend/dist/{ui → frontend/src/ui}/image.d.ts +4 -0
  298. package/frontend/dist/{ui → frontend/src/ui}/image.js +5 -5
  299. package/frontend/dist/frontend/src/ui/input.d.ts +392 -0
  300. package/frontend/dist/{ui → frontend/src/ui}/input.js +346 -360
  301. package/frontend/dist/{ui → frontend/src/ui}/link.d.ts +4 -0
  302. package/frontend/dist/{ui → frontend/src/ui}/link.js +3 -3
  303. package/frontend/dist/{ui → frontend/src/ui}/list.d.ts +4 -0
  304. package/frontend/dist/{ui → frontend/src/ui}/list.js +12 -6
  305. package/frontend/dist/frontend/src/ui/loader_button.d.ts +80 -0
  306. package/frontend/dist/{ui → frontend/src/ui}/loader_button.js +35 -47
  307. package/frontend/dist/frontend/src/ui/loaders.d.ts +57 -0
  308. package/frontend/dist/{ui → frontend/src/ui}/loaders.js +11 -11
  309. package/frontend/dist/{ui → frontend/src/ui}/popup.d.ts +11 -6
  310. package/frontend/dist/{ui → frontend/src/ui}/popup.js +32 -18
  311. package/frontend/dist/frontend/src/ui/pseudo.d.ts +44 -0
  312. package/frontend/dist/{ui → frontend/src/ui}/pseudo.js +84 -8
  313. package/frontend/dist/{ui → frontend/src/ui}/scroller.d.ts +14 -2
  314. package/frontend/dist/{ui → frontend/src/ui}/scroller.js +37 -43
  315. package/frontend/dist/{ui → frontend/src/ui}/slider.d.ts +5 -1
  316. package/frontend/dist/{ui → frontend/src/ui}/slider.js +4 -4
  317. package/frontend/dist/{ui → frontend/src/ui}/spacer.d.ts +4 -0
  318. package/frontend/dist/{ui → frontend/src/ui}/spacer.js +3 -3
  319. package/frontend/dist/{ui → frontend/src/ui}/span.d.ts +4 -0
  320. package/frontend/dist/{ui → frontend/src/ui}/span.js +3 -3
  321. package/frontend/dist/{ui → frontend/src/ui}/stack.d.ts +4 -0
  322. package/frontend/dist/{ui → frontend/src/ui}/stack.js +3 -9
  323. package/frontend/dist/frontend/src/ui/steps.d.ts +131 -0
  324. package/frontend/dist/{ui → frontend/src/ui}/steps.js +30 -45
  325. package/frontend/dist/{ui → frontend/src/ui}/style.d.ts +4 -0
  326. package/frontend/dist/{ui → frontend/src/ui}/style.js +3 -3
  327. package/frontend/dist/{ui → frontend/src/ui}/switch.d.ts +5 -1
  328. package/frontend/dist/{ui → frontend/src/ui}/switch.js +4 -4
  329. package/frontend/dist/{ui → frontend/src/ui}/table.d.ts +4 -0
  330. package/frontend/dist/{ui → frontend/src/ui}/table.js +6 -6
  331. package/frontend/dist/{ui → frontend/src/ui}/tabs.d.ts +45 -3
  332. package/frontend/dist/{ui → frontend/src/ui}/tabs.js +65 -40
  333. package/frontend/dist/{ui → frontend/src/ui}/text.d.ts +4 -0
  334. package/frontend/dist/{ui → frontend/src/ui}/text.js +3 -3
  335. package/frontend/dist/frontend/src/ui/title.d.ts +91 -0
  336. package/frontend/dist/frontend/src/ui/title.js +272 -0
  337. package/frontend/dist/{ui → frontend/src/ui}/view.d.ts +4 -0
  338. package/frontend/dist/{ui → frontend/src/ui}/view.js +3 -3
  339. package/frontend/dist/{volt.d.ts → frontend/src/volt.d.ts} +3 -0
  340. package/frontend/dist/{volt.js → frontend/src/volt.js} +4 -0
  341. package/frontend/tools/bundle_d_ts.js +71 -0
  342. package/frontend/tools/convert_to_jsdoc_input.txt +9452 -0
  343. package/frontend/tools/convert_to_jsdoc_output.txt +7626 -0
  344. package/frontend/tools/convert_to_jsdoc_tmp.js +345 -0
  345. package/package.json +11 -12
  346. package/backend/dist/cjs/database/collection.d.ts +0 -160
  347. package/backend/dist/cjs/database/collection.js +0 -842
  348. package/backend/dist/cjs/database/database.d.ts +0 -121
  349. package/backend/dist/cjs/database/document.d.ts +0 -131
  350. package/backend/dist/cjs/database/document.js +0 -224
  351. package/backend/dist/cjs/database.d.ts +0 -502
  352. package/backend/dist/cjs/database.js +0 -2248
  353. package/backend/dist/cjs/logger.d.ts +0 -3
  354. package/backend/dist/cjs/meta.d.ts +0 -50
  355. package/backend/dist/cjs/mutex.d.ts +0 -24
  356. package/backend/dist/cjs/payments/paddle.d.ts +0 -160
  357. package/backend/dist/cjs/plugins/browser.d.ts +0 -36
  358. package/backend/dist/cjs/plugins/browser.js +0 -198
  359. package/backend/dist/cjs/plugins/css.d.ts +0 -11
  360. package/backend/dist/cjs/plugins/css.js +0 -80
  361. package/backend/dist/cjs/plugins/mail.d.ts +0 -277
  362. package/backend/dist/cjs/plugins/mail.js +0 -1370
  363. package/backend/dist/cjs/plugins/ts/compiler.d.ts +0 -139
  364. package/backend/dist/cjs/plugins/ts/compiler.js +0 -750
  365. package/backend/dist/cjs/plugins/ts/preprocessing.d.ts +0 -14
  366. package/backend/dist/cjs/plugins/ts/preprocessing.js +0 -440
  367. package/backend/dist/cjs/rate_limit.d.ts +0 -63
  368. package/backend/dist/cjs/rate_limit.js +0 -348
  369. package/backend/dist/cjs/request.deprc.d.ts +0 -48
  370. package/backend/dist/cjs/request.deprc.js +0 -572
  371. package/backend/dist/cjs/response.deprc.d.ts +0 -55
  372. package/backend/dist/cjs/response.deprc.js +0 -275
  373. package/backend/dist/cjs/server.d.ts +0 -342
  374. package/backend/dist/cjs/splash_screen.d.ts +0 -35
  375. package/backend/dist/cjs/status.d.ts +0 -61
  376. package/backend/dist/cjs/stream.d.ts +0 -79
  377. package/backend/dist/cjs/users.d.ts +0 -111
  378. package/backend/dist/cjs/users.js +0 -1817
  379. package/backend/dist/cjs/view.js +0 -352
  380. package/backend/dist/cjs/vinc.dev.d.ts +0 -3
  381. package/backend/dist/cjs/vinc.dev.js +0 -7
  382. package/backend/dist/css/adyen.css +0 -92
  383. package/backend/dist/css/volt.css +0 -70
  384. package/backend/dist/esm/database/collection.d.ts +0 -160
  385. package/backend/dist/esm/database/collection.js +0 -1328
  386. package/backend/dist/esm/database/database.d.ts +0 -121
  387. package/backend/dist/esm/database/document.d.ts +0 -131
  388. package/backend/dist/esm/database/document.js +0 -247
  389. package/backend/dist/esm/database.d.ts +0 -502
  390. package/backend/dist/esm/database.js +0 -2423
  391. package/backend/dist/esm/file_watcher.js +0 -329
  392. package/backend/dist/esm/logger.d.ts +0 -3
  393. package/backend/dist/esm/logger.js +0 -11
  394. package/backend/dist/esm/meta.d.ts +0 -50
  395. package/backend/dist/esm/mutex.d.ts +0 -24
  396. package/backend/dist/esm/mutex.js +0 -48
  397. package/backend/dist/esm/payments/paddle.d.ts +0 -160
  398. package/backend/dist/esm/plugins/browser.d.ts +0 -36
  399. package/backend/dist/esm/plugins/browser.js +0 -176
  400. package/backend/dist/esm/plugins/css.d.ts +0 -11
  401. package/backend/dist/esm/plugins/css.js +0 -90
  402. package/backend/dist/esm/plugins/ts/compiler.d.ts +0 -139
  403. package/backend/dist/esm/plugins/ts/compiler.js +0 -1194
  404. package/backend/dist/esm/plugins/ts/preprocessing.d.ts +0 -14
  405. package/backend/dist/esm/plugins/ts/preprocessing.js +0 -726
  406. package/backend/dist/esm/rate_limit.d.ts +0 -63
  407. package/backend/dist/esm/rate_limit.js +0 -417
  408. package/backend/dist/esm/request.deprc.d.ts +0 -48
  409. package/backend/dist/esm/request.deprc.js +0 -572
  410. package/backend/dist/esm/response.deprc.d.ts +0 -55
  411. package/backend/dist/esm/response.deprc.js +0 -275
  412. package/backend/dist/esm/server.d.ts +0 -342
  413. package/backend/dist/esm/splash_screen.d.ts +0 -35
  414. package/backend/dist/esm/status.d.ts +0 -61
  415. package/backend/dist/esm/status.js +0 -197
  416. package/backend/dist/esm/stream.d.ts +0 -79
  417. package/backend/dist/esm/users.d.ts +0 -111
  418. package/backend/dist/esm/users.js +0 -1935
  419. package/backend/dist/esm/vinc.dev.d.ts +0 -3
  420. package/backend/dist/esm/vinc.dev.js +0 -7
  421. package/frontend/dist/elements/base.d.ts +0 -9889
  422. package/frontend/dist/elements/module.d.ts +0 -30
  423. package/frontend/dist/modules/array.d.ts +0 -94
  424. package/frontend/dist/modules/array.js +0 -634
  425. package/frontend/dist/modules/auth.d.ts +0 -46
  426. package/frontend/dist/modules/auth.js +0 -139
  427. package/frontend/dist/modules/colors.d.ts +0 -1
  428. package/frontend/dist/modules/colors.js +0 -417
  429. package/frontend/dist/modules/compression.d.ts +0 -6
  430. package/frontend/dist/modules/compression.js +0 -999
  431. package/frontend/dist/modules/cookies.d.ts +0 -18
  432. package/frontend/dist/modules/cookies.js +0 -167
  433. package/frontend/dist/modules/date.d.ts +0 -142
  434. package/frontend/dist/modules/date.js +0 -493
  435. package/frontend/dist/modules/events.d.ts +0 -8
  436. package/frontend/dist/modules/events.js +0 -91
  437. package/frontend/dist/modules/google.d.ts +0 -11
  438. package/frontend/dist/modules/google.js +0 -54
  439. package/frontend/dist/modules/meta.d.ts +0 -10
  440. package/frontend/dist/modules/mutex.d.ts +0 -7
  441. package/frontend/dist/modules/mutex.js +0 -51
  442. package/frontend/dist/modules/number.d.ts +0 -16
  443. package/frontend/dist/modules/number.js +0 -23
  444. package/frontend/dist/modules/object.d.ts +0 -52
  445. package/frontend/dist/modules/object.js +0 -383
  446. package/frontend/dist/modules/scheme.d.ts +0 -227
  447. package/frontend/dist/modules/scheme.js +0 -531
  448. package/frontend/dist/modules/settings.d.ts +0 -3
  449. package/frontend/dist/modules/settings.js +0 -4
  450. package/frontend/dist/modules/statics.d.ts +0 -5
  451. package/frontend/dist/modules/string.d.ts +0 -124
  452. package/frontend/dist/modules/string.js +0 -745
  453. package/frontend/dist/modules/support.d.ts +0 -19
  454. package/frontend/dist/modules/support.js +0 -103
  455. package/frontend/dist/modules/themes.d.ts +0 -8
  456. package/frontend/dist/modules/themes.js +0 -18
  457. package/frontend/dist/modules/user.d.ts +0 -59
  458. package/frontend/dist/modules/user.js +0 -280
  459. package/frontend/dist/modules/utils.d.ts +0 -87
  460. package/frontend/dist/modules/utils.js +0 -923
  461. package/frontend/dist/types/gradient.d.ts +0 -12
  462. package/frontend/dist/ui/border_button.d.ts +0 -152
  463. package/frontend/dist/ui/button.d.ts +0 -21
  464. package/frontend/dist/ui/canvas.d.ts +0 -56
  465. package/frontend/dist/ui/checkbox.d.ts +0 -52
  466. package/frontend/dist/ui/frame_modes.d.ts +0 -25
  467. package/frontend/dist/ui/input.d.ts +0 -241
  468. package/frontend/dist/ui/loader_button.d.ts +0 -93
  469. package/frontend/dist/ui/loaders.d.ts +0 -57
  470. package/frontend/dist/ui/pseudo.d.ts +0 -16
  471. package/frontend/dist/ui/steps.d.ts +0 -59
  472. package/frontend/dist/ui/title.d.ts +0 -21
  473. package/frontend/dist/ui/title.js +0 -121
  474. package/frontend/examples/dashboard/dashboard.ts +0 -776
  475. /package/backend/dist/cjs/{cli.d.ts → backend/src/cli.d.ts} +0 -0
  476. /package/backend/dist/cjs/{file_watcher.d.ts → backend/src/database/document.d.ts} +0 -0
  477. /package/backend/dist/cjs/{file_watcher.js → backend/src/database/document.js} +0 -0
  478. /package/backend/dist/cjs/{plugins/pdf.d.ts → backend/src/database/filters/strict_filter_test.d.ts} +0 -0
  479. /package/backend/dist/{esm/file_watcher.d.ts → cjs/backend/src/database/filters/strict_filter_test_v0.d.ts} +0 -0
  480. /package/backend/dist/{esm/plugins/pdf.d.ts → cjs/backend/src/database/flatten_test.d.ts} +0 -0
  481. /package/backend/dist/cjs/{frontend.d.ts → backend/src/frontend.d.ts} +0 -0
  482. /package/backend/dist/cjs/{plugins → backend/src/plugins}/communication.d.ts +0 -0
  483. /package/backend/dist/cjs/{plugins → backend/src/plugins}/communication.js +0 -0
  484. /package/backend/dist/cjs/{plugins → backend/src/plugins}/mail/ui.js +0 -0
  485. /package/backend/dist/cjs/{plugins → backend/src/plugins}/pdf.js +0 -0
  486. /package/backend/dist/cjs/{plugins → backend/src/plugins}/thread_monitor.d.ts +0 -0
  487. /package/backend/dist/cjs/{plugins → backend/src/plugins}/thread_monitor.js +0 -0
  488. /package/backend/dist/cjs/{vinc.d.ts → backend/src/vinc.d.ts} +0 -0
  489. /package/backend/dist/cjs/{vinc.js → backend/src/vinc.js} +0 -0
  490. /package/backend/dist/esm/{cli.d.ts → backend/src/cli.d.ts} +0 -0
  491. /package/backend/dist/esm/{frontend.d.ts → backend/src/frontend.d.ts} +0 -0
  492. /package/backend/dist/esm/{plugins → backend/src/plugins}/communication.d.ts +0 -0
  493. /package/backend/dist/esm/{plugins → backend/src/plugins}/communication.js +0 -0
  494. /package/backend/dist/esm/{plugins → backend/src/plugins}/thread_monitor.d.ts +0 -0
  495. /package/backend/dist/esm/{plugins → backend/src/plugins}/thread_monitor.js +0 -0
  496. /package/backend/dist/esm/{vinc.d.ts → backend/src/vinc.d.ts} +0 -0
  497. /package/backend/dist/esm/{vinc.js → backend/src/vinc.js} +0 -0
  498. /package/frontend/dist/{elements → frontend/src/elements}/register_element.d.ts +0 -0
  499. /package/frontend/dist/{elements → frontend/src/elements}/register_element.js +0 -0
  500. /package/frontend/dist/{modules → frontend/src/modules}/color.d.ts +0 -0
  501. /package/frontend/dist/{ui → frontend/src/ui}/ui.d.ts +0 -0
  502. /package/frontend/dist/{ui → frontend/src/ui}/ui.js +0 -0
@@ -1,1817 +0,0 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __export = (target, all) => {
8
- for (var name in all)
9
- __defProp(target, name, { get: all[name], enumerable: true });
10
- };
11
- var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") {
13
- for (let key of __getOwnPropNames(from))
14
- if (!__hasOwnProp.call(to, key) && key !== except)
15
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
- }
17
- return to;
18
- };
19
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
- // If the importer is in node compatibility mode or this is not an ESM
21
- // file that has been converted to a CommonJS file using a Babel-
22
- // compatible transform (i.e. "__esModule" has not been set), then set
23
- // "default" to the CommonJS "module.exports" for node compatibility.
24
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
- mod
26
- ));
27
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
- var stdin_exports = {};
29
- __export(stdin_exports, {
30
- Users: () => Users,
31
- default: () => stdin_default
32
- });
33
- module.exports = __toCommonJS(stdin_exports);
34
- var vlib = __toESM(require("@vandenberghinc/vlib"));
35
- var utils = __toESM(require("./utils.js"));
36
- var Mail = __toESM(require("./plugins/mail/ui.js"));
37
- var import_status = require("./status.js");
38
- var import_logger = require("./logger.js");
39
- const { ExternalError } = utils;
40
- const { log, error } = import_logger.logger;
41
- class Users {
42
- server;
43
- avg_send_2fa_time = [];
44
- _tokens_db;
45
- _users_db;
46
- public;
47
- protected;
48
- private;
49
- constructor(_server) {
50
- this.server = _server;
51
- }
52
- // ---------------------------------------------------------
53
- // Utils.
54
- // Generate a code.
55
- _generate_code(length = 6) {
56
- const charset = "0123456789";
57
- let key = "";
58
- for (let i = 0; i < length; i++) {
59
- key += charset.charAt(Math.floor(Math.random() * charset.length));
60
- }
61
- return key;
62
- }
63
- // Generate a str.
64
- _generate_str(length = 32) {
65
- const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
66
- let key = "";
67
- for (let i = 0; i < length; i++) {
68
- key += charset.charAt(Math.floor(Math.random() * charset.length));
69
- }
70
- return key;
71
- }
72
- // Create a new uid.
73
- async _generate_uid() {
74
- while (true) {
75
- const uid = this._generate_str(16);
76
- if (await this.uid_exists(uid) === false) {
77
- return uid;
78
- }
79
- }
80
- }
81
- // Generate an api key.
82
- _generate_api_key(uid) {
83
- return `0${uid}:${this._generate_str(64)}`;
84
- }
85
- // Generate a token.
86
- _generate_token(uid) {
87
- return `1${uid}:${this._generate_str(64)}`;
88
- }
89
- // Check a password and the verify password.
90
- _verify_new_pass(pass, verify_pass) {
91
- let error2 = null;
92
- if (pass !== verify_pass) {
93
- error2 = "Passwords do not match.";
94
- return { error: error2, invalid_fields: { password: error2, verify_password: error2 } };
95
- } else if (pass.length < 8) {
96
- error2 = "The password should at least include eight characters.";
97
- return { error: error2, invalid_fields: { password: error2, verify_password: error2 } };
98
- } else if (pass.toLowerCase() === pass) {
99
- error2 = "The password should at least include one capital letter.";
100
- return { error: error2, invalid_fields: { password: error2, verify_password: error2 } };
101
- } else if (!/\d|[!@#$%^&*]/.test(pass)) {
102
- error2 = "The password should at least include one numeric or special character.";
103
- return { error: error2, invalid_fields: { password: error2, verify_password: error2 } };
104
- }
105
- return { error: null, invalid_fields: null };
106
- }
107
- // ---------------------------------------------------------
108
- // Authentication (private).
109
- // Generate a token by uid.
110
- async _create_token(uid) {
111
- const token = this._generate_token(uid);
112
- await this._tokens_db.save({ uid, type: "token" }, {
113
- expiration: Date.now() + this.server.token_expiration * 1e3,
114
- token: this.server._hmac(token),
115
- active: true
116
- });
117
- return token;
118
- }
119
- // Deactivate a token by uid.
120
- async _deactivate_token(uid) {
121
- await this._tokens_db.save({ uid, type: "token" }, { active: false });
122
- }
123
- // Create a 2FA token.
124
- async _create_2fa_token(uid_or_email, expiration) {
125
- const code = this._generate_code(6);
126
- await this._tokens_db.save({ uid: uid_or_email, type: "2fa" }, {
127
- expiration: Date.now() + expiration * 1e3,
128
- code,
129
- active: true
130
- });
131
- return code;
132
- }
133
- // Deactivate a 2FA token.
134
- async _deactivate_2fa_token(uid_or_email) {
135
- await this._tokens_db.save({ uid: uid_or_email, type: "2fa" }, { active: false });
136
- }
137
- // Perform authentication on a request.
138
- async _authenticate(stream) {
139
- const authorization = stream.headers["authorization"];
140
- if (authorization !== void 0) {
141
- if (typeof authorization !== "string") {
142
- return {
143
- status: import_status.Status.bad_request,
144
- data: "Invalid authorization header."
145
- };
146
- }
147
- if (!authorization.startsWith("Bearer ")) {
148
- return {
149
- status: import_status.Status.bad_request,
150
- data: 'Invalid authorization scheme, the authorization scheme must be "Bearer".'
151
- };
152
- }
153
- let api_key = "";
154
- for (let i = 7; i < authorization.length; i++) {
155
- const c = authorization[i];
156
- if (c == " ") {
157
- continue;
158
- }
159
- api_key += c;
160
- }
161
- let uid;
162
- try {
163
- uid = await this.get_uid_by_api_key(api_key);
164
- } catch (e) {
165
- return {
166
- status: import_status.Status.unauthorized,
167
- data: "Unauthorized."
168
- };
169
- }
170
- if (await this.verify_api_key_by_uid(uid, api_key) !== true) {
171
- return {
172
- status: import_status.Status.unauthorized,
173
- data: "Unauthorized."
174
- };
175
- }
176
- stream.uid = uid;
177
- return null;
178
- } else {
179
- if (stream.cookies.T == null || stream.cookies.T.value == null) {
180
- return {
181
- status: 302,
182
- headers: { Location: `/signin?next=${stream.endpoint}` },
183
- data: "Permission denied."
184
- };
185
- }
186
- const token = stream.cookies.T.value;
187
- let uid;
188
- try {
189
- uid = await this.get_uid_by_api_key(token);
190
- } catch (e) {
191
- return {
192
- status: 302,
193
- headers: { Location: `/signin?next=${stream.endpoint}` },
194
- data: "Permission denied."
195
- };
196
- }
197
- if (await this.verify_token_by_uid(uid, token) !== true) {
198
- return {
199
- status: 302,
200
- headers: { Location: `/signin?next=${stream.endpoint}` },
201
- data: "Permission denied."
202
- };
203
- }
204
- stream.uid = uid;
205
- return null;
206
- }
207
- }
208
- // Sign a user in and return a response.
209
- async _sign_in_response(stream, uid) {
210
- const token = await this._create_token(uid);
211
- this._create_token_cookie(stream, token);
212
- await this._create_user_cookie(stream, uid);
213
- await this._create_detailed_user_cookie(stream, uid);
214
- stream.send({
215
- status: 200,
216
- data: { message: "Successfully signed in." }
217
- });
218
- }
219
- // ---------------------------------------------------------
220
- // Cookies (private).
221
- // Create token headers.
222
- _create_token_cookie(stream, token) {
223
- stream.set_header("Cache-Control", "max-age=0, no-cache, no-store, must-revalidate, proxy-revalidate");
224
- stream.set_header("Access-Control-Allow-Credentials", "true");
225
- const expires = new Date((/* @__PURE__ */ new Date()).getTime() + this.server.token_expiration * 1e3);
226
- if (typeof token === "object") {
227
- token = token.token;
228
- }
229
- stream.set_cookie(`T=${token}; Max-Age=86400; Path=/; Expires=${expires.toUTCString()}; SameSite=None; ${this.server.https === void 0 ? "" : "Secure"}; HttpOnly;`);
230
- }
231
- // Create user headers.
232
- async _create_user_cookie(stream, uid) {
233
- const secure = this.server.https === void 0 ? "" : "Secure";
234
- if (typeof uid === "string") {
235
- stream.set_cookie(`UserID=${uid}; Path=/; SameSite=None; ${secure};`);
236
- const is_activated = this.server.enable_account_activation ? await this.is_activated(uid) : true;
237
- stream.set_cookie(`UserActivated=${is_activated}; Path=/; SameSite=None; ${secure};`);
238
- } else {
239
- stream.set_cookie(`UserID=-1; Path=/; SameSite=None; ${secure};`);
240
- const is_activated = this.server.enable_account_activation ? false : true;
241
- stream.set_cookie(`UserActivated=${is_activated}; Path=/; SameSite=None; ${secure};`);
242
- }
243
- }
244
- // Create detailed user headers.
245
- async _create_detailed_user_cookie(stream, uid) {
246
- const secure = this.server.https === void 0 ? "" : "Secure";
247
- const user = await this.get(uid);
248
- stream.set_cookie(`UserName=${user.username}; Path=/; SameSite=None; ${secure};`);
249
- stream.set_cookie(`UserFirstName=${user.first_name}; Path=/; SameSite=None; ${secure};`);
250
- stream.set_cookie(`UserLastName=${user.last_name}; Path=/; SameSite=None; ${secure};`);
251
- stream.set_cookie(`UserEmail=${user.email}; Path=/; SameSite=None; ${secure};`);
252
- }
253
- // Reset all default cookies.
254
- _reset_cookies(stream) {
255
- const secure = this.server.https === void 0 ? "" : "Secure";
256
- stream.set_cookie(`T=; Path=/; SameSite=None; ${secure}; HttpOnly;`);
257
- stream.set_cookie(`UserID=-1; Path=/; SameSite=None; ${secure};`);
258
- stream.set_cookie(`UserActivated=false; Path=/; SameSite=None; ${secure};`);
259
- stream.set_cookie(`UserName=; Path=/; SameSite=None; ${secure};`);
260
- stream.set_cookie(`UserFirstName=; Path=/; SameSite=None; ${secure};`);
261
- stream.set_cookie(`UserLastName=; Path=/; SameSite=None; ${secure};`);
262
- stream.set_cookie(`UserEmail=; Path=/; SameSite=None; ${secure};`);
263
- }
264
- // ---------------------------------------------------------
265
- // Initialization (private).
266
- // Initialize.
267
- async _initialize() {
268
- this._tokens_db = await this.server.db.collection({
269
- name: "Volt.Server.Users.Tokens",
270
- indexes: ["uid", "type", "token"]
271
- });
272
- this._users_db = await this.server.db.collection({
273
- name: "Volt.Server.Users.Users",
274
- indexes: ["email", "username", "uid", "api_key"]
275
- });
276
- this.public = await this.server.db.collection({
277
- name: "Volt.Server.Users.Public",
278
- indexes: ["uid", "path"]
279
- });
280
- this.protected = await this.server.db.collection({
281
- name: "Volt.Server.Users.Protected",
282
- indexes: ["uid", "path"]
283
- });
284
- this.private = await this.server.db.collection({
285
- name: "Volt.Server.Users.Private",
286
- indexes: ["uid", "path"]
287
- });
288
- this.server.endpoint({
289
- method: "GET",
290
- endpoint: "/volt/auth/2fa",
291
- content_type: "application/json",
292
- rate_limit: "global",
293
- params: {
294
- email: "string"
295
- },
296
- callback: async (stream, params) => {
297
- let uid;
298
- if ((uid = await this.get_uid_by_email(params.email)) == null) {
299
- return stream.success({
300
- data: { message: "A 2FA code was sent if the specified email exists." }
301
- });
302
- }
303
- await this.send_2fa({ uid, stream });
304
- return stream.success({
305
- data: { message: "A 2FA code was sent if the specified email exists." }
306
- });
307
- }
308
- });
309
- this.server.endpoint({
310
- method: "POST",
311
- endpoint: "/volt/auth/signin",
312
- content_type: "application/json",
313
- rate_limit: {
314
- limit: 10,
315
- interval: 60,
316
- group: "volt.auth"
317
- },
318
- callback: async (stream) => {
319
- let email, email_err, username, username_err, password, uid, code;
320
- try {
321
- email = stream.param("email");
322
- } catch (err) {
323
- email_err = err;
324
- }
325
- try {
326
- username = stream.param("username");
327
- } catch (err) {
328
- username_err = err;
329
- }
330
- if (email_err && username_err) {
331
- return stream.error({
332
- status: import_status.Status.bad_request,
333
- type: "InvalidParams",
334
- message: email_err.message
335
- });
336
- }
337
- try {
338
- password = stream.param("password");
339
- } catch (err) {
340
- return stream.error({
341
- status: import_status.Status.bad_request,
342
- type: "InvalidParams",
343
- message: err.message
344
- });
345
- }
346
- if (email) {
347
- if ((uid = await this.get_uid_by_email(email)) == null) {
348
- return stream.error({
349
- status: import_status.Status.unauthorized,
350
- type: "Unauthorized",
351
- message: "Unauthorized.",
352
- invalid_fields: {
353
- "email": "Invalid or unrecognized email",
354
- "password": "Invalid or unrecognized password"
355
- }
356
- });
357
- }
358
- } else {
359
- if ((uid = await this.get_uid(username)) == null) {
360
- return stream.error({
361
- status: import_status.Status.unauthorized,
362
- type: "Unauthorized",
363
- message: "Unauthorized.",
364
- invalid_fields: {
365
- "username": "Invalid or unrecognized username",
366
- "password": "Invalid or unrecognized password"
367
- }
368
- });
369
- }
370
- }
371
- if (await this.verify_password(uid, password)) {
372
- if (this.server.enable_2fa) {
373
- try {
374
- code = stream.param("code");
375
- } catch (err2) {
376
- const start_time = Date.now();
377
- await this.send_2fa({ uid, stream });
378
- if (this.avg_send_2fa_time.length >= 1e4) {
379
- this.avg_send_2fa_time.shift();
380
- }
381
- this.avg_send_2fa_time.push(Date.now() - start_time);
382
- return stream.send({
383
- status: import_status.Status.two_factor_auth_required,
384
- data: { error: "2FA required." }
385
- });
386
- }
387
- const err = await this.verify_2fa(uid, code);
388
- if (err) {
389
- return stream.send({
390
- status: import_status.Status.unauthorized,
391
- data: {
392
- error: "Invalid 2FA code.",
393
- invalid_fields: {
394
- "code": err
395
- }
396
- }
397
- });
398
- }
399
- }
400
- return await this._sign_in_response(stream, uid);
401
- }
402
- if (this.avg_send_2fa_time.length >= 10) {
403
- const sorted = [...this.avg_send_2fa_time].sort((a, b) => a - b);
404
- const mid = Math.floor(sorted.length / 2);
405
- if (sorted.length % 2 === 0) {
406
- return (sorted[mid - 1] + sorted[mid]) / 2;
407
- }
408
- await new Promise((resolve) => setTimeout(resolve, sorted[mid]));
409
- }
410
- return stream.send({
411
- status: import_status.Status.unauthorized,
412
- data: {
413
- error: "Unauthorized.",
414
- invalid_fields: {
415
- "username": "Invalid or unrecognized username",
416
- "password": "Invalid or unrecognized password"
417
- }
418
- }
419
- });
420
- }
421
- });
422
- this.server.endpoint({
423
- method: "POST",
424
- endpoint: "/volt/auth/signout",
425
- content_type: "application/json",
426
- authenticated: true,
427
- rate_limit: "global",
428
- callback: async (stream) => {
429
- await this._deactivate_token(stream.uid);
430
- this._reset_cookies(stream);
431
- return stream.success({
432
- data: { message: "Successfully signed out." }
433
- });
434
- }
435
- });
436
- this.server.endpoint({
437
- method: "POST",
438
- endpoint: "/volt/auth/signup",
439
- content_type: "application/json",
440
- rate_limit: "global",
441
- params: {
442
- username: "string",
443
- first_name: "string",
444
- last_name: "string",
445
- email: "string",
446
- password: "string",
447
- verify_password: "string",
448
- phone_number: { type: "string", required: false },
449
- code: { type: "string", required: false }
450
- },
451
- callback: async (stream, params) => {
452
- const { error: error2, invalid_fields } = this._verify_new_pass(params.password, params.verify_password);
453
- if (error2) {
454
- return stream.error({
455
- status: import_status.Status.bad_request,
456
- type: "InvalidParams",
457
- message: error2,
458
- invalid_fields: invalid_fields ?? void 0
459
- });
460
- }
461
- if (await this.username_exists(params.username)) {
462
- throw new ExternalError({
463
- type: "UsernameAlreadyExists",
464
- message: `Username "${params.username}" is already registered.`,
465
- status: import_status.Status.bad_request,
466
- invalid_fields: { "username": "Username is already registered" }
467
- });
468
- }
469
- if (await this.email_exists(params.email)) {
470
- throw new ExternalError({
471
- type: "EmailAlreadyExists",
472
- message: `Email "${params.email}" is already registered.`,
473
- status: import_status.Status.bad_request,
474
- invalid_fields: { "email": "Email is already registered" }
475
- });
476
- }
477
- if (this.server.enable_2fa) {
478
- if (params.code == null || params.code == "") {
479
- const start_time = Date.now();
480
- await this.send_2fa({
481
- _email: params.email,
482
- _username: params.username,
483
- stream,
484
- uid: void 0
485
- // keep uid required param but use _email sys arg here.
486
- });
487
- if (this.avg_send_2fa_time.length >= 1e4) {
488
- this.avg_send_2fa_time.shift();
489
- }
490
- this.avg_send_2fa_time.push(Date.now() - start_time);
491
- return stream.send({
492
- status: import_status.Status.two_factor_auth_required,
493
- data: { error: "2FA required." }
494
- });
495
- }
496
- const err = await this.verify_2fa(params.email, params.code);
497
- if (err) {
498
- return stream.send({
499
- status: import_status.Status.unauthorized,
500
- data: {
501
- error: "Invalid 2FA code.",
502
- invalid_fields: {
503
- "code": err
504
- }
505
- }
506
- });
507
- }
508
- }
509
- delete params.verify_password;
510
- delete params.code;
511
- params.is_activated = true;
512
- params._check_username_email = false;
513
- let uid;
514
- try {
515
- uid = await this.create(params);
516
- } catch (err) {
517
- return stream.error({
518
- status: import_status.Status.bad_request,
519
- type: "InvalidParams",
520
- message: err.message,
521
- invalid_fields: err.invalid_fields || {}
522
- });
523
- }
524
- return await this._sign_in_response(stream, uid);
525
- }
526
- });
527
- this.server.endpoint({
528
- method: "POST",
529
- endpoint: "/volt/auth/activate",
530
- content_type: "application/json",
531
- rate_limit: "global",
532
- params: {
533
- "code": "string"
534
- },
535
- callback: async (stream, params) => {
536
- let uid = stream.uid;
537
- if (uid == null) {
538
- uid = stream.cookies["UserID"].value;
539
- if (uid === "null" || uid === "-1") {
540
- uid = null;
541
- }
542
- }
543
- if (uid == null) {
544
- return stream.error({ status: import_status.Status.forbidden, message: "Permission denied." });
545
- }
546
- const err = await this.verify_2fa(uid, params.code);
547
- if (err) {
548
- return stream.error({
549
- status: import_status.Status.forbidden,
550
- message: "Permission denied.",
551
- invalid_fields: {
552
- "code": err
553
- }
554
- });
555
- }
556
- await this.set_activated(uid, true);
557
- await this._create_user_cookie(stream, uid);
558
- return stream.success({ data: { message: "Successfully verified the 2FA code." } });
559
- }
560
- });
561
- this.server.endpoint({
562
- method: "POST",
563
- endpoint: "/volt/auth/forgot_password",
564
- content_type: "application/json",
565
- rate_limit: "global",
566
- params: {
567
- email: "string",
568
- code: "string",
569
- password: "string",
570
- verify_password: "string"
571
- },
572
- callback: async (stream, params) => {
573
- const { error: error2, invalid_fields } = this._verify_new_pass(params.password, params.verify_password);
574
- if (error2) {
575
- return stream.error({
576
- status: import_status.Status.bad_request,
577
- message: error2,
578
- invalid_fields: invalid_fields ?? void 0
579
- });
580
- }
581
- let uid;
582
- if ((uid = await this.get_uid_by_email(params.email)) == null) {
583
- return stream.error({ status: import_status.Status.forbidden, message: "Invalid email." });
584
- }
585
- const err = await this.verify_2fa(uid, params.code);
586
- if (err) {
587
- return stream.error({
588
- status: import_status.Status.forbidden,
589
- message: "Invalid 2FA code.",
590
- invalid_fields: {
591
- "code": "Invalid code"
592
- }
593
- });
594
- }
595
- await this.set_password(uid, params.password);
596
- return await this._sign_in_response(stream, uid);
597
- }
598
- });
599
- this.server.endpoint({
600
- method: "GET",
601
- endpoint: "/volt/user",
602
- content_type: "application/json",
603
- authenticated: true,
604
- rate_limit: "global",
605
- params: {
606
- // detailed: { type: "boolean", default: false },
607
- },
608
- callback: async (stream, params) => {
609
- const user = await this.get(stream.uid);
610
- if (user.password) {
611
- user.password = "*".repeat(user.password.length);
612
- }
613
- if (user.api_key) {
614
- user.api_key = "*".repeat(user.api_key.length);
615
- }
616
- user.first_name ??= "";
617
- user.last_name ??= "";
618
- user.username ??= "";
619
- user.email ??= "";
620
- user.password ??= "";
621
- user.api_key ??= "";
622
- user.support_pin ??= "";
623
- return stream.success({ data: user });
624
- }
625
- });
626
- this.server.endpoint({
627
- method: "POST",
628
- endpoint: "/volt/user",
629
- content_type: "application/json",
630
- authenticated: true,
631
- rate_limit: "global",
632
- callback: async (stream) => {
633
- await this.set(stream.uid, stream.params);
634
- await this._create_detailed_user_cookie(stream, stream.uid);
635
- return stream.success({ data: { message: "Successfully updated your account." } });
636
- }
637
- });
638
- this.server.endpoint({
639
- method: "POST",
640
- endpoint: "/volt/user/change_password",
641
- content_type: "application/json",
642
- authenticated: true,
643
- rate_limit: "global",
644
- params: {
645
- current_password: "string",
646
- password: "string",
647
- verify_password: "string"
648
- },
649
- callback: async (stream, params) => {
650
- if (await this.verify_password(stream.uid, params.current_password) !== true) {
651
- return stream.error({
652
- status: import_status.Status.unauthorized,
653
- message: "Incorrect password.",
654
- invalid_fields: {
655
- current_password: "Incorrect password."
656
- }
657
- });
658
- }
659
- const { error: error2, invalid_fields } = this._verify_new_pass(params.password, params.verify_password);
660
- if (error2) {
661
- return stream.error({
662
- status: import_status.Status.bad_request,
663
- message: error2,
664
- invalid_fields: invalid_fields ?? void 0
665
- });
666
- }
667
- await this.set_password(stream.uid, params.password);
668
- return stream.success({
669
- status: import_status.Status.success,
670
- data: { message: "Successfully updated your password." }
671
- });
672
- }
673
- });
674
- this.server.endpoint({
675
- method: "DELETE",
676
- endpoint: "/volt/user",
677
- content_type: "application/json",
678
- authenticated: true,
679
- rate_limit: "global",
680
- callback: async (stream) => {
681
- await this.delete(stream.uid);
682
- this._reset_cookies(stream);
683
- return stream.success({
684
- status: import_status.Status.success,
685
- data: { message: "Successfully deleted your account." }
686
- });
687
- }
688
- });
689
- this.server.endpoint({
690
- method: "POST",
691
- endpoint: "/volt/user/api_key",
692
- content_type: "application/json",
693
- authenticated: true,
694
- rate_limit: "global",
695
- callback: async (stream) => {
696
- return stream.success({
697
- data: {
698
- message: "Successfully generated an API key.",
699
- api_key: await this.generate_api_key(stream.uid)
700
- }
701
- });
702
- }
703
- });
704
- this.server.endpoint({
705
- method: "DELETE",
706
- endpoint: "/volt/user/api_key",
707
- content_type: "application/json",
708
- authenticated: true,
709
- rate_limit: "global",
710
- callback: async (stream) => {
711
- await this.revoke_api_key(stream.uid);
712
- return stream.send({
713
- status: import_status.Status.success,
714
- data: { message: "Successfully revoked your API key." }
715
- });
716
- }
717
- });
718
- this.server.endpoint({
719
- method: "GET",
720
- endpoint: "/volt/user/data",
721
- content_type: "application/json",
722
- authenticated: true,
723
- rate_limit: "global",
724
- params: {
725
- path: "string",
726
- default: { type: "string", default: null }
727
- },
728
- callback: async (stream, params) => {
729
- return stream.send({
730
- status: import_status.Status.success,
731
- data: await this.public.load({ uid: stream.uid, path: params.path }, { default: params.default })
732
- });
733
- }
734
- });
735
- this.server.endpoint({
736
- method: "POST",
737
- endpoint: "/volt/user/data",
738
- content_type: "application/json",
739
- authenticated: true,
740
- rate_limit: "global",
741
- params: {
742
- path: "string",
743
- data: { type: void 0 }
744
- },
745
- callback: async (stream, params) => {
746
- await this.public.save({ uid: stream.uid, path: params.path }, params.data);
747
- return stream.send({
748
- status: import_status.Status.success,
749
- data: { message: "Successfully saved." }
750
- });
751
- }
752
- });
753
- this.server.endpoint({
754
- method: "DELETE",
755
- endpoint: "/volt/user/data",
756
- content_type: "application/json",
757
- authenticated: true,
758
- rate_limit: "global",
759
- params: {
760
- path: "string",
761
- data: { type: void 0 },
762
- recursive: { type: "string", default: false }
763
- },
764
- callback: async (stream, params) => {
765
- await this.public.delete({ uid: stream.uid, path: params.path }, params.recursive);
766
- return stream.send({
767
- status: import_status.Status.success,
768
- data: { message: "Successfully deleted." }
769
- });
770
- }
771
- });
772
- this.server.endpoint({
773
- method: "GET",
774
- endpoint: "/volt/user/data/protected",
775
- content_type: "application/json",
776
- authenticated: true,
777
- rate_limit: "global",
778
- params: {
779
- path: "string",
780
- default: { type: "string", default: null }
781
- },
782
- callback: async (stream, params) => {
783
- return stream.send({
784
- status: import_status.Status.success,
785
- data: await this.protected.load({ uid: stream.uid, path: params.path }, { default: params.default })
786
- });
787
- }
788
- });
789
- this.server.endpoint({
790
- method: "GET",
791
- endpoint: "/volt/support/pin",
792
- content_type: "application/json",
793
- authenticated: true,
794
- rate_limit: "global",
795
- callback: async (stream) => {
796
- const pin = await this.get_support_pin(stream.uid);
797
- return stream.success({
798
- data: {
799
- message: "Successfully retrieved your support PIN.",
800
- pin
801
- }
802
- });
803
- }
804
- });
805
- this.server.endpoint({
806
- method: "POST",
807
- endpoint: "/volt/support/submit",
808
- content_type: "application/json",
809
- rate_limit: "global",
810
- callback: async (stream) => {
811
- let params = stream.params;
812
- let user = null, email, first_name, last_name;
813
- if (stream.uid == null) {
814
- try {
815
- email = stream.param("email");
816
- first_name = stream.param("first_name");
817
- last_name = stream.param("last_name");
818
- } catch (err) {
819
- return stream.error({ status: import_status.Status.bad_request, message: err.message });
820
- }
821
- } else {
822
- user = await this.get(stream.uid);
823
- email = user.email;
824
- first_name = user.first_name;
825
- last_name = user.last_name;
826
- }
827
- let body = "";
828
- const subject = params.subject || (params.type == null ? "Support" : `Support ${params.type}`);
829
- body += `<h1>${subject}</h1>`;
830
- if (params.subject) {
831
- delete params.subject;
832
- }
833
- if (params.type) {
834
- body += `<span style='font-weight: bold'>Type</span>: ${params.type}<br>`;
835
- delete params.type;
836
- }
837
- if (user) {
838
- body += `<span style='font-weight: bold'>UID</span>: ${stream.uid}<br>`;
839
- body += `<span style='font-weight: bold'>User</span>: ${user.username}<br>`;
840
- }
841
- body += `<span style='font-weight: bold'>Email</span>: ${email}<br>`;
842
- body += `<span style='font-weight: bold'>First Name</span>: ${first_name}<br>`;
843
- body += `<span style='font-weight: bold'>Last Name</span>: ${last_name}<br>`;
844
- if (stream.uid != null) {
845
- const support_pin = await this.get_support_pin(stream.uid);
846
- body += `<span style='font-weight: bold'>Support PIN</span>: ${support_pin} <span style='color: green'>verified</span><br>`;
847
- } else if (params.support_pin) {
848
- body += `<span style='font-weight: bold'>Support PIN</span>: ${params.support_pin} <span style='color: red'>not yet verified</span><br>`;
849
- delete params.support_pin;
850
- } else {
851
- body += `<span style='font-weight: bold'>Support PIN</span>: Unknown<br>`;
852
- }
853
- if (params.summary) {
854
- body += `<br><span style='font-weight: bold'>Summary</span>:<br>${params.summary}<br>`;
855
- delete params.summary;
856
- }
857
- if (params.detailed) {
858
- body += `<br><span style='font-weight: bold'>Detailed</span>:<br>${params.detailed}<br>`;
859
- delete params.detailed;
860
- }
861
- Object.keys(params).forEach((key) => {
862
- if (key !== "attachments" && key !== "recipient") {
863
- body += `<br><span style='font-weight: bold'>${key}</span>: ${params[key]}<br>`;
864
- }
865
- });
866
- body += "<br>";
867
- let attachments = [];
868
- if (params.attachments) {
869
- Object.keys(params.attachments).forEach((key) => {
870
- attachments.push({
871
- filename: key,
872
- content: Buffer.from(params.attachments[key], "utf-8")
873
- });
874
- });
875
- }
876
- await this.server.send_mail({
877
- recipients: [params.recipient || this.server.smtp_sender],
878
- subject,
879
- body,
880
- attachments
881
- });
882
- return stream.success({ data: { message: "Successfully sent your request." } });
883
- }
884
- });
885
- }
886
- // ---------------------------------------------------------
887
- // Users.
888
- // Check if a username exists.
889
- async uid_exists(uid) {
890
- return await this._users_db.find({ uid }) != null;
891
- }
892
- // Check if a username exists.
893
- /* @docs:
894
- * @title: Username Exists
895
- * @description: Check if a username exists.
896
- * @type: boolean
897
- * @return: Returns a boolean indicating whether the username exists or not.
898
- * @parameter:
899
- * @name: username
900
- * @description: The username to check.
901
- * @type: string
902
- * @usage:
903
- * ...
904
- * const exists = await server.users.username_exists("someusername");
905
- */
906
- async username_exists(username) {
907
- return await this._users_db.find({ username }) != null;
908
- }
909
- // Check if an email exists.
910
- /* @docs:
911
- * @title: Email Exists
912
- * @description: Check if a email exists.
913
- * @type: boolean
914
- * @return: Returns a boolean indicating whether the email exists or not.
915
- * @parameter:
916
- * @name: email
917
- * @description: The email to check.
918
- * @type: string
919
- * @usage:
920
- * ...
921
- * const exists = await server.users.email_exists("some\@email.com");
922
- */
923
- async email_exists(email) {
924
- return await this._users_db.find({ email }) != null;
925
- }
926
- // Is activated.
927
- /* @docs:
928
- * @title: Is Activated
929
- * @description: Check if a user account is activated.
930
- * @return: Returns a boolean indicating whether the account is activated or not.
931
- * @parameter:
932
- * @name: uid
933
- * @description: The id of the user.
934
- * @type: string
935
- * @cache: Users:uid:param
936
- * @usage:
937
- * ...
938
- * const activated = await server.users.is_activated(0);
939
- */
940
- async is_activated(uid) {
941
- return (await this.get(uid)).is_activated == true;
942
- }
943
- // Set activated.
944
- /* @docs:
945
- * @title: Set Activated
946
- * @description: Set the activated status of a user account is activated.
947
- * @parameter:
948
- * @name: uid
949
- * @cached: Users:uid:param
950
- * @parameter:
951
- * @name: activated
952
- * @description: The boolean with the new activated status.
953
- * @type: boolean
954
- * @usage:
955
- * ...
956
- * await server.users.set_activated(1, true);
957
- */
958
- async set_activated(uid, is_activated) {
959
- await this._sys_set(uid, { is_activated });
960
- }
961
- // Create a user.
962
- /* @docs:
963
- * @title: Create User
964
- * @description:
965
- * Create a user account.
966
- *
967
- * Only the hashed password will be saved.
968
- * @return: Returns the uid of the newly created user.
969
- * @parameter:
970
- * @name: first_name
971
- * @description: The user's first name.
972
- * @type: string
973
- * @required: true
974
- * @parameter:
975
- * @name: last_name
976
- * @description: The user's last name.
977
- * @type: string
978
- * @required: true
979
- * @parameter:
980
- * @name: username
981
- * @description: The username of the new account.
982
- * @type: string
983
- * @required: true
984
- * @parameter:
985
- * @name: email
986
- * @description: The email of the new account.
987
- * @type: string
988
- * @required: true
989
- * @parameter:
990
- * @name: password
991
- * @description: The password of the new account.
992
- * @type: string
993
- * @required: true
994
- * @parameter:
995
- * @name: phone_number
996
- * @description: The phone number of the user account.
997
- * @type: string
998
- * @parameter:
999
- * @name: is_activated
1000
- * @description: A boolean indicating if the account should be set to activated or not, accounts created through the /volt/api/signup endpoint are always immediately activated due to the required 2FA code. When called manually the default value of `!Server.enable_account_activation` will be used for parameter `is_activated`.
1001
- * @type: boolean
1002
- * @parameter:
1003
- * @name: _check_username_email
1004
- * @ignore: true
1005
- * @usage:
1006
- * ...
1007
- * const uid = await server.users.create{
1008
- * first_name: "John",
1009
- * last_name: "Doe",
1010
- * username: "johndoe",
1011
- * email: "johndoe\@email.com",
1012
- * password: "HelloWorld!"
1013
- * });
1014
- */
1015
- async create({ first_name, last_name, username, email, password, phone_number = "", is_activated = null, _check_username_email = false }) {
1016
- vlib.Scheme.validate(arguments[0], {
1017
- strict: true,
1018
- scheme: {
1019
- first_name: "string",
1020
- last_name: "string",
1021
- username: "string",
1022
- email: "string",
1023
- password: "string",
1024
- phone_number: { type: "string", default: "" },
1025
- is_activated: { type: "boolean", required: false },
1026
- _check_username_email: { type: "boolean", required: false }
1027
- }
1028
- });
1029
- if (_check_username_email) {
1030
- if (await this.username_exists(username)) {
1031
- throw new ExternalError({
1032
- type: "UsernameAlreadyExists",
1033
- message: `Username "${username}" is already registered.`,
1034
- status: import_status.Status.bad_request,
1035
- invalid_fields: { "username": "Username is already registered" }
1036
- });
1037
- }
1038
- if (await this.email_exists(email)) {
1039
- throw new ExternalError({
1040
- type: "EmailAlreadyExists",
1041
- message: `Email "${email}" is already registered.`,
1042
- status: import_status.Status.bad_request,
1043
- invalid_fields: { "email": "Email is already registered" }
1044
- });
1045
- }
1046
- }
1047
- const uid = await this._generate_uid();
1048
- await this._users_db.save({ uid }, {
1049
- uid,
1050
- first_name,
1051
- last_name,
1052
- username,
1053
- email,
1054
- password: this.server._hmac(password),
1055
- phone_number,
1056
- created: Date.now(),
1057
- api_key: null,
1058
- support_pin: this._generate_code(8),
1059
- is_activated: is_activated ?? !this.server.enable_account_activation
1060
- });
1061
- return uid;
1062
- }
1063
- // Delete a user.
1064
- /* @docs:
1065
- * @title: Delete User
1066
- * @description: Delete a user account.
1067
- * @parameter:
1068
- * @name: uid
1069
- * @cached: Users:uid:param
1070
- * @usage:
1071
- * ...
1072
- * await server.users.delete(0);
1073
- */
1074
- async delete(uid) {
1075
- await this._users_db.delete_all({ uid });
1076
- await this._tokens_db.delete_all({ uid });
1077
- await this.public.delete_all({ uid });
1078
- await this.protected.delete_all({ uid });
1079
- await this.private.delete_all({ uid });
1080
- if (this.server.payments !== void 0) {
1081
- await this.server.payments._delete_user(uid);
1082
- }
1083
- const res = this.server.on_delete_user({ uid });
1084
- if (res instanceof Promise) {
1085
- await res;
1086
- }
1087
- }
1088
- // Set a user's first name.
1089
- /* @docs:
1090
- * @title: Set First Name
1091
- * @description:
1092
- * Set a user's first name
1093
- *
1094
- * If the uid does not exist an `Error` will be thrown.
1095
- * @parameter:
1096
- * @name: uid
1097
- * @cached: Users:uid:param
1098
- * @parameter:
1099
- * @name: first_name
1100
- * @description: The new first name.
1101
- * @type: string
1102
- * @usage:
1103
- * ...
1104
- * await server.users.set_first_name(1, "John");
1105
- */
1106
- async set_first_name(uid, first_name) {
1107
- const user = await this.get(uid);
1108
- await this._sys_set(uid, { first_name });
1109
- }
1110
- // Set a user's last name.
1111
- /* @docs:
1112
- * @title: Set Last Name
1113
- * @description:
1114
- * Set a user's last name
1115
- *
1116
- * If the uid does not exist an `Error` will be thrown.
1117
- * @parameter:
1118
- * @name: uid
1119
- * @cached: Users:uid:param
1120
- * @parameter:
1121
- * @name: last_name
1122
- * @description: The new last name.
1123
- * @type: string
1124
- * @usage:
1125
- * ...
1126
- * await server.users.set_last_name(1, "Doe");
1127
- */
1128
- async set_last_name(uid, last_name) {
1129
- const user = await this.get(uid);
1130
- await this._sys_set(uid, { last_name });
1131
- }
1132
- // Set a user's username.
1133
- /* @docs:
1134
- * @title: Set Username
1135
- * @description:
1136
- * Set a user's username
1137
- *
1138
- * If the uid does not exist an `Error` will be thrown.
1139
- * @parameter:
1140
- * @name: uid
1141
- * @cached: Users:uid:param
1142
- * @parameter:
1143
- * @name: username
1144
- * @description: The new username.
1145
- * @type: string
1146
- * @usage:
1147
- * ...
1148
- * await server.users.set_username(1, "newusername");
1149
- */
1150
- async set_username(uid, username) {
1151
- if (await this.username_exists(username)) {
1152
- throw Error(`Username "${username}" already exists.`);
1153
- }
1154
- await this._sys_set(uid, { username });
1155
- }
1156
- // Set a user's email.
1157
- /* @docs:
1158
- * @title: Set Email
1159
- * @description:
1160
- * Set a user's email
1161
- *
1162
- * If the uid does not exist an `Error` will be thrown.
1163
- * @parameter:
1164
- * @name: uid
1165
- * @cached: Users:uid:param
1166
- * @parameter:
1167
- * @name: email
1168
- * @description: The new email.
1169
- * @type: string
1170
- * @usage:
1171
- * ...
1172
- * await server.users.set_email(1, "new\@email.com");
1173
- */
1174
- async set_email(uid, email) {
1175
- if (await this.email_exists(email)) {
1176
- throw Error(`Email "${email}" already exists.`);
1177
- }
1178
- await this._sys_set(uid, { email });
1179
- }
1180
- // Set a user's password.
1181
- /* @docs:
1182
- * @title: Set Password
1183
- * @description:
1184
- * Set a user's password
1185
- *
1186
- * If the uid does not exist an `Error` will be thrown.
1187
- * @parameter:
1188
- * @name: uid
1189
- * @cached: Users:uid:param
1190
- * @parameter:
1191
- * @name: password
1192
- * @description: The new password.
1193
- * @type: string
1194
- * @usage:
1195
- * ...
1196
- * await server.users.set_password(1, "XXXXXX");
1197
- */
1198
- async set_password(uid, password) {
1199
- await this._sys_set(uid, { password: this.server._hmac(password) });
1200
- }
1201
- // Update a user.
1202
- /* @docs:
1203
- * @title: Set user
1204
- * @description:
1205
- * Set a user's data
1206
- *
1207
- * This function only updates the passed user attributes, unpresent attributes will not be deleted.
1208
- *
1209
- * If the uid does not exist an `Error` will be thrown.
1210
- *
1211
- * @note: The username can not be changed using this function, use `Server.set_username()` instead.
1212
- * @note: The email can not be changed using this function, use `Server.set_email()` instead.
1213
- * @note: The password can not be changed using this function, use `Server.set_password()` instead.
1214
- * @parameter:
1215
- * @name: uid
1216
- * @cached: Users:uid:param
1217
- * @parameter:
1218
- * @name: data
1219
- * @description: The new user object.
1220
- * @type: object
1221
- * @usage:
1222
- * ...
1223
- * await server.users.set(1, {first_name: "John", last_name: "Doe"});
1224
- */
1225
- async set(uid, data) {
1226
- let old_data;
1227
- const set_data = {};
1228
- for (const key of Object.keys(data)) {
1229
- switch (key) {
1230
- case "first_name":
1231
- case "last_name":
1232
- case "phone_number":
1233
- case "is_activated":
1234
- set_data[key] = data[key];
1235
- break;
1236
- case "password":
1237
- set_data[key] = this.server._hmac(data[key]);
1238
- break;
1239
- case "username":
1240
- if (old_data === void 0) {
1241
- old_data = await this.get(uid);
1242
- }
1243
- if (old_data.username !== data.username) {
1244
- if (await this.username_exists(data.username)) {
1245
- throw Error(`Username "${data.username}" already exists.`);
1246
- }
1247
- set_data[key] = data[key];
1248
- }
1249
- break;
1250
- case "email":
1251
- if (old_data === void 0) {
1252
- old_data = await this.get(uid);
1253
- }
1254
- if (old_data.email !== data.email) {
1255
- if (await this.email_exists(data.email)) {
1256
- throw Error(`Email "${data.email}" already exists.`);
1257
- }
1258
- set_data[key] = data[key];
1259
- }
1260
- break;
1261
- default:
1262
- break;
1263
- }
1264
- }
1265
- data = await this._users_db.save({ uid }, set_data);
1266
- if (data == null) {
1267
- throw new Error(`Unable to find a user by uid "${uid}".`);
1268
- }
1269
- return data;
1270
- }
1271
- async _sys_set(uid, data) {
1272
- data = await this._users_db.save({ uid }, data);
1273
- if (data == null) {
1274
- throw new Error(`Unable to find a user by uid "${uid}".`);
1275
- }
1276
- return data;
1277
- }
1278
- // Get user info by uid.
1279
- /* @docs:
1280
- * @title: Get User
1281
- * @description:
1282
- * Get a user by uid.
1283
- *
1284
- * If the uid does not exist an `Error` will be thrown.
1285
- * @return:
1286
- * Returns a User object.
1287
- * @parameter:
1288
- * @name: uid
1289
- * @cached: Users:uid:param
1290
- * @parameter:
1291
- * @name: detailed
1292
- * @description: Also retrieve the detailed user data.
1293
- * @type: boolean
1294
- * @usage:
1295
- * ...
1296
- * const user = await server.users.get(0);
1297
- */
1298
- async get(uid) {
1299
- const data = await this._users_db.load({ uid });
1300
- if (data == null) {
1301
- throw new Error(`Unable to find a user by uid "${uid}".`);
1302
- }
1303
- return data;
1304
- }
1305
- // Get user info by username.
1306
- /* @docs:
1307
- * @title: Get User By Username
1308
- * @description:
1309
- * Get a user by username.
1310
- *
1311
- * If the username does not exist an `Error` will be thrown.
1312
- * @return:
1313
- * Returns a User object.
1314
- * @parameter:
1315
- * @name: username
1316
- * @description: The username of the user to fetch.
1317
- * @type: string
1318
- * @usage:
1319
- * ...
1320
- * const user = await server.users.get_by_username("myusername");
1321
- */
1322
- async get_by_username(username) {
1323
- const data = await this._users_db.find({ username });
1324
- if (data == null) {
1325
- throw new Error(`Unable to find a user by username "${username}".`);
1326
- }
1327
- return data;
1328
- }
1329
- // Get user info by email.
1330
- /* @docs:
1331
- * @title: Get User By Email
1332
- * @description:
1333
- * Get a user by email.
1334
- *
1335
- * If the email does not exist an `Error` will be thrown.
1336
- * @return:
1337
- * Returns a User object.
1338
- * @parameter:
1339
- * @name: email
1340
- * @description: The email of the user to fetch.
1341
- * @type: string
1342
- * @usage:
1343
- * ...
1344
- * const user = await server.users.get_by_email("my\@email.com");
1345
- */
1346
- async get_by_email(email) {
1347
- const data = await this._users_db.find({ email });
1348
- if (data == null) {
1349
- throw new Error(`Unable to find a user by email "${email}".`);
1350
- }
1351
- return data;
1352
- }
1353
- // Get user info by api key.
1354
- /* @docs:
1355
- * @title: Get User By API Key
1356
- * @description:
1357
- * Get a user by API key.
1358
- *
1359
- * If the API key does not exist an `Error` will be thrown.
1360
- * @return:
1361
- * Returns a User object.
1362
- * @parameter:
1363
- * @name: api_key
1364
- * @description: The API key of the user to fetch.
1365
- * @type: string
1366
- * @usage:
1367
- * ...
1368
- * const user = await server.users.get_by_api_key("XXXXXX");
1369
- */
1370
- async get_by_api_key(api_key) {
1371
- const data = await this._users_db.find({ api_key });
1372
- if (data == null) {
1373
- throw new Error(`Unable to find a user by api key "${api_key}".`);
1374
- }
1375
- return data;
1376
- }
1377
- // Get user info by token.
1378
- /* @docs:
1379
- * @title: Get User By Token
1380
- * @description:
1381
- * Get a user by token.
1382
- *
1383
- * If the token does not exist an `Error` will be thrown.
1384
- * @return:
1385
- * Returns a User object.
1386
- * @parameter:
1387
- * @name: token
1388
- * @description: The authentication token of the user to fetch.
1389
- * @type: string
1390
- * @usage:
1391
- * ...
1392
- * const user = await server.users.get_by_token("XXXXXX");
1393
- */
1394
- async get_by_token(token) {
1395
- const data = await this._tokens_db.find({ type: "token", token });
1396
- if (data == null) {
1397
- throw new Error(`Unable to find a user by token "${token}".`);
1398
- }
1399
- return await this.get(data.uid);
1400
- }
1401
- // Get uid by username.
1402
- /* @docs:
1403
- * @title: Get UID
1404
- * @description: Get a uid by username.
1405
- * @return:
1406
- * Returns the uid of the username.
1407
- *
1408
- * If the user does not exist `null` is returned.
1409
- * @parameter:
1410
- * @name: username
1411
- * @description: The username of the uid to fetch.
1412
- * @type: string
1413
- * @usage:
1414
- * ...
1415
- * let uid;
1416
- * if ((uid = await server.users.get_uid("myusername")) != null) { ... }
1417
- */
1418
- async get_uid(username) {
1419
- try {
1420
- return (await this.get_by_username(username)).uid;
1421
- } catch (e) {
1422
- return null;
1423
- }
1424
- }
1425
- // Get uid by username.
1426
- /* @docs:
1427
- * @title: Get UID By Email
1428
- * @description: Get a uid by username.
1429
- * @return:
1430
- * Returns the uid of the username.
1431
- *
1432
- * If the user does not exist `null` is returned.
1433
- * @parameter:
1434
- * @name: username
1435
- * @description: The username of the uid to fetch.
1436
- * @type: string
1437
- * @usage:
1438
- * ...
1439
- * let uid;
1440
- * if ((uid = await server.users.get_uid_by_username("myuser")) != null) { ... }
1441
- */
1442
- async get_uid_by_username(username) {
1443
- try {
1444
- return (await this.get_by_username(username)).uid;
1445
- } catch (e) {
1446
- return null;
1447
- }
1448
- }
1449
- // Get uid by email.
1450
- /* @docs:
1451
- * @title: Get UID By Email
1452
- * @description: Get a uid by email.
1453
- * @return:
1454
- * Returns the uid of the email.
1455
- *
1456
- * If the user does not exist `null` is returned.
1457
- * @parameter:
1458
- * @name: email
1459
- * @description: The email of the uid to fetch.
1460
- * @type: string
1461
- * @usage:
1462
- * ...
1463
- * let uid;
1464
- * if ((uid = await server.users.get_uid_by_email("my\@email.com")) != null) { ... }
1465
- */
1466
- async get_uid_by_email(email) {
1467
- try {
1468
- return (await this.get_by_email(email)).uid;
1469
- } catch (e) {
1470
- return null;
1471
- }
1472
- }
1473
- // Get uid by api key.
1474
- /* @docs:
1475
- * @title: Get UID By API Key
1476
- * @description: Get a uid by API key.
1477
- * @return:
1478
- * Returns the uid of the api key.
1479
- *
1480
- * If the user does not exist `null` is returned.
1481
- * @parameter:
1482
- * @name: api_key
1483
- * @description: The API key of the uid to fetch.
1484
- * @type: string
1485
- * @usage:
1486
- * ...
1487
- * let uid;
1488
- * if ((uid = await server.users.get_uid_by_api_key("XXXXXXXXXX")) != null) { ... }
1489
- */
1490
- async get_uid_by_api_key(api_key) {
1491
- if (typeof api_key !== "string") {
1492
- return null;
1493
- }
1494
- const pos = api_key.indexOf(":");
1495
- if (pos === -1) {
1496
- return null;
1497
- }
1498
- return api_key.substr(1, pos - 1);
1499
- }
1500
- // Get uid by token.
1501
- /* @docs:
1502
- * @title: Get UID By Token
1503
- * @description: Get a uid by token.
1504
- * @return:
1505
- * Returns the uid of the token.
1506
- *
1507
- * If the user does not exist `null` is returned.
1508
- * @parameter:
1509
- * @name: token
1510
- * @description: The token of the uid to fetch.
1511
- * @type: string
1512
- * @usage:
1513
- * ...
1514
- * let uid;
1515
- * if ((uid = await server.users.get_uid_by_token("XXXXXXXXXX")) != null) { ... }
1516
- */
1517
- async get_uid_by_token(token) {
1518
- return await this.get_uid_by_api_key(token);
1519
- }
1520
- // Get a user's support pin by uid.
1521
- /* @docs:
1522
- * @title: Get Support PIN
1523
- * @description:
1524
- * Get a user's support pin by uid.
1525
- * @return:
1526
- * Returns a User object.
1527
- * @parameter:
1528
- * @name: uid
1529
- * @cached: Users:uid:param
1530
- * @usage:
1531
- * ...
1532
- * const pin = await server.users.get_support_pin(1);
1533
- */
1534
- async get_support_pin(uid) {
1535
- return (await this.get(uid)).support_pin;
1536
- }
1537
- // Generate an api key by uid.
1538
- /* @docs:
1539
- * @title: Generate API Key
1540
- * @description:
1541
- * Generate an API key for a user.
1542
- *
1543
- * Generating an API key overwrites all existing API keys.
1544
- *
1545
- * If the uid does not exist an `Error` will be thrown.
1546
- * @return:
1547
- * Returns the API key string.
1548
- * @parameter:
1549
- * @name: uid
1550
- * @cached: Users:uid:param
1551
- * @usage:
1552
- * ...
1553
- * const api_key = await server.users.generate_api_key(0);
1554
- */
1555
- async generate_api_key(uid) {
1556
- const api_key = this._generate_api_key(uid);
1557
- await this._sys_set(uid, { api_key: this.server._hmac(api_key) });
1558
- return api_key;
1559
- }
1560
- // Revoke the API key of a user.
1561
- /* @docs:
1562
- * @title: Revoke API Key
1563
- * @description:
1564
- * Revoke the API key of a user.
1565
- *
1566
- * If the uid does not exist an `Error` will be thrown.
1567
- * @parameter:
1568
- * @name: uid
1569
- * @cached: Users:uid:param
1570
- * @usage:
1571
- * ...
1572
- * await server.users.revoke_api_key(0);
1573
- */
1574
- async revoke_api_key(uid) {
1575
- await this._sys_set(uid, { api_key: "" });
1576
- }
1577
- // Verify a plaintext password.
1578
- // Use async to keep it persistent with other functions.
1579
- /* @docs:
1580
- * @title: Verify Password
1581
- * @description:
1582
- * Verify a plaintext password.
1583
- *
1584
- * If the uid does not exist an `Error` will be thrown.
1585
- * @return:
1586
- * Returns a boolean indicating whether the verification was successful.
1587
- * @parameter:
1588
- * @name: uid
1589
- * @cached: Users:uid:param
1590
- * @parameter:
1591
- * @name: password
1592
- * @description: The plaintext password.
1593
- * @type: string
1594
- * @usage:
1595
- * ...
1596
- * const success = await server.users.verify_password(1, "XXXXXX");
1597
- */
1598
- async verify_password(uid, password) {
1599
- try {
1600
- const user = await this.get(uid);
1601
- return user.uid != null && user.password === this.server._hmac(password);
1602
- } catch (err) {
1603
- return false;
1604
- }
1605
- }
1606
- // Verify a plaintext api key.
1607
- // Use async to keep it persistent with other functions.
1608
- /* @docs:
1609
- * @title: Verify API Key
1610
- * @description:
1611
- * Verify an plaintext API key.
1612
- *
1613
- * If the uid does not exist an `Error` will be thrown.
1614
- * @return:
1615
- * Returns a boolean indicating whether the verification was successful.
1616
- * @parameter:
1617
- * @name: api_key
1618
- * @description: The api key to verify.
1619
- * @type: string
1620
- * @usage:
1621
- * ...
1622
- * const success = await server.users.verify_api_key("XXXXXX");
1623
- */
1624
- async verify_api_key(api_key) {
1625
- return await this.verify_api_key_by_uid(await this.get_uid_by_api_key(api_key), api_key);
1626
- }
1627
- // Verify a plaintext api key by uid.
1628
- // Use async to keep it persistent with other functions.
1629
- /* @docs:
1630
- * @title: Verify API Key By UID
1631
- * @description:
1632
- * Verify an plaintext API key by uid.
1633
- *
1634
- * If the uid does not exist an `Error` will be thrown.
1635
- * @return:
1636
- * Returns a boolean indicating whether the verification was successful.
1637
- * @parameter:
1638
- * @name: uid
1639
- * @cached: Users:uid:param
1640
- * @parameter:
1641
- * @name: api_key
1642
- * @description: The api key to verify.
1643
- * @type: string
1644
- * @usage:
1645
- * ...
1646
- * const success = await server.users.verify_api_key_by_uid(1, "XXXXXX");
1647
- */
1648
- async verify_api_key_by_uid(uid, api_key) {
1649
- try {
1650
- const user = await this.get(uid);
1651
- return user.uid != null && user.api_key != null && user.api_key?.length > 0 && user.api_key == this.server._hmac(api_key);
1652
- } catch (err) {
1653
- return false;
1654
- }
1655
- }
1656
- // Verify a token.
1657
- // Use async to keep it persistent with other functions.
1658
- /* @docs:
1659
- * @title: Verify Token
1660
- * @description:
1661
- * Verify an plaintext token.
1662
- *
1663
- * If the uid does not exist an `Error` will be thrown.
1664
- * @return:
1665
- * Returns a boolean indicating whether the verification was successful.
1666
- * @parameter:
1667
- * @name: api_key
1668
- * @description: The token to verify.
1669
- * @type: string
1670
- * @usage:
1671
- * ...
1672
- * const success = await server.users.verify_token("XXXXXX");
1673
- */
1674
- async verify_token(token) {
1675
- return await this.verify_token_by_uid(await this.get_uid_by_api_key(token), token);
1676
- }
1677
- // Verify a token by uid.
1678
- // Use async to keep it persistent with other functions.
1679
- /* @docs:
1680
- * @title: Verify Token By UID.
1681
- * @description:
1682
- * Verify an plaintext token by uid.
1683
- *
1684
- * If the uid does not exist an `Error` will be thrown.
1685
- * @return:
1686
- * Returns a boolean indicating whether the verification was successful.
1687
- * @parameter:
1688
- * @name: uid
1689
- * @cached: Users:uid:param
1690
- * @parameter:
1691
- * @name: api_key
1692
- * @description: The token to verify.
1693
- * @type: string
1694
- * @usage:
1695
- * ...
1696
- * const success = await server.users.verify_token_by_uid(1, "XXXXXX");
1697
- */
1698
- async verify_token_by_uid(uid, token) {
1699
- try {
1700
- const correct_token = await this._tokens_db.load({ uid, type: "token" });
1701
- return correct_token != null && correct_token.token != null && correct_token.active !== false && Date.now() < correct_token.expiration && correct_token.token == this.server._hmac(token);
1702
- } catch (err) {
1703
- return false;
1704
- }
1705
- }
1706
- // Verify a 2fa code.
1707
- // Use async to keep it persistent with other functions.
1708
- /* @docs:
1709
- * @title: Verify 2FA Code
1710
- * @description:
1711
- * Verify a 2FA code by user id.
1712
- * @parameter:
1713
- * @name: uid
1714
- * @cached: Users:uid:param
1715
- * @parameter:
1716
- * @name: code
1717
- * @description: The 2FA code.
1718
- * @type: string
1719
- * @return: Returns a boolean indicating whether the verification was successful or not.
1720
- * @usage:
1721
- * ...
1722
- * await server.users.verify_2fa(1, "123456");
1723
- */
1724
- async verify_2fa(uid, code) {
1725
- try {
1726
- const auth = await this._tokens_db.load({ uid, type: "2fa" });
1727
- if (auth == null) {
1728
- return "Invalid 2FA code.";
1729
- }
1730
- const now = Date.now();
1731
- if (now >= auth.expiration) {
1732
- await this._deactivate_2fa_token(uid);
1733
- return "The 2FA code has expired.";
1734
- }
1735
- const status = auth != null && auth.code != null && now < auth.expiration && auth.code == code && auth.active !== false;
1736
- if (status === false) {
1737
- return "Invalid 2FA code.";
1738
- }
1739
- return null;
1740
- } catch (err) {
1741
- error("Encountered an error while validating the 2FA code.");
1742
- error(`${err}.`);
1743
- return "Unknown error.";
1744
- }
1745
- }
1746
- // Send a 2fa code.
1747
- /* @docs:
1748
- * @title: Send 2FA Code
1749
- * @description:
1750
- * Send a 2FA code to a user by user id.
1751
- *
1752
- * By default the 2FA code will be valid for 5 minutes.
1753
- *
1754
- * The mail body will be generated using the `Server.on_2fa_mail({code, username, email, date, ip, device})` callback. When the callback is not defined an error will be thrown.
1755
- * @return:
1756
- * Returns a promise that will be resolved or rejected when the 2fa mail has been sent.
1757
- * @parameter:
1758
- * @name: uid
1759
- * @cached: Users:uid:param
1760
- * @parameter:
1761
- * @name: stream
1762
- * @description: The stream object from the client request.
1763
- * @type: object
1764
- * @parameter:
1765
- * @name: expiration
1766
- * @description: The amount of seconds in which the code will expire.
1767
- * @type: number
1768
- * @usage:
1769
- * ...
1770
- * await server.users.send_2fa({uid: 0, stream});
1771
- */
1772
- async send_2fa({ uid, stream, expiration = 300, _device = null, _username = null, _email = null }) {
1773
- let code;
1774
- if (_username == null && _email == null) {
1775
- code = await this._create_2fa_token(uid, expiration);
1776
- const user = await this.get(uid);
1777
- _username = user.username;
1778
- _email = user.email;
1779
- } else {
1780
- code = await this._create_2fa_token(_email, expiration);
1781
- }
1782
- let device;
1783
- if (_device == null) {
1784
- device = stream.headers["user-agent"];
1785
- }
1786
- if (this.server.on_2fa_mail === void 0) {
1787
- throw Error('Define server callback "Server.on_2fa_mail" to generate the HTML mail body.');
1788
- }
1789
- let mail = this.server.on_2fa_mail({
1790
- code,
1791
- username: _username,
1792
- email: _email,
1793
- date: (/* @__PURE__ */ new Date()).toUTCString(),
1794
- ip: stream.ip,
1795
- device: device ? device : "Unknown"
1796
- });
1797
- let body = mail, subject = null;
1798
- if (mail instanceof Mail.MailElement) {
1799
- body = mail.html();
1800
- subject = mail.subject();
1801
- }
1802
- await this.server.send_mail({
1803
- recipients: [_email],
1804
- subject: subject ?? "Two Factor Authentication Code",
1805
- body
1806
- });
1807
- }
1808
- // List all users.
1809
- async list() {
1810
- return await this._users_db.list_all();
1811
- }
1812
- }
1813
- var stdin_default = Users;
1814
- // Annotate the CommonJS export names for ESM import in node:
1815
- 0 && (module.exports = {
1816
- Users
1817
- });