create-lupine 1.0.31 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -0
- package/index.js +238 -112
- package/package.json +1 -1
- package/templates/common/.agents/skills/lupine-framework/SKILL.md +63 -0
- package/templates/common/.agents/skills/lupine-framework/references/backend-api.md +113 -0
- package/templates/common/.agents/skills/lupine-framework/references/icon-handling.md +71 -0
- package/templates/common/.agents/skills/lupine-framework/references/mobile-interactions.md +29 -0
- package/templates/common/.agents/skills/lupine-framework/references/native-apps.md +190 -0
- package/templates/common/.agents/skills/lupine-framework/references/navigation-and-routing.md +55 -0
- package/templates/common/.agents/skills/lupine-framework/references/reactivity-and-state.md +108 -0
- package/templates/common/.agents/skills/lupine-framework/references/styling-and-themes.md +121 -0
- package/templates/common/.env.mobile +4 -3
- package/templates/common/_env +10 -4
- package/templates/common/apps/server/README.md +10 -0
- package/templates/common/dev/cp-folder.js +9 -4
- package/templates/lupine-template-cv-starter/README.md +37 -0
- package/templates/lupine-template-cv-starter/app/package.json +20 -0
- package/templates/lupine-template-cv-starter/app/src/app.tsx +6 -0
- package/templates/lupine-template-cv-starter/app/src/index.native.tsx +4 -0
- package/templates/lupine-template-cv-starter/app/src/index.test.tsx +71 -0
- package/templates/lupine-template-cv-starter/app/src/index.web.tsx +28 -0
- package/templates/lupine-template-cv-starter/app/tsconfig.json +17 -0
- package/templates/lupine-template-cv-starter/lupine.json +8 -7
- package/templates/lupine-template-cv-starter/web/src/index.html +9 -2
- package/templates/lupine-template-cv-starter/web/src/index.tsx +9 -23
- package/templates/lupine-template-cv-starter/web/src/lupine-template-cv-starter/index.html +9 -2
- package/templates/lupine-template-cv-starter/web/src/lupine-template-cv-starter/index.tsx +4 -29
- package/templates/lupine-template-doc-starter/README.md +37 -0
- package/templates/lupine-template-doc-starter/app/package.json +20 -0
- package/templates/lupine-template-doc-starter/app/src/app.tsx +6 -0
- package/templates/lupine-template-doc-starter/app/src/index.native.tsx +4 -0
- package/templates/lupine-template-doc-starter/app/src/index.test.tsx +78 -0
- package/templates/lupine-template-doc-starter/app/src/index.web.tsx +28 -0
- package/templates/lupine-template-doc-starter/app/tsconfig.json +17 -0
- package/templates/lupine-template-doc-starter/lupine.json +8 -7
- package/templates/lupine-template-doc-starter/web/src/index.html +9 -2
- package/templates/lupine-template-doc-starter/web/src/index.tsx +9 -23
- package/templates/lupine-template-doc-starter/web/src/lupine-template-doc-starter/index.html +9 -2
- package/templates/lupine-template-doc-starter/web/src/lupine-template-doc-starter/index.tsx +4 -29
- package/templates/lupine-template-fullstack-note/README.md +83 -0
- package/templates/{lupine-template-hello-world → lupine-template-fullstack-note}/api/package.json +1 -1
- package/templates/lupine-template-fullstack-note/api/resources/install.mysql.sql +32 -0
- package/templates/lupine-template-fullstack-note/api/resources/install.sqlite.sql +27 -0
- package/templates/lupine-template-fullstack-note/api/src/service/answer.ts +39 -0
- package/templates/lupine-template-fullstack-note/api/src/service/api.ts +52 -0
- package/templates/lupine-template-fullstack-note/api/src/service/db.ts +78 -0
- package/templates/lupine-template-fullstack-note/api/src/service/notes-api.ts +86 -0
- package/templates/lupine-template-fullstack-note/api/src/service/root-api.ts +19 -0
- package/templates/lupine-template-fullstack-note/api/src/service/user-api.ts +182 -0
- package/templates/lupine-template-fullstack-note/app/package.json +19 -0
- package/templates/lupine-template-fullstack-note/app/src/app.tsx +75 -0
- package/templates/lupine-template-fullstack-note/app/src/index.native.tsx +4 -0
- package/templates/lupine-template-fullstack-note/app/src/index.test.tsx +373 -0
- package/templates/lupine-template-fullstack-note/app/src/index.web.tsx +20 -0
- package/templates/lupine-template-fullstack-note/app/src/pages/components.tsx +65 -0
- package/templates/lupine-template-fullstack-note/app/src/pages/details.tsx +38 -0
- package/templates/lupine-template-fullstack-note/app/src/pages/discover.tsx +25 -0
- package/templates/lupine-template-fullstack-note/app/src/pages/home.tsx +76 -0
- package/templates/lupine-template-fullstack-note/app/src/pages/inbox.tsx +49 -0
- package/templates/lupine-template-fullstack-note/app/src/pages/login.tsx +54 -0
- package/templates/lupine-template-fullstack-note/app/src/pages/me.tsx +32 -0
- package/templates/lupine-template-fullstack-note/app/src/pages/notes.tsx +128 -0
- package/templates/lupine-template-fullstack-note/app/src/pages/settings.tsx +45 -0
- package/templates/lupine-template-fullstack-note/app/src/shell/AppMenu.tsx +44 -0
- package/templates/lupine-template-fullstack-note/app/src/shell/api.ts +159 -0
- package/templates/lupine-template-fullstack-note/app/src/shell/data.ts +20 -0
- package/templates/lupine-template-fullstack-note/app/src/shell/locales/ja.ts +80 -0
- package/templates/lupine-template-fullstack-note/app/src/shell/locales/zh-CN.ts +80 -0
- package/templates/lupine-template-fullstack-note/app/src/shell/texts.ts +14 -0
- package/templates/lupine-template-fullstack-note/app/tsconfig.json +17 -0
- package/templates/{lupine-template-hello-world → lupine-template-fullstack-note}/lupine.json +4 -2
- package/templates/{lupine-template-hello-world → lupine-template-fullstack-note}/web/package.json +1 -1
- package/templates/{lupine-template-hello-world → lupine-template-fullstack-note}/web/src/index.html +10 -3
- package/templates/lupine-template-fullstack-note/web/src/index.tsx +15 -0
- package/templates/lupine-template-fullstack-starter/README.md +70 -0
- package/templates/lupine-template-fullstack-starter/api/package.json +10 -0
- package/templates/lupine-template-fullstack-starter/api/resources/install.mysql.sql +107 -0
- package/templates/lupine-template-fullstack-starter/api/resources/install.sqlite.sql +75 -0
- package/templates/lupine-template-fullstack-starter/api/src/lang/api-lang-en.ts +39 -0
- package/templates/lupine-template-fullstack-starter/api/src/lang/api-lang-zh-cn.ts +37 -0
- package/templates/lupine-template-fullstack-starter/api/src/service/admin-api.ts +139 -0
- package/templates/lupine-template-fullstack-starter/api/src/service/api.ts +111 -0
- package/templates/lupine-template-fullstack-starter/api/src/service/contact-api.ts +50 -0
- package/templates/lupine-template-fullstack-starter/api/src/service/file-api-base.ts +30 -0
- package/templates/lupine-template-fullstack-starter/api/src/service/root-api.ts +28 -0
- package/templates/lupine-template-fullstack-starter/api/src/service/send-email.ts +47 -0
- package/templates/lupine-template-fullstack-starter/api/src/service/setting-api.ts +60 -0
- package/templates/lupine-template-fullstack-starter/api/src/service/user-api.ts +1046 -0
- package/templates/lupine-template-fullstack-starter/app/assets/sample.png +0 -0
- package/templates/lupine-template-fullstack-starter/app/package.json +20 -0
- package/templates/lupine-template-fullstack-starter/app/src/app.tsx +76 -0
- package/templates/lupine-template-fullstack-starter/app/src/index.native.tsx +4 -0
- package/templates/lupine-template-fullstack-starter/app/src/index.test.tsx +276 -0
- package/templates/lupine-template-fullstack-starter/app/src/index.web.tsx +28 -0
- package/templates/lupine-template-fullstack-starter/app/src/pages/about.tsx +27 -0
- package/templates/lupine-template-fullstack-starter/app/src/pages/contact.tsx +41 -0
- package/templates/lupine-template-fullstack-starter/app/src/pages/favorite.tsx +27 -0
- package/templates/lupine-template-fullstack-starter/app/src/pages/home.tsx +63 -0
- package/templates/lupine-template-fullstack-starter/app/src/pages/login.tsx +63 -0
- package/templates/lupine-template-fullstack-starter/app/src/pages/mine.tsx +44 -0
- package/templates/lupine-template-fullstack-starter/app/src/pages/profile.tsx +58 -0
- package/templates/lupine-template-fullstack-starter/app/src/pages/register.tsx +48 -0
- package/templates/lupine-template-fullstack-starter/app/src/pages/reset-password.tsx +41 -0
- package/templates/lupine-template-fullstack-starter/app/src/pages/settings.tsx +33 -0
- package/templates/lupine-template-fullstack-starter/app/src/pages/story.tsx +46 -0
- package/templates/lupine-template-fullstack-starter/app/src/shell/api.ts +127 -0
- package/templates/lupine-template-fullstack-starter/app/src/shell/parts.tsx +100 -0
- package/templates/lupine-template-fullstack-starter/app/src/shell/stories.ts +65 -0
- package/templates/lupine-template-fullstack-starter/app/src/shell/texts.ts +79 -0
- package/templates/lupine-template-fullstack-starter/app/tsconfig.json +17 -0
- package/templates/lupine-template-fullstack-starter/lupine.json +31 -0
- package/templates/lupine-template-fullstack-starter/web/assets/android-chrome-192x192.png +0 -0
- package/templates/lupine-template-fullstack-starter/web/assets/apple-touch-icon.png +0 -0
- package/templates/lupine-template-fullstack-starter/web/assets/favicon-16x16.png +0 -0
- package/templates/lupine-template-fullstack-starter/web/assets/favicon-32x32.png +0 -0
- package/templates/lupine-template-fullstack-starter/web/assets/favicon.ico +0 -0
- package/templates/lupine-template-fullstack-starter/web/assets/icons-font/License-carbon-icons.txt +71 -0
- package/templates/lupine-template-fullstack-starter/web/assets/icons-font/License-material-design.txt +20 -0
- package/templates/lupine-template-fullstack-starter/web/assets/icons-font/i-font.woff2 +0 -0
- package/templates/lupine-template-fullstack-starter/web/assets/icons-font/sample.html +124 -0
- package/templates/lupine-template-fullstack-starter/web/assets/icons-font/style.css +103 -0
- package/templates/lupine-template-fullstack-starter/web/assets/lupine-flower.png +0 -0
- package/templates/lupine-template-fullstack-starter/web/assets/lupine.svg +71 -0
- package/templates/lupine-template-fullstack-starter/web/assets/site.webmanifest +14 -0
- package/templates/lupine-template-fullstack-starter/web/icons-font.config.js +129 -0
- package/templates/{lupine-template-responsive-starter/api → lupine-template-fullstack-starter/web}/package.json +1 -1
- package/templates/lupine-template-fullstack-starter/web/src/admin/admin-patch.tsx +34 -0
- package/templates/lupine-template-fullstack-starter/web/src/admin/components/home-search-component.tsx +125 -0
- package/templates/lupine-template-fullstack-starter/web/src/admin/components/story-card.tsx +105 -0
- package/templates/lupine-template-fullstack-starter/web/src/admin/components/story-list.tsx +105 -0
- package/templates/lupine-template-fullstack-starter/web/src/admin/components/story-props.ts +34 -0
- package/templates/lupine-template-fullstack-starter/web/src/admin/index.html +23 -0
- package/templates/lupine-template-fullstack-starter/web/src/admin/index.tsx +49 -0
- package/templates/lupine-template-fullstack-starter/web/src/admin/pages/admin-cfg.tsx +144 -0
- package/templates/lupine-template-fullstack-starter/web/src/admin/pages/admin-item-edit.tsx +20 -0
- package/templates/lupine-template-fullstack-starter/web/src/admin/pages/admin-item-list.tsx +23 -0
- package/templates/lupine-template-fullstack-starter/web/src/admin/pages/admin-user.tsx +145 -0
- package/templates/lupine-template-fullstack-starter/web/src/index.html +28 -0
- package/templates/lupine-template-fullstack-starter/web/src/index.tsx +16 -0
- package/templates/lupine-template-fullstack-starter/web/src/services/fetch-data.ts +37 -0
- package/templates/lupine-template-fullstack-starter/web/src/styles/base-css.ts +14 -0
- package/templates/{lupine-template-hello-world → lupine-template-fullstack-starter}/web/src/styles/global.css +10 -57
- package/templates/{lupine-template-hello-world → lupine-template-fullstack-starter}/web/src/styles/theme.ts +2 -1
- package/templates/lupine-template-responsive-starter/README.md +51 -0
- package/templates/lupine-template-responsive-starter/app/package.json +19 -0
- package/templates/lupine-template-responsive-starter/app/src/app.tsx +80 -0
- package/templates/lupine-template-responsive-starter/app/src/index.native.tsx +4 -0
- package/templates/lupine-template-responsive-starter/app/src/index.test.tsx +283 -0
- package/templates/lupine-template-responsive-starter/app/src/index.web.tsx +31 -0
- package/templates/lupine-template-responsive-starter/app/src/pages/about.tsx +23 -0
- package/templates/lupine-template-responsive-starter/app/src/pages/finance.tsx +48 -0
- package/templates/lupine-template-responsive-starter/app/src/pages/mine.tsx +60 -0
- package/templates/lupine-template-responsive-starter/app/src/pages/not-found.tsx +9 -0
- package/templates/lupine-template-responsive-starter/app/src/pages/note-detail.tsx +43 -0
- package/templates/lupine-template-responsive-starter/app/src/pages/note-edit.tsx +45 -0
- package/templates/lupine-template-responsive-starter/app/src/pages/note-search.tsx +62 -0
- package/templates/lupine-template-responsive-starter/app/src/pages/notes.tsx +71 -0
- package/templates/lupine-template-responsive-starter/app/src/pages/settings.tsx +55 -0
- package/templates/lupine-template-responsive-starter/app/src/pages/tools.tsx +112 -0
- package/templates/lupine-template-responsive-starter/app/src/shell/icons.ts +7 -0
- package/templates/lupine-template-responsive-starter/app/src/shell/layout.ts +18 -0
- package/templates/lupine-template-responsive-starter/app/src/shell/locales/zh-CN.ts +89 -0
- package/templates/lupine-template-responsive-starter/app/src/shell/menu.tsx +81 -0
- package/templates/lupine-template-responsive-starter/app/src/shell/nav.ts +12 -0
- package/templates/lupine-template-responsive-starter/app/src/shell/store.ts +72 -0
- package/templates/lupine-template-responsive-starter/app/src/shell/texts.ts +11 -0
- package/templates/lupine-template-responsive-starter/app/tsconfig.json +17 -0
- package/templates/lupine-template-responsive-starter/lupine.json +7 -6
- package/templates/lupine-template-responsive-starter/web/src/index.html +9 -2
- package/templates/lupine-template-responsive-starter/web/src/index.tsx +9 -34
- package/templates/lupine-template-responsive-starter/web/src/lupine-template-responsive-starter/index.html +12 -6
- package/templates/lupine-template-responsive-starter/web/src/lupine-template-responsive-starter/index.tsx +12 -31
- package/templates/common/AI_CONTEXT.md +0 -1002
- package/templates/common/apps/server/src/app-loader.ts +0 -4
- package/templates/lupine-template-cv-starter/api/package.json +0 -6
- package/templates/lupine-template-cv-starter/api/resources/install.sqlite.sql +0 -4
- package/templates/lupine-template-cv-starter/api/src/index.ts +0 -15
- package/templates/lupine-template-cv-starter/web/src/styles/base-css.ts +0 -15
- package/templates/lupine-template-doc-starter/api/package.json +0 -6
- package/templates/lupine-template-doc-starter/api/resources/config_default.json +0 -6
- package/templates/lupine-template-doc-starter/api/resources/install.sqlite.sql +0 -4
- package/templates/lupine-template-doc-starter/api/src/index.ts +0 -15
- package/templates/lupine-template-doc-starter/api/src/resources/config_default.json +0 -6
- package/templates/lupine-template-doc-starter/api/src/resources/install.sqlite.sql +0 -4
- package/templates/lupine-template-doc-starter/web/src/client-env-keys.ts +0 -5
- package/templates/lupine-template-doc-starter/web/src/styles/base-css.ts +0 -15
- package/templates/lupine-template-hello-world/api/resources/config_default.json +0 -6
- package/templates/lupine-template-hello-world/api/resources/install.sqlite.sql +0 -4
- package/templates/lupine-template-hello-world/api/src/service/root-api.ts +0 -18
- package/templates/lupine-template-hello-world/web/src/index.tsx +0 -73
- package/templates/lupine-template-hello-world/web/src/styles/app.css +0 -0
- package/templates/lupine-template-hello-world/web/src/styles/base-css.ts +0 -1
- package/templates/lupine-template-responsive-starter/api/resources/config_default.json +0 -6
- package/templates/lupine-template-responsive-starter/api/resources/install.sqlite.sql +0 -4
- package/templates/lupine-template-responsive-starter/api/src/service/root-api.ts +0 -18
- package/templates/lupine-template-responsive-starter/web/src/app-icons.ts +0 -76
- package/templates/lupine-template-responsive-starter/web/src/components/input-history-component.tsx +0 -93
- package/templates/lupine-template-responsive-starter/web/src/components/mine-about-page.tsx +0 -86
- package/templates/lupine-template-responsive-starter/web/src/components/mine-premium-page.tsx +0 -80
- package/templates/lupine-template-responsive-starter/web/src/components/mine-settings-page.tsx +0 -220
- package/templates/lupine-template-responsive-starter/web/src/components/mobile-top-search-menu.tsx +0 -119
- package/templates/lupine-template-responsive-starter/web/src/components/note-detail.tsx +0 -116
- package/templates/lupine-template-responsive-starter/web/src/components/note-edit.tsx +0 -154
- package/templates/lupine-template-responsive-starter/web/src/components/note-search-page.tsx +0 -193
- package/templates/lupine-template-responsive-starter/web/src/components/search-input.tsx +0 -95
- package/templates/lupine-template-responsive-starter/web/src/components/side-menu-content.tsx +0 -182
- package/templates/lupine-template-responsive-starter/web/src/frames/app-responsive-frame.tsx +0 -52
- package/templates/lupine-template-responsive-starter/web/src/pages/about-page.tsx +0 -43
- package/templates/lupine-template-responsive-starter/web/src/pages/finance-page.tsx +0 -46
- package/templates/lupine-template-responsive-starter/web/src/pages/home-page.tsx +0 -452
- package/templates/lupine-template-responsive-starter/web/src/pages/mine-page.tsx +0 -325
- package/templates/lupine-template-responsive-starter/web/src/pages/tools-page.tsx +0 -46
- package/templates/lupine-template-responsive-starter/web/src/services/local-notes-service.ts +0 -87
- package/templates/lupine-template-responsive-starter/web/src/services/mine-service.ts +0 -45
- package/templates/lupine-template-responsive-starter/web/src/styles/app.css +0 -0
- package/templates/lupine-template-responsive-starter/web/src/styles/base-css.ts +0 -65
- package/templates/lupine-template-responsive-starter/web/src/styles/global.css +0 -2130
- package/templates/lupine-template-responsive-starter/web/src/styles/theme.ts +0 -16
- /package/templates/lupine-template-cv-starter/{web → app}/markdown/en/01-experience/index.md +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/markdown/en/02-projects/index.md +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/markdown/en/03-skills/index.md +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/markdown/en/04-education/index.md +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/markdown/en/index.md +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/markdown/index.md +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/markdown/zh/01-experience/index.md +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/markdown/zh/02-projects/index.md +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/markdown/zh/03-skills/index.md +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/markdown/zh/04-education/index.md +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/markdown/zh/index.md +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/en/01-experience/index.html +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/en/02-projects/index.html +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/en/03-skills/index.html +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/en/04-education/index.html +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/en/index.html +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/index.html +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/markdown-config.ts +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/zh/01-experience/index.html +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/zh/02-projects/index.html +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/zh/03-skills/index.html +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/zh/04-education/index.html +0 -0
- /package/templates/lupine-template-cv-starter/{web → app}/src/markdown-built/zh/index.html +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/markdown/en/essentials/index.md +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/markdown/en/essentials/list.md +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/markdown/en/guide/install.md +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/markdown/en/guide/started.md +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/markdown/en/index.md +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/markdown/index.md +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/markdown/zh/essentials/index.md +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/markdown/zh/essentials/list.md +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/markdown/zh/guide/install.md +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/markdown/zh/guide/started.md +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/markdown/zh/index.md +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/en/essentials/index.html +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/en/essentials/list.html +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/en/guide/install.html +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/en/guide/started.html +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/en/index.html +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/index.html +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/markdown-config.ts +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/zh/essentials/index.html +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/zh/essentials/list.html +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/zh/guide/install.html +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/zh/guide/started.html +0 -0
- /package/templates/lupine-template-doc-starter/{web → app}/src/markdown-built/zh/index.html +0 -0
- /package/templates/{lupine-template-hello-world → lupine-template-fullstack-note}/api/src/index.ts +0 -0
- /package/templates/{lupine-template-hello-world → lupine-template-fullstack-note}/web/assets/favicon.ico +0 -0
- /package/templates/{lupine-template-cv-starter → lupine-template-fullstack-starter}/api/resources/config_default.json +0 -0
- /package/templates/{lupine-template-responsive-starter → lupine-template-fullstack-starter}/api/src/index.ts +0 -0
- /package/templates/{lupine-template-cv-starter/web/src → lupine-template-fullstack-starter/web/src/models}/client-env-keys.ts +0 -0
|
@@ -0,0 +1,1046 @@
|
|
|
1
|
+
import { ServerResponse } from 'http';
|
|
2
|
+
import {
|
|
3
|
+
DateUtils,
|
|
4
|
+
CryptoUtils,
|
|
5
|
+
Logger,
|
|
6
|
+
apiCache,
|
|
7
|
+
ServerRequest,
|
|
8
|
+
ApiHelper,
|
|
9
|
+
JsonKeyValue,
|
|
10
|
+
langHelper,
|
|
11
|
+
apiStorage,
|
|
12
|
+
AppAdminHookSetCookieProps,
|
|
13
|
+
AppAdminHookCheckLoginProps,
|
|
14
|
+
AppAdminHookLogoutProps,
|
|
15
|
+
adminApiHelper,
|
|
16
|
+
getSessionExpireDays,
|
|
17
|
+
isSessionExpired,
|
|
18
|
+
isHttpsRequest,
|
|
19
|
+
DEV_ADMIN_USER_KEY_NAME,
|
|
20
|
+
DEV_ADMIN_PASS_KEY_NAME,
|
|
21
|
+
DEV_ADMIN_SESSION_INVALID_BEFORE_KEY_NAME,
|
|
22
|
+
} from 'lupine.api';
|
|
23
|
+
import { sendEmail, sendSiteEmail } from './send-email';
|
|
24
|
+
|
|
25
|
+
const PW_RETRY_MAX = 5;
|
|
26
|
+
const PW_RETRY_RESET_MINUTES = 15;
|
|
27
|
+
const logger = new Logger('user-api');
|
|
28
|
+
|
|
29
|
+
export type LoginJsonProps = {
|
|
30
|
+
id: number; // user id
|
|
31
|
+
u: string; // username
|
|
32
|
+
t: string; // type: admin, user
|
|
33
|
+
ip: string;
|
|
34
|
+
h: string; // hash of name+pass
|
|
35
|
+
l?: number; // login time
|
|
36
|
+
r?: any; // record, user info after verification
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const ADMIN_SESSION_INVALID_BEFORE_KEY_NAME = 'ADMIN_SESSION_INVALID_BEFORE';
|
|
40
|
+
const ADMIN_SESSION_EXPIRE_DAYS_KEY_NAME = 'ADMIN_SESSION_EXPIRE_DAYS';
|
|
41
|
+
|
|
42
|
+
// The token of a sign-in is in the answer only for the apps (iOS, Android, macOS, Windows): they have no cookies and
|
|
43
|
+
// send it back in a Cookie header. A browser keeps it in its httpOnly cookie alone, where the scripts of a page can't
|
|
44
|
+
// read it. Browsers send Origin with every POST, the apps don't.
|
|
45
|
+
const tokenForApps = (req: ServerRequest, token: string) => (req.headers.origin ? undefined : token);
|
|
46
|
+
|
|
47
|
+
export const appAdminHookSetCookie: AppAdminHookSetCookieProps = async (
|
|
48
|
+
req: ServerRequest,
|
|
49
|
+
res: ServerResponse,
|
|
50
|
+
username?: string,
|
|
51
|
+
adminKeyHex?: string
|
|
52
|
+
) => {
|
|
53
|
+
const cryptoKey = process.env['CRYPTO_KEY'];
|
|
54
|
+
const u = username || process.env['ADMIN_USER'];
|
|
55
|
+
if (!cryptoKey || !u || !adminKeyHex) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const loginJson: LoginJsonProps = {
|
|
60
|
+
ip: '',
|
|
61
|
+
id: 0,
|
|
62
|
+
u: u,
|
|
63
|
+
t: 'admin',
|
|
64
|
+
h: adminKeyHex,
|
|
65
|
+
l: Date.now(),
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const token = JSON.stringify(loginJson);
|
|
69
|
+
const tokenCookie = CryptoUtils.encrypt(token, cryptoKey);
|
|
70
|
+
const response = {
|
|
71
|
+
status: 'ok',
|
|
72
|
+
message: langHelper.getLang('shared:login_success'),
|
|
73
|
+
appLogin: 1,
|
|
74
|
+
user: {
|
|
75
|
+
nickname: loginJson.u,
|
|
76
|
+
admin: loginJson.t === 'admin' ? '1' : '0',
|
|
77
|
+
u: loginJson.u,
|
|
78
|
+
t: loginJson.t,
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// sameSite: 'none' needs secure=true
|
|
83
|
+
req.locals.setCookie('_token', tokenCookie, {
|
|
84
|
+
expireDays: getSessionExpireDays(ADMIN_SESSION_EXPIRE_DAYS_KEY_NAME),
|
|
85
|
+
path: '/',
|
|
86
|
+
httpOnly: true,
|
|
87
|
+
secure: isHttpsRequest(req),
|
|
88
|
+
sameSite: 'lax',
|
|
89
|
+
});
|
|
90
|
+
return response;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export const appAdminHookCheckLogin: AppAdminHookCheckLoginProps = async (
|
|
94
|
+
req: ServerRequest,
|
|
95
|
+
res: ServerResponse,
|
|
96
|
+
username: string,
|
|
97
|
+
password: string,
|
|
98
|
+
sendResponseWhenError: boolean
|
|
99
|
+
) => {
|
|
100
|
+
if (!username || !password) {
|
|
101
|
+
const json = await getUserFromCookie(req, res, false);
|
|
102
|
+
if (json && json.t === 'admin' && json.h) {
|
|
103
|
+
if (sendResponseWhenError) {
|
|
104
|
+
ApiHelper.sendJson(req, res, {
|
|
105
|
+
status: 'ok',
|
|
106
|
+
message: langHelper.getLang('shared:login_success'),
|
|
107
|
+
appLogin: 1,
|
|
108
|
+
user: {
|
|
109
|
+
nickname: json.u,
|
|
110
|
+
admin: json.t === 'admin' ? '1' : '0',
|
|
111
|
+
u: json.u,
|
|
112
|
+
t: json.t,
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const adminUser = process.env['ADMIN_USER'] || '';
|
|
122
|
+
const adminPass = process.env['ADMIN_PASS'] || '';
|
|
123
|
+
const pwCheck =
|
|
124
|
+
username && password && password.length <= 256 && adminUser && adminPass
|
|
125
|
+
? CryptoUtils.verifyPasswordLayered(password, adminPass)
|
|
126
|
+
: { valid: false as const };
|
|
127
|
+
if (
|
|
128
|
+
adminUser &&
|
|
129
|
+
adminApiHelper.timingSafeEqual(username, adminUser) &&
|
|
130
|
+
pwCheck.valid &&
|
|
131
|
+
pwCheck.keyHex
|
|
132
|
+
) {
|
|
133
|
+
const appAdminResponse = await appAdminHookSetCookie(req, res, username, pwCheck.keyHex);
|
|
134
|
+
sendResponseWhenError && ApiHelper.sendJson(req, res, appAdminResponse);
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
return false;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
export const appAdminHookLogout: AppAdminHookLogoutProps = async (req: ServerRequest, res: ServerResponse) => {
|
|
141
|
+
req.locals.setCookie('_token', '', {
|
|
142
|
+
expireDays: 0,
|
|
143
|
+
path: '/',
|
|
144
|
+
httpOnly: true,
|
|
145
|
+
secure: isHttpsRequest(req),
|
|
146
|
+
sameSite: 'lax',
|
|
147
|
+
});
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const computeUserSessionHash = (username: string, storedPassword: string): string => {
|
|
151
|
+
if (storedPassword.startsWith('scrypt$')) {
|
|
152
|
+
return CryptoUtils.sha256(`${username}:${storedPassword}`);
|
|
153
|
+
}
|
|
154
|
+
return CryptoUtils.hash(`${username}:${storedPassword}`);
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const verifyUserPassword = (plainPassword: string, storedPassword: string): boolean => {
|
|
158
|
+
if (!plainPassword || plainPassword.length > 256 || !storedPassword) {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
if (storedPassword.startsWith('scrypt$')) {
|
|
162
|
+
return CryptoUtils.verifyPassword(plainPassword, storedPassword);
|
|
163
|
+
}
|
|
164
|
+
return CryptoUtils.timingSafeEqual(CryptoUtils.hash(plainPassword), storedPassword);
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
export const getUserFromCookie = async (
|
|
168
|
+
req: ServerRequest,
|
|
169
|
+
res: ServerResponse,
|
|
170
|
+
sendResponseWhenError = true
|
|
171
|
+
): Promise<LoginJsonProps | false> => {
|
|
172
|
+
try {
|
|
173
|
+
const cookies = req.locals.cookies();
|
|
174
|
+
const token = cookies.get('_token', '');
|
|
175
|
+
if (token) {
|
|
176
|
+
// encrypted with CRYPTO_KEY by the sign-in (decryptJson takes DEV_CRYPTO_KEY without a key)
|
|
177
|
+
const json = adminApiHelper.decryptJson(token, process.env['CRYPTO_KEY']) as LoginJsonProps;
|
|
178
|
+
if (!json || !json.u) {
|
|
179
|
+
if (sendResponseWhenError) {
|
|
180
|
+
const response = {
|
|
181
|
+
status: 'error',
|
|
182
|
+
message: langHelper.getLang('shared:not_logged_in'),
|
|
183
|
+
};
|
|
184
|
+
ApiHelper.sendJson(req, res, response);
|
|
185
|
+
}
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
// if it's special admin or dev-admin delegated session
|
|
189
|
+
const expireDays = getSessionExpireDays(ADMIN_SESSION_EXPIRE_DAYS_KEY_NAME);
|
|
190
|
+
if (json.h && !isSessionExpired(json.l, expireDays)) {
|
|
191
|
+
const hashStr = String(json.h);
|
|
192
|
+
let validAdmin = false;
|
|
193
|
+
if (hashStr.startsWith('dev:')) {
|
|
194
|
+
const devUser = process.env[DEV_ADMIN_USER_KEY_NAME] || '';
|
|
195
|
+
const devPass = process.env[DEV_ADMIN_PASS_KEY_NAME] || '';
|
|
196
|
+
const devInvalidBefore = Number(process.env[DEV_ADMIN_SESSION_INVALID_BEFORE_KEY_NAME] || 0);
|
|
197
|
+
if (
|
|
198
|
+
devUser &&
|
|
199
|
+
devPass &&
|
|
200
|
+
(devInvalidBefore <= 0 || (json.l && json.l >= devInvalidBefore)) &&
|
|
201
|
+
adminApiHelper.timingSafeEqual(String(json.u), devUser) &&
|
|
202
|
+
CryptoUtils.verifyPasswordLayeredKey(hashStr.slice(4), devPass)
|
|
203
|
+
) {
|
|
204
|
+
validAdmin = true;
|
|
205
|
+
}
|
|
206
|
+
} else {
|
|
207
|
+
const adminUser = process.env['ADMIN_USER'] || '';
|
|
208
|
+
const adminPass = process.env['ADMIN_PASS'] || '';
|
|
209
|
+
const invalidBefore = Number(process.env[ADMIN_SESSION_INVALID_BEFORE_KEY_NAME] || 0);
|
|
210
|
+
if (
|
|
211
|
+
adminUser &&
|
|
212
|
+
adminPass &&
|
|
213
|
+
(invalidBefore <= 0 || (json.l && json.l >= invalidBefore)) &&
|
|
214
|
+
adminApiHelper.timingSafeEqual(String(json.u), adminUser) &&
|
|
215
|
+
CryptoUtils.verifyPasswordLayeredKey(hashStr, adminPass)
|
|
216
|
+
) {
|
|
217
|
+
validAdmin = true;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
if (validAdmin) {
|
|
221
|
+
json.r = {
|
|
222
|
+
id: 0,
|
|
223
|
+
email: json.u,
|
|
224
|
+
username: json.u,
|
|
225
|
+
nickname: json.u,
|
|
226
|
+
usertype: json.t,
|
|
227
|
+
};
|
|
228
|
+
return json;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// verify whether the user is blocked
|
|
233
|
+
const db = apiCache.getDb();
|
|
234
|
+
const record = await db.selectObject('$__s_user', ['*'], {
|
|
235
|
+
username: json.u,
|
|
236
|
+
});
|
|
237
|
+
if (!record || record.length !== 1 || record[0].block === '1') {
|
|
238
|
+
const msg =
|
|
239
|
+
!record || record.length !== 1
|
|
240
|
+
? langHelper.getLang('shared:user_not_found')
|
|
241
|
+
: langHelper.getLang('shared:user_locked');
|
|
242
|
+
if (sendResponseWhenError) {
|
|
243
|
+
const response = {
|
|
244
|
+
status: 'error',
|
|
245
|
+
message: msg,
|
|
246
|
+
};
|
|
247
|
+
ApiHelper.sendJson(req, res, response);
|
|
248
|
+
}
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// when user changes password, the cookie info will be invalid
|
|
253
|
+
const expectedShaHash = CryptoUtils.sha256(`${record[0].username}:${record[0].password}`);
|
|
254
|
+
const expectedLegacyHash = computeUserSessionHash(String(record[0].username), String(record[0].password));
|
|
255
|
+
const providedHash = String(json.h || '');
|
|
256
|
+
const hashMatches =
|
|
257
|
+
CryptoUtils.timingSafeEqual(providedHash, expectedShaHash) ||
|
|
258
|
+
(!String(record[0].password || '').startsWith('scrypt$') &&
|
|
259
|
+
CryptoUtils.timingSafeEqual(providedHash, expectedLegacyHash));
|
|
260
|
+
if (!hashMatches) {
|
|
261
|
+
if (sendResponseWhenError) {
|
|
262
|
+
const response = {
|
|
263
|
+
status: 'error',
|
|
264
|
+
message: langHelper.getLang('shared:wrong_hash'),
|
|
265
|
+
};
|
|
266
|
+
ApiHelper.sendJson(req, res, response);
|
|
267
|
+
}
|
|
268
|
+
return false;
|
|
269
|
+
}
|
|
270
|
+
// Do not trust identity/permission fields from the encrypted cookie.
|
|
271
|
+
// If CRYPTO_KEY is leaked, an attacker may tamper with id/t and re-encrypt the token.
|
|
272
|
+
// After the password-derived hash is verified, restore trusted fields from DB.
|
|
273
|
+
json.id = record[0].id;
|
|
274
|
+
json.t = record[0].usertype;
|
|
275
|
+
json.r = record[0];
|
|
276
|
+
return json;
|
|
277
|
+
}
|
|
278
|
+
} catch (error: any) {
|
|
279
|
+
logger.error(error.message);
|
|
280
|
+
}
|
|
281
|
+
if (sendResponseWhenError) {
|
|
282
|
+
const response = {
|
|
283
|
+
status: 'error',
|
|
284
|
+
message: langHelper.getLang('shared:not_logged_in'),
|
|
285
|
+
};
|
|
286
|
+
ApiHelper.sendJson(req, res, response);
|
|
287
|
+
}
|
|
288
|
+
return false;
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
export const getUserInfo = async (req: ServerRequest, res: ServerResponse) => {
|
|
292
|
+
const loginJson = await getUserFromCookie(req, res, true);
|
|
293
|
+
if (!loginJson) {
|
|
294
|
+
return true;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const user = loginJson.r;
|
|
298
|
+
const response = {
|
|
299
|
+
status: 'ok',
|
|
300
|
+
results: {
|
|
301
|
+
id: user.id,
|
|
302
|
+
email: user.email,
|
|
303
|
+
username: user.username,
|
|
304
|
+
nickname: user.nickname,
|
|
305
|
+
usertype: user.usertype,
|
|
306
|
+
},
|
|
307
|
+
};
|
|
308
|
+
ApiHelper.sendJson(req, res, response);
|
|
309
|
+
return true;
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
export const getUserList = async (req: ServerRequest, res: ServerResponse) => {
|
|
313
|
+
const db = apiCache.getDb();
|
|
314
|
+
const loginJson = await getUserFromCookie(req, res, true);
|
|
315
|
+
if (!loginJson) {
|
|
316
|
+
return true;
|
|
317
|
+
}
|
|
318
|
+
if (loginJson.t !== 'admin') {
|
|
319
|
+
const response = {
|
|
320
|
+
status: 'error',
|
|
321
|
+
message: langHelper.getLang('shared:permission_denied'),
|
|
322
|
+
};
|
|
323
|
+
ApiHelper.sendJson(req, res, response);
|
|
324
|
+
return true;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const users = await db.selectObject(
|
|
328
|
+
'$__s_user',
|
|
329
|
+
['id', 'username', 'nickname', 'email', 'usertype', 'level', 'activated', 'block', 'registerdate', 'lastvisitdate'],
|
|
330
|
+
undefined
|
|
331
|
+
);
|
|
332
|
+
const response = {
|
|
333
|
+
status: 'ok',
|
|
334
|
+
results: users,
|
|
335
|
+
};
|
|
336
|
+
ApiHelper.sendJson(req, res, response);
|
|
337
|
+
return true;
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
export const needLoginSession = async (req: ServerRequest, res: ServerResponse) => {
|
|
341
|
+
const cryptoKey = process.env['CRYPTO_KEY'];
|
|
342
|
+
if (!cryptoKey) {
|
|
343
|
+
const response = {
|
|
344
|
+
status: 'error',
|
|
345
|
+
message: langHelper.getLang('shared:name_not_set', {
|
|
346
|
+
name: 'ENV CRYPTO_KEY',
|
|
347
|
+
}),
|
|
348
|
+
};
|
|
349
|
+
ApiHelper.sendJson(req, res, response);
|
|
350
|
+
return true;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const json = await getUserFromCookie(req, res, false);
|
|
354
|
+
if (json && json.u) {
|
|
355
|
+
// the session has logged in info, so continue to process the request
|
|
356
|
+
return false;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const response = {
|
|
360
|
+
status: 'error',
|
|
361
|
+
message: langHelper.getLang('shared:not_logged_in'),
|
|
362
|
+
};
|
|
363
|
+
ApiHelper.sendJson(req, res, response);
|
|
364
|
+
return true;
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
export const userLogin = async (req: ServerRequest, res: ServerResponse) => {
|
|
368
|
+
const db = apiCache.getDb();
|
|
369
|
+
const cryptoKey = process.env['CRYPTO_KEY'];
|
|
370
|
+
if (!cryptoKey) {
|
|
371
|
+
const response = {
|
|
372
|
+
status: 'error',
|
|
373
|
+
message: langHelper.getLang('shared:name_not_set', {
|
|
374
|
+
name: 'ENV CRYPTO_KEY',
|
|
375
|
+
}),
|
|
376
|
+
};
|
|
377
|
+
ApiHelper.sendJson(req, res, response);
|
|
378
|
+
return true;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// when login without username and password, use cookie info
|
|
382
|
+
const data = req.locals.json();
|
|
383
|
+
if (!data || Array.isArray(data) || !data.u || (!data.p && !data.c)) {
|
|
384
|
+
const cookies = req.locals.cookies();
|
|
385
|
+
const token = cookies.get('_token', '');
|
|
386
|
+
if (token) {
|
|
387
|
+
const loginJson = await getUserFromCookie(req, res, false);
|
|
388
|
+
if (loginJson) {
|
|
389
|
+
const response = {
|
|
390
|
+
status: 'ok',
|
|
391
|
+
message: langHelper.getLang('shared:login_success'),
|
|
392
|
+
result: tokenForApps(req, token),
|
|
393
|
+
user: {
|
|
394
|
+
nickname: loginJson.u,
|
|
395
|
+
admin: loginJson.t === 'admin' ? '1' : '0',
|
|
396
|
+
u: loginJson.u,
|
|
397
|
+
t: loginJson.t,
|
|
398
|
+
},
|
|
399
|
+
};
|
|
400
|
+
ApiHelper.sendJson(req, res, response);
|
|
401
|
+
return true;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
const response = {
|
|
405
|
+
status: 'error',
|
|
406
|
+
message: langHelper.getLang('shared:not_logged_in'),
|
|
407
|
+
};
|
|
408
|
+
ApiHelper.sendJson(req, res, response);
|
|
409
|
+
return true;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
const adminUser = process.env['ADMIN_USER'] || '';
|
|
413
|
+
const adminPass = process.env['ADMIN_PASS'] || '';
|
|
414
|
+
const adminPwCheck =
|
|
415
|
+
typeof data.u === 'string' &&
|
|
416
|
+
typeof data.p === 'string' &&
|
|
417
|
+
data.p.length <= 256 &&
|
|
418
|
+
adminUser &&
|
|
419
|
+
adminPass
|
|
420
|
+
? CryptoUtils.verifyPasswordLayered(data.p, adminPass)
|
|
421
|
+
: { valid: false as const };
|
|
422
|
+
if (adminUser && adminApiHelper.timingSafeEqual(String(data.u || ''), adminUser) && adminPwCheck.valid && adminPwCheck.keyHex) {
|
|
423
|
+
const loginJson: LoginJsonProps = {
|
|
424
|
+
ip: req.socket.remoteAddress as string,
|
|
425
|
+
id: 0,
|
|
426
|
+
u: data.u as string,
|
|
427
|
+
t: 'admin',
|
|
428
|
+
h: adminPwCheck.keyHex,
|
|
429
|
+
l: Date.now(),
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
const token = JSON.stringify(loginJson);
|
|
433
|
+
const tokenCookie = CryptoUtils.encrypt(token, cryptoKey);
|
|
434
|
+
const response = {
|
|
435
|
+
status: 'ok',
|
|
436
|
+
message: langHelper.getLang('shared:login_success'),
|
|
437
|
+
result: tokenForApps(req, tokenCookie),
|
|
438
|
+
user: {
|
|
439
|
+
nickname: loginJson.u,
|
|
440
|
+
admin: loginJson.t === 'admin' ? '1' : '0',
|
|
441
|
+
u: loginJson.u,
|
|
442
|
+
t: loginJson.t,
|
|
443
|
+
},
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
// sameSite: 'none' needs secure=true
|
|
447
|
+
req.locals.setCookie('_token', tokenCookie, {
|
|
448
|
+
expireDays: getSessionExpireDays(ADMIN_SESSION_EXPIRE_DAYS_KEY_NAME),
|
|
449
|
+
path: '/',
|
|
450
|
+
httpOnly: true,
|
|
451
|
+
secure: isHttpsRequest(req),
|
|
452
|
+
sameSite: 'lax',
|
|
453
|
+
});
|
|
454
|
+
ApiHelper.sendJson(req, res, response);
|
|
455
|
+
return true;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
const result = await db.selectObject(
|
|
459
|
+
'$__s_user',
|
|
460
|
+
['id', 'username', 'password', 'level', 'usertype', 'pwretry', 'activation', 'activated', 'lastvisitdate', 'block'],
|
|
461
|
+
{ username: data.u as string }
|
|
462
|
+
);
|
|
463
|
+
if (result && result.length === 1 && result[0].username === data.u) {
|
|
464
|
+
if (result[0].block === '1') {
|
|
465
|
+
const response = {
|
|
466
|
+
status: 'error',
|
|
467
|
+
action: 'block',
|
|
468
|
+
message: langHelper.getLang('shared:user_locked'),
|
|
469
|
+
};
|
|
470
|
+
ApiHelper.sendJson(req, res, response);
|
|
471
|
+
return true;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
const checkRetryLockout = async (): Promise<boolean> => {
|
|
475
|
+
if (result[0].pwretry > PW_RETRY_MAX) {
|
|
476
|
+
const lastvisitdate = DateUtils.fromJSONString(result[0].lastvisitdate);
|
|
477
|
+
if (lastvisitdate && Date.now() - lastvisitdate.getTime() < 1000 * 60 * PW_RETRY_RESET_MINUTES) {
|
|
478
|
+
const response = {
|
|
479
|
+
status: 'error',
|
|
480
|
+
action: 'pwretry',
|
|
481
|
+
message: langHelper.getLang('shared:login_failed_too_many_times', {
|
|
482
|
+
minutes: PW_RETRY_RESET_MINUTES,
|
|
483
|
+
}),
|
|
484
|
+
};
|
|
485
|
+
ApiHelper.sendJson(req, res, response);
|
|
486
|
+
return true;
|
|
487
|
+
}
|
|
488
|
+
// reset pwretry and lastvisitdate time
|
|
489
|
+
result[0].pwretry = 0;
|
|
490
|
+
await db.updateObject(
|
|
491
|
+
'$__s_user',
|
|
492
|
+
{ pwretry: 0, lastvisitdate: DateUtils.toJSONString(new Date()) },
|
|
493
|
+
{ username: data.u as string }
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
return false;
|
|
497
|
+
};
|
|
498
|
+
|
|
499
|
+
const recordUserRetry = async () => {
|
|
500
|
+
await db.updateObject(
|
|
501
|
+
'$__s_user',
|
|
502
|
+
{
|
|
503
|
+
pwretry: (Number(result[0].pwretry) || 0) + 1,
|
|
504
|
+
lastvisitdate: DateUtils.toJSONString(new Date()),
|
|
505
|
+
},
|
|
506
|
+
{ username: data.u as string }
|
|
507
|
+
);
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
let loginOk = false;
|
|
511
|
+
if (result[0].activated !== '1') {
|
|
512
|
+
if (!data.c) {
|
|
513
|
+
const response = {
|
|
514
|
+
status: 'error',
|
|
515
|
+
action: 'activate',
|
|
516
|
+
message: langHelper.getLang('shared:user_not_activated'),
|
|
517
|
+
};
|
|
518
|
+
ApiHelper.sendJson(req, res, response);
|
|
519
|
+
return true;
|
|
520
|
+
}
|
|
521
|
+
if (await checkRetryLockout()) {
|
|
522
|
+
return true;
|
|
523
|
+
}
|
|
524
|
+
const providedCode = String(data.c || '');
|
|
525
|
+
const storedCode = String(result[0].activation || '');
|
|
526
|
+
if (storedCode.length < 6 || !CryptoUtils.timingSafeEqual(storedCode, providedCode)) {
|
|
527
|
+
await recordUserRetry();
|
|
528
|
+
const response = {
|
|
529
|
+
status: 'error',
|
|
530
|
+
message: langHelper.getLang('shared:wrong_activation'),
|
|
531
|
+
};
|
|
532
|
+
ApiHelper.sendJson(req, res, response);
|
|
533
|
+
return true;
|
|
534
|
+
}
|
|
535
|
+
loginOk = true;
|
|
536
|
+
} else if (data.c) {
|
|
537
|
+
if (await checkRetryLockout()) {
|
|
538
|
+
return true;
|
|
539
|
+
}
|
|
540
|
+
const providedCode = String(data.c || '');
|
|
541
|
+
const storedCode = String(result[0].activation || '');
|
|
542
|
+
const codeIssuedAt = DateUtils.fromJSONString(result[0].lastvisitdate);
|
|
543
|
+
const isCodeExpired = !codeIssuedAt || Date.now() - codeIssuedAt.getTime() > 30 * 60 * 1000;
|
|
544
|
+
if (storedCode.length < 6 || isCodeExpired || !CryptoUtils.timingSafeEqual(storedCode, providedCode)) {
|
|
545
|
+
await recordUserRetry();
|
|
546
|
+
const response = {
|
|
547
|
+
status: 'error',
|
|
548
|
+
message: langHelper.getLang('shared:wrong_activation'),
|
|
549
|
+
};
|
|
550
|
+
ApiHelper.sendJson(req, res, response);
|
|
551
|
+
return true;
|
|
552
|
+
}
|
|
553
|
+
loginOk = true;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
// if retries exceed, don't allow login
|
|
557
|
+
if (!loginOk && (await checkRetryLockout())) {
|
|
558
|
+
return true;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
if (!loginOk && !verifyUserPassword(String(data.p || ''), String(result[0].password || ''))) {
|
|
562
|
+
await recordUserRetry();
|
|
563
|
+
const response = {
|
|
564
|
+
status: 'error',
|
|
565
|
+
message: langHelper.getLang('shared:login_failed'),
|
|
566
|
+
};
|
|
567
|
+
ApiHelper.sendJson(req, res, response);
|
|
568
|
+
return true;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
let activePasswordHash = String(result[0].password || '');
|
|
572
|
+
const updateLoginObj: any = {
|
|
573
|
+
pwretry: 0,
|
|
574
|
+
activation: '',
|
|
575
|
+
activated: '1',
|
|
576
|
+
lastvisitdate: DateUtils.toJSONString(new Date()),
|
|
577
|
+
};
|
|
578
|
+
if (!loginOk && typeof data.p === 'string' && activePasswordHash && !activePasswordHash.startsWith('scrypt$')) {
|
|
579
|
+
activePasswordHash = CryptoUtils.hashPassword(data.p);
|
|
580
|
+
updateLoginObj.password = activePasswordHash;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
await db.updateObject('$__s_user', updateLoginObj, { username: data.u as string });
|
|
584
|
+
const nameAndPassHash = CryptoUtils.sha256(`${result[0].username}:${activePasswordHash}`);
|
|
585
|
+
const loginJson: LoginJsonProps = {
|
|
586
|
+
ip: req.socket.remoteAddress as string,
|
|
587
|
+
id: result[0].id,
|
|
588
|
+
u: result[0].username,
|
|
589
|
+
t: result[0].usertype,
|
|
590
|
+
h: nameAndPassHash,
|
|
591
|
+
};
|
|
592
|
+
|
|
593
|
+
const token = JSON.stringify(loginJson);
|
|
594
|
+
const tokenCookie = CryptoUtils.encrypt(token, cryptoKey);
|
|
595
|
+
const response = {
|
|
596
|
+
status: 'ok',
|
|
597
|
+
message: langHelper.getLang('shared:login_success'),
|
|
598
|
+
result: tokenForApps(req, tokenCookie),
|
|
599
|
+
user: {
|
|
600
|
+
nickname: loginJson.u,
|
|
601
|
+
admin: loginJson.t === 'admin' ? '1' : '0',
|
|
602
|
+
u: loginJson.u,
|
|
603
|
+
t: loginJson.t,
|
|
604
|
+
},
|
|
605
|
+
};
|
|
606
|
+
req.locals.setCookie('_token', tokenCookie, {
|
|
607
|
+
expireDays: getSessionExpireDays(ADMIN_SESSION_EXPIRE_DAYS_KEY_NAME, 90),
|
|
608
|
+
path: '/',
|
|
609
|
+
httpOnly: true,
|
|
610
|
+
secure: isHttpsRequest(req),
|
|
611
|
+
sameSite: 'lax',
|
|
612
|
+
});
|
|
613
|
+
ApiHelper.sendJson(req, res, response);
|
|
614
|
+
return true;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
const response = {
|
|
618
|
+
status: 'error',
|
|
619
|
+
message: langHelper.getLang('shared:login_failed'),
|
|
620
|
+
};
|
|
621
|
+
ApiHelper.sendJson(req, res, response);
|
|
622
|
+
return true;
|
|
623
|
+
};
|
|
624
|
+
|
|
625
|
+
export const userLogout = async (req: ServerRequest, res: ServerResponse) => {
|
|
626
|
+
req.locals.setCookie('_token', '', {
|
|
627
|
+
expireDays: -1,
|
|
628
|
+
path: '/',
|
|
629
|
+
httpOnly: true,
|
|
630
|
+
secure: isHttpsRequest(req),
|
|
631
|
+
sameSite: 'lax',
|
|
632
|
+
});
|
|
633
|
+
ApiHelper.sendJson(req, res, { status: 'ok', message: 'Logged out.' });
|
|
634
|
+
return true;
|
|
635
|
+
};
|
|
636
|
+
|
|
637
|
+
export const userReg = async (req: ServerRequest, res: ServerResponse) => {
|
|
638
|
+
const db = apiCache.getDb();
|
|
639
|
+
const cryptoKey = process.env['CRYPTO_KEY'];
|
|
640
|
+
if (!cryptoKey) {
|
|
641
|
+
const response = {
|
|
642
|
+
status: 'error',
|
|
643
|
+
message: langHelper.getLang('shared:name_not_set', {
|
|
644
|
+
name: 'ENV CRYPTO_KEY',
|
|
645
|
+
}),
|
|
646
|
+
};
|
|
647
|
+
ApiHelper.sendJson(req, res, response);
|
|
648
|
+
return true;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
const data = req.locals.json();
|
|
652
|
+
if (!data || Array.isArray(data) || !data.u || !data.p || !data.preferredName || !data.sex) {
|
|
653
|
+
const response = {
|
|
654
|
+
status: 'error',
|
|
655
|
+
message: langHelper.getLang('shared:wrong_data'),
|
|
656
|
+
};
|
|
657
|
+
ApiHelper.sendJson(req, res, response);
|
|
658
|
+
return true;
|
|
659
|
+
}
|
|
660
|
+
if (req.locals.body && req.locals.body.length > 1024) {
|
|
661
|
+
const response = {
|
|
662
|
+
status: 'error',
|
|
663
|
+
message: langHelper.getLang('shared:wrong_data'),
|
|
664
|
+
};
|
|
665
|
+
ApiHelper.sendJson(req, res, response);
|
|
666
|
+
return true;
|
|
667
|
+
}
|
|
668
|
+
const re =
|
|
669
|
+
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
|
670
|
+
if (!re.test(data.u as string)) {
|
|
671
|
+
const response = {
|
|
672
|
+
status: 'error',
|
|
673
|
+
message: langHelper.getLang('shared:wrong_email_format'),
|
|
674
|
+
};
|
|
675
|
+
ApiHelper.sendJson(req, res, response);
|
|
676
|
+
return true;
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
const result = await db.selectObject('$__s_user', ['username'], {
|
|
680
|
+
username: data.u as string,
|
|
681
|
+
});
|
|
682
|
+
if (result && result.length > 0) {
|
|
683
|
+
const response = {
|
|
684
|
+
status: 'error',
|
|
685
|
+
message: langHelper.getLang('shared:register_username_exists'),
|
|
686
|
+
};
|
|
687
|
+
ApiHelper.sendJson(req, res, response);
|
|
688
|
+
return true;
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
if (typeof data.p !== 'string' || data.p.length < 6 || data.p.length > 256) {
|
|
692
|
+
ApiHelper.sendJson(req, res, {
|
|
693
|
+
status: 'error',
|
|
694
|
+
message: langHelper.getLang('shared:password_length_wrong', { len: 6 }),
|
|
695
|
+
});
|
|
696
|
+
return true;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
const lastvisitdate = DateUtils.toJSONString(new Date());
|
|
700
|
+
// const random = CryptoUtils.randomString(16);
|
|
701
|
+
const result2 = await db.insertObject('$__s_user', {
|
|
702
|
+
nickname: data.preferredName as string,
|
|
703
|
+
username: data.u as string,
|
|
704
|
+
email: data.u as string,
|
|
705
|
+
password: CryptoUtils.hashPassword(data.p),
|
|
706
|
+
gender: data.sex as string,
|
|
707
|
+
usertype: 'user',
|
|
708
|
+
registerdate: lastvisitdate,
|
|
709
|
+
lastvisitdate: lastvisitdate,
|
|
710
|
+
activated: '0', // '' or 0: not activated, 1: activated
|
|
711
|
+
activation: '',
|
|
712
|
+
});
|
|
713
|
+
if (!result2) {
|
|
714
|
+
const response = {
|
|
715
|
+
status: 'error',
|
|
716
|
+
message: langHelper.getLang('shared:register_failed'),
|
|
717
|
+
};
|
|
718
|
+
ApiHelper.sendJson(req, res, response);
|
|
719
|
+
return true;
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
// const currentSettings = AppConfig.get(AppConfig.WEB_SETTINGS_KEY);
|
|
723
|
+
// const siteUrl = currentSettings['siteUrl'] || 'https://your-site.com';
|
|
724
|
+
// const siteUrl = (await apiStorage.getWeb('siteUrl')) || 'https://your-site.com';
|
|
725
|
+
|
|
726
|
+
// const regEmailTitle = 'activate your account';
|
|
727
|
+
// const body = `Please click the link to activate your account: ${siteUrl}/api/user-activate?u=${data.u}&a=${random}`;
|
|
728
|
+
// sendEmail(data.u as string, regEmailTitle, body);
|
|
729
|
+
const response = {
|
|
730
|
+
status: 'ok',
|
|
731
|
+
message: langHelper.getLang('shared:register_success'),
|
|
732
|
+
};
|
|
733
|
+
ApiHelper.sendJson(req, res, response);
|
|
734
|
+
return true;
|
|
735
|
+
};
|
|
736
|
+
|
|
737
|
+
// // Response is HTML page
|
|
738
|
+
// export const userActivation = async (req: ServerRequest, res: ServerResponse) => {
|
|
739
|
+
// const userEmail = req.locals.query.get('u');
|
|
740
|
+
// const activation = req.locals.query.get('a');
|
|
741
|
+
// if (!userEmail || !activation || activation.length !== 32) {
|
|
742
|
+
// const response = 'Wrong data.';
|
|
743
|
+
// ApiHelper.sendHtml(req, res, response);
|
|
744
|
+
// return true;
|
|
745
|
+
// }
|
|
746
|
+
|
|
747
|
+
// const db = apiCache.getDb();
|
|
748
|
+
// const result = await db.selectObject('$__s_user', ['id', 'activation'], {
|
|
749
|
+
// username: userEmail,
|
|
750
|
+
// });
|
|
751
|
+
// if (!result || result.length !== 1) {
|
|
752
|
+
// const response = '<meta charset="utf-8" />Activation failed. User does not exist, please contact admin.';
|
|
753
|
+
// ApiHelper.sendHtml(req, res, response);
|
|
754
|
+
// return true;
|
|
755
|
+
// }
|
|
756
|
+
|
|
757
|
+
// if (result[0].activation !== activation) {
|
|
758
|
+
// const response = '<meta charset="utf-8" />Activation failed. Activation code is incorrect, please contact admin.';
|
|
759
|
+
// ApiHelper.sendHtml(req, res, response);
|
|
760
|
+
// return true;
|
|
761
|
+
// }
|
|
762
|
+
|
|
763
|
+
// await db.updateObject('$__s_user', { activation: '' }, { username: userEmail });
|
|
764
|
+
// const response = '<meta charset="utf-8" />Activation successful, please <a href="/login">login</a>.';
|
|
765
|
+
// ApiHelper.sendHtml(req, res, response);
|
|
766
|
+
// return true;
|
|
767
|
+
// };
|
|
768
|
+
|
|
769
|
+
export const userResetCode = async (req: ServerRequest, res: ServerResponse) => {
|
|
770
|
+
const json = req.locals.json() as JsonKeyValue;
|
|
771
|
+
const userEmail = json.u as string;
|
|
772
|
+
if (!userEmail) {
|
|
773
|
+
const response = {
|
|
774
|
+
status: 'error',
|
|
775
|
+
message: langHelper.getLang('shared:wrong_data'),
|
|
776
|
+
};
|
|
777
|
+
ApiHelper.sendJson(req, res, response);
|
|
778
|
+
return true;
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
const db = apiCache.getDb();
|
|
782
|
+
const result = await db.selectObject('$__s_user', ['id', 'activation', 'lastvisitdate'], {
|
|
783
|
+
username: userEmail,
|
|
784
|
+
});
|
|
785
|
+
// the same answer whether the address is known or not: nobody learns who has an account here
|
|
786
|
+
const answer = () => ApiHelper.sendJson(req, res, { status: 'ok', message: langHelper.getLang('shared:send_and_copy_activation') });
|
|
787
|
+
if (!result || result.length !== 1) {
|
|
788
|
+
answer();
|
|
789
|
+
return true;
|
|
790
|
+
}
|
|
791
|
+
// one code a minute: the form can't be used to flood a mailbox
|
|
792
|
+
const issued = result[0].activation && DateUtils.fromJSONString(result[0].lastvisitdate);
|
|
793
|
+
if (issued && Date.now() - issued.getTime() < 60 * 1000) {
|
|
794
|
+
answer();
|
|
795
|
+
return true;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
const lastvisitdate = DateUtils.toJSONString(new Date());
|
|
799
|
+
const random = CryptoUtils.randomNumberString(8);
|
|
800
|
+
await db.updateObject('$__s_user', { activation: random, lastvisitdate: lastvisitdate }, { username: userEmail });
|
|
801
|
+
|
|
802
|
+
const siteUrl = (await apiStorage.getWeb('siteUrl')) || 'https://your-site.com';
|
|
803
|
+
const emailTitle = 'Authentication code';
|
|
804
|
+
const body = `This is the authentication code [${random}] for your account at ${siteUrl}. Please copy this Authentication code to login or reset your password.`;
|
|
805
|
+
sendEmail(userEmail, emailTitle, body);
|
|
806
|
+
const response = {
|
|
807
|
+
status: 'ok',
|
|
808
|
+
message: langHelper.getLang('shared:send_and_copy_activation'),
|
|
809
|
+
};
|
|
810
|
+
ApiHelper.sendJson(req, res, response);
|
|
811
|
+
return true;
|
|
812
|
+
};
|
|
813
|
+
|
|
814
|
+
export const userResetPw = async (req: ServerRequest, res: ServerResponse) => {
|
|
815
|
+
const json = req.locals.json() as JsonKeyValue;
|
|
816
|
+
const userEmail = json.u as string;
|
|
817
|
+
const pass = json.p as string;
|
|
818
|
+
const activation = json.a as string;
|
|
819
|
+
if (!userEmail || !activation || activation.length < 6 || !pass || pass.length < 6 || pass.length > 256) {
|
|
820
|
+
const response = {
|
|
821
|
+
status: 'error',
|
|
822
|
+
message: langHelper.getLang('shared:wrong_data'),
|
|
823
|
+
};
|
|
824
|
+
ApiHelper.sendJson(req, res, response);
|
|
825
|
+
return true;
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
const db = apiCache.getDb();
|
|
829
|
+
const result = await db.selectObject('$__s_user', ['id', 'activation', 'pwretry', 'lastvisitdate'], {
|
|
830
|
+
username: userEmail,
|
|
831
|
+
});
|
|
832
|
+
if (!result || result.length !== 1) {
|
|
833
|
+
const response = {
|
|
834
|
+
status: 'error',
|
|
835
|
+
message: langHelper.getLang('shared:user_not_found'),
|
|
836
|
+
};
|
|
837
|
+
ApiHelper.sendJson(req, res, response);
|
|
838
|
+
return true;
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
if (result[0].pwretry > PW_RETRY_MAX) {
|
|
842
|
+
const lastvisitdate = DateUtils.fromJSONString(result[0].lastvisitdate);
|
|
843
|
+
if (lastvisitdate && Date.now() - lastvisitdate.getTime() < 1000 * 60 * PW_RETRY_RESET_MINUTES) {
|
|
844
|
+
ApiHelper.sendJson(req, res, {
|
|
845
|
+
status: 'error',
|
|
846
|
+
action: 'pwretry',
|
|
847
|
+
message: langHelper.getLang('shared:login_failed_too_many_times', {
|
|
848
|
+
minutes: PW_RETRY_RESET_MINUTES,
|
|
849
|
+
}),
|
|
850
|
+
});
|
|
851
|
+
return true;
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
const storedCode = String(result[0].activation || '');
|
|
856
|
+
const codeIssuedAt = DateUtils.fromJSONString(result[0].lastvisitdate);
|
|
857
|
+
const isCodeExpired = !codeIssuedAt || Date.now() - codeIssuedAt.getTime() > 30 * 60 * 1000;
|
|
858
|
+
if (storedCode.length < 6 || isCodeExpired || !CryptoUtils.timingSafeEqual(storedCode, String(activation))) {
|
|
859
|
+
await db.updateObject(
|
|
860
|
+
'$__s_user',
|
|
861
|
+
{ pwretry: (Number(result[0].pwretry) || 0) + 1, lastvisitdate: DateUtils.toJSONString(new Date()) },
|
|
862
|
+
{ username: userEmail }
|
|
863
|
+
);
|
|
864
|
+
const response = {
|
|
865
|
+
status: 'error',
|
|
866
|
+
message: langHelper.getLang('shared:wrong_activation'),
|
|
867
|
+
};
|
|
868
|
+
ApiHelper.sendJson(req, res, response);
|
|
869
|
+
return true;
|
|
870
|
+
}
|
|
871
|
+
await db.updateObject(
|
|
872
|
+
'$__s_user',
|
|
873
|
+
{ activation: '', pwretry: 0, password: CryptoUtils.hashPassword(pass) },
|
|
874
|
+
{ username: userEmail }
|
|
875
|
+
);
|
|
876
|
+
const response = {
|
|
877
|
+
status: 'ok',
|
|
878
|
+
message: langHelper.getLang('shared:password_reset_success'),
|
|
879
|
+
};
|
|
880
|
+
ApiHelper.sendJson(req, res, response);
|
|
881
|
+
return true;
|
|
882
|
+
};
|
|
883
|
+
|
|
884
|
+
export const updateUser = async (req: ServerRequest, res: ServerResponse) => {
|
|
885
|
+
const loginJson = await getUserFromCookie(req, res, true);
|
|
886
|
+
if (!loginJson || loginJson.t !== 'admin') {
|
|
887
|
+
const response = {
|
|
888
|
+
status: 'error',
|
|
889
|
+
message: langHelper.getLang('shared:permission_denied'),
|
|
890
|
+
};
|
|
891
|
+
ApiHelper.sendJson(req, res, response);
|
|
892
|
+
return true;
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
const json = req.locals.json() as JsonKeyValue;
|
|
896
|
+
const id = json.id as number;
|
|
897
|
+
const usertype = json.usertype as string;
|
|
898
|
+
const password = json.password as string;
|
|
899
|
+
const db = apiCache.getDb();
|
|
900
|
+
if (!id) {
|
|
901
|
+
const response = {
|
|
902
|
+
status: 'error',
|
|
903
|
+
message: langHelper.getLang('shared:wrong_data'),
|
|
904
|
+
};
|
|
905
|
+
ApiHelper.sendJson(req, res, response);
|
|
906
|
+
return true;
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
const updateObj: any = { usertype: usertype };
|
|
910
|
+
if (password) {
|
|
911
|
+
updateObj['password'] = CryptoUtils.hashPassword(password);
|
|
912
|
+
}
|
|
913
|
+
// clear activation, only admin can update
|
|
914
|
+
updateObj['activation'] = '';
|
|
915
|
+
updateObj['activated'] = '1';
|
|
916
|
+
updateObj['pwretry'] = 0;
|
|
917
|
+
const result = await db.updateObject('$__s_user', updateObj, { id: id });
|
|
918
|
+
const response = {
|
|
919
|
+
status: 'ok',
|
|
920
|
+
results: result,
|
|
921
|
+
message: `Updated user.`,
|
|
922
|
+
};
|
|
923
|
+
ApiHelper.sendJson(req, res, response);
|
|
924
|
+
return true;
|
|
925
|
+
};
|
|
926
|
+
|
|
927
|
+
export const updateProfile = async (req: ServerRequest, res: ServerResponse) => {
|
|
928
|
+
const loginJson = await getUserFromCookie(req, res, true);
|
|
929
|
+
if (!loginJson) {
|
|
930
|
+
const response = {
|
|
931
|
+
status: 'error',
|
|
932
|
+
message: 'Permission denied.',
|
|
933
|
+
};
|
|
934
|
+
ApiHelper.sendJson(req, res, response);
|
|
935
|
+
return true;
|
|
936
|
+
}
|
|
937
|
+
// when special admin updates, nothing will be updated
|
|
938
|
+
if (loginJson.t === 'admin' && loginJson.id === 0) {
|
|
939
|
+
const response = {
|
|
940
|
+
status: 'error',
|
|
941
|
+
message: langHelper.getLang('shared:no_update_for_special_user'),
|
|
942
|
+
};
|
|
943
|
+
ApiHelper.sendJson(req, res, response);
|
|
944
|
+
return true;
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
const json = req.locals.json() as JsonKeyValue;
|
|
948
|
+
const nickname = json.nickname as string;
|
|
949
|
+
const password = json.password as string;
|
|
950
|
+
const db = apiCache.getDb();
|
|
951
|
+
if (!nickname) {
|
|
952
|
+
const response = {
|
|
953
|
+
status: 'error',
|
|
954
|
+
message: 'Wrong data.',
|
|
955
|
+
};
|
|
956
|
+
ApiHelper.sendJson(req, res, response);
|
|
957
|
+
return true;
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
const updateObj: any = { nickname };
|
|
961
|
+
if (password) {
|
|
962
|
+
if (password.length < 6 || password.length > 256) {
|
|
963
|
+
const response = {
|
|
964
|
+
status: 'error',
|
|
965
|
+
message: langHelper.getLang('shared:password_length_wrong', { len: 6 }),
|
|
966
|
+
};
|
|
967
|
+
ApiHelper.sendJson(req, res, response);
|
|
968
|
+
return true;
|
|
969
|
+
}
|
|
970
|
+
updateObj['password'] = CryptoUtils.hashPassword(password);
|
|
971
|
+
}
|
|
972
|
+
const result = await db.updateObject('$__s_user', updateObj, {
|
|
973
|
+
id: loginJson.id,
|
|
974
|
+
});
|
|
975
|
+
const response = {
|
|
976
|
+
status: 'ok',
|
|
977
|
+
results: result,
|
|
978
|
+
message: langHelper.getLang('shared:updated_profile'),
|
|
979
|
+
};
|
|
980
|
+
ApiHelper.sendJson(req, res, response);
|
|
981
|
+
return true;
|
|
982
|
+
};
|
|
983
|
+
|
|
984
|
+
export const blockUser = async (req: ServerRequest, res: ServerResponse) => {
|
|
985
|
+
const id = req.locals.query.get('id');
|
|
986
|
+
const block = req.locals.query.get('block');
|
|
987
|
+
const db = apiCache.getDb();
|
|
988
|
+
const loginJson = await getUserFromCookie(req, res, true);
|
|
989
|
+
if (!id || typeof block === 'undefined' || block === null || !loginJson || loginJson.t !== 'admin') {
|
|
990
|
+
const response = {
|
|
991
|
+
status: 'error',
|
|
992
|
+
message: langHelper.getLang('shared:permission_denied'),
|
|
993
|
+
};
|
|
994
|
+
ApiHelper.sendJson(req, res, response);
|
|
995
|
+
return true;
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
const result = await db.updateObject('$__s_user', { block: block }, { id: id });
|
|
999
|
+
const response = {
|
|
1000
|
+
status: 'ok',
|
|
1001
|
+
results: result,
|
|
1002
|
+
message: `User lock updated to [${block}].`,
|
|
1003
|
+
};
|
|
1004
|
+
ApiHelper.sendJson(req, res, response);
|
|
1005
|
+
return true;
|
|
1006
|
+
};
|
|
1007
|
+
|
|
1008
|
+
export const sendUserNote = async (req: ServerRequest, res: ServerResponse) => {
|
|
1009
|
+
const loginJson = await getUserFromCookie(req, res, true);
|
|
1010
|
+
if (!loginJson) {
|
|
1011
|
+
return true;
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
const ty = req.locals.query.get('ty');
|
|
1015
|
+
const db = apiCache.getDb();
|
|
1016
|
+
const user = await db.selectObject('$__s_user', undefined, {
|
|
1017
|
+
id: loginJson.id,
|
|
1018
|
+
});
|
|
1019
|
+
if (!user || user.length !== 1 || !ty) {
|
|
1020
|
+
const response = {
|
|
1021
|
+
status: 'error',
|
|
1022
|
+
message: langHelper.getLang('shared:user_not_found'),
|
|
1023
|
+
};
|
|
1024
|
+
ApiHelper.sendJson(req, res, response);
|
|
1025
|
+
return true;
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
if (ty === 'deposit') {
|
|
1029
|
+
await sendSiteEmail(`user: ${user[0].username} has done a Deposit`, `user: ${user[0].username} has done a Deposit`);
|
|
1030
|
+
}
|
|
1031
|
+
if (ty === 'withdraw') {
|
|
1032
|
+
const points = req.locals.query.get('points');
|
|
1033
|
+
const account = req.locals.query.get('account');
|
|
1034
|
+
await sendSiteEmail(
|
|
1035
|
+
`user: ${user[0].username} requires Withdraw`,
|
|
1036
|
+
`user: ${user[0].username} requires Withdraw points: ${points} to account: ${account}`
|
|
1037
|
+
);
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
const response = {
|
|
1041
|
+
status: 'ok',
|
|
1042
|
+
message: langHelper.getLang('shared:send_email_notification'),
|
|
1043
|
+
};
|
|
1044
|
+
ApiHelper.sendJson(req, res, response);
|
|
1045
|
+
return true;
|
|
1046
|
+
};
|