From 52b4d182363bcd6c60b51af1207b99a4c1aa451a Mon Sep 17 00:00:00 2001 From: Angel Beltran Date: Sat, 24 Jan 2026 12:20:11 -0700 Subject: [PATCH] auth clean up and redirect fixes --- internal/server/api/templates/router.go | 10 +++++++++ internal/server/middleware/auth.go | 25 ++++++++++------------- internal/server/response/write.go | 3 ++- internal/server/server.go | 27 +++++-------------------- 4 files changed, 28 insertions(+), 37 deletions(-) diff --git a/internal/server/api/templates/router.go b/internal/server/api/templates/router.go index 9a24705..3095213 100644 --- a/internal/server/api/templates/router.go +++ b/internal/server/api/templates/router.go @@ -24,6 +24,7 @@ import ( type ( webpageRouter struct { log *logging.Logger + uiPath string contentDir string templater *templater.Templater rawEvents *raw_events.Store @@ -41,6 +42,7 @@ type ( func SetupRoutes( logger *logging.Logger, r gin.IRoutes, + uiPath string, contentDir string, rawEvents *raw_events.Store, accts *accounts.Store, @@ -50,6 +52,7 @@ func SetupRoutes( s := &webpageRouter{ log: logger, + uiPath: uiPath, contentDir: contentDir, // TODO: clean up the references to the templates dir throughout as well (should only be referred to in the 'main' package templater: new(templater.Templater).With(templater.Config{ @@ -101,6 +104,12 @@ func SetupRoutes( // GET / // compiles the page template or component template matching the url func (s *webpageRouter) serveTemplate(c *gin.Context) (response.Response, error) { + // trim the url path prefix before loading the templates + c.Request.URL.Path = c.Request.URL.Path[len(s.uiPath):] + defer func() { + c.Request.URL.Path = s.uiPath + c.Request.URL.Path + }() + r := c.Request ctx := r.Context() @@ -222,6 +231,7 @@ func authorizeByMatchingAccountID(r *http.Request, acctIDPathPosition int) error return nil } +// TODO: is this error type even needed anymore? func (e ErrTemplateNotFound) Error() string { if e.err != nil { return fmt.Sprintf("template not found: %v", e.err) diff --git a/internal/server/middleware/auth.go b/internal/server/middleware/auth.go index ed2ba32..bcb75af 100644 --- a/internal/server/middleware/auth.go +++ b/internal/server/middleware/auth.go @@ -20,10 +20,9 @@ import ( type ( Auth struct { - log *logging.Logger - auth *authentication.Authenticator - newLoginURL LoginURLProviderFunc - accts *accounts.Store + log *logging.Logger + auth *authentication.Authenticator + accts *accounts.Store } Identity struct { @@ -41,26 +40,24 @@ type ( func NewAuth( logger *logging.Logger, auth *authentication.Authenticator, - newLoginURL LoginURLProviderFunc, accts *accounts.Store, ) *Auth { return &Auth{ - log: logger, - auth: auth, - newLoginURL: newLoginURL, - accts: accts, + log: logger, + auth: auth, + accts: accts, } } -// AddIdentityToRequest will add an Identity to the context that can then be retrieved via GetIdentity. -func (a *Auth) AddIdentityToRequest(c *gin.Context) { - if err := a.addIdentityToRequest(c); err != nil { +// AddIdentity will add an Identity to the context that can then be retrieved via GetIdentity. +func (a *Auth) AddIdentity(c *gin.Context) { + if err := a.addIdentity(c); err != nil { c.Error(err) c.Abort() } } -func (a *Auth) addIdentityToRequest(c *gin.Context) error { +func (a *Auth) addIdentity(c *gin.Context) error { r := c.Request ck, err := r.Cookie("access_token") @@ -101,7 +98,7 @@ func (a *Auth) addIdentityToRequest(c *gin.Context) error { return nil } -// Authenticate should only be used along with and after AddIdentityToRequest +// Authenticate should only be used along with and after AddIdentity // Typically used with response.Handler to make a gin.HandlerFunc. func (a *Auth) Authenticate(assertions ...AuthorizationAssertions) func(c *gin.Context) (response.Response, error) { return func(c *gin.Context) (response.Response, error) { diff --git a/internal/server/response/write.go b/internal/server/response/write.go index 66039d5..98224c8 100644 --- a/internal/server/response/write.go +++ b/internal/server/response/write.go @@ -81,7 +81,8 @@ func writeResponse(c *gin.Context, res Response) { } if code, to, ok := res.GetRedirect(); ok { - http.Redirect(w, c.Request, to, code.Int()) + c.Redirect(code.Int(), to) + c.Abort() return } diff --git a/internal/server/server.go b/internal/server/server.go index ddb7fe0..b4c7a49 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -51,7 +51,7 @@ func NewRouter( authMiddleware := middleware.NewAuth( logger.WithGroup("auth-middleware"), auth, - auth_api.NewLoginURL, + //auth_api.NewLoginURL, accts, ) @@ -81,9 +81,9 @@ func NewRouter( logger.WithGroup("templates"), r.Group( "/ui", - stripPrefix("/ui"), - authMiddleware.AddIdentityToRequest, + authMiddleware.AddIdentity, ), + "/ui", contentDir, rawEvents, accts, @@ -113,7 +113,7 @@ func NewRouter( sse_api.Routes( api.Group( "/events", - authMiddleware.AddIdentityToRequest, + authMiddleware.AddIdentity, response.Handler(authMiddleware.Authenticate()), ), apiLogger.WithGroup("/events"), @@ -122,7 +122,7 @@ func NewRouter( accounts_api.Routes( api.Group( "/accounts", - authMiddleware.AddIdentityToRequest, + authMiddleware.AddIdentity, response.Handler(authMiddleware.Authenticate()), ), apiLogger.WithGroup("/accounts"), @@ -165,22 +165,5 @@ func fileServer(urlPrefix, dir string, beforeServe func(c *gin.Context)) gin.Han scfs.ServeHTTP(c.Writer, r) c.Abort() } - - // TODO: need a c.Next()? - } -} - -func stripPrefix(prefix string) gin.HandlerFunc { - return func(c *gin.Context) { - if !strings.HasPrefix(c.Request.URL.Path, prefix) { - return - } - - c.Request.URL.Path = c.Request.URL.Path[len(prefix):] - defer func() { - c.Request.URL.Path = prefix + c.Request.URL.Path - }() - - c.Next() } }