38 lines
962 B
Go
38 lines
962 B
Go
package auth
|
|
|
|
import (
|
|
"errors"
|
|
"gorm.io/gorm"
|
|
"reichard.io/imagini/internal/db"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func AuthenticateUser(userIdentifier string, userPassword string) bool {
|
|
// By Username
|
|
foundUser, err := db.GetUser(db.User{Username: userIdentifier})
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
foundUser, err = db.GetUser(db.User{Email: userIdentifier})
|
|
}
|
|
|
|
// Error Checking
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
log.Warn("[auth] User not found: ", userIdentifier)
|
|
return false
|
|
} else if err != nil {
|
|
log.Error(err)
|
|
return false
|
|
}
|
|
|
|
log.Info("[auth] Authenticating user: ", foundUser.Username)
|
|
|
|
// Determine Type
|
|
switch foundUser.AuthType {
|
|
case "Local":
|
|
return authenticateLocalUser(foundUser, userPassword)
|
|
case "LDAP":
|
|
return authenticateLDAPUser(foundUser, userPassword)
|
|
default:
|
|
return false
|
|
}
|
|
}
|