mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2025-10-29 05:05:42 +00:00
refactor: use db instance instead of service in auth service
This commit is contained in:
@@ -92,16 +92,28 @@ func (app *BootstrapApp) Setup() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bootstrap database
|
||||||
databaseService := service.NewDatabaseService(service.DatabaseServiceConfig{
|
databaseService := service.NewDatabaseService(service.DatabaseServiceConfig{
|
||||||
DatabasePath: app.Config.DatabasePath,
|
DatabasePath: app.Config.DatabasePath,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
log.Debug().Str("service", fmt.Sprintf("%T", databaseService)).Msg("Initializing service")
|
||||||
|
|
||||||
|
err = databaseService.Init()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to initialize database service: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
database := databaseService.GetDatabase()
|
||||||
|
|
||||||
|
// Create services
|
||||||
dockerService := service.NewDockerService()
|
dockerService := service.NewDockerService()
|
||||||
authService := service.NewAuthService(authConfig, dockerService, ldapService, databaseService)
|
authService := service.NewAuthService(authConfig, dockerService, ldapService, database)
|
||||||
oauthBrokerService := service.NewOAuthBrokerService(app.getOAuthBrokerConfig())
|
oauthBrokerService := service.NewOAuthBrokerService(app.getOAuthBrokerConfig())
|
||||||
|
|
||||||
// Initialize services
|
// Initialize services
|
||||||
services := []Service{
|
services := []Service{
|
||||||
databaseService,
|
|
||||||
dockerService,
|
dockerService,
|
||||||
authService,
|
authService,
|
||||||
oauthBrokerService,
|
oauthBrokerService,
|
||||||
|
|||||||
@@ -49,18 +49,24 @@ func (m *ZerologMiddleware) Middleware() gin.HandlerFunc {
|
|||||||
|
|
||||||
latency := time.Since(tStart).String()
|
latency := time.Since(tStart).String()
|
||||||
|
|
||||||
// logPath check if the path should be logged normally or with debug
|
subLogger := log.With().Str("method", method).
|
||||||
|
Str("path", path).
|
||||||
|
Str("address", address).
|
||||||
|
Str("client_ip", clientIP).
|
||||||
|
Int("status", code).
|
||||||
|
Str("latency", latency).Logger()
|
||||||
|
|
||||||
if m.logPath(method + " " + path) {
|
if m.logPath(method + " " + path) {
|
||||||
switch {
|
switch {
|
||||||
case code >= 200 && code < 300:
|
case code >= 400 && code < 500:
|
||||||
log.Info().Str("method", method).Str("path", path).Str("address", address).Str("clientIp", clientIP).Int("status", code).Str("latency", latency).Msg("Request")
|
subLogger.Warn().Msg("Client Error")
|
||||||
case code >= 300 && code < 400:
|
case code >= 500:
|
||||||
log.Warn().Str("method", method).Str("path", path).Str("address", address).Str("clientIp", clientIP).Int("status", code).Str("latency", latency).Msg("Request")
|
subLogger.Error().Msg("Server Error")
|
||||||
case code >= 400:
|
default:
|
||||||
log.Error().Str("method", method).Str("path", path).Str("address", address).Str("clientIp", clientIP).Int("status", code).Str("latency", latency).Msg("Request")
|
subLogger.Info().Msg("Request")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Debug().Str("method", method).Str("path", path).Str("address", address).Int("status", code).Str("latency", latency).Msg("Request")
|
subLogger.Debug().Msg("Request")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import (
|
|||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type LoginAttempt struct {
|
type LoginAttempt struct {
|
||||||
@@ -39,10 +40,10 @@ type AuthService struct {
|
|||||||
LoginAttempts map[string]*LoginAttempt
|
LoginAttempts map[string]*LoginAttempt
|
||||||
LoginMutex sync.RWMutex
|
LoginMutex sync.RWMutex
|
||||||
LDAP *LdapService
|
LDAP *LdapService
|
||||||
Database *DatabaseService
|
Database *gorm.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAuthService(config AuthServiceConfig, docker *DockerService, ldap *LdapService, database *DatabaseService) *AuthService {
|
func NewAuthService(config AuthServiceConfig, docker *DockerService, ldap *LdapService, database *gorm.DB) *AuthService {
|
||||||
return &AuthService{
|
return &AuthService{
|
||||||
Config: config,
|
Config: config,
|
||||||
Docker: docker,
|
Docker: docker,
|
||||||
@@ -184,7 +185,6 @@ func (auth *AuthService) IsEmailWhitelisted(email string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (auth *AuthService) CreateSessionCookie(c *gin.Context, data *config.SessionCookie) error {
|
func (auth *AuthService) CreateSessionCookie(c *gin.Context, data *config.SessionCookie) error {
|
||||||
db := auth.Database.GetDatabase()
|
|
||||||
uuid, err := uuid.NewRandom()
|
uuid, err := uuid.NewRandom()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -210,7 +210,7 @@ func (auth *AuthService) CreateSessionCookie(c *gin.Context, data *config.Sessio
|
|||||||
Expiry: time.Now().Add(time.Duration(expiry) * time.Second).Unix(),
|
Expiry: time.Now().Add(time.Duration(expiry) * time.Second).Unix(),
|
||||||
}
|
}
|
||||||
|
|
||||||
err = db.Create(&session).Error
|
err = auth.Database.Create(&session).Error
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -222,14 +222,13 @@ func (auth *AuthService) CreateSessionCookie(c *gin.Context, data *config.Sessio
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (auth *AuthService) DeleteSessionCookie(c *gin.Context) error {
|
func (auth *AuthService) DeleteSessionCookie(c *gin.Context) error {
|
||||||
db := auth.Database.GetDatabase()
|
|
||||||
session, err := auth.GetSessionCookie(c)
|
session, err := auth.GetSessionCookie(c)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
res := db.Unscoped().Where("uuid = ?", session.UUID).Delete(&model.Session{})
|
res := auth.Database.Unscoped().Where("uuid = ?", session.UUID).Delete(&model.Session{})
|
||||||
|
|
||||||
if res.Error != nil {
|
if res.Error != nil {
|
||||||
return res.Error
|
return res.Error
|
||||||
@@ -241,7 +240,6 @@ func (auth *AuthService) DeleteSessionCookie(c *gin.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (auth *AuthService) GetSessionCookie(c *gin.Context) (config.SessionCookie, error) {
|
func (auth *AuthService) GetSessionCookie(c *gin.Context) (config.SessionCookie, error) {
|
||||||
db := auth.Database.GetDatabase()
|
|
||||||
cookie, err := c.Cookie(auth.Config.SessionCookieName)
|
cookie, err := c.Cookie(auth.Config.SessionCookieName)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -250,7 +248,7 @@ func (auth *AuthService) GetSessionCookie(c *gin.Context) (config.SessionCookie,
|
|||||||
|
|
||||||
var session model.Session
|
var session model.Session
|
||||||
|
|
||||||
res := db.Unscoped().Where("uuid = ?", cookie).First(&session)
|
res := auth.Database.Unscoped().Where("uuid = ?", cookie).First(&session)
|
||||||
|
|
||||||
if res.Error != nil {
|
if res.Error != nil {
|
||||||
return config.SessionCookie{}, res.Error
|
return config.SessionCookie{}, res.Error
|
||||||
@@ -263,7 +261,7 @@ func (auth *AuthService) GetSessionCookie(c *gin.Context) (config.SessionCookie,
|
|||||||
currentTime := time.Now().Unix()
|
currentTime := time.Now().Unix()
|
||||||
|
|
||||||
if currentTime > session.Expiry {
|
if currentTime > session.Expiry {
|
||||||
res := db.Unscoped().Where("uuid = ?", session.UUID).Delete(&model.Session{})
|
res := auth.Database.Unscoped().Where("uuid = ?", session.UUID).Delete(&model.Session{})
|
||||||
if res.Error != nil {
|
if res.Error != nil {
|
||||||
log.Error().Err(res.Error).Msg("Failed to delete expired session")
|
log.Error().Err(res.Error).Msg("Failed to delete expired session")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user