diff options
| author | mo khan <mo@mokhan.ca> | 2025-04-21 13:06:56 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-04-21 13:06:56 -0600 |
| commit | 1ece3b42051d26050cd612a3ed9a20122d501746 (patch) | |
| tree | 1e873073ac585efc610fa5e734f3eeeaaa69b01e /pkg/web/middleware/user.go | |
| parent | f157746e34f62621d85b2cbda982b90d9af06125 (diff) | |
feat: attach current user if they are in the db
Diffstat (limited to 'pkg/web/middleware/user.go')
| -rw-r--r-- | pkg/web/middleware/user.go | 24 |
1 files changed, 21 insertions, 3 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))) + } }) } } |
