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/auth.go
2021-01-16 17:00:17 -05:00

49 lines
1.1 KiB
Go

package auth
import (
"errors"
"gorm.io/gorm"
"reichard.io/imagini/internal/db"
"reichard.io/imagini/internal/models"
log "github.com/sirupsen/logrus"
)
type AuthManager struct {
DB *db.DBManager
}
func NewMgr(db *db.DBManager) *AuthManager {
return &AuthManager{
DB: db,
}
}
func (auth *AuthManager) AuthenticateUser(creds models.APICredentials) bool {
// By Username
foundUser, err := auth.DB.User(models.User{Username: creds.User})
if errors.Is(err, gorm.ErrRecordNotFound) {
foundUser, err = auth.DB.User(models.User{Email: creds.User})
}
// Error Checking
if errors.Is(err, gorm.ErrRecordNotFound) {
log.Warn("[auth] User not found: ", creds.User)
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, creds.Password)
case "LDAP":
return authenticateLDAPUser(foundUser, creds.Password)
default:
return false
}
}