refactor: use context fom middleware in handlers

This commit is contained in:
Stavros
2025-08-25 14:19:52 +03:00
parent ace22acdb2
commit e1d8ce3cb5
11 changed files with 142 additions and 57 deletions

View File

@@ -37,8 +37,28 @@ func (h *Handlers) AppContextHandler(c *gin.Context) {
func (h *Handlers) UserContextHandler(c *gin.Context) {
log.Debug().Msg("Getting user context")
// Create user context using hooks
userContext := h.Hooks.UseUserContext(c)
// Get user context from middleware
userContextValue, exists := c.Get("context")
if !exists {
c.JSON(200, types.UserContextResponse{
Status: 200,
Message: "Unauthorized",
IsLoggedIn: false,
})
return
}
userContext, ok := userContextValue.(*types.UserContext)
if !ok {
c.JSON(200, types.UserContextResponse{
Status: 200,
Message: "Unauthorized",
IsLoggedIn: false,
})
return
}
userContextResponse := types.UserContextResponse{
Status: 200,

View File

@@ -3,26 +3,36 @@ package handlers
import (
"tinyauth/internal/auth"
"tinyauth/internal/docker"
"tinyauth/internal/hooks"
"tinyauth/internal/providers"
"tinyauth/internal/types"
"github.com/gin-gonic/gin"
)
type HandlersConfig struct {
AppURL string
Domain string
CookieSecure bool
DisableContinue bool
GenericName string
Title string
ForgotPasswordMessage string
BackgroundImage string
OAuthAutoRedirect string
CsrfCookieName string
RedirectCookieName string
}
type Handlers struct {
Config types.HandlersConfig
Config HandlersConfig
Auth *auth.Auth
Hooks *hooks.Hooks
Providers *providers.Providers
Docker *docker.Docker
}
func NewHandlers(config types.HandlersConfig, auth *auth.Auth, hooks *hooks.Hooks, providers *providers.Providers, docker *docker.Docker) *Handlers {
func NewHandlers(config HandlersConfig, auth *auth.Auth, providers *providers.Providers, docker *docker.Docker) *Handlers {
return &Handlers{
Config: config,
Auth: auth,
Hooks: hooks,
Providers: providers,
Docker: docker,
}

View File

@@ -146,7 +146,24 @@ func (h *Handlers) ProxyHandler(c *gin.Context) {
return
}
userContext := h.Hooks.UseUserContext(c)
var userContext *types.UserContext
userContextValue, exists := c.Get("context")
if !exists {
userContext = &types.UserContext{
IsLoggedIn: false,
}
} else {
var ok bool
userContext, ok = userContextValue.(*types.UserContext)
if !ok {
userContext = &types.UserContext{
IsLoggedIn: false,
}
}
}
// If we are using basic auth, we need to check if the user has totp and if it does then disable basic auth
if userContext.Provider == "basic" && userContext.TotpEnabled {
@@ -158,7 +175,7 @@ func (h *Handlers) ProxyHandler(c *gin.Context) {
log.Debug().Msg("Authenticated")
// Check if user is allowed to access subdomain, if request is nginx.example.com the subdomain (resource) is nginx
appAllowed := h.Auth.ResourceAllowed(c, userContext, labels)
appAllowed := h.Auth.ResourceAllowed(c, *userContext, labels)
log.Debug().Bool("appAllowed", appAllowed).Msg("Checking if app is allowed")
@@ -195,7 +212,7 @@ func (h *Handlers) ProxyHandler(c *gin.Context) {
}
if userContext.OAuth {
groupOk := h.Auth.OAuthGroup(c, userContext, labels)
groupOk := h.Auth.OAuthGroup(c, *userContext, labels)
log.Debug().Bool("groupOk", groupOk).Msg("Checking if user is in required groups")

View File

@@ -141,7 +141,25 @@ func (h *Handlers) TOTPHandler(c *gin.Context) {
log.Debug().Msg("Checking totp")
// Get user context
userContext := h.Hooks.UseUserContext(c)
userContextValue, exists := c.Get("context")
if !exists {
c.JSON(401, gin.H{
"status": 401,
"message": "Unauthorized",
})
return
}
userContext, ok := userContextValue.(*types.UserContext)
if !ok {
c.JSON(401, gin.H{
"status": 401,
"message": "Unauthorized",
})
return
}
// Check if we have a user
if userContext.Username == "" {
@@ -157,7 +175,7 @@ func (h *Handlers) TOTPHandler(c *gin.Context) {
user := h.Auth.GetLocalUser(userContext.Username)
// Check if totp is correct
ok := totp.Validate(totpReq.Code, user.TotpSecret)
ok = totp.Validate(totpReq.Code, user.TotpSecret)
if !ok {
log.Debug().Msg("Totp incorrect")