adminforth 1.1.73 → 1.1.75

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/auth.ts +14 -0
  2. package/dataConnectors/baseConnector.ts +107 -0
  3. package/dataConnectors/mongo.ts +6 -12
  4. package/dataConnectors/postgres.ts +17 -37
  5. package/dataConnectors/sqlite.ts +44 -55
  6. package/dist/auth.js +7 -0
  7. package/dist/dataConnectors/baseConnector.js +90 -0
  8. package/dist/dataConnectors/mongo.js +6 -12
  9. package/dist/dataConnectors/postgres.js +12 -33
  10. package/dist/dataConnectors/sqlite.js +67 -83
  11. package/dist/index.js +125 -66
  12. package/dist/modules/codeInjector.js +29 -15
  13. package/dist/plugins/AuditLogPlugin/index.js +2 -17
  14. package/dist/plugins/S3UploadPlugin/custom/s3uploader.vue +120 -0
  15. package/dist/plugins/S3UploadPlugin/index.js +103 -0
  16. package/dist/plugins/S3UploadPlugin/package-lock.json +431 -0
  17. package/dist/plugins/S3UploadPlugin/package.json +15 -0
  18. package/dist/plugins/TwoFactorsAuthPlugin/custom/TwoFactorsConfirmation.vue +152 -0
  19. package/dist/plugins/TwoFactorsAuthPlugin/custom/TwoFactorsSetup.vue +188 -0
  20. package/dist/plugins/TwoFactorsAuthPlugin/index.js +150 -0
  21. package/dist/plugins/TwoFactorsAuthPlugin/types.js +1 -0
  22. package/dist/spa/index.html +1 -1
  23. package/dist/spa/package-lock.json +111 -0
  24. package/dist/spa/package.json +1 -0
  25. package/dist/spa/src/App.vue +178 -133
  26. package/dist/spa/src/components/MenuLink.vue +3 -3
  27. package/dist/spa/src/components/ResourceForm.vue +96 -96
  28. package/dist/spa/src/components/ResourceListTable.vue +69 -84
  29. package/dist/spa/src/components/ValueRenderer.vue +2 -3
  30. package/dist/spa/src/composables/useStores.ts +24 -11
  31. package/dist/spa/src/main.ts +1 -1
  32. package/dist/spa/src/router/index.ts +0 -1
  33. package/dist/spa/src/utils.ts +1 -1
  34. package/dist/spa/src/views/CreateView.vue +1 -9
  35. package/dist/spa/src/views/EditView.vue +1 -9
  36. package/dist/spa/src/views/ListView.vue +12 -3
  37. package/dist/spa/src/views/LoginView.vue +6 -1
  38. package/dist/spa/src/views/ResourceParent.vue +1 -2
  39. package/dist/spa/src/views/ShowView.vue +6 -14
  40. package/dist/spa/tailwind.config.js +3 -1
  41. package/index.ts +103 -36
  42. package/modules/codeInjector.ts +38 -27
  43. package/package.json +2 -1
  44. package/plugins/AuditLogPlugin/index.ts +4 -20
  45. package/plugins/S3UploadPlugin/custom/s3uploader.vue +120 -0
  46. package/plugins/S3UploadPlugin/index.ts +112 -0
  47. package/plugins/S3UploadPlugin/package-lock.json +431 -0
  48. package/plugins/S3UploadPlugin/package.json +15 -0
  49. package/plugins/S3UploadPlugin/types.ts +76 -0
  50. package/plugins/TwoFactorsAuthPlugin/custom/TwoFactorsConfirmation.vue +152 -0
  51. package/plugins/TwoFactorsAuthPlugin/custom/TwoFactorsOtp.vue +0 -0
  52. package/plugins/TwoFactorsAuthPlugin/custom/TwoFactorsSetup.vue +188 -0
  53. package/plugins/TwoFactorsAuthPlugin/index.ts +150 -0
  54. package/plugins/TwoFactorsAuthPlugin/types.ts +10 -0
  55. package/servers/express.ts +1 -0
  56. package/spa/src/App.vue +154 -121
  57. package/spa/src/components/ResourceForm.vue +91 -91
  58. package/spa/src/router/index.ts +0 -1
  59. package/spa/src/utils.ts +1 -1
  60. package/spa/src/views/CreateView.vue +0 -1
  61. package/spa/src/views/LoginView.vue +4 -1
  62. package/types/AdminForthConfig.ts +166 -5
  63. package/dist/plugins/AuditLog/index.js +0 -13
  64. package/dist/spa/spa/.eslintrc.cjs +0 -14
  65. package/dist/spa/spa/.vscode/extensions.json +0 -6
  66. package/dist/spa/spa/README.md +0 -39
  67. package/dist/spa/spa/env.d.ts +0 -1
  68. package/dist/spa/spa/index.html +0 -23
  69. package/dist/spa/spa/package-lock.json +0 -4470
  70. package/dist/spa/spa/package.json +0 -48
  71. package/dist/spa/spa/postcss.config.js +0 -6
  72. package/dist/spa/spa/public/assets/favicon.png +0 -0
  73. package/dist/spa/spa/src/App.vue +0 -283
  74. package/dist/spa/spa/src/assets/logo.svg +0 -19
  75. package/dist/spa/spa/src/components/AcceptModal.vue +0 -45
  76. package/dist/spa/spa/src/components/Breadcrumbs.vue +0 -40
  77. package/dist/spa/spa/src/components/BreadcrumbsWithButtons.vue +0 -26
  78. package/dist/spa/spa/src/components/CustomDatePicker.vue +0 -174
  79. package/dist/spa/spa/src/components/CustomDateRangePicker.vue +0 -218
  80. package/dist/spa/spa/src/components/CustomRangePicker.vue +0 -148
  81. package/dist/spa/spa/src/components/Dropdown.vue +0 -168
  82. package/dist/spa/spa/src/components/Filters.vue +0 -211
  83. package/dist/spa/spa/src/components/HelloWorld.vue +0 -17
  84. package/dist/spa/spa/src/components/MenuLink.vue +0 -24
  85. package/dist/spa/spa/src/components/ResourceForm.vue +0 -265
  86. package/dist/spa/spa/src/components/ResourceListTable.vue +0 -404
  87. package/dist/spa/spa/src/components/SingleSkeletLoader.vue +0 -13
  88. package/dist/spa/spa/src/components/Toast.vue +0 -66
  89. package/dist/spa/spa/src/components/ValueRenderer.vue +0 -59
  90. package/dist/spa/spa/src/components/icons/IconCalendar.vue +0 -5
  91. package/dist/spa/spa/src/components/icons/IconCommunity.vue +0 -7
  92. package/dist/spa/spa/src/components/icons/IconDocumentation.vue +0 -7
  93. package/dist/spa/spa/src/components/icons/IconEcosystem.vue +0 -7
  94. package/dist/spa/spa/src/components/icons/IconSupport.vue +0 -7
  95. package/dist/spa/spa/src/components/icons/IconTime.vue +0 -5
  96. package/dist/spa/spa/src/components/icons/IconTooling.vue +0 -19
  97. package/dist/spa/spa/src/composables/useFrontendApi.ts +0 -26
  98. package/dist/spa/spa/src/composables/useStores.ts +0 -126
  99. package/dist/spa/spa/src/index.scss +0 -26
  100. package/dist/spa/spa/src/main.ts +0 -18
  101. package/dist/spa/spa/src/router/index.ts +0 -64
  102. package/dist/spa/spa/src/spa_types/core.ts +0 -51
  103. package/dist/spa/spa/src/stores/core.ts +0 -144
  104. package/dist/spa/spa/src/stores/filters.ts +0 -22
  105. package/dist/spa/spa/src/stores/modal.ts +0 -48
  106. package/dist/spa/spa/src/stores/toast.ts +0 -15
  107. package/dist/spa/spa/src/stores/user.ts +0 -54
  108. package/dist/spa/spa/src/utils.ts +0 -101
  109. package/dist/spa/spa/src/views/CreateView.vue +0 -156
  110. package/dist/spa/spa/src/views/EditView.vue +0 -157
  111. package/dist/spa/spa/src/views/ListView.vue +0 -266
  112. package/dist/spa/spa/src/views/LoginView.vue +0 -136
  113. package/dist/spa/spa/src/views/ResourceParent.vue +0 -17
  114. package/dist/spa/spa/src/views/ShowView.vue +0 -180
  115. package/dist/spa/spa/tailwind.config.js +0 -17
  116. package/dist/spa/spa/tsconfig.app.json +0 -14
  117. package/dist/spa/spa/tsconfig.json +0 -11
  118. package/dist/spa/spa/tsconfig.node.json +0 -19
  119. package/dist/spa/spa/vite.config.ts +0 -49
  120. /package/dist/plugins/{AuditLog → S3UploadPlugin}/types.js +0 -0
  121. /package/dist/{spa/spa/src/assets/base.css → plugins/TwoFactorsAuthPlugin/custom/TwoFactorsOtp.vue} +0 -0
  122. /package/dist/spa/{spa/src → src}/components/SkeleteLoader.vue +0 -0
@@ -0,0 +1,76 @@
1
+
2
+ export type PluginOptions = {
3
+
4
+ /**
5
+ * the name of the column where the path to the uploaded file is stored
6
+ */
7
+ pathColumnName: string;
8
+
9
+ /**
10
+ * the list of allowed file extensions
11
+ */
12
+ allowedFileExtensions?: string[]; // allowed file extensions
13
+
14
+ /**
15
+ * the maximum file size in bytes
16
+ */
17
+ maxFileSize?: number;
18
+
19
+ /**
20
+ * This plugin creates a virtual column on edit and create views
21
+ * where the user can upload a file. This is the label of that column
22
+ *
23
+ * Defaulted to 'Upload <Name of the column defined in {@link pathColumnName}>'
24
+ */
25
+ uploadColumnLabel: string,
26
+
27
+ /**
28
+ * S3 bucket name where we will upload the files, e.g. 'my-bucket'
29
+ */
30
+ s3Bucket: string,
31
+
32
+ /**
33
+ * S3 region, e.g. 'us-east-1'
34
+ */
35
+ s3Region: string,
36
+
37
+ /**
38
+ * S3 access key id
39
+ */
40
+ s3AccessKeyId: string,
41
+
42
+ /**
43
+ * S3 secret access key
44
+ */
45
+ s3SecretAccessKey: string,
46
+
47
+ /**
48
+ * ACL which will be set to uploaded file, e.g. 'public-read'
49
+ */
50
+ s3ACL: string,
51
+
52
+ /**
53
+ * The path where the file will be uploaded to the S3 bucket, same path will be stored in the database
54
+ * in the column specified in {@link pathColumnName}
55
+ *
56
+ * example:
57
+ *
58
+ * ```typescript
59
+ * s3Path: ({record, originalFilename}) => `/aparts/${record.id}/${originalFilename}`
60
+ * ```
61
+ *
62
+ */
63
+ s3Path: ({originalFilename, originalExtension, contentType}) => string,
64
+
65
+ /**
66
+ * Used to display preview (if it is image) in list and show views.
67
+ * Defaulted to the AWS S3 presigned URL
68
+ * Example:
69
+ *
70
+ * ```typescript
71
+ * previewUrl: ({record, path}) => `https://my-bucket.s3.amazonaws.com/${path}`,
72
+ * ```
73
+ *
74
+ */
75
+ previewUrl?: ({s3Path}) => string,
76
+ }
@@ -0,0 +1,152 @@
1
+ <template>
2
+ <div class="relative flex items-center justify-center min-h-screen bg-gray-100 dark:bg-gray-800"
3
+ :style="coreStore.config?.loginBackgroundImage ? {
4
+ 'background-image': 'url(' + loadFile(coreStore.config?.loginBackgroundImage) + ')',
5
+ 'background-size': 'cover',
6
+ 'background-position': 'center',
7
+ 'background-blend-mode': 'darken'
8
+ }: {}"
9
+ >
10
+
11
+ <div id="authentication-modal" tabindex="-1" class=" overflow-y-auto overflow-x-hidden z-50 min-w-[00px] justify-center items-center md:inset-0 h-[calc(100%-1rem)] max-h-full">
12
+ <div class="relative p-4 w-full max-w-md max-h-full">
13
+ <!-- Modal content -->
14
+ <div class="relative bg-white rounded-lg shadow dark:bg-gray-700 dark:shadow-black text-gray-500" >
15
+ <div class="p-8 w-full max-w-md max-h-full" >
16
+ <div class="m-3" >Please enter your authenticator code </div>
17
+
18
+ <form class="max-w-sm mx-auto mt-6 flex flex-col items-center ">
19
+ <div class="flex mb-2 space-x-2 rtl:space-x-reverse ">
20
+ <div>
21
+ <label for="code-1" class="sr-only">First code</label>
22
+ <input type="text" maxlength="1" data-focus-input-init data-focus-input-next="code-2" id="code-1" class="block w-9 h-9 py-3 text-sm font-extrabold text-center text-gray-900 bg-white border border-gray-300 rounded-lg focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" required />
23
+ </div>
24
+ <div>
25
+ <label for="code-2" class="sr-only">Second code</label>
26
+ <input type="text" maxlength="1" data-focus-input-init data-focus-input-prev="code-1" data-focus-input-next="code-3" id="code-2" class="block w-9 h-9 py-3 text-sm font-extrabold text-center text-gray-900 bg-white border border-gray-300 rounded-lg focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" required />
27
+ </div>
28
+ <div>
29
+ <label for="code-3" class="sr-only">Third code</label>
30
+ <input type="text" maxlength="1" data-focus-input-init data-focus-input-prev="code-2" data-focus-input-next="code-4" id="code-3" class="block w-9 h-9 py-3 text-sm font-extrabold text-center text-gray-900 bg-white border border-gray-300 rounded-lg focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" required />
31
+ </div>
32
+ <div>
33
+ <label for="code-4" class="sr-only">Fourth code</label>
34
+ <input type="text" maxlength="1" data-focus-input-init data-focus-input-prev="code-3" data-focus-input-next="code-5" id="code-4" class="block w-9 h-9 py-3 text-sm font-extrabold text-center text-gray-900 bg-white border border-gray-300 rounded-lg focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" required />
35
+ </div>
36
+ <div>
37
+ <label for="code-5" class="sr-only">Fifth code</label>
38
+ <input type="text" maxlength="1" data-focus-input-init data-focus-input-prev="code-4" data-focus-input-next="code-6" id="code-5" class="block w-9 h-9 py-3 text-sm font-extrabold text-center text-gray-900 bg-white border border-gray-300 rounded-lg focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" required />
39
+ </div>
40
+ <div>
41
+ <label for="code-6" class="sr-only">Sixth code</label>
42
+ <input type="text" maxlength="1" data-focus-input-init data-focus-input-prev="code-5" id="code-6" class="block w-9 h-9 py-3 text-sm font-extrabold text-center text-gray-900 bg-white border border-gray-300 rounded-lg focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" required />
43
+ </div>
44
+ </div>
45
+ <p id="helper-text-explanation" class="mt-2 text-sm text-gray-500 dark:text-gray-400"></p>
46
+ </form>
47
+ <button @click="()=>{router.push('./login')}" class="flex items-center justify-center gap-1 w-full text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"
48
+ >Cancel</button>
49
+ </div>
50
+ </div>
51
+ </div>
52
+ </div>
53
+
54
+ </div>
55
+
56
+
57
+ </template>
58
+
59
+
60
+ <script setup>
61
+
62
+ import { onMounted, ref, watchEffect,computed } from 'vue';
63
+ import { useCoreStore } from '@/stores/core';
64
+ import { useUserStore } from '@/stores/user';
65
+ import { IconEyeSolid, IconEyeSlashSolid } from '@iconify-prerendered/vue-flowbite';
66
+ import { callAdminForthApi, loadFile } from '@/utils';
67
+ import { useRouter } from 'vue-router';
68
+ import { initFlowbite } from 'flowbite'
69
+ import { showErrorTost } from '@/composables/useFrontendApi';
70
+
71
+
72
+
73
+
74
+ const router = useRouter();
75
+ const inProgress = ref(false);
76
+
77
+ const coreStore = useCoreStore();
78
+ const user = useUserStore();
79
+
80
+ // use this simple function to automatically focus on the next input
81
+
82
+
83
+
84
+ const showPw = ref(false);
85
+
86
+ const error = ref(null);
87
+ const totp = ref({});
88
+ const totpJWT = ref(null);
89
+
90
+
91
+ function parseJwt(token) {
92
+ // Split the token into its parts
93
+ const base64Url = token.split('.')[1];
94
+
95
+ // Base64-decode the payload
96
+ const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
97
+ const jsonPayload = decodeURIComponent(atob(base64).split('').map(function(c) {
98
+ return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
99
+ }).join(''));
100
+
101
+ // Parse the JSON payload
102
+ return JSON.parse(jsonPayload);
103
+ }
104
+
105
+ onMounted(async () => {
106
+ coreStore.getPublicConfig()
107
+
108
+ function focusNextInput(el, prevId, nextId) {
109
+ if (el.value.length === 0) {
110
+ if (prevId) {
111
+ document.getElementById(prevId).focus();
112
+ }
113
+ } else {
114
+ if (nextId) {
115
+ document.getElementById(nextId).focus();
116
+ } else {
117
+ sendCode()
118
+ }
119
+ }
120
+ }
121
+
122
+ document.querySelectorAll('[data-focus-input-init]').forEach(function(element) {
123
+ element.addEventListener('keyup', function() {
124
+ const prevId = this.getAttribute('data-focus-input-prev');
125
+ const nextId = this.getAttribute('data-focus-input-next');
126
+ focusNextInput(this, prevId, nextId);
127
+ });
128
+ });
129
+
130
+
131
+
132
+ });
133
+
134
+ async function sendCode () {
135
+ inProgress.value = true;
136
+ const code = document.getElementById('code-1').value + document.getElementById('code-2').value + document.getElementById('code-3').value + document.getElementById('code-4').value + document.getElementById('code-5').value + document.getElementById('code-6').value;
137
+ const resp = await callAdminForthApi({
138
+ method: 'POST',
139
+ path: '/plugin/twofa/confirmSetup',
140
+ body: {
141
+ code: code,
142
+ secret: null,
143
+ }
144
+ })
145
+ if (resp.allowedLogin){
146
+ router.push('/');
147
+ } else {
148
+ showErrorTost('Invalid code');
149
+ }
150
+ }
151
+ </script>
152
+
@@ -0,0 +1,188 @@
1
+ <template>
2
+ <div class="relative flex items-center justify-center min-h-screen bg-gray-100 dark:bg-gray-800"
3
+ :style="coreStore.config?.loginBackgroundImage ? {
4
+ 'background-image': 'url(' + loadFile(coreStore.config?.loginBackgroundImage) + ')',
5
+ 'background-size': 'cover',
6
+ 'background-position': 'center',
7
+ 'background-blend-mode': 'darken'
8
+ }: {}"
9
+ >
10
+
11
+ <div id="authentication-modal" tabindex="-1" class=" overflow-y-auto overflow-x-hidden z-50 min-w-[00px] justify-center items-center md:inset-0 h-[calc(100%-1rem)] max-h-full">
12
+ <div class="relative p-4 w-full max-w-md max-h-full">
13
+ <!-- Modal content -->
14
+ <div class="relative bg-white rounded-lg shadow dark:bg-gray-700 dark:shadow-black text-gray-500" >
15
+ <div class="p-8 w-full max-w-md max-h-full" >
16
+ <div class="m-3" >Scan this QR code with your authenticator app or open by <a class="text-blue-600" :href="totpUri">click</a></div>
17
+ <div class="flex justify-center m-3" >
18
+ <img :src="totpQrCode" alt="QR code" />
19
+ </div>
20
+ <div class="m-3 ">
21
+ <div class="m-1">Or copy this code to app manually:</div>
22
+ <div class="w-full max-w-[46rem]">
23
+ <div class="relative">
24
+ <label for="npm-install-copy-text" class="sr-only">Label</label>
25
+ <input id="npm-install-copy-text" type="text" class="col-span-10 bg-gray-50 border border-gray-300 text-gray-500 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full px-2.5 py-4 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-gray-400 dark:focus:ring-blue-500 dark:focus:border-blue-500" :value="totp.newSecret" disabled readonly>
26
+ <button data-copy-to-clipboard-target="npm-install-copy-text" class="absolute end-2.5 top-1/2 -translate-y-1/2 text-gray-900 dark:text-gray-400 hover:bg-gray-100 dark:bg-gray-800 dark:border-gray-600 dark:hover:bg-gray-700 rounded-lg py-2 px-2.5 inline-flex items-center justify-center bg-white border-gray-200 border">
27
+ <span id="default-message" class="inline-flex items-center">
28
+ <svg class="w-3 h-3 me-1.5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 18 20">
29
+ <path d="M16 1h-3.278A1.992 1.992 0 0 0 11 0H7a1.993 1.993 0 0 0-1.722 1H2a2 2 0 0 0-2 2v15a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V3a2 2 0 0 0-2-2Zm-3 14H5a1 1 0 0 1 0-2h8a1 1 0 0 1 0 2Zm0-4H5a1 1 0 0 1 0-2h8a1 1 0 1 1 0 2Zm0-5H5a1 1 0 0 1 0-2h2V2h4v2h2a1 1 0 1 1 0 2Z"/>
30
+ </svg>
31
+ <span class="text-xs font-semibold">Copy</span>
32
+ </span>
33
+ <span id="success-message" class="hidden inline-flex items-center">
34
+ <svg class="w-3 h-3 text-blue-700 dark:text-blue-500 me-1.5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 16 12">
35
+ <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M1 5.917 5.724 10.5 15 1.5"/>
36
+ </svg>
37
+ <span class="text-xs font-semibold text-blue-700 dark:text-blue-500">Copied</span>
38
+ </span>
39
+ </button>
40
+ </div>
41
+ </div>
42
+ </div>
43
+ <form class="max-w-sm mx-auto mt-6 flex flex-col items-center ">
44
+ <div class="flex mb-2 space-x-2 rtl:space-x-reverse ">
45
+ <div>
46
+ <label for="code-1" class="sr-only">First code</label>
47
+ <input type="text" maxlength="1" data-focus-input-init data-focus-input-next="code-2" id="code-1" class="block w-9 h-9 py-3 text-sm font-extrabold text-center text-gray-900 bg-white border border-gray-300 rounded-lg focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" required />
48
+ </div>
49
+ <div>
50
+ <label for="code-2" class="sr-only">Second code</label>
51
+ <input type="text" maxlength="1" data-focus-input-init data-focus-input-prev="code-1" data-focus-input-next="code-3" id="code-2" class="block w-9 h-9 py-3 text-sm font-extrabold text-center text-gray-900 bg-white border border-gray-300 rounded-lg focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" required />
52
+ </div>
53
+ <div>
54
+ <label for="code-3" class="sr-only">Third code</label>
55
+ <input type="text" maxlength="1" data-focus-input-init data-focus-input-prev="code-2" data-focus-input-next="code-4" id="code-3" class="block w-9 h-9 py-3 text-sm font-extrabold text-center text-gray-900 bg-white border border-gray-300 rounded-lg focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" required />
56
+ </div>
57
+ <div>
58
+ <label for="code-4" class="sr-only">Fourth code</label>
59
+ <input type="text" maxlength="1" data-focus-input-init data-focus-input-prev="code-3" data-focus-input-next="code-5" id="code-4" class="block w-9 h-9 py-3 text-sm font-extrabold text-center text-gray-900 bg-white border border-gray-300 rounded-lg focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" required />
60
+ </div>
61
+ <div>
62
+ <label for="code-5" class="sr-only">Fifth code</label>
63
+ <input type="text" maxlength="1" data-focus-input-init data-focus-input-prev="code-4" data-focus-input-next="code-6" id="code-5" class="block w-9 h-9 py-3 text-sm font-extrabold text-center text-gray-900 bg-white border border-gray-300 rounded-lg focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" required />
64
+ </div>
65
+ <div>
66
+ <label for="code-6" class="sr-only">Sixth code</label>
67
+ <input type="text" maxlength="1" data-focus-input-init data-focus-input-prev="code-5" id="code-6" class="block w-9 h-9 py-3 text-sm font-extrabold text-center text-gray-900 bg-white border border-gray-300 rounded-lg focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" required />
68
+ </div>
69
+ </div>
70
+ <p id="helper-text-explanation" class="mt-2 text-sm text-gray-500 dark:text-gray-400"></p>
71
+ </form>
72
+ <button @click="()=>{router.push('./login')}" class="flex items-center justify-center gap-1 w-full text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">Cancel</button>
73
+ </div>
74
+ </div>
75
+ </div>
76
+ </div>
77
+
78
+ </div>
79
+
80
+
81
+ </template>
82
+
83
+
84
+ <script setup>
85
+
86
+ import { onMounted, ref, watchEffect,computed } from 'vue';
87
+ import { useCoreStore } from '@/stores/core';
88
+ import { useUserStore } from '@/stores/user';
89
+ import { IconEyeSolid, IconEyeSlashSolid } from '@iconify-prerendered/vue-flowbite';
90
+ import { callAdminForthApi, loadFile } from '@/utils';
91
+ import { useRouter } from 'vue-router';
92
+ import { initFlowbite } from 'flowbite'
93
+ import { showErrorTost } from '@/composables/useFrontendApi';
94
+
95
+
96
+
97
+
98
+ const router = useRouter();
99
+ const inProgress = ref(false);
100
+
101
+ const coreStore = useCoreStore();
102
+ const user = useUserStore();
103
+
104
+ // use this simple function to automatically focus on the next input
105
+
106
+
107
+
108
+ const showPw = ref(false);
109
+
110
+ const error = ref(null);
111
+ const totp = ref({});
112
+ const totpJWT = ref(null);
113
+ const totpUri = computed(() => {
114
+ if (totp.value) {
115
+ return `otpauth://totp/${totp.value.issuer}:${totp.value.userName}?secret=${totp.value.newSecret}&issuer=${totp.value.issuer}`;
116
+ }});
117
+ const totpQrCode = computed(() => {
118
+ if (totpUri.value) {
119
+ return `https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=${encodeURIComponent(totpUri.value)}`;
120
+ }});
121
+
122
+ function parseJwt(token) {
123
+ // Split the token into its parts
124
+ const base64Url = token.split('.')[1];
125
+
126
+ // Base64-decode the payload
127
+ const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
128
+ const jsonPayload = decodeURIComponent(atob(base64).split('').map(function(c) {
129
+ return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
130
+ }).join(''));
131
+
132
+ // Parse the JSON payload
133
+ return JSON.parse(jsonPayload);
134
+ }
135
+
136
+ onMounted(async () => {
137
+ coreStore.getPublicConfig()
138
+ const resp = await callAdminForthApi({
139
+ path: '/plugin/twofa/initSetup',
140
+ method: 'POST',});
141
+ if (resp && resp.totpJWT){
142
+ totpJWT.value = resp.totpJWT;
143
+ }
144
+ function focusNextInput(el, prevId, nextId) {
145
+ if (el.value.length === 0) {
146
+ if (prevId) {
147
+ document.getElementById(prevId).focus();
148
+ }
149
+ } else {
150
+ if (nextId) {
151
+ document.getElementById(nextId).focus();
152
+ } else {
153
+ sendCode()
154
+ }
155
+ }
156
+ }
157
+
158
+ document.querySelectorAll('[data-focus-input-init]').forEach(function(element) {
159
+ element.addEventListener('keyup', function() {
160
+ const prevId = this.getAttribute('data-focus-input-prev');
161
+ const nextId = this.getAttribute('data-focus-input-next');
162
+ focusNextInput(this, prevId, nextId);
163
+ });
164
+ });
165
+
166
+ totp.value=parseJwt(totpJWT.value);
167
+
168
+
169
+ });
170
+
171
+ async function sendCode () {
172
+ inProgress.value = true;
173
+ const code = document.getElementById('code-1').value + document.getElementById('code-2').value + document.getElementById('code-3').value + document.getElementById('code-4').value + document.getElementById('code-5').value + document.getElementById('code-6').value;
174
+ const resp = await callAdminForthApi({
175
+ method: 'POST',
176
+ path: '/plugin/twofa/confirmSetup',
177
+ body: {
178
+ code: code,
179
+ secret: totp.value.newSecret,
180
+ }
181
+ })
182
+ if (resp.allowedLogin){
183
+ router.push('/');
184
+ } else {
185
+ showErrorTost('Invalid code');
186
+ }
187
+ }
188
+ </script>
@@ -0,0 +1,150 @@
1
+ import { AdminForthResource, AdminForthResourcePages, AdminForthClass, GenericHttpServer } from "../../types/AdminForthConfig.js";
2
+ import AdminForthPlugin from "../base.js";
3
+ import twofactor from 'node-2fa';
4
+ import AdminForthAuth from "../../auth.js";
5
+ import { PluginOptions } from "./types.js"
6
+
7
+
8
+ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
9
+ options: PluginOptions;
10
+ adminforth: AdminForthClass;
11
+ authResource: AdminForthResource;
12
+ connectors: any;
13
+ adminForthAuth: AdminForthAuth ;
14
+
15
+ constructor(options: PluginOptions) {
16
+ super(options, import.meta.url);
17
+ this.options = options;
18
+ }
19
+
20
+ modifyResourceConfig(adminforth: AdminForthClass, resourceConfig: AdminForthResource) {
21
+ super.modifyResourceConfig(adminforth, resourceConfig);
22
+ this.adminforth = adminforth;
23
+ this.adminForthAuth = new AdminForthAuth(adminforth)
24
+ const customPages = this.adminforth.config.customization.customPages
25
+ customPages.push({
26
+ path:'/confirm2fa',
27
+ component: { file: this.componentPath('TwoFactorsConfirmation.vue'), meta: { customLayout: true }}
28
+ })
29
+ customPages.push({
30
+ path:'/setup2fa',
31
+ component: { file: this.componentPath('TwoFactorsSetup.vue'), meta: { title: 'Setup 2FA', customLayout: true }}
32
+ })
33
+ this.activate( resourceConfig,adminforth )
34
+ }
35
+
36
+ activate ( resourceConfig: AdminForthResource, adminforth: AdminForthClass ){
37
+ if (!this.options.twoFaSecretFieldName){
38
+ throw new Error('twoFaSecretFieldName is required')
39
+ }
40
+ if (typeof this.options.twoFaSecretFieldName !=='string'){
41
+ throw new Error('twoFaSecretFieldName must be a string')
42
+ }
43
+ this.authResource = resourceConfig
44
+ if(!this.authResource.columns.some((col)=>col.name === this.options.twoFaSecretFieldName)){
45
+ throw new Error(`Column ${this.options.twoFaSecretFieldName} not found in ${this.authResource.label}`)
46
+ }
47
+ const beforeLoginConfirmation = this.adminforth.config.auth as any
48
+ if (!beforeLoginConfirmation.beforeLoginConfirmation){
49
+ beforeLoginConfirmation.beforeLoginConfirmation = []
50
+ }
51
+ beforeLoginConfirmation.beforeLoginConfirmation.push(async({adminUser})=>{
52
+ if(adminUser.isRoot){
53
+ return { body: {loginAllowed: true}, ok: true}
54
+ }
55
+ const secret = adminUser.dbUser[this.options.twoFaSecretFieldName]
56
+ const userName = adminUser.dbUser[adminforth.config.auth.usernameField]
57
+ const brandName = adminforth.config.customization.brandName
58
+ const authResource = adminforth.config.resources.find((res)=>res.resourceId === adminforth.config.auth.resourceId )
59
+ const authPk = authResource.columns.find((col)=>col.primaryKey).name
60
+ const userPk = adminUser.dbUser[authPk]
61
+ let newSecret = null
62
+ if (!secret){
63
+ const tempSecret = twofactor.generateSecret({name: brandName,account: userName})
64
+ newSecret = tempSecret.secret
65
+ } else {
66
+ return {
67
+ body:{
68
+ loginAllowed: false,
69
+ redirectTo: '/confirm2fa',
70
+ setCookie: [{
71
+ name: 'totpTemporaryJWT',
72
+ value: this.adminforth.auth.issueJWT({userName, issuer:brandName, pk:userPk },'tempTotp', ),
73
+ expiry: '1h',
74
+ httpOnly: true
75
+ },]
76
+ },
77
+ ok: true
78
+
79
+ }
80
+ }
81
+ const totpTemporaryJWT = this.adminforth.auth.issueJWT({userName, newSecret, issuer:brandName, pk:userPk },'tempTotp', )
82
+ return {
83
+ body:{
84
+ loginAllowed: false,
85
+ redirectTo: secret ? '/confirm2fa' : '/setup2fa',
86
+ setCookie: [{
87
+ name: 'totpTemporaryJWT',
88
+ value: totpTemporaryJWT,
89
+ expiry: '1h',
90
+ httpOnly: true
91
+ },]},
92
+ ok: true
93
+ }
94
+ })
95
+ }
96
+
97
+ setupEndpoints(server: GenericHttpServer): void {
98
+ server.endpoint({
99
+ method: 'POST',
100
+ path: `/plugin/twofa/initSetup`,
101
+ noAuth: true,
102
+ handler: async (server) => {
103
+ const toReturn = {totpJWT:null,status:'ok',}
104
+ const totpTemporaryJWT = server.cookies['adminforth_totpTemporaryJWT']
105
+ if (totpTemporaryJWT){
106
+ toReturn.totpJWT = totpTemporaryJWT
107
+ }
108
+ return toReturn
109
+ }
110
+ })
111
+ server.endpoint({
112
+ method: 'POST',
113
+ path: `/plugin/twofa/confirmSetup`,
114
+ noAuth: true,
115
+ handler: async ({ body, adminUser, response, cookies }) => {
116
+ const totpTemporaryJWT = cookies['adminforth_totpTemporaryJWT']
117
+ const decoded = await this.adminforth.auth.verify(totpTemporaryJWT, 'tempTotp');
118
+ if ( !decoded ) {
119
+ return {status:'error',message:'Invalid token'}
120
+ }
121
+ if (decoded.newSecret) {
122
+ const verified = twofactor.verifyToken(decoded.newSecret, body.code);
123
+ if (verified) {
124
+ this.connectors = this.adminforth.connectors
125
+ const connector = this.connectors[this.authResource.dataSource];
126
+ await connector.updateRecord({resource:this.authResource, recordId:decoded.pk, newValues:{[this.options.twoFaSecretFieldName]: decoded.newSecret}})
127
+ this.adminForthAuth.removeCustomCookie({response, name:'totpTemporaryJWT'})
128
+ this.adminForthAuth.setAuthCookie({response, username:decoded.userName, pk:decoded.pk})
129
+ return { status: 'ok',allowedLogin: true }
130
+ } else {
131
+ return {error: 'Wrong or expired OTP code'}
132
+ }
133
+ } else {
134
+ // user already has secret, get it
135
+ this.connectors = this.adminforth.connectors
136
+ const connector = this.connectors[this.authResource.dataSource];
137
+ const user = await connector.getRecordByPrimaryKey(this.authResource, decoded.pk)
138
+ const verified = twofactor.verifyToken(user[this.options.twoFaSecretFieldName], body.code);
139
+ if (verified) {
140
+ this.adminForthAuth.removeCustomCookie({response, name:'totpTemporaryJWT'})
141
+ this.adminForthAuth.setAuthCookie({response, username:decoded.userName, pk:decoded.pk})
142
+ return { status: 'ok',allowedLogin: true }
143
+ } else {
144
+ return {error: 'Wrong or expired OTP code'}
145
+ }
146
+ }
147
+ }
148
+ })
149
+ }
150
+ }
@@ -0,0 +1,10 @@
1
+ export type PluginOptions = {
2
+
3
+ /**
4
+ * Name of the field in the auth resource which will store 2FA secret.
5
+ *
6
+ * Resource mandatory should have one columns which defined {@link AdminForthResourceColumn} which
7
+ * name should be equal to the value .
8
+ */
9
+ twoFaSecretFieldName: string;
10
+ }
@@ -196,6 +196,7 @@ class ExpressServer implements ExpressHttpServer {
196
196
  setHeader(name, value) {
197
197
  this.headers.push([name, value]);
198
198
  },
199
+
199
200
  setStatus(code, message) {
200
201
  this.status = code;
201
202
  this.message = message;