Skip to content

Commit 1a219bf

Browse files
authored
feat(middleware): enhance middleware configuration for auth (#67)
1 parent d6915f5 commit 1a219bf

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

middleware/authzMiddlewares.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@ import (
44
"net/http"
55
)
66

7-
// Middleware defines a function that wraps an http.Handler.
8-
type Middleware func(http.Handler) http.Handler
9-
10-
// CreateAuthMiddleware returns a slice of Middleware functions for authentication and authorization.
7+
// CreateAuthMiddleware returns a slice of middleware functions for authentication and authorization.
118
// The returned middlewares are: StoreWebToken, StoreAuthHeader, and StoreSpiffeHeader.
12-
func CreateAuthMiddleware() []Middleware {
13-
return []Middleware{
9+
func CreateAuthMiddleware() []func(http.Handler) http.Handler {
10+
return []func(http.Handler) http.Handler{
1411
StoreWebToken(),
1512
StoreAuthHeader(),
1613
StoreSpiffeHeader(),

middleware/middleware.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@ import (
66
"github.com/platform-mesh/golang-commons/logger"
77
)
88

9-
// attaches a request-scoped logger (using the provided logger), assigns a request ID, and propagates that ID into the logger.
10-
func CreateMiddleware(log *logger.Logger) []func(http.Handler) http.Handler {
11-
return []func(http.Handler) http.Handler{
9+
// CreateMiddleware creates a middleware chain with logging, tracing, and optional authentication.
10+
// It attaches a request-scoped logger (using the provided logger), assigns a request ID, and propagates that ID into the logger.
11+
// When auth is true, authentication middlewares (StoreWebToken, StoreAuthHeader, StoreSpiffeHeader) are included.
12+
func CreateMiddleware(log *logger.Logger, auth bool) []func(http.Handler) http.Handler {
13+
mws := []func(http.Handler) http.Handler{
1214
SetOtelTracingContext(),
1315
SentryRecoverer,
1416
StoreLoggerMiddleware(log),
1517
SetRequestId(),
1618
SetRequestIdInLogger(),
1719
}
20+
21+
if auth {
22+
mws = append(mws, CreateAuthMiddleware()...)
23+
}
24+
return mws
1825
}

middleware/middleware_test.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
"github.com/stretchr/testify/assert"
1010
)
1111

12-
func TestCreateMiddleware(t *testing.T) {
12+
func TestCreateMiddleware_WithoutAuth(t *testing.T) {
1313
log := testlogger.New()
14-
middlewares := CreateMiddleware(log.Logger)
14+
middlewares := CreateMiddleware(log.Logger, false)
1515

16-
// Should return 5 middlewares
16+
// Should return 5 middlewares when auth is false
1717
assert.Len(t, middlewares, 5)
1818

1919
// Each middleware should be a valid function
@@ -39,3 +39,34 @@ func TestCreateMiddleware(t *testing.T) {
3939

4040
assert.Equal(t, http.StatusOK, recorder.Code)
4141
}
42+
43+
func TestCreateMiddleware_WithAuth(t *testing.T) {
44+
log := testlogger.New()
45+
middlewares := CreateMiddleware(log.Logger, true)
46+
47+
// Should return 8 middlewares when auth is true (5 base + 3 auth)
48+
assert.Len(t, middlewares, 8)
49+
50+
// Each middleware should be a valid function
51+
for _, mw := range middlewares {
52+
assert.NotNil(t, mw)
53+
}
54+
55+
// Test that middlewares can be chained
56+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
57+
w.WriteHeader(http.StatusOK)
58+
})
59+
60+
// Apply all middlewares
61+
var finalHandler http.Handler = handler
62+
for i := len(middlewares) - 1; i >= 0; i-- {
63+
finalHandler = middlewares[i](finalHandler)
64+
}
65+
66+
req := httptest.NewRequest("GET", "http://testing", nil)
67+
recorder := httptest.NewRecorder()
68+
69+
finalHandler.ServeHTTP(recorder, req)
70+
71+
assert.Equal(t, http.StatusOK, recorder.Code)
72+
}

0 commit comments

Comments
 (0)