summaryrefslogtreecommitdiff
path: root/pkg/web
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-04-21 13:06:56 -0600
committermo khan <mo@mokhan.ca>2025-04-21 13:06:56 -0600
commit1ece3b42051d26050cd612a3ed9a20122d501746 (patch)
tree1e873073ac585efc610fa5e734f3eeeaaa69b01e /pkg/web
parentf157746e34f62621d85b2cbda982b90d9af06125 (diff)
feat: attach current user if they are in the db
Diffstat (limited to 'pkg/web')
-rw-r--r--pkg/web/middleware/user.go24
-rw-r--r--pkg/web/middleware/user_test.go24
2 files changed, 43 insertions, 5 deletions
diff --git a/pkg/web/middleware/user.go b/pkg/web/middleware/user.go
index 9dc1a1f..b01ae48 100644
--- a/pkg/web/middleware/user.go
+++ b/pkg/web/middleware/user.go
@@ -1,11 +1,29 @@
package middleware
-import "net/http"
+import (
+ "net/http"
-func User() func(http.Handler) http.Handler {
+ "github.com/xlgmokha/x/pkg/x"
+ "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/db"
+ "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/domain"
+ "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/key"
+)
+
+func User(db db.Repository[*domain.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)
+ idToken := key.IDToken.From(r.Context())
+ if x.IsZero(idToken) {
+ next.ServeHTTP(w, r)
+ return
+ }
+
+ user := db.Find(idToken.Subject)
+ if x.IsZero(user) {
+ next.ServeHTTP(w, r)
+ } else {
+ next.ServeHTTP(w, r.WithContext(key.CurrentUser.With(r.Context(), user)))
+ }
})
}
}
diff --git a/pkg/web/middleware/user_test.go b/pkg/web/middleware/user_test.go
index 7119b41..cde7dec 100644
--- a/pkg/web/middleware/user_test.go
+++ b/pkg/web/middleware/user_test.go
@@ -6,16 +6,36 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
+ "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/db"
+ "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/domain"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/key"
+ "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/oidc"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/test"
)
func TestUser(t *testing.T) {
- middleware := User()
+ repository := db.NewRepository[*domain.User]()
+ middleware := User(repository)
+
+ knownUser := &domain.User{ID: "1"}
+ require.NoError(t, repository.Save(knownUser))
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 found in the db", func(t *testing.T) {
+ server := middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ user := key.CurrentUser.From(r.Context())
+ require.NotNil(t, user)
+
+ w.WriteHeader(http.StatusTeapot)
+ }))
+
+ idToken := &oidc.IDToken{Subject: knownUser.ID}
+ ctx := key.IDToken.With(t.Context(), idToken)
+
+ r, w := test.RequestResponse("GET", "/example", test.WithContext(ctx))
+ server.ServeHTTP(w, r)
+ assert.Equal(t, http.StatusTeapot, w.Code)
})
t.Run("when the user is not found in the db", func(t *testing.T) {