goipam/web/auth/hashPassword.go

43 lines
987 B
Go
Raw Normal View History

2024-02-22 10:18:59 +01:00
package auth
import (
2024-02-23 11:22:28 +01:00
"log"
2024-02-22 10:18:59 +01:00
"github.com/gin-contrib/sessions"
"git.jmbit.de/jmb/goipam/db"
"git.jmbit.de/jmb/goipam/utils"
)
func storePasswordHash(username string, hash string) error {
user, err := db.GetUserByName(username)
err = db.SetUserPassHash(user, hash)
if err != nil {
return err
}
return nil
}
// CheckPassword checks if the password correctly correlates to the password hash stored in the Database
func CheckPassword(username string, password string, session sessions.Session) error {
var user db.User
user, err := db.GetUserByName(username)
if err != nil {
return err
}
if utils.DoPasswordsMatch(user.PassHash, password) {
session.Set("username", username)
session.Set("admin", user.Admin)
session.Set("isLoggedIn", true)
err := session.Save()
if err != nil {
return err
}
2024-02-23 11:22:28 +01:00
log.Println("Logged in ", user.Name)
return nil
2024-02-22 10:18:59 +01:00
}
2024-02-23 11:22:28 +01:00
log.Println("Login error for ", username)
return &utils.CustomError{Message: "Passwords do not match"}
2024-02-22 10:18:59 +01:00
}