filegate/web/auth/hashPassword.go

37 lines
847 B
Go

package auth
import (
"git.jmbit.de/filegate/filegate/db"
"git.jmbit.de/filegate/filegate/utils"
"github.com/gin-contrib/sessions"
)
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
}
}
return nil
}