Skip to content

Commit 5d11e6e

Browse files
shishkevichdsomebodywashereMHSanaei
authored
chore: reset two-factor authentication after changing admin credentials (#3029)
* chore: add `resetTwoFactor` argument for main.go fixes #3025 * chore: reset two-factor authentication after changing admin credentials * chore: reset two-factor authentication after changing admin credentials --------- Co-authored-by: somebodywashere <[email protected]> Co-authored-by: Sanaei <[email protected]>
1 parent f3d0b92 commit 5d11e6e

19 files changed

+104
-23
lines changed

main.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func updateTgbotSetting(tgBotToken string, tgBotChatid string, tgBotRuntime stri
232232
}
233233
}
234234

235-
func updateSetting(port int, username string, password string, webBasePath string, listenIP string) {
235+
func updateSetting(port int, username string, password string, webBasePath string, listenIP string, resetTwoFactor bool) {
236236
err := database.InitDB(config.GetDBPath())
237237
if err != nil {
238238
fmt.Println("Database initialization failed:", err)
@@ -269,6 +269,17 @@ func updateSetting(port int, username string, password string, webBasePath strin
269269
}
270270
}
271271

272+
if resetTwoFactor {
273+
err := settingService.SetTwoFactorEnable(false)
274+
275+
if err != nil {
276+
fmt.Println("Failed to reset two-factor authentication:", err)
277+
} else {
278+
settingService.SetTwoFactorToken("")
279+
fmt.Println("Two-factor authentication reset successfully")
280+
}
281+
}
282+
272283
if listenIP != "" {
273284
err := settingService.SetListen(listenIP)
274285
if err != nil {
@@ -376,13 +387,15 @@ func main() {
376387
var reset bool
377388
var show bool
378389
var getCert bool
390+
var resetTwoFactor bool
379391
settingCmd.BoolVar(&reset, "reset", false, "Reset all settings")
380392
settingCmd.BoolVar(&show, "show", false, "Display current settings")
381393
settingCmd.IntVar(&port, "port", 0, "Set panel port number")
382394
settingCmd.StringVar(&username, "username", "", "Set login username")
383395
settingCmd.StringVar(&password, "password", "", "Set login password")
384396
settingCmd.StringVar(&webBasePath, "webBasePath", "", "Set base path for Panel")
385397
settingCmd.StringVar(&listenIP, "listenIP", "", "set panel listenIP IP")
398+
settingCmd.BoolVar(&resetTwoFactor, "resetTwoFactor", false, "Reset two-factor authentication settings")
386399
settingCmd.BoolVar(&getListen, "getListen", false, "Display current panel listenIP IP")
387400
settingCmd.BoolVar(&getCert, "getCert", false, "Display current certificate settings")
388401
settingCmd.StringVar(&webCertFile, "webCert", "", "Set path to public key file for panel")
@@ -427,7 +440,7 @@ func main() {
427440
if reset {
428441
resetSetting()
429442
} else {
430-
updateSetting(port, username, password, webBasePath, listenIP)
443+
updateSetting(port, username, password, webBasePath, listenIP, resetTwoFactor)
431444
}
432445
if show {
433446
showSetting(show)

web/html/modals/two_factor_modal.html

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
<p>{{ i18n "pages.settings.security.twoFactorModalSecondStep" }}</p>
1616
<a-input v-model.trim="twoFactorModal.enteredCode" :style="{ width: '100%' }"></a-input>
1717
</template>
18-
<template v-if="twoFactorModal.type === 'remove'">
19-
<p>{{ i18n "pages.settings.security.twoFactorModalRemoveStep" }}</p>
18+
<template v-if="twoFactorModal.type === 'confirm'">
19+
<p>[[ twoFactorModal.description ]]</p>
2020
<a-input v-model.trim="twoFactorModal.enteredCode" :style="{ width: '100%' }"></a-input>
2121
</template>
2222
<template slot="footer">
@@ -32,6 +32,7 @@
3232
<script>
3333
const twoFactorModal = {
3434
title: '',
35+
description: '',
3536
fileName: '',
3637
token: '',
3738
enteredCode: '',
@@ -45,17 +46,6 @@
4546
ObjectUtil.execute(twoFactorModal.confirm, true)
4647

4748
twoFactorModal.close()
48-
49-
switch (twoFactorModal.type) {
50-
case 'set':
51-
Vue.prototype.$message['success']('{{ i18n "pages.settings.security.twoFactorModalSetSuccess" }}')
52-
break;
53-
case 'remove':
54-
Vue.prototype.$message['success']('{{ i18n "pages.settings.security.twoFactorModalDeleteSuccess" }}')
55-
break;
56-
default:
57-
break;
58-
}
5949
} else {
6050
Vue.prototype.$message['error']('{{ i18n "pages.settings.security.twoFactorModalError" }}')
6151
}
@@ -67,11 +57,13 @@
6757
},
6858
show: function ({
6959
title = '',
60+
description = '',
7061
token = '',
7162
type = 'set',
7263
confirm = (success) => { }
7364
}) {
7465
this.title = title;
66+
this.description = description;
7567
this.token = token;
7668
this.visible = true;
7769
this.confirm = confirm;

web/html/settings.html

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -310,12 +310,30 @@
310310
}
311311
},
312312
async updateUser() {
313-
this.loading(true);
314-
const msg = await HttpUtil.post("/panel/setting/updateUser", this.user);
315-
this.loading(false);
316-
if (msg.success) {
317-
this.user = {};
318-
window.location.replace(basePath + "logout");
313+
const sendUpdateUserRequest = async () => {
314+
this.loading(true);
315+
const msg = await HttpUtil.post("/panel/setting/updateUser", this.user);
316+
this.loading(false);
317+
if (msg.success) {
318+
this.user = {};
319+
window.location.replace(basePath + "logout");
320+
}
321+
}
322+
323+
if (this.allSetting.twoFactorEnable) {
324+
twoFactorModal.show({
325+
title: '{{ i18n "pages.settings.security.twoFactorModalChangeCredentialsTitle" }}',
326+
description: '{{ i18n "pages.settings.security.twoFactorModalChangeCredentialsStep" }}',
327+
token: this.allSetting.twoFactorToken,
328+
type: 'confirm',
329+
confirm: (success) => {
330+
if (success) {
331+
sendUpdateUserRequest();
332+
}
333+
}
334+
})
335+
} else {
336+
sendUpdateUserRequest();
319337
}
320338
},
321339
async restartPanel() {
@@ -353,6 +371,8 @@
353371
type: 'set',
354372
confirm: (success) => {
355373
if (success) {
374+
Vue.prototype.$message['success']('{{ i18n "pages.settings.security.twoFactorModalSetSuccess" }}')
375+
356376
this.allSetting.twoFactorToken = newTwoFactorToken
357377
}
358378

@@ -362,10 +382,13 @@
362382
} else {
363383
twoFactorModal.show({
364384
title: '{{ i18n "pages.settings.security.twoFactorModalDeleteTitle" }}',
385+
description: '{{ i18n "pages.settings.security.twoFactorModalRemoveStep" }}',
365386
token: this.allSetting.twoFactorToken,
366-
type: 'remove',
387+
type: 'confirm',
367388
confirm: (success) => {
368389
if (success) {
390+
Vue.prototype.$message['success']('{{ i18n "pages.settings.security.twoFactorModalDeleteSuccess" }}')
391+
369392
this.allSetting.twoFactorEnable = false
370393
this.allSetting.twoFactorToken = ""
371394
}

web/service/setting.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,18 @@ func (s *SettingService) GetTwoFactorEnable() (bool, error) {
322322
return s.getBool("twoFactorEnable")
323323
}
324324

325+
func (s *SettingService) SetTwoFactorEnable(value bool) error {
326+
return s.setBool("twoFactorEnable", value)
327+
}
328+
325329
func (s *SettingService) GetTwoFactorToken() (string, error) {
326330
return s.getString("twoFactorToken")
327331
}
328332

333+
func (s *SettingService) SetTwoFactorToken(value string) error {
334+
return s.setString("twoFactorToken", value)
335+
}
336+
329337
func (s *SettingService) GetPort() (int, error) {
330338
return s.getInt("webPort")
331339
}

web/service/user.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ func (s *UserService) UpdateUser(id int, username string, password string) error
7979
return err
8080
}
8181

82+
twoFactorEnable, err := s.settingService.GetTwoFactorEnable()
83+
if err != nil {
84+
return err
85+
}
86+
87+
if twoFactorEnable {
88+
s.settingService.SetTwoFactorEnable(false)
89+
s.settingService.SetTwoFactorToken("")
90+
}
91+
8292
return db.Model(model.User{}).
8393
Where("id = ?", id).
8494
Updates(map[string]any{"username": username, "password": hashedPassword}).

web/translation/translate.ar_EG.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,8 @@
542542
"twoFactorModalFirstStep" = "1. امسح رمز QR هذا في تطبيق المصادقة أو انسخ الرمز الموجود بجانب رمز QR والصقه في التطبيق"
543543
"twoFactorModalSecondStep" = "2. أدخل الرمز من التطبيق"
544544
"twoFactorModalRemoveStep" = "أدخل الرمز من التطبيق لإزالة المصادقة الثنائية."
545+
"twoFactorModalChangeCredentialsTitle" = "تغيير بيانات الاعتماد"
546+
"twoFactorModalChangeCredentialsStep" = "أدخل الرمز من التطبيق لتغيير بيانات اعتماد المسؤول."
545547
"twoFactorModalSetSuccess" = "تم إنشاء المصادقة الثنائية بنجاح"
546548
"twoFactorModalDeleteSuccess" = "تم حذف المصادقة الثنائية بنجاح"
547549
"twoFactorModalError" = "رمز خاطئ"

web/translation/translate.en_US.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,8 @@
541541
"twoFactorModalFirstStep" = "1. Scan this QR code in the app for authentication or copy the token near the QR code and paste it into the app"
542542
"twoFactorModalSecondStep" = "2. Enter the code from the app"
543543
"twoFactorModalRemoveStep" = "Enter the code from the application to remove two-factor authentication."
544+
"twoFactorModalChangeCredentialsTitle" = "Change credentials"
545+
"twoFactorModalChangeCredentialsStep" = "Enter the code from the application to change administrator credentials."
544546
"twoFactorModalSetSuccess" = "Two-factor authentication has been successfully established"
545547
"twoFactorModalDeleteSuccess" = "Two-factor authentication has been successfully deleted"
546548
"twoFactorModalError" = "Wrong code"

web/translation/translate.es_ES.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,8 @@
544544
"twoFactorModalFirstStep" = "1. Escanea este código QR en la aplicación de autenticación o copia el token cerca del código QR y pégalo en la aplicación"
545545
"twoFactorModalSecondStep" = "2. Ingresa el código de la aplicación"
546546
"twoFactorModalRemoveStep" = "Ingresa el código de la aplicación para eliminar la autenticación de dos factores."
547+
"twoFactorModalChangeCredentialsTitle" = "Cambiar credenciales"
548+
"twoFactorModalChangeCredentialsStep" = "Ingrese el código de la aplicación para cambiar las credenciales del administrador."
547549
"twoFactorModalSetSuccess" = "La autenticación de dos factores se ha establecido con éxito"
548550
"twoFactorModalDeleteSuccess" = "La autenticación de dos factores se ha eliminado con éxito"
549551
"twoFactorModalError" = "Código incorrecto"

web/translation/translate.fa_IR.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,8 @@
544544
"twoFactorModalFirstStep" = "1. این کد QR را در برنامه احراز هویت اسکن کنید یا توکن کنار کد QR را کپی کرده و در برنامه بچسبانید"
545545
"twoFactorModalSecondStep" = "2. کد را از برنامه وارد کنید"
546546
"twoFactorModalRemoveStep" = "برای حذف احراز هویت دو مرحله‌ای، کد را از برنامه وارد کنید."
547+
"twoFactorModalChangeCredentialsTitle" = "تغییر اعتبارنامه‌ها"
548+
"twoFactorModalChangeCredentialsStep" = "برای تغییر اعتبارنامه‌های مدیر، کد را از برنامه وارد کنید."
547549
"twoFactorModalSetSuccess" = "احراز هویت دو مرحله‌ای با موفقیت برقرار شد"
548550
"twoFactorModalDeleteSuccess" = "احراز هویت دو مرحله‌ای با موفقیت حذف شد"
549551
"twoFactorModalError" = "کد نادرست"

web/translation/translate.id_ID.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,8 @@
545545
"twoFactorModalFirstStep" = "1. Pindai kode QR ini di aplikasi autentikasi atau salin token di dekat kode QR dan tempelkan ke aplikasi"
546546
"twoFactorModalSecondStep" = "2. Masukkan kode dari aplikasi"
547547
"twoFactorModalRemoveStep" = "Masukkan kode dari aplikasi untuk menghapus autentikasi dua faktor."
548+
"twoFactorModalChangeCredentialsTitle" = "Ubah kredensial"
549+
"twoFactorModalChangeCredentialsStep" = "Masukkan kode dari aplikasi untuk mengubah kredensial administrator."
548550
"twoFactorModalSetSuccess" = "Autentikasi dua faktor telah berhasil dibuat"
549551
"twoFactorModalDeleteSuccess" = "Autentikasi dua faktor telah berhasil dihapus"
550552
"twoFactorModalError" = "Kode salah"

0 commit comments

Comments
 (0)