mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-12 19:05:55 +02:00
refactor(otp): generate enroll QR code in front-end
This commit is contained in:
parent
fb532b6144
commit
aedf631254
4 changed files with 36 additions and 55 deletions
|
@ -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
1
app/components.d.ts
vendored
|
@ -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']
|
||||||
|
|
|
@ -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 = {
|
||||||
|
|
|
@ -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,13 +110,13 @@ function reset2FA() {
|
||||||
</AButton>
|
</AButton>
|
||||||
|
|
||||||
<template v-if="enrolling">
|
<template v-if="enrolling">
|
||||||
|
<div class="flex flex-col items-center">
|
||||||
<div class="mt-4 mb-2">
|
<div class="mt-4 mb-2">
|
||||||
<img
|
<AQrcode
|
||||||
v-if="qrCode"
|
v-if="generatedUrl"
|
||||||
class="w-64 h-64"
|
:value="generatedUrl"
|
||||||
:src="qrCode"
|
:size="256"
|
||||||
alt="qr code"
|
/>
|
||||||
>
|
|
||||||
<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
|
||||||
|
@ -140,6 +138,7 @@ function reset2FA() {
|
||||||
@on-complete="enroll"
|
@on-complete="enroll"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue