39 lines
		
	
	
		
			1013 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			39 lines
		
	
	
		
			1013 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| package auth
 | |
| 
 | |
| import (
 | |
|     "errors"
 | |
| 	"gorm.io/gorm"
 | |
|     "reichard.io/imagini/internal/query"
 | |
|     "reichard.io/imagini/internal/models"
 | |
|     log "github.com/sirupsen/logrus"
 | |
| )
 | |
| 
 | |
| func AuthenticateUser(db *gorm.DB, creds models.APICredentials) bool {
 | |
|     // By Username
 | |
|     foundUser, err := query.User(db, models.User{Username: creds.User})
 | |
|     if errors.Is(err, gorm.ErrRecordNotFound) {
 | |
|         foundUser, err = query.User(db, 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
 | |
|     }
 | |
| }
 |