summaryrefslogtreecommitdiff
path: root/app/middleware/user.go
blob: 90bf6aaceeeb68322968aafaa2c73c10d9acb03a (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"
)

func User() func(http.Handler) http.Handler {
	parser := UserParser()
	return func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			user := parser(r)
			if x.IsPresent(user) {
				next.ServeHTTP(w, r.WithContext(cfg.CurrentUser.With(r.Context(), user)))
			} else {
				next.ServeHTTP(w, r)
			}
		})
	}
}