Skip to content

Commit dfd6e8e

Browse files
committed
refactor: use better ignore paths in context middleware
1 parent 08e6b84 commit dfd6e8e

2 files changed

Lines changed: 31 additions & 7 deletions

File tree

internal/middleware/context_middleware.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package middleware
22

33
import (
4-
"slices"
54
"strings"
65
"time"
76

@@ -13,7 +12,24 @@ import (
1312
"github.com/gin-gonic/gin"
1413
)
1514

16-
var OIDCIgnorePaths = []string{"/api/oidc/token", "/api/oidc/userinfo"}
15+
// Gin won't let us set a middleware on a specific route (at least it doesn't work,
16+
// see https://github.com/gin-gonic/gin/issues/531) so we have to do some hackery
17+
var (
18+
contextSkipPathsPrefix = []string{
19+
"GET /api/context/app",
20+
"GET /api/healthz",
21+
"HEAD /api/healthz",
22+
"GET /api/oauth/url",
23+
"GET /api/oauth/callback",
24+
"GET /api/oidc/clients",
25+
"POST /api/oidc/token",
26+
"GET /api/oidc/userinfo",
27+
"GET /resources",
28+
"POST /api/user/login",
29+
"GET /.well-known/openid-configuration",
30+
"GET /.well-known/jwks.json",
31+
}
32+
)
1733

1834
type ContextMiddlewareConfig struct {
1935
CookieDomain string
@@ -39,9 +55,7 @@ func (m *ContextMiddleware) Init() error {
3955

4056
func (m *ContextMiddleware) Middleware() gin.HandlerFunc {
4157
return func(c *gin.Context) {
42-
// There is no point in trying to get credentials if it's an OIDC endpoint
43-
path := c.Request.URL.Path
44-
if slices.Contains(OIDCIgnorePaths, strings.TrimSuffix(path, "/")) {
58+
if m.isIgnorePath(c.Request.Method + " " + c.Request.URL.Path) {
4559
c.Next()
4660
return
4761
}
@@ -224,3 +238,12 @@ func (m *ContextMiddleware) Middleware() gin.HandlerFunc {
224238
c.Next()
225239
}
226240
}
241+
242+
func (m *ContextMiddleware) isIgnorePath(path string) bool {
243+
for _, prefix := range contextSkipPathsPrefix {
244+
if strings.HasPrefix(path, prefix) {
245+
return true
246+
}
247+
}
248+
return false
249+
}

internal/middleware/zerolog_middleware.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import (
88
"github.com/steveiliop56/tinyauth/internal/utils/tlog"
99
)
1010

11+
// See context middleware for explanation of why we have to do this
1112
var (
1213
loggerSkipPathsPrefix = []string{
13-
"GET /api/health",
14-
"HEAD /api/health",
14+
"GET /api/healthz",
15+
"HEAD /api/healthz",
1516
"GET /favicon.ico",
1617
}
1718
)

0 commit comments

Comments
 (0)