WIP
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ type Config struct {
|
||||
DataPath string
|
||||
ConfigPath string
|
||||
JWTSecret string
|
||||
ListenPort string
|
||||
}
|
||||
|
||||
func NewConfig() *Config {
|
||||
@@ -21,6 +22,7 @@ func NewConfig() *Config {
|
||||
ConfigPath: getEnv("CONFIG_PATH", "/config"),
|
||||
DataPath: getEnv("DATA_PATH", "/data"),
|
||||
JWTSecret: getEnv("JWT_SECRET", "58b9340c0472cf045db226bc445966524e780cd38bc3dd707afce80c95d4de6f"),
|
||||
ListenPort: getEnv("LISTEN_PORT", "8484"),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
21
internal/context/context.go
Normal file
21
internal/context/context.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package context
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"reichard.io/imagini/internal/query"
|
||||
"reichard.io/imagini/internal/config"
|
||||
)
|
||||
|
||||
type ImaginiContext struct {
|
||||
DB *gorm.DB
|
||||
Config *config.Config
|
||||
}
|
||||
|
||||
func NewImaginiContext() *ImaginiContext {
|
||||
c := config.NewConfig()
|
||||
gormDB := query.NewDB(c)
|
||||
return &ImaginiContext{
|
||||
DB: gormDB,
|
||||
Config: c,
|
||||
}
|
||||
}
|
||||
22
internal/models/api.go
Normal file
22
internal/models/api.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package models
|
||||
|
||||
type APICredentials struct {
|
||||
User string `json:"user"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type APIMeta struct {
|
||||
Count int `json:"count"`
|
||||
Page int `json:"page"`
|
||||
}
|
||||
|
||||
type APIError struct {
|
||||
Message string `json:"message"`
|
||||
Code int `json:"code"`
|
||||
}
|
||||
|
||||
type APIResponse struct {
|
||||
Data []interface{} `json:"data"`
|
||||
Meta APIMeta `json:"meta"`
|
||||
Error APIError `json:"error"`
|
||||
}
|
||||
45
internal/models/db.go
Normal file
45
internal/models/db.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ServerSetting struct {
|
||||
gorm.Model
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
gorm.Model
|
||||
Email string `json:"email" gorm:"unique;not null"`
|
||||
Username string `json:"username" gorm:"unique;not null"`
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
AuthType string `json:"auth_type"`
|
||||
HashedPassword string `json:"hashed_password"`
|
||||
}
|
||||
|
||||
type MediaItem struct {
|
||||
gorm.Model
|
||||
User User `json:"user" gorm:"ForeignKey:ID"`
|
||||
EXIFDate time.Time `json:"exif_date"`
|
||||
Latitude string `json:"latitude"`
|
||||
Longitude string `json:"longitude"`
|
||||
MediaType uint `json:"media_type"`
|
||||
RelPath string `json:"rel_path"`
|
||||
Tags []Tag `json:"tags" gorm:"many2many:media_tags;"`
|
||||
Albums []Album `json:"albums" gorm:"many2many:media_albums;"`
|
||||
}
|
||||
|
||||
type Tag struct {
|
||||
gorm.Model
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type Album struct {
|
||||
gorm.Model
|
||||
Name string `json:"name"`
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ServerSetting struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
Description string
|
||||
Value string
|
||||
}
|
||||
|
||||
type User struct {
|
||||
gorm.Model
|
||||
Email string `gorm:"unique;not null"`
|
||||
Username string `gorm:"unique;not null"`
|
||||
FirstName string
|
||||
LastName string
|
||||
AuthType string
|
||||
HashedPassword string
|
||||
}
|
||||
|
||||
type MediaItem struct {
|
||||
gorm.Model
|
||||
User User `gorm:"ForeignKey:ID"`
|
||||
EXIFDate time.Time
|
||||
Latitude string
|
||||
Longitude string
|
||||
MediaType uint
|
||||
RelPath string
|
||||
Tags []Tag `gorm:"many2many:media_tags;"`
|
||||
Albums []Album `gorm:"many2many:media_albums;"`
|
||||
}
|
||||
|
||||
type Tag struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
}
|
||||
|
||||
type Album struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package db
|
||||
package query
|
||||
|
||||
import (
|
||||
"log"
|
||||
@@ -13,13 +13,12 @@ import (
|
||||
"reichard.io/imagini/internal/models"
|
||||
)
|
||||
|
||||
var db *gorm.DB
|
||||
|
||||
func ConnectDB(c *config.Config) {
|
||||
func NewDB(c *config.Config) *gorm.DB {
|
||||
gormConfig := &gorm.Config{
|
||||
PrepareStmt: true,
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
}
|
||||
var db *gorm.DB
|
||||
|
||||
if c.DBType == "SQLite" {
|
||||
dbLocation := path.Join(c.ConfigPath, "imagini.db")
|
||||
@@ -34,9 +33,10 @@ func ConnectDB(c *config.Config) {
|
||||
db.AutoMigrate(&models.MediaItem{})
|
||||
db.AutoMigrate(&models.Tag{})
|
||||
db.AutoMigrate(&models.Album{})
|
||||
return db
|
||||
}
|
||||
|
||||
func ItemsFromAlbum(user models.User, album models.Album) []models.MediaItem {
|
||||
func ItemsFromAlbum(db *gorm.DB, user models.User, album models.Album) []models.MediaItem {
|
||||
var mediaItems []models.MediaItem
|
||||
// db.Table("media_albums").
|
||||
// Select("media_item.*").
|
||||
@@ -1,4 +1,4 @@
|
||||
package db
|
||||
package query
|
||||
|
||||
import "errors"
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
package db
|
||||
package query
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"reichard.io/imagini/internal/models"
|
||||
)
|
||||
|
||||
func CreateUser (user models.User) error {
|
||||
func CreateUser (db *gorm.DB, user models.User) error {
|
||||
err := db.Create(&user).Error
|
||||
return err
|
||||
}
|
||||
|
||||
func User (user models.User) (models.User, error) {
|
||||
func User (db *gorm.DB, user models.User) (models.User, error) {
|
||||
var foundUser models.User
|
||||
var count int64
|
||||
err := db.Where(&user).First(&foundUser).Count(&count).Error
|
||||
Reference in New Issue
Block a user