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

39 lines
1013 B
Go
Raw Normal View History

2021-01-10 00:44:02 +00:00
package auth
import (
"errors"
"gorm.io/gorm"
2021-01-12 04:48:32 +00:00
"reichard.io/imagini/internal/query"
"reichard.io/imagini/internal/models"
2021-01-10 00:44:02 +00:00
log "github.com/sirupsen/logrus"
)
2021-01-12 04:48:32 +00:00
func AuthenticateUser(db *gorm.DB, creds models.APICredentials) bool {
2021-01-10 00:44:02 +00:00
// By Username
2021-01-12 04:48:32 +00:00
foundUser, err := query.User(db, models.User{Username: creds.User})
2021-01-10 00:44:02 +00:00
if errors.Is(err, gorm.ErrRecordNotFound) {
2021-01-12 04:48:32 +00:00
foundUser, err = query.User(db, 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
}
}