Skip to content

Commit

Permalink
Merge pull request #318 from bilogic/main
Browse files Browse the repository at this point in the history
refactored for readability
  • Loading branch information
wj-xiao authored Feb 17, 2025
2 parents 42dc6ad + 5c1e617 commit a24c1b0
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions server/service/auth/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const AccountFile = "/etc/kvm/pwd"

type Account struct {
Username string `json:"username"`
Password string `json:"password"`
Password string `json:"password"` // should be named HashedPassword for clarity
}

func GetAccount() (*Account, error) {
Expand All @@ -40,10 +40,10 @@ func GetAccount() (*Account, error) {
return &account, nil
}

func SetAccount(username string, password string) error {
func SetAccount(username string, hashedPassword string) error {
account, err := json.Marshal(&Account{
Username: username,
Password: password,
Password: hashedPassword,
})
if err != nil {
log.Errorf("failed to marshal account information to json: %s", err)
Expand All @@ -65,7 +65,7 @@ func SetAccount(username string, password string) error {
return nil
}

func CompareAccount(username string, password string) bool {
func CompareAccount(username string, plainPassword string) bool {
account, err := GetAccount()
if err != nil {
return false
Expand All @@ -75,16 +75,16 @@ func CompareAccount(username string, password string) bool {
return false
}

decryptedPassword, err := utils.DecodeDecrypt(password)
if err != nil || decryptedPassword == "" {
hashedPassword, err := utils.DecodeDecrypt(plainPassword)
if err != nil || hashedPassword == "" {
return false
}

err = bcrypt.CompareHashAndPassword([]byte(account.Password), []byte(decryptedPassword))
err = bcrypt.CompareHashAndPassword([]byte(account.Password), []byte(hashedPassword))
if err != nil {
// Compatible with old versions
accountDecryptedPassword, _ := utils.DecodeDecrypt(account.Password)
if accountDecryptedPassword == decryptedPassword {
accountHashedPassword, _ := utils.DecodeDecrypt(account.Password)
if accountHashedPassword == hashedPassword {
return true
}

Expand All @@ -104,10 +104,10 @@ func DelAccount() error {
}

func getDefaultAccount() *Account {
password, _ := bcrypt.GenerateFromPassword([]byte("admin"), bcrypt.DefaultCost)
hashedPassword, _ := bcrypt.GenerateFromPassword([]byte("admin"), bcrypt.DefaultCost)

return &Account{
Username: "admin",
Password: string(password),
Password: string(hashedPassword),
}
}

0 comments on commit a24c1b0

Please sign in to comment.