This commit is contained in:
2021-01-11 23:48:32 -05:00
parent 96b0c888ed
commit bc3b437ebc
22 changed files with 339 additions and 186 deletions

View File

@@ -3,20 +3,21 @@ package auth
import (
"errors"
"gorm.io/gorm"
"reichard.io/imagini/internal/db"
"reichard.io/imagini/internal/query"
"reichard.io/imagini/internal/models"
log "github.com/sirupsen/logrus"
)
func AuthenticateUser(userIdentifier string, userPassword string) bool {
func AuthenticateUser(db *gorm.DB, creds models.APICredentials) bool {
// By Username
foundUser, err := db.GetUser(db.User{Username: userIdentifier})
foundUser, err := query.User(db, models.User{Username: creds.User})
if errors.Is(err, gorm.ErrRecordNotFound) {
foundUser, err = db.GetUser(db.User{Email: userIdentifier})
foundUser, err = query.User(db, models.User{Email: creds.User})
}
// Error Checking
if errors.Is(err, gorm.ErrRecordNotFound) {
log.Warn("[auth] User not found: ", userIdentifier)
log.Warn("[auth] User not found: ", creds.User)
return false
} else if err != nil {
log.Error(err)
@@ -28,9 +29,9 @@ func AuthenticateUser(userIdentifier string, userPassword string) bool {
// Determine Type
switch foundUser.AuthType {
case "Local":
return authenticateLocalUser(foundUser, userPassword)
return authenticateLocalUser(foundUser, creds.Password)
case "LDAP":
return authenticateLDAPUser(foundUser, userPassword)
return authenticateLDAPUser(foundUser, creds.Password)
default:
return false
}

View File

@@ -1,9 +1,9 @@
package auth
import (
"reichard.io/imagini/internal/db"
"reichard.io/imagini/internal/models"
)
func authenticateLDAPUser(user db.User, pw string) bool {
func authenticateLDAPUser(user models.User, pw string) bool {
return false
}

View File

@@ -5,23 +5,24 @@ import (
"gorm.io/gorm"
"golang.org/x/crypto/bcrypt"
log "github.com/sirupsen/logrus"
"reichard.io/imagini/internal/db"
"reichard.io/imagini/internal/query"
"reichard.io/imagini/internal/models"
)
func authenticateLocalUser(user db.User, pw string) bool {
func authenticateLocalUser(user models.User, pw string) bool {
bPassword :=[]byte(pw)
err := bcrypt.CompareHashAndPassword([]byte(user.HashedPassword), bPassword)
if err == nil {
log.Info("[local] Authentication successfull: ", user.Username)
log.Info("[auth] Authentication successfull: ", user.Username)
return true
}
log.Warn("[local] Authentication failed: ", user.Username)
log.Warn("[auth] Authentication failed: ", user.Username)
return false
}
func CreateUser(user db.User, pw string) error {
log.Info("[local] Creating user: ", user.Username)
_, err := db.GetUser(user)
func CreateUser(db *gorm.DB, user models.User, pw string) error {
log.Info("[auth] Creating user: ", user.Username)
_, err := query.User(db, user)
if !errors.Is(err, gorm.ErrRecordNotFound) {
log.Warn("[auth] User already exists: ", user.Username)
return errors.New("User already exists")
@@ -33,5 +34,5 @@ func CreateUser(user db.User, pw string) error {
return err
}
user.HashedPassword = string(hashedPassword)
return db.CreateUser(user)
return query.CreateUser(db, user)
}