summaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-04-21 12:37:29 -0600
committermo khan <mo@mokhan.ca>2025-04-21 12:37:29 -0600
commitf157746e34f62621d85b2cbda982b90d9af06125 (patch)
tree83a816c8e2e605350f3ce2ba95184b28c05a21d0 /pkg
parentab8075f02f50d8bd0be3c23b87e63f10828528ed (diff)
feat: start to build middleware to attach the current user
Diffstat (limited to 'pkg')
-rw-r--r--pkg/web/middleware/user.go11
-rw-r--r--pkg/web/middleware/user_test.go39
2 files changed, 50 insertions, 0 deletions
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)
+ })
+}