feat: TOTP script setup
continuous-integration/drone/pr Build is passing Details

This commit is contained in:
Dominik Pschenitschni 2022-05-21 00:31:56 +02:00
parent 2c270d063e
commit 4e4bb0603c
Signed by: dpschen
GPG Key ID: B257AC0149F43A77
1 changed files with 68 additions and 61 deletions

View File

@ -65,70 +65,77 @@
<script lang="ts"> <script lang="ts">
import {defineComponent} from 'vue' import {defineComponent} from 'vue'
export default defineComponent({ name: 'user-settings-totp' })
</script>
<script lang="ts" setup>
import {computed, ref, shallowReactive} from 'vue'
import {useStore} from 'vuex'
import {useI18n} from 'vue-i18n'
import TotpService from '@/services/totp' import TotpService from '@/services/totp'
import TotpModel from '@/models/totp' import TotpModel from '@/models/totp'
import {mapState} from 'vuex'
export default defineComponent({ import {success} from '@/message'
name: 'user-settings-totp',
data() { import { useTitle } from '@/composables/useTitle'
return {
totpService: new TotpService(), const {t} = useI18n()
totp: new TotpModel(), useTitle(() => `${t('user.settings.totp.title')} - ${t('user.settings.title')}`)
totpQR: '',
totpEnrolled: false,
totpConfirmPasscode: '', const totpService = shallowReactive(new TotpService())
totpDisableForm: false, const totp = ref(new TotpModel())
totpDisablePassword: '', const totpQR = ref('')
const totpEnrolled = ref(false)
const totpConfirmPasscode = ref('')
const totpDisableForm = ref(false)
const totpDisablePassword = ref('')
const store = useStore()
const totpEnabled = computed(() => store.state.config.totpEnabled)
totpStatus()
async function totpStatus() {
if (!totpEnabled.value) {
return
}
try {
totp.value = await totpService.get()
totpSetQrCode()
} catch(e) {
// Error code 1016 means totp is not enabled, we don't need an error in that case.
if (e.response?.data?.code === 1016) {
totpEnrolled.value = false
return
} }
},
created() {
this.totpStatus()
},
computed: mapState({
totpEnabled: state => state.config.totpEnabled,
}),
mounted() {
this.setTitle(`${this.$t('user.settings.totp.title')} - ${this.$t('user.settings.title')}`)
},
methods: {
async totpStatus() {
if (!this.totpEnabled) {
return
}
try {
this.totp = await this.totpService.get()
this.totpSetQrCode()
} catch(e) {
// Error code 1016 means totp is not enabled, we don't need an error in that case.
if (e.response && e.response.data && e.response.data.code && e.response.data.code === 1016) {
this.totpEnrolled = false
return
}
throw e throw e
} }
}, }
async totpSetQrCode() {
const qr = await this.totpService.qrcode() async function totpSetQrCode() {
this.totpQR = window.URL.createObjectURL(qr) const qr = await totpService.qrcode()
}, totpQR.value = window.URL.createObjectURL(qr)
async totpEnroll() { }
this.totp = await this.totpService.enroll()
this.totpEnrolled = true async function totpEnroll() {
this.totpSetQrCode() totp.value = await totpService.enroll()
}, totpEnrolled.value = true
async totpConfirm() { totpSetQrCode()
await this.totpService.enable({passcode: this.totpConfirmPasscode}) }
this.totp.enabled = true
this.$message.success({message: this.$t('user.settings.totp.confirmSuccess')}) async function totpConfirm() {
}, await totpService.enable({passcode: totpConfirmPasscode.value})
async totpDisable() { totp.value.enabled = true
await this.totpService.disable({password: this.totpDisablePassword}) success({message: t('user.settings.totp.confirmSuccess')})
this.totpEnrolled = false }
this.totp = new TotpModel()
this.$message.success({message: this.$t('user.settings.totp.disableSuccess')}) async function totpDisable() {
}, await totpService.disable({password: totpDisablePassword.value})
}, totpEnrolled.value = false
}) totp.value = new TotpModel()
success({message: t('user.settings.totp.disableSuccess')})
}
</script> </script>