This repository has been archived on 2023-11-13. You can view files and clone it, but cannot push or open issues or pull requests.
imagini/internal/auth/local.go

38 lines
1.0 KiB
Go

package auth
import (
"errors"
"gorm.io/gorm"
"golang.org/x/crypto/bcrypt"
log "github.com/sirupsen/logrus"
"reichard.io/imagini/internal/db"
)
func authenticateLocalUser(user db.User, pw string) bool {
bPassword :=[]byte(pw)
err := bcrypt.CompareHashAndPassword([]byte(user.HashedPassword), bPassword)
if err == nil {
log.Info("[local] Authentication successfull: ", user.Username)
return true
}
log.Warn("[local] Authentication failed: ", user.Username)
return false
}
func CreateUser(user db.User, pw string) error {
log.Info("[local] Creating user: ", user.Username)
_, err := db.GetUser(user)
if !errors.Is(err, gorm.ErrRecordNotFound) {
log.Warn("[auth] User already exists: ", user.Username)
return errors.New("User already exists")
}
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(pw), bcrypt.DefaultCost)
if err != nil {
log.Error(err)
return err
}
user.HashedPassword = string(hashedPassword)
return db.CreateUser(user)
}