From f157746e34f62621d85b2cbda982b90d9af06125 Mon Sep 17 00:00:00 2001 From: mo khan Date: Mon, 21 Apr 2025 12:37:29 -0600 Subject: feat: start to build middleware to attach the current user --- pkg/web/middleware/user.go | 11 +++++++++++ pkg/web/middleware/user_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 pkg/web/middleware/user.go create mode 100644 pkg/web/middleware/user_test.go diff --git a/pkg/web/middleware/user.go b/pkg/web/middleware/user.go new file mode 100644 index 0000000..9dc1a1f --- /dev/null +++ b/pkg/web/middleware/user.go @@ -0,0 +1,11 @@ +package middleware + +import "net/http" + +func User() func(http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + next.ServeHTTP(w, r) + }) + } +} diff --git a/pkg/web/middleware/user_test.go b/pkg/web/middleware/user_test.go new file mode 100644 index 0000000..7119b41 --- /dev/null +++ b/pkg/web/middleware/user_test.go @@ -0,0 +1,39 @@ +package middleware + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/key" + "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/test" +) + +func TestUser(t *testing.T) { + middleware := User() + + t.Run("when an ID Token is found in the context", func(t *testing.T) { + t.Run("When the user is found in the db", func(t *testing.T) { + + }) + + t.Run("when the user is not found in the db", func(t *testing.T) { + + }) + }) + + t.Run("when an ID Token is not found in the context", func(t *testing.T) { + server := middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + user := key.CurrentUser.From(r.Context()) + require.Nil(t, user) + + w.WriteHeader(http.StatusTeapot) + })) + + r, w := test.RequestResponse("GET", "/example") + server.ServeHTTP(w, r) + + assert.Equal(t, http.StatusTeapot, w.Code) + }) +} -- cgit v1.2.3