@adminforth/two-factors-auth 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.woodpecker/buildRelease.sh +13 -0
- package/.woodpecker/buildSlackNotify.sh +44 -0
- package/.woodpecker/release.yml +43 -0
- package/CHANGELOG.md +5 -0
- package/LICENSE +21 -0
- package/build.log +4 -0
- package/custom/TwoFactorsConfirmation.vue +175 -0
- package/custom/TwoFactorsSetup.vue +207 -0
- package/custom/package-lock.json +301 -0
- package/custom/package.json +16 -0
- package/custom/tsconfig.json +19 -0
- package/dist/index.js +174 -0
- package/dist/types.js +1 -0
- package/index.ts +188 -0
- package/package.json +56 -0
- package/tsconfig.json +112 -0
- package/types.ts +34 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
|
|
2
|
+
#!/bin/bash
|
|
3
|
+
|
|
4
|
+
# write npm run output both to console and to build.log
|
|
5
|
+
npm run build 2>&1 | tee build.log
|
|
6
|
+
build_status=${PIPESTATUS[0]}
|
|
7
|
+
|
|
8
|
+
# if exist status from the npm run build is not 0
|
|
9
|
+
# then exit with the status code from the npm run build
|
|
10
|
+
if [ $build_status -ne 0 ]; then
|
|
11
|
+
echo "Build failed. Exiting with status code $build_status"
|
|
12
|
+
exit $build_status
|
|
13
|
+
fi
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
|
|
3
|
+
set -x
|
|
4
|
+
|
|
5
|
+
COMMIT_SHORT_SHA=$(echo $CI_COMMIT_SHA | cut -c1-8)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
if [ "$CI_STEP_STATUS" = "success" ]; then
|
|
9
|
+
MESSAGE="Did a build without issues on \`$CI_REPO_NAME/$CI_COMMIT_BRANCH\`. Commit: _${CI_COMMIT_MESSAGE}_ (<$CI_COMMIT_URL|$COMMIT_SHORT_SHA>)"
|
|
10
|
+
|
|
11
|
+
curl -s -X POST -H "Content-Type: application/json" -d '{
|
|
12
|
+
"username": "'"$CI_COMMIT_AUTHOR"'",
|
|
13
|
+
"icon_url": "'"$CI_COMMIT_AUTHOR_AVATAR"'",
|
|
14
|
+
"attachments": [
|
|
15
|
+
{
|
|
16
|
+
"mrkdwn_in": ["text", "pretext"],
|
|
17
|
+
"color": "#36a64f",
|
|
18
|
+
"text": "'"$MESSAGE"'"
|
|
19
|
+
}
|
|
20
|
+
]
|
|
21
|
+
}' "$DEVELOPERS_SLACK_WEBHOOK"
|
|
22
|
+
exit 0
|
|
23
|
+
fi
|
|
24
|
+
export BUILD_LOG=$(cat ./build.log)
|
|
25
|
+
|
|
26
|
+
BUILD_LOG=$(echo $BUILD_LOG | sed 's/"/\\"/g')
|
|
27
|
+
|
|
28
|
+
MESSAGE="Broke \`$CI_REPO_NAME/$CI_COMMIT_BRANCH\` with commit _${CI_COMMIT_MESSAGE}_ (<$CI_COMMIT_URL|$COMMIT_SHORT_SHA>)"
|
|
29
|
+
CODE_BLOCK="\`\`\`$BUILD_LOG\n\`\`\`"
|
|
30
|
+
|
|
31
|
+
echo "Sending slack message to developers $MESSAGE"
|
|
32
|
+
# Send the message
|
|
33
|
+
curl -sS -X POST -H "Content-Type: application/json" -d '{
|
|
34
|
+
"username": "'"$CI_COMMIT_AUTHOR"'",
|
|
35
|
+
"icon_url": "'"$CI_COMMIT_AUTHOR_AVATAR"'",
|
|
36
|
+
"attachments": [
|
|
37
|
+
{
|
|
38
|
+
"mrkdwn_in": ["text", "pretext"],
|
|
39
|
+
"color": "#8A1C12",
|
|
40
|
+
"text": "'"$CODE_BLOCK"'",
|
|
41
|
+
"pretext": "'"$MESSAGE"'"
|
|
42
|
+
}
|
|
43
|
+
]
|
|
44
|
+
}' "$DEVELOPERS_SLACK_WEBHOOK" 2>&1
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
clone:
|
|
2
|
+
git:
|
|
3
|
+
image: woodpeckerci/plugin-git
|
|
4
|
+
settings:
|
|
5
|
+
partial: false
|
|
6
|
+
depth: 5
|
|
7
|
+
|
|
8
|
+
steps:
|
|
9
|
+
init-secrets:
|
|
10
|
+
when:
|
|
11
|
+
- event: push
|
|
12
|
+
image: infisical/cli
|
|
13
|
+
environment:
|
|
14
|
+
INFISICAL_TOKEN:
|
|
15
|
+
from_secret: VAULT_TOKEN
|
|
16
|
+
commands:
|
|
17
|
+
- infisical export --domain https://vault.devforth.io/api --format=dotenv-export --env="prod" > /woodpecker/deploy.vault.env
|
|
18
|
+
secrets:
|
|
19
|
+
- VAULT_TOKEN
|
|
20
|
+
|
|
21
|
+
release:
|
|
22
|
+
image: node:20
|
|
23
|
+
when:
|
|
24
|
+
- event: push
|
|
25
|
+
volumes:
|
|
26
|
+
- /var/run/docker.sock:/var/run/docker.sock
|
|
27
|
+
commands:
|
|
28
|
+
- export $(cat /woodpecker/deploy.vault.env | xargs)
|
|
29
|
+
- npm clean-install
|
|
30
|
+
- /bin/bash ./.woodpecker/buildRelease.sh
|
|
31
|
+
- npm audit signatures
|
|
32
|
+
- npx semantic-release
|
|
33
|
+
|
|
34
|
+
slack-on-failure:
|
|
35
|
+
when:
|
|
36
|
+
- event: push
|
|
37
|
+
status: [failure, success]
|
|
38
|
+
- event: push
|
|
39
|
+
image: curlimages/curl
|
|
40
|
+
commands:
|
|
41
|
+
- export $(cat /woodpecker/deploy.vault.env | xargs)
|
|
42
|
+
- /bin/sh ./.woodpecker/buildSlackNotify.sh
|
|
43
|
+
|
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Devforth.io
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/build.log
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
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 && coreStore.config?.loginBackgroundPosition === 'over') ? {
|
|
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">{{$t('Please enter your authenticator code')}} </div>
|
|
17
|
+
<div class="my-4 flex justify-center items-center">
|
|
18
|
+
<v-otp-input
|
|
19
|
+
ref="code"
|
|
20
|
+
input-classes="bg-gray-50 text-center flex justify-center otp-input border leading-none border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-10 h-10 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
|
|
21
|
+
:conditionalClass="['one', 'two', 'three', 'four', 'five', 'six']"
|
|
22
|
+
inputType="number"
|
|
23
|
+
:num-inputs="6"
|
|
24
|
+
v-model:value="bindValue"
|
|
25
|
+
:should-auto-focus="true"
|
|
26
|
+
:should-focus-order="true"
|
|
27
|
+
@on-complete="handleOnComplete"
|
|
28
|
+
/>
|
|
29
|
+
</div>
|
|
30
|
+
<!-- <Vue2FACodeInput v-model="code"/> -->
|
|
31
|
+
<LinkButton to="/login" class="w-full">{{$t('Back to login')}}</LinkButton>
|
|
32
|
+
</div>
|
|
33
|
+
</div>
|
|
34
|
+
</div>
|
|
35
|
+
</div>
|
|
36
|
+
|
|
37
|
+
</div>
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
</template>
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
<script setup>
|
|
44
|
+
|
|
45
|
+
import { onMounted, onBeforeUnmount, ref, watchEffect,computed,watch } from 'vue';
|
|
46
|
+
import { useCoreStore } from '@/stores/core';
|
|
47
|
+
import { useUserStore } from '@/stores/user';
|
|
48
|
+
import { IconEyeSolid, IconEyeSlashSolid } from '@iconify-prerendered/vue-flowbite';
|
|
49
|
+
import { callAdminForthApi, loadFile } from '@/utils';
|
|
50
|
+
import { useRouter } from 'vue-router';
|
|
51
|
+
import { showErrorTost } from '@/composables/useFrontendApi';
|
|
52
|
+
import { LinkButton } from '@/afcl';
|
|
53
|
+
import Vue2FACodeInput from '@loltech/vue3-2fa-code-input';
|
|
54
|
+
import VOtpInput from "vue3-otp-input";
|
|
55
|
+
|
|
56
|
+
const code = ref(null);
|
|
57
|
+
|
|
58
|
+
const handleOnComplete = (value) => {
|
|
59
|
+
sendCode(value);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const fillInput = (value) => {
|
|
63
|
+
code.value?.fillInput(value);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const router = useRouter();
|
|
67
|
+
const inProgress = ref(false);
|
|
68
|
+
|
|
69
|
+
const coreStore = useCoreStore();
|
|
70
|
+
const user = useUserStore();
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
// use this simple function to automatically focus on the next input
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
const showPw = ref(false);
|
|
78
|
+
|
|
79
|
+
const error = ref(null);
|
|
80
|
+
const totp = ref({});
|
|
81
|
+
const totpJWT = ref(null);
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
function parseJwt(token) {
|
|
85
|
+
// Split the token into its parts
|
|
86
|
+
const base64Url = token.split('.')[1];
|
|
87
|
+
|
|
88
|
+
// Base64-decode the payload
|
|
89
|
+
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
|
|
90
|
+
const jsonPayload = decodeURIComponent(atob(base64).split('').map(function(c) {
|
|
91
|
+
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
|
|
92
|
+
}).join(''));
|
|
93
|
+
|
|
94
|
+
// Parse the JSON payload
|
|
95
|
+
return JSON.parse(jsonPayload);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
onMounted(async () => {
|
|
99
|
+
coreStore.getPublicConfig();
|
|
100
|
+
window.addEventListener('paste', handlePaste);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
onBeforeUnmount(() => {
|
|
104
|
+
window.removeEventListener('paste', handlePaste);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
async function sendCode (value) {
|
|
108
|
+
inProgress.value = true;
|
|
109
|
+
const resp = await callAdminForthApi({
|
|
110
|
+
method: 'POST',
|
|
111
|
+
path: '/plugin/twofa/confirmSetup',
|
|
112
|
+
body: {
|
|
113
|
+
code: value,
|
|
114
|
+
secret: null,
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
if (resp.allowedLogin){
|
|
118
|
+
await user.finishLogin();
|
|
119
|
+
} else {
|
|
120
|
+
showErrorTost('Invalid code');
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// watch(code, async (nv)=>{
|
|
125
|
+
// if (nv){
|
|
126
|
+
// sendCode();
|
|
127
|
+
// }
|
|
128
|
+
// })
|
|
129
|
+
|
|
130
|
+
function handlePaste(event) {
|
|
131
|
+
event.preventDefault();
|
|
132
|
+
if (event.target.classList.contains('otp-input')) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
const pastedText = event.clipboardData?.getData('text') || '';
|
|
136
|
+
if (pastedText.length === 6) {
|
|
137
|
+
code.value?.fillInput(pastedText);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
</script>
|
|
141
|
+
|
|
142
|
+
<style lang='scss'>
|
|
143
|
+
.vue3-2fa-code-input {
|
|
144
|
+
display: flex;
|
|
145
|
+
justify-content: center;
|
|
146
|
+
gap: 1rem;
|
|
147
|
+
margin-bottom: 1rem;
|
|
148
|
+
}
|
|
149
|
+
.otp-input {
|
|
150
|
+
margin: 0 5px;
|
|
151
|
+
}
|
|
152
|
+
.vue3-2fa-code-input-box {
|
|
153
|
+
&[type='text'] {
|
|
154
|
+
@apply w-10 h-10 text-center text-xl border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500;
|
|
155
|
+
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* This particular piece of code makes the last input have a gap in the middle.
|
|
162
|
+
*/
|
|
163
|
+
.spaced-code-input {
|
|
164
|
+
& .vue3-2fa-code-input-box {
|
|
165
|
+
&:nth-child(3) {
|
|
166
|
+
@apply mr-4;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
&:nth-child(4) {
|
|
170
|
+
@apply ml-4;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
</style>
|
|
175
|
+
|
|
@@ -0,0 +1,207 @@
|
|
|
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 && coreStore.config?.loginBackgroundPosition === 'over' ? {
|
|
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" >{{$t('Scan this QR code with your authenticator app or open by')}} <a class="text-blue-600" :href="totpUri">{{$t('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">{{$t('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">{{$t('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 pr-12" :value="totp.newSecret" readonly>
|
|
26
|
+
<button @click="onCopyClick" 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-4" 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>
|
|
32
|
+
</button>
|
|
33
|
+
</div>
|
|
34
|
+
</div>
|
|
35
|
+
</div>
|
|
36
|
+
<div class="my-4 flex justify-center items-center">
|
|
37
|
+
<v-otp-input
|
|
38
|
+
ref="code"
|
|
39
|
+
input-classes="bg-gray-50 text-center flex justify-center otp-input border leading-none border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-10 h-10 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
|
|
40
|
+
:conditionalClass="['one', 'two', 'three', 'four', 'five', 'six']"
|
|
41
|
+
inputType="number"
|
|
42
|
+
:num-inputs="6"
|
|
43
|
+
:should-auto-focus="true"
|
|
44
|
+
:should-focus-order="true"
|
|
45
|
+
@on-complete="handleOnComplete"
|
|
46
|
+
/>
|
|
47
|
+
</div>
|
|
48
|
+
<!-- <Vue2FACodeInput v-model="code" autofocus /> -->
|
|
49
|
+
<div class="flex flex-row gap-2.5">
|
|
50
|
+
<LinkButton to="/login" class="w-full">
|
|
51
|
+
{{$t('Back to login')}}
|
|
52
|
+
</LinkButton>
|
|
53
|
+
<Button v-if="skipAllowed" @click="handleSkip" class="w-full">
|
|
54
|
+
{{$t('Skip for now')}}
|
|
55
|
+
</Button>
|
|
56
|
+
</div>
|
|
57
|
+
</div>
|
|
58
|
+
</div>
|
|
59
|
+
</div>
|
|
60
|
+
</div>
|
|
61
|
+
</div>
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
</template>
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
<script setup lang="ts">
|
|
68
|
+
|
|
69
|
+
import { onMounted, onBeforeUnmount, ref, watchEffect,computed,watch } from 'vue';
|
|
70
|
+
import { useCoreStore } from '@/stores/core';
|
|
71
|
+
import { useUserStore } from '@/stores/user';
|
|
72
|
+
import { IconEyeSolid, IconEyeSlashSolid } from '@iconify-prerendered/vue-flowbite';
|
|
73
|
+
import { callAdminForthApi, loadFile } from '@/utils';
|
|
74
|
+
import { useRouter } from 'vue-router';
|
|
75
|
+
import { showErrorTost } from '@/composables/useFrontendApi';
|
|
76
|
+
import { Button, LinkButton } from '@/afcl';
|
|
77
|
+
import Vue2FACodeInput from '@loltech/vue3-2fa-code-input';
|
|
78
|
+
import VOtpInput from "vue3-otp-input";
|
|
79
|
+
import adminforth from '@/adminforth';
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
const code = ref(null);
|
|
83
|
+
const handleOnComplete = (value) => {
|
|
84
|
+
sendCode(value);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const router = useRouter();
|
|
88
|
+
const inProgress = ref(false);
|
|
89
|
+
|
|
90
|
+
const coreStore = useCoreStore();
|
|
91
|
+
const user = useUserStore();
|
|
92
|
+
|
|
93
|
+
const skipAllowed = ref(false);
|
|
94
|
+
|
|
95
|
+
// use this simple function to automatically focus on the next input
|
|
96
|
+
|
|
97
|
+
const showPw = ref(false);
|
|
98
|
+
|
|
99
|
+
const error = ref(null);
|
|
100
|
+
const totp = ref({});
|
|
101
|
+
const totpJWT = ref(null);
|
|
102
|
+
const totpUri = computed(() => {
|
|
103
|
+
if (totp.value) {
|
|
104
|
+
return `otpauth://totp/${totp.value.issuer}:${totp.value.userName}?secret=${totp.value.newSecret}&issuer=${totp.value.issuer}`;
|
|
105
|
+
}});
|
|
106
|
+
const totpQrCode = computed(() => {
|
|
107
|
+
if (totpUri.value) {
|
|
108
|
+
return `https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=${encodeURIComponent(totpUri.value)}`;
|
|
109
|
+
}});
|
|
110
|
+
|
|
111
|
+
function parseJwt(token) {
|
|
112
|
+
// Split the token into its parts
|
|
113
|
+
const base64Url = token.split('.')[1];
|
|
114
|
+
|
|
115
|
+
// Base64-decode the payload
|
|
116
|
+
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
|
|
117
|
+
const jsonPayload = decodeURIComponent(atob(base64).split('').map(function(c) {
|
|
118
|
+
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
|
|
119
|
+
}).join(''));
|
|
120
|
+
|
|
121
|
+
// Parse the JSON payload
|
|
122
|
+
return JSON.parse(jsonPayload);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function onCopyClick(){
|
|
126
|
+
navigator.clipboard.writeText(totp.value.newSecret);
|
|
127
|
+
adminforth.alert({message: 'Copied to clipboard', variant: 'success'})
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
onMounted(async () => {
|
|
131
|
+
coreStore.getPublicConfig()
|
|
132
|
+
const resp = await callAdminForthApi({
|
|
133
|
+
path: '/plugin/twofa/initSetup',
|
|
134
|
+
method: 'POST',});
|
|
135
|
+
if (resp && resp.totpJWT){
|
|
136
|
+
totpJWT.value = resp.totpJWT;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
totp.value=parseJwt(totpJWT.value);
|
|
140
|
+
|
|
141
|
+
const allowSkipResp = await callAdminForthApi({
|
|
142
|
+
path: '/plugin/twofa/skip-allow',
|
|
143
|
+
method: 'GET',
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
if (allowSkipResp?.skipAllowed){
|
|
147
|
+
skipAllowed.value = true;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
window.addEventListener('paste', handlePaste);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
onBeforeUnmount(() => {
|
|
154
|
+
window.removeEventListener('paste', handlePaste);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
async function sendCode (value) {
|
|
158
|
+
inProgress.value = true;
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
const resp = await callAdminForthApi({
|
|
162
|
+
method: 'POST',
|
|
163
|
+
path: '/plugin/twofa/confirmSetup',
|
|
164
|
+
body: {
|
|
165
|
+
code: value,
|
|
166
|
+
secret: totp.value.newSecret,
|
|
167
|
+
}
|
|
168
|
+
})
|
|
169
|
+
if (resp.allowedLogin){
|
|
170
|
+
await user.finishLogin()
|
|
171
|
+
} else {
|
|
172
|
+
showErrorTost('Invalid code');
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
function handlePaste(event) {
|
|
178
|
+
event.preventDefault();
|
|
179
|
+
if (event.target.classList.contains('otp-input')) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const pastedText = event.clipboardData?.getData('text') || '';
|
|
183
|
+
if (pastedText.length === 6) {
|
|
184
|
+
code.value?.fillInput(pastedText);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const handleSkip = async () => {
|
|
189
|
+
const resp = await callAdminForthApi({
|
|
190
|
+
method: 'POST',
|
|
191
|
+
path: '/plugin/twofa/confirmSetup',
|
|
192
|
+
body: {
|
|
193
|
+
skip: true,
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
if (resp.allowedLogin){
|
|
197
|
+
await user.finishLogin()
|
|
198
|
+
} else {
|
|
199
|
+
showErrorTost('Something went wrong');
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
</script>
|
|
203
|
+
<style>
|
|
204
|
+
.otp-input {
|
|
205
|
+
margin: 0 5px;
|
|
206
|
+
}
|
|
207
|
+
</style>
|