goipam/web/auth/hashPassword.go

43 lines
987 B
Go

package auth
import (
"log"
"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
}
log.Println("Logged in ", user.Name)
return nil
}
log.Println("Login error for ", username)
return &utils.CustomError{Message: "Passwords do not match"}
}