@@ -9,17 +9,16 @@ import (
99 "time"
1010
1111 "github.com/mirkobrombin/goup/internal/config"
12+ "github.com/mirkobrombin/goup/internal/plugin"
1213 log "github.com/sirupsen/logrus"
1314)
1415
1516// AuthPlugin provides HTTP Basic Authentication for protected paths.
1617type AuthPlugin struct {
17- // Keeps the parsed config for the plugin.
18- conf AuthPluginConfig
19- // State holds the active sessions.
18+ plugin.BasePlugin
19+
20+ conf AuthPluginConfig
2021 state * AuthPluginState
21- // Logger instance for this plugin.
22- logger * log.Logger
2322}
2423
2524// AuthPluginConfig represents the configuration for the AuthPlugin.
@@ -33,7 +32,6 @@ type AuthPluginConfig struct {
3332 SessionExpiration int `json:"session_expiration"`
3433}
3534
36- // session represents an authenticated session.
3735type session struct {
3836 Username string
3937 Expiry time.Time
@@ -45,28 +43,24 @@ type AuthPluginState struct {
4543 mu sync.RWMutex
4644}
4745
48- // Name returns the name of the plugin.
4946func (p * AuthPlugin ) Name () string {
5047 return "AuthPlugin"
5148}
5249
53- // OnInit is called once during the global plugin initialization.
5450func (p * AuthPlugin ) OnInit () error {
55- // No global setup needed here for now.
5651 return nil
5752}
5853
59- // OnInitForSite is called for each site configuration.
60- func (p * AuthPlugin ) OnInitForSite (conf config.SiteConfig , logger * log.Logger ) error {
61- p .logger = logger
54+ func (p * AuthPlugin ) OnInitForSite (conf config.SiteConfig , domainLogger * log.Logger ) error {
55+ if err := p .SetupLoggers (conf , p .Name (), domainLogger ); err != nil {
56+ return err
57+ }
6258 p .state = & AuthPluginState {
6359 sessions : make (map [string ]session ),
6460 }
6561
66- // Try to parse this plugin's config if present.
6762 pluginConfigRaw , ok := conf .PluginConfigs [p .Name ()]
6863 if ! ok {
69- // If there's no AuthPlugin config, just do nothing.
7064 return nil
7165 }
7266
@@ -100,7 +94,7 @@ func (p *AuthPlugin) OnInitForSite(conf config.SiteConfig, logger *log.Logger) e
10094
10195 // Validate session expiration
10296 if authConfig .SessionExpiration > 86400 {
103- return errors .New ("session_expiration cannot exceed 86400 seconds (24 hours )" )
97+ return errors .New ("session_expiration cannot exceed 86400 seconds (24h )" )
10498 }
10599 if authConfig .SessionExpiration < - 1 {
106100 return errors .New ("session_expiration cannot be less than -1" )
@@ -110,28 +104,22 @@ func (p *AuthPlugin) OnInitForSite(conf config.SiteConfig, logger *log.Logger) e
110104
111105 // Initialization of the plugin state with optional session cleanup.
112106 if p .conf .SessionExpiration != - 1 {
113- go p .state .cleanupExpiredSessions (time .Minute , logger )
107+ go p .state .cleanupExpiredSessions (time .Minute , p . DomainLogger )
114108 }
115109
116- logger . Infof ("Initializing AuthPlugin for domain: %s with session_expiration: %d" ,
117- conf .Domain , authConfig .SessionExpiration )
110+ p . DomainLogger . Infof ("[ AuthPlugin] Initialized for domain= %s with session_expiration= %d" ,
111+ conf .Domain , p . conf .SessionExpiration )
118112
119113 return nil
120114}
121115
122- // BeforeRequest is invoked before serving each request.
123- func (p * AuthPlugin ) BeforeRequest (r * http.Request ) {
124- // No specific pre-processing is needed here; logic is in HandleRequest.
125- }
116+ func (p * AuthPlugin ) BeforeRequest (r * http.Request ) {}
126117
127- // HandleRequest can fully handle the request, returning true if it does so.
128118func (p * AuthPlugin ) HandleRequest (w http.ResponseWriter , r * http.Request ) bool {
129- // If there's no plugin config, do nothing.
130119 if p .conf .Credentials == nil {
131120 return false
132121 }
133122
134- // Check if the path is protected.
135123 protected := false
136124 for _ , path := range p .conf .ProtectedPaths {
137125 if strings .HasPrefix (r .URL .Path , path ) {
@@ -147,8 +135,8 @@ func (p *AuthPlugin) HandleRequest(w http.ResponseWriter, r *http.Request) bool
147135 // The path is protected, check session or credentials.
148136 ip := getClientIP (r )
149137 if sess , exists := p .state .getSession (ip ); exists {
150- p .logger .Infof ("Session valid for IP: %s, user: %s" , ip , sess .Username )
151- return false // Let the next handler continue.
138+ p .DomainLogger .Infof ("[AuthPlugin] Valid session for IP=%s user= %s" , ip , sess .Username )
139+ return false
152140 }
153141
154142 // No valid session, check for Authorization header.
@@ -173,21 +161,15 @@ func (p *AuthPlugin) HandleRequest(w http.ResponseWriter, r *http.Request) bool
173161 }
174162
175163 // Create a new session
176- p .state .createSession (ip , username , p .conf .SessionExpiration , p .logger )
177- p .logger .Infof ("Authenticated IP: %s, user: %s" , ip , username )
164+ p .state .createSession (ip , username , p .conf .SessionExpiration , p .PluginLogger )
165+ p .PluginLogger .Infof ("[AuthPlugin] Authenticated IP=%s user= %s" , ip , username )
178166
179- // Return false to continue normal flow.
180167 return false
181168}
182169
183- // AfterRequest is invoked after the request has been served or handled.
184- func (p * AuthPlugin ) AfterRequest (w http.ResponseWriter , r * http.Request ) {
185- // Nothing to do here in this plugin.
186- }
170+ func (p * AuthPlugin ) AfterRequest (w http.ResponseWriter , r * http.Request ) {}
187171
188- // OnExit is called when the server is shutting down.
189172func (p * AuthPlugin ) OnExit () error {
190- // No cleanup needed, but you could stop session cleanup goroutines if needed.
191173 return nil
192174}
193175
@@ -200,6 +182,7 @@ func getClientIP(r *http.Request) string {
200182 // X-Forwarded-For may contain multiple IPs, take the first one
201183 return strings .Split (ips , "," )[0 ]
202184 }
185+
203186 // Fallback to RemoteAddr
204187 ip := r .RemoteAddr
205188 if colonIndex := strings .LastIndex (ip , ":" ); colonIndex != - 1 {
@@ -263,9 +246,9 @@ func (s *AuthPluginState) createSession(ip, username string, expiration int, log
263246 Expiry : expiry ,
264247 }
265248 if expiration != - 1 {
266- logger .Infof ("Session created for IP: %s, user: %s, expires at: %v" , ip , username , expiry )
249+ logger .Infof ("[AuthPlugin] Created session IP=%s user=%s expires= %v" , ip , username , expiry )
267250 } else {
268- logger .Infof ("Session created for IP: %s, user: %s, never expires" , ip , username )
251+ logger .Infof ("[AuthPlugin] Created session IP=%s user=%s never expires" , ip , username )
269252 }
270253}
271254
@@ -278,7 +261,7 @@ func (s *AuthPluginState) cleanupExpiredSessions(interval time.Duration, logger
278261 for ip , sess := range s .sessions {
279262 if ! sess .Expiry .IsZero () && sess .Expiry .Before (time .Now ()) {
280263 delete (s .sessions , ip )
281- logger .Infof ("Session expired and removed for IP: %s, user: %s" , ip , sess .Username )
264+ logger .Infof ("[AuthPlugin] Session expired removed IP=%s user= %s" , ip , sess .Username )
282265 }
283266 }
284267 s .mu .Unlock ()
0 commit comments