summaryrefslogtreecommitdiff
path: root/app/middleware/user.go
blob: 0916cfabf4b1a25d2e62148a22cc3eafda72aa26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package middleware

import (
	"net/http"

	"github.com/xlgmokha/x/pkg/x"
	"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/cfg"
	"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain"
)

func User(db domain.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) {
			user := UserParser(db)(r)
			if x.IsPresent(user) {
				next.ServeHTTP(w, r.WithContext(cfg.CurrentUser.With(r.Context(), user)))
			} else {
				next.ServeHTTP(w, r)
			}
		})
	}
}