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