auth clean up and redirect fixes

This commit is contained in:
2026-01-24 12:20:11 -07:00
parent e4069e18e2
commit 52b4d18236
4 changed files with 28 additions and 37 deletions
+10
View File
@@ -24,6 +24,7 @@ import (
type ( type (
webpageRouter struct { webpageRouter struct {
log *logging.Logger log *logging.Logger
uiPath string
contentDir string contentDir string
templater *templater.Templater templater *templater.Templater
rawEvents *raw_events.Store rawEvents *raw_events.Store
@@ -41,6 +42,7 @@ type (
func SetupRoutes( func SetupRoutes(
logger *logging.Logger, logger *logging.Logger,
r gin.IRoutes, r gin.IRoutes,
uiPath string,
contentDir string, contentDir string,
rawEvents *raw_events.Store, rawEvents *raw_events.Store,
accts *accounts.Store, accts *accounts.Store,
@@ -50,6 +52,7 @@ func SetupRoutes(
s := &webpageRouter{ s := &webpageRouter{
log: logger, log: logger,
uiPath: uiPath,
contentDir: contentDir, contentDir: contentDir,
// TODO: clean up the references to the templates dir throughout as well (should only be referred to in the 'main' package // 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{ templater: new(templater.Templater).With(templater.Config{
@@ -101,6 +104,12 @@ func SetupRoutes(
// GET / // GET /
// compiles the page template or component template matching the url // compiles the page template or component template matching the url
func (s *webpageRouter) serveTemplate(c *gin.Context) (response.Response, error) { 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 r := c.Request
ctx := r.Context() ctx := r.Context()
@@ -222,6 +231,7 @@ func authorizeByMatchingAccountID(r *http.Request, acctIDPathPosition int) error
return nil return nil
} }
// TODO: is this error type even needed anymore?
func (e ErrTemplateNotFound) Error() string { func (e ErrTemplateNotFound) Error() string {
if e.err != nil { if e.err != nil {
return fmt.Sprintf("template not found: %v", e.err) return fmt.Sprintf("template not found: %v", e.err)
+11 -14
View File
@@ -20,10 +20,9 @@ import (
type ( type (
Auth struct { Auth struct {
log *logging.Logger log *logging.Logger
auth *authentication.Authenticator auth *authentication.Authenticator
newLoginURL LoginURLProviderFunc accts *accounts.Store
accts *accounts.Store
} }
Identity struct { Identity struct {
@@ -41,26 +40,24 @@ type (
func NewAuth( func NewAuth(
logger *logging.Logger, logger *logging.Logger,
auth *authentication.Authenticator, auth *authentication.Authenticator,
newLoginURL LoginURLProviderFunc,
accts *accounts.Store, accts *accounts.Store,
) *Auth { ) *Auth {
return &Auth{ return &Auth{
log: logger, log: logger,
auth: auth, auth: auth,
newLoginURL: newLoginURL, accts: accts,
accts: accts,
} }
} }
// AddIdentityToRequest will add an Identity to the context that can then be retrieved via GetIdentity. // AddIdentity will add an Identity to the context that can then be retrieved via GetIdentity.
func (a *Auth) AddIdentityToRequest(c *gin.Context) { func (a *Auth) AddIdentity(c *gin.Context) {
if err := a.addIdentityToRequest(c); err != nil { if err := a.addIdentity(c); err != nil {
c.Error(err) c.Error(err)
c.Abort() c.Abort()
} }
} }
func (a *Auth) addIdentityToRequest(c *gin.Context) error { func (a *Auth) addIdentity(c *gin.Context) error {
r := c.Request r := c.Request
ck, err := r.Cookie("access_token") ck, err := r.Cookie("access_token")
@@ -101,7 +98,7 @@ func (a *Auth) addIdentityToRequest(c *gin.Context) error {
return nil 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. // Typically used with response.Handler to make a gin.HandlerFunc.
func (a *Auth) Authenticate(assertions ...AuthorizationAssertions) func(c *gin.Context) (response.Response, error) { func (a *Auth) Authenticate(assertions ...AuthorizationAssertions) func(c *gin.Context) (response.Response, error) {
return func(c *gin.Context) (response.Response, error) { return func(c *gin.Context) (response.Response, error) {
+2 -1
View File
@@ -81,7 +81,8 @@ func writeResponse(c *gin.Context, res Response) {
} }
if code, to, ok := res.GetRedirect(); ok { if code, to, ok := res.GetRedirect(); ok {
http.Redirect(w, c.Request, to, code.Int()) c.Redirect(code.Int(), to)
c.Abort()
return return
} }
+5 -22
View File
@@ -51,7 +51,7 @@ func NewRouter(
authMiddleware := middleware.NewAuth( authMiddleware := middleware.NewAuth(
logger.WithGroup("auth-middleware"), logger.WithGroup("auth-middleware"),
auth, auth,
auth_api.NewLoginURL, //auth_api.NewLoginURL,
accts, accts,
) )
@@ -81,9 +81,9 @@ func NewRouter(
logger.WithGroup("templates"), logger.WithGroup("templates"),
r.Group( r.Group(
"/ui", "/ui",
stripPrefix("/ui"), authMiddleware.AddIdentity,
authMiddleware.AddIdentityToRequest,
), ),
"/ui",
contentDir, contentDir,
rawEvents, rawEvents,
accts, accts,
@@ -113,7 +113,7 @@ func NewRouter(
sse_api.Routes( sse_api.Routes(
api.Group( api.Group(
"/events", "/events",
authMiddleware.AddIdentityToRequest, authMiddleware.AddIdentity,
response.Handler(authMiddleware.Authenticate()), response.Handler(authMiddleware.Authenticate()),
), ),
apiLogger.WithGroup("/events"), apiLogger.WithGroup("/events"),
@@ -122,7 +122,7 @@ func NewRouter(
accounts_api.Routes( accounts_api.Routes(
api.Group( api.Group(
"/accounts", "/accounts",
authMiddleware.AddIdentityToRequest, authMiddleware.AddIdentity,
response.Handler(authMiddleware.Authenticate()), response.Handler(authMiddleware.Authenticate()),
), ),
apiLogger.WithGroup("/accounts"), apiLogger.WithGroup("/accounts"),
@@ -165,22 +165,5 @@ func fileServer(urlPrefix, dir string, beforeServe func(c *gin.Context)) gin.Han
scfs.ServeHTTP(c.Writer, r) scfs.ServeHTTP(c.Writer, r)
c.Abort() 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()
} }
} }