blob: 1e46187b5e11aadfbd36dcccf1c35ca94c16c2aa (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
package middleware
import (
"net/http"
"github.com/xlgmokha/x/pkg/log"
"github.com/xlgmokha/x/pkg/mapper"
"github.com/xlgmokha/x/pkg/x"
"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"
)
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) {
idToken := key.IDToken.From(r.Context())
if x.IsZero(idToken) {
next.ServeHTTP(w, r)
return
}
user := db.Find(domain.ID(idToken.Subject))
if x.IsZero(user) {
user = mapper.MapFrom[*oidc.IDToken, *domain.User](idToken)
if err := db.Save(user); err != nil {
log.WithFields(r.Context(), log.Fields{"error": err})
next.ServeHTTP(w, r)
return
}
}
next.ServeHTTP(w, r.WithContext(key.CurrentUser.With(r.Context(), user)))
})
}
}
|