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

49 lines
1.1 KiB
Go
Raw Normal View History

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