feat: validate new recovery code

This commit is contained in:
Hintay 2025-02-10 14:44:01 +09:00
parent c7731667f4
commit 9184711d43
No known key found for this signature in database
GPG key ID: 120FC7FF121F2F2D
5 changed files with 69 additions and 41 deletions

View file

@ -6,7 +6,7 @@ export interface TwoFAStatus {
otp_status: boolean otp_status: boolean
passkey_status: boolean passkey_status: boolean
recovery_codes_generated: boolean recovery_codes_generated: boolean
recovery_codes_viewed: boolean recovery_codes_viewed?: boolean
} }
const twoFA = { const twoFA = {

View file

@ -65,23 +65,13 @@ onMounted(() => {
<template> <template>
<div> <div>
<div v-if="twoFAStatus.otp_status">
<div v-if="!useRecoveryCode">
<p>{{ $gettext('Please enter the OTP code:') }}</p>
<OTPInput
ref="refOTP"
v-model="passcode"
class="justify-center mb-6"
@on-complete="onSubmit"
/>
</div>
<div <div
v-else v-if="useRecoveryCode"
class="mt-2 mb-4" class="mt-2 mb-4"
> >
<p>{{ $gettext('Input the recovery code:') }}</p> <p>{{ $gettext('Input the recovery code:') }}</p>
<AInputGroup compact> <AInputGroup compact>
<AInput v-model:value="recoveryCode" /> <AInput v-model:value="recoveryCode" placeholder="xxxxx-xxxxx" />
<AButton <AButton
type="primary" type="primary"
@click="onSubmit" @click="onSubmit"
@ -91,16 +81,14 @@ onMounted(() => {
</AInputGroup> </AInputGroup>
</div> </div>
<div class="flex justify-center"> <div v-if="twoFAStatus.otp_status && !useRecoveryCode">
<a <p>{{ $gettext('Please enter the OTP code:') }}</p>
v-if="!useRecoveryCode" <OTPInput
@click="clickUseRecoveryCode" ref="refOTP"
>{{ $gettext('Use recovery code') }}</a> v-model="passcode"
<a class="justify-center mb-6"
v-else @on-complete="onSubmit"
@click="clickUseOTP" />
>{{ $gettext('Use OTP') }}</a>
</div>
</div> </div>
<div <div
@ -121,6 +109,17 @@ onMounted(() => {
{{ $gettext('Authenticate with a passkey') }} {{ $gettext('Authenticate with a passkey') }}
</AButton> </AButton>
</div> </div>
<div v-if="twoFAStatus.otp_status || twoFAStatus.recovery_codes_generated" class="flex justify-center">
<a
v-if="!useRecoveryCode"
@click="clickUseRecoveryCode"
>{{ $gettext('Use recovery code') }}</a>
<a
v-else-if="twoFAStatus.otp_status"
@click="clickUseOTP"
>{{ $gettext('Use OTP') }}</a>
</div>
</div> </div>
</template> </template>

View file

@ -205,6 +205,7 @@ async function handlePasskeyLogin() {
enabled: true, enabled: true,
otp_status: true, otp_status: true,
passkey_status: false, passkey_status: false,
recovery_codes_generated: true,
}" }"
@submit-o-t-p="handleOTPSubmit" @submit-o-t-p="handleOTPSubmit"
/> />

View file

@ -8,6 +8,7 @@ var (
ErrUserBanned = e.New(40303, "user banned") ErrUserBanned = e.New(40303, "user banned")
ErrOTPCode = e.New(40304, "invalid otp code") ErrOTPCode = e.New(40304, "invalid otp code")
ErrRecoveryCode = e.New(40305, "invalid recovery code") ErrRecoveryCode = e.New(40305, "invalid recovery code")
ErrTOTPNotEnabled = e.New(40306, "legacy recovery code not allowed since totp is not enabled")
ErrWebAuthnNotConfigured = e.New(50000, "WebAuthn settings are not configured") ErrWebAuthnNotConfigured = e.New(50000, "WebAuthn settings are not configured")
ErrUserNotEnabledOTPAs2FA = e.New(50001, "user not enabled otp as 2fa") ErrUserNotEnabledOTPAs2FA = e.New(50001, "user not enabled otp as 2fa")
ErrOTPOrRecoveryCodeEmpty = e.New(50002, "otp or recovery code empty") ErrOTPOrRecoveryCodeEmpty = e.New(50002, "otp or recovery code empty")

View file

@ -5,12 +5,14 @@ import (
"crypto/sha1" "crypto/sha1"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"time"
"github.com/0xJacky/Nginx-UI/internal/cache" "github.com/0xJacky/Nginx-UI/internal/cache"
"github.com/0xJacky/Nginx-UI/internal/crypto" "github.com/0xJacky/Nginx-UI/internal/crypto"
"github.com/0xJacky/Nginx-UI/model" "github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/pquerna/otp/totp" "github.com/pquerna/otp/totp"
"time"
) )
func VerifyOTP(user *model.User, otp, recoveryCode string) (err error) { func VerifyOTP(user *model.User, otp, recoveryCode string) (err error) {
@ -24,6 +26,19 @@ func VerifyOTP(user *model.User, otp, recoveryCode string) (err error) {
return ErrOTPCode return ErrOTPCode
} }
} else { } else {
// get user from db
u := query.User
user, err = u.Where(u.ID.Eq(user.ID)).First()
if err != nil {
return err
}
// legacy recovery code
if !user.RecoveryCodeGenerated() {
if user.OTPSecret == nil {
return ErrTOTPNotEnabled
}
recoverCode, err := hex.DecodeString(recoveryCode) recoverCode, err := hex.DecodeString(recoveryCode)
if err != nil { if err != nil {
return err return err
@ -33,6 +48,18 @@ func VerifyOTP(user *model.User, otp, recoveryCode string) (err error) {
return ErrRecoveryCode return ErrRecoveryCode
} }
} }
// check recovery code
for _, code := range user.RecoveryCodes.Codes {
if code.Code == recoveryCode && code.UsedTime == nil {
t := time.Now()
code.UsedTime = &t
_, err = u.Where(u.ID.Eq(user.ID)).Updates(user)
return
}
}
return ErrRecoveryCode
}
return return
} }