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

58 lines
1.2 KiB
Go
Raw Normal View History

2021-01-10 00:44:02 +00:00
package auth
import (
2021-02-04 10:16:13 +00:00
"errors"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
"reichard.io/imagini/graph/model"
"reichard.io/imagini/internal/config"
"reichard.io/imagini/internal/db"
2021-01-10 00:44:02 +00:00
)
2021-01-16 22:00:17 +00:00
type AuthManager struct {
2021-02-04 10:16:13 +00:00
DB *db.DBManager
Config *config.Config
2021-01-16 22:00:17 +00:00
}
2021-01-18 04:56:56 +00:00
func NewMgr(db *db.DBManager, c *config.Config) *AuthManager {
2021-02-04 10:16:13 +00:00
return &AuthManager{
DB: db,
Config: c,
}
2021-01-16 22:00:17 +00:00
}
2021-02-04 10:16:13 +00:00
func (auth *AuthManager) AuthenticateUser(user, password string) (model.User, bool) {
// Find User by Username / Email
foundUser := &model.User{Username: user}
_, err := auth.DB.User(foundUser)
// By Username
if errors.Is(err, gorm.ErrRecordNotFound) {
foundUser = &model.User{Email: user}
_, err = auth.DB.User(foundUser)
}
// By Email
if errors.Is(err, gorm.ErrRecordNotFound) {
log.Warn("[auth] User not found: ", user)
return *foundUser, false
} else if err != nil {
log.Error(err)
return *foundUser, false
}
log.Info("[auth] Authenticating user: ", foundUser.Username)
// Determine Type
switch foundUser.AuthType {
case "Local":
return *foundUser, authenticateLocalUser(*foundUser, password)
case "LDAP":
return *foundUser, authenticateLDAPUser(*foundUser, password)
default:
return *foundUser, false
}
2021-01-18 21:16:52 +00:00
}