refactor(otp): generate enroll QR code in front-end

This commit is contained in:
Hintay 2025-02-07 21:05:17 +09:00
parent fb532b6144
commit aedf631254
No known key found for this signature in database
GPG key ID: 120FC7FF121F2F2D
4 changed files with 36 additions and 55 deletions

View file

@ -3,9 +3,11 @@ package user
import ( import (
"bytes" "bytes"
"crypto/sha1" "crypto/sha1"
"encoding/base64"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"net/http"
"strings"
"github.com/0xJacky/Nginx-UI/api" "github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/internal/crypto" "github.com/0xJacky/Nginx-UI/internal/crypto"
"github.com/0xJacky/Nginx-UI/query" "github.com/0xJacky/Nginx-UI/query"
@ -14,9 +16,6 @@ import (
"github.com/pquerna/otp" "github.com/pquerna/otp"
"github.com/pquerna/otp/totp" "github.com/pquerna/otp/totp"
"github.com/uozi-tech/cosy" "github.com/uozi-tech/cosy"
"image/jpeg"
"net/http"
"strings"
) )
func GenerateTOTP(c *gin.Context) { func GenerateTOTP(c *gin.Context) {
@ -38,27 +37,9 @@ func GenerateTOTP(c *gin.Context) {
return return
} }
qrCode, err := otpKey.Image(512, 512)
if err != nil {
api.ErrHandler(c, err)
return
}
// Encode the image to a buffer
var buf []byte
buffer := bytes.NewBuffer(buf)
err = jpeg.Encode(buffer, qrCode, nil)
if err != nil {
fmt.Println("Error encoding image:", err)
return
}
// Convert the buffer to a base64 string
base64Str := "data:image/jpeg;base64," + base64.StdEncoding.EncodeToString(buffer.Bytes())
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"secret": otpKey.Secret(), "secret": otpKey.Secret(),
"qr_code": base64Str, "url": otpKey.URL(),
}) })
} }

1
app/components.d.ts vendored
View file

@ -49,6 +49,7 @@ declare module 'vue' {
APopconfirm: typeof import('ant-design-vue/es')['Popconfirm'] APopconfirm: typeof import('ant-design-vue/es')['Popconfirm']
APopover: typeof import('ant-design-vue/es')['Popover'] APopover: typeof import('ant-design-vue/es')['Popover']
AProgress: typeof import('ant-design-vue/es')['Progress'] AProgress: typeof import('ant-design-vue/es')['Progress']
AQrcode: typeof import('ant-design-vue/es')['QRCode']
ARadioButton: typeof import('ant-design-vue/es')['RadioButton'] ARadioButton: typeof import('ant-design-vue/es')['RadioButton']
ARadioGroup: typeof import('ant-design-vue/es')['RadioGroup'] ARadioGroup: typeof import('ant-design-vue/es')['RadioGroup']
AResult: typeof import('ant-design-vue/es')['Result'] AResult: typeof import('ant-design-vue/es')['Result']

View file

@ -2,7 +2,7 @@ import http from '@/lib/http'
export interface OTPGenerateSecretResponse { export interface OTPGenerateSecretResponse {
secret: string secret: string
qr_code: string url: string
} }
const otp = { const otp = {

View file

@ -10,7 +10,7 @@ import { message } from 'ant-design-vue'
const status = ref(false) const status = ref(false)
const enrolling = ref(false) const enrolling = ref(false)
const resetting = ref(false) const resetting = ref(false)
const qrCode = ref('') const generatedUrl = ref('')
const secret = ref('') const secret = ref('')
const passcode = ref('') const passcode = ref('')
const refOtp = useTemplateRef('refOtp') const refOtp = useTemplateRef('refOtp')
@ -25,7 +25,7 @@ function clickEnable2FA() {
function generateSecret() { function generateSecret() {
otp.generate_secret().then(r => { otp.generate_secret().then(r => {
secret.value = r.secret secret.value = r.secret
qrCode.value = r.qr_code generatedUrl.value = r.url
refOtp.value?.clearInput() refOtp.value?.clearInput()
}) })
} }
@ -70,9 +70,7 @@ function reset2FA() {
<p>{{ $gettext('TOTP is a two-factor authentication method that uses a time-based one-time password algorithm.') }}</p> <p>{{ $gettext('TOTP is a two-factor authentication method that uses a time-based one-time password algorithm.') }}</p>
<p>{{ $gettext('To enable it, you need to install the Google or Microsoft Authenticator app on your mobile phone.') }}</p> <p>{{ $gettext('To enable it, you need to install the Google or Microsoft Authenticator app on your mobile phone.') }}</p>
<p>{{ $gettext('Scan the QR code with your mobile phone to add the account to the app.') }}</p> <p>{{ $gettext('Scan the QR code with your mobile phone to add the account to the app.') }}</p>
<p v-if="!status"> <AAlert v-if="!status" type="warning" :message="$gettext('Current account is not enabled TOTP.')" show-icon />
{{ $gettext('Current account is not enabled TOTP.') }}
</p>
<div v-else> <div v-else>
<p><CheckCircleOutlined class="mr-2 text-green-600" />{{ $gettext('Current account is enabled TOTP.') }}</p> <p><CheckCircleOutlined class="mr-2 text-green-600" />{{ $gettext('Current account is enabled TOTP.') }}</p>
</div> </div>
@ -112,33 +110,34 @@ function reset2FA() {
</AButton> </AButton>
<template v-if="enrolling"> <template v-if="enrolling">
<div class="mt-4 mb-2"> <div class="flex flex-col items-center">
<img <div class="mt-4 mb-2">
v-if="qrCode" <AQrcode
class="w-64 h-64" v-if="generatedUrl"
:src="qrCode" :value="generatedUrl"
alt="qr code" :size="256"
> />
<div class="w-64 flex justify-center"> <div class="w-64 flex justify-center">
<UseClipboard v-slot="{ copy, copied }"> <UseClipboard v-slot="{ copy, copied }">
<a <a
class="mr-2" class="mr-2"
@click="() => copy(secret)" @click="() => copy(secret)"
> >
{{ copied ? $gettext('Secret has been copied') {{ copied ? $gettext('Secret has been copied')
: $gettext('Can\'t scan? Use text key binding') }} : $gettext('Can\'t scan? Use text key binding') }}
</a> </a>
</UseClipboard> </UseClipboard>
</div>
</div> </div>
</div>
<div> <div>
<p>{{ $gettext('Input the code from the app:') }}</p> <p>{{ $gettext('Input the code from the app:') }}</p>
<OTPInput <OTPInput
ref="refOtp" ref="refOtp"
v-model="passcode" v-model="passcode"
@on-complete="enroll" @on-complete="enroll"
/> />
</div>
</div> </div>
</template> </template>