@@ -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-
4338type 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 }
0 commit comments