Skip to content

Commit 55b53c7

Browse files
committed
refactor: rework logging and config in middlewares
1 parent 112a30f commit 55b53c7

3 files changed

Lines changed: 33 additions & 30 deletions

File tree

internal/middleware/context_middleware.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/tinyauthapp/tinyauth/internal/model"
1111
"github.com/tinyauthapp/tinyauth/internal/service"
1212
"github.com/tinyauthapp/tinyauth/internal/utils"
13-
"github.com/tinyauthapp/tinyauth/internal/utils/tlog"
13+
"github.com/tinyauthapp/tinyauth/internal/utils/logger"
1414

1515
"github.com/gin-gonic/gin"
1616
)
@@ -35,22 +35,24 @@ var (
3535
}
3636
)
3737

38-
type ContextMiddlewareConfig struct {
39-
CookieDomain string
40-
SessionCookieName string
41-
}
42-
4338
type ContextMiddleware struct {
44-
config ContextMiddlewareConfig
45-
auth *service.AuthService
46-
broker *service.OAuthBrokerService
39+
log *logger.Logger
40+
runtime model.RuntimeConfig
41+
auth *service.AuthService
42+
broker *service.OAuthBrokerService
4743
}
4844

49-
func NewContextMiddleware(config ContextMiddlewareConfig, auth *service.AuthService, broker *service.OAuthBrokerService) *ContextMiddleware {
45+
func NewContextMiddleware(
46+
log *logger.Logger,
47+
runtime model.RuntimeConfig,
48+
auth *service.AuthService,
49+
broker *service.OAuthBrokerService,
50+
) *ContextMiddleware {
5051
return &ContextMiddleware{
51-
config: config,
52-
auth: auth,
53-
broker: broker,
52+
log: log,
53+
runtime: runtime,
54+
auth: auth,
55+
broker: broker,
5456
}
5557
}
5658

@@ -65,7 +67,7 @@ func (m *ContextMiddleware) Middleware() gin.HandlerFunc {
6567
return
6668
}
6769

68-
uuid, err := c.Cookie(m.config.SessionCookieName)
70+
uuid, err := c.Cookie(m.runtime.SessionCookieName)
6971

7072
if err == nil {
7173
userContext, cookie, err := m.cookieAuth(c.Request.Context(), uuid)
@@ -75,12 +77,12 @@ func (m *ContextMiddleware) Middleware() gin.HandlerFunc {
7577
http.SetCookie(c.Writer, cookie)
7678
}
7779

78-
tlog.App.Trace().Msgf("Authenticated user from session cookie: %s", userContext.GetUsername())
80+
m.log.App.Debug().Msgf("Authenticated user %s via session cookie", userContext.GetUsername())
7981
c.Set("context", userContext)
8082
c.Next()
8183
return
8284
} else {
83-
tlog.App.Error().Msgf("Error authenticating session cookie: %v", err)
85+
m.log.App.Error().Msgf("Error authenticating session cookie: %v", err)
8486
}
8587
}
8688

@@ -90,7 +92,7 @@ func (m *ContextMiddleware) Middleware() gin.HandlerFunc {
9092
userContext, headers, err := m.basicAuth(username, password)
9193

9294
if err != nil {
93-
tlog.App.Error().Msgf("Error authenticating basic auth: %v", err)
95+
m.log.App.Error().Msgf("Error authenticating basic auth: %v", err)
9496
c.Next()
9597
return
9698
}
@@ -141,7 +143,7 @@ func (m *ContextMiddleware) cookieAuth(ctx context.Context, uuid string) (*model
141143
}
142144

143145
if userContext.Local.Attributes.Email == "" {
144-
userContext.Local.Attributes.Email = utils.CompileUserEmail(user.Username, m.config.CookieDomain)
146+
userContext.Local.Attributes.Email = utils.CompileUserEmail(user.Username, m.runtime.CookieDomain)
145147
}
146148
case model.ProviderLDAP:
147149
search, err := m.auth.SearchUser(userContext.LDAP.Username)
@@ -162,7 +164,7 @@ func (m *ContextMiddleware) cookieAuth(ctx context.Context, uuid string) (*model
162164

163165
userContext.LDAP.Groups = user.Groups
164166
userContext.LDAP.Name = utils.Capitalize(userContext.LDAP.Username)
165-
userContext.LDAP.Email = utils.CompileUserEmail(userContext.LDAP.Username, m.config.CookieDomain)
167+
userContext.LDAP.Email = utils.CompileUserEmail(userContext.LDAP.Username, m.runtime.CookieDomain)
166168
case model.ProviderOAuth:
167169
_, exists := m.broker.GetService(userContext.OAuth.ID)
168170

@@ -191,7 +193,7 @@ func (m *ContextMiddleware) basicAuth(username string, password string) (*model.
191193
locked, remaining := m.auth.IsAccountLocked(username)
192194

193195
if locked {
194-
tlog.App.Debug().Msgf("Account for user %s is locked for %d seconds, denying auth", username, remaining)
196+
m.log.App.Debug().Msgf("Account for user %s is locked for %d seconds, denying auth", username, remaining)
195197
headers["x-tinyauth-lock-locked"] = "true"
196198
headers["x-tinyauth-lock-reset"] = time.Now().Add(time.Duration(remaining) * time.Second).Format(time.RFC3339)
197199
return nil, headers, nil
@@ -224,7 +226,7 @@ func (m *ContextMiddleware) basicAuth(username string, password string) (*model.
224226
BaseContext: model.BaseContext{
225227
Username: user.Username,
226228
Name: utils.Capitalize(user.Username),
227-
Email: utils.CompileUserEmail(user.Username, m.config.CookieDomain),
229+
Email: utils.CompileUserEmail(user.Username, m.runtime.CookieDomain),
228230
},
229231
Attributes: user.Attributes,
230232
}
@@ -240,7 +242,7 @@ func (m *ContextMiddleware) basicAuth(username string, password string) (*model.
240242
BaseContext: model.BaseContext{
241243
Username: username,
242244
Name: utils.Capitalize(username),
243-
Email: utils.CompileUserEmail(username, m.config.CookieDomain),
245+
Email: utils.CompileUserEmail(username, m.runtime.CookieDomain),
244246
},
245247
Groups: user.Groups,
246248
}

internal/middleware/ui_middleware.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"time"
1010

1111
"github.com/tinyauthapp/tinyauth/internal/assets"
12-
"github.com/tinyauthapp/tinyauth/internal/utils/tlog"
1312

1413
"github.com/gin-gonic/gin"
1514
)
@@ -40,8 +39,6 @@ func (m *UIMiddleware) Middleware() gin.HandlerFunc {
4039
return func(c *gin.Context) {
4140
path := strings.TrimPrefix(c.Request.URL.Path, "/")
4241

43-
tlog.App.Debug().Str("path", path).Msg("path")
44-
4542
switch strings.SplitN(path, "/", 2)[0] {
4643
case "api", "resources", ".well-known":
4744
c.Next()

internal/middleware/zerolog_middleware.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"time"
66

77
"github.com/gin-gonic/gin"
8-
"github.com/tinyauthapp/tinyauth/internal/utils/tlog"
8+
"github.com/tinyauthapp/tinyauth/internal/utils/logger"
99
)
1010

1111
// See context middleware for explanation of why we have to do this
@@ -17,10 +17,14 @@ var (
1717
}
1818
)
1919

20-
type ZerologMiddleware struct{}
20+
type ZerologMiddleware struct {
21+
log *logger.Logger
22+
}
2123

22-
func NewZerologMiddleware() *ZerologMiddleware {
23-
return &ZerologMiddleware{}
24+
func NewZerologMiddleware(log *logger.Logger) *ZerologMiddleware {
25+
return &ZerologMiddleware{
26+
log: log,
27+
}
2428
}
2529

2630
func (m *ZerologMiddleware) Init() error {
@@ -50,7 +54,7 @@ func (m *ZerologMiddleware) Middleware() gin.HandlerFunc {
5054

5155
latency := time.Since(tStart).String()
5256

53-
subLogger := tlog.HTTP.With().Str("method", method).
57+
subLogger := m.log.HTTP.With().Str("method", method).
5458
Str("path", path).
5559
Str("address", address).
5660
Str("client_ip", clientIP).

0 commit comments

Comments
 (0)