- 优化双因素认证流程

This commit is contained in:
dushixiang
2021-01-24 17:16:33 +08:00
parent 53e4048944
commit 7c4860ce28
5 changed files with 138 additions and 24 deletions

View File

@ -156,7 +156,7 @@ func ConfirmTOTPEndpoint(c echo.Context) error {
return Success(c, nil)
}
func ResetTOTPEndpoint(c echo.Context) error {
func ReloadTOTPEndpoint(c echo.Context) error {
account, _ := GetCurrentAccount(c)
key, err := totp.NewTOTP(totp.GenerateOpts{
@ -183,6 +183,15 @@ func ResetTOTPEndpoint(c echo.Context) error {
})
}
func ResetTOTPEndpoint(c echo.Context) error {
account, _ := GetCurrentAccount(c)
u := &model.User{
TOTPSecret: "-",
}
model.UpdateUserById(u, account.ID)
return Success(c, "")
}
func ChangePasswordEndpoint(c echo.Context) error {
account, _ := GetCurrentAccount(c)
@ -208,7 +217,28 @@ func ChangePasswordEndpoint(c echo.Context) error {
return LogoutEndpoint(c)
}
type AccountInfo struct {
Id string `json:"id"`
Username string `json:"username"`
Nickname string `json:"nickname"`
Type string `json:"type"`
EnableTotp bool `json:"enableTotp"`
}
func InfoEndpoint(c echo.Context) error {
account, _ := GetCurrentAccount(c)
return Success(c, account)
user, err := model.FindUserById(account.ID)
if err != nil {
return err
}
info := AccountInfo{
Id: user.ID,
Username: user.Username,
Nickname: user.Nickname,
Type: user.Type,
EnableTotp: user.TOTPSecret != "" && user.TOTPSecret != "-",
}
return Success(c, info)
}

View File

@ -38,6 +38,7 @@ func SetupRoutes() *echo.Echo {
e.POST("/logout", LogoutEndpoint)
e.POST("/change-password", ChangePasswordEndpoint)
e.GET("/reload-totp", ReloadTOTPEndpoint)
e.POST("/reset-totp", ResetTOTPEndpoint)
e.POST("/confirm-totp", ConfirmTOTPEndpoint)
e.GET("/info", InfoEndpoint)