forked from vmware-archive/atc
/
wrap_handler.go
46 lines (40 loc) · 1.11 KB
/
wrap_handler.go
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
37
38
39
40
41
42
43
44
45
46
package auth
import (
"context"
"net/http"
)
var authenticated = "authenticated"
var teamNameKey = "teamName"
var teamIDKey = "teamID"
var isAdminKey = "isAdmin"
var isSystemKey = "system"
func WrapHandler(
handler http.Handler,
validator Validator,
userContextReader UserContextReader,
) http.Handler {
return authHandler{
handler: handler,
validator: validator,
userContextReader: userContextReader,
}
}
type authHandler struct {
handler http.Handler
validator Validator
userContextReader UserContextReader
}
func (h authHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), authenticated, h.validator.IsAuthenticated(r))
teamName, teamID, isAdmin, found := h.userContextReader.GetTeam(r)
if found {
ctx = context.WithValue(ctx, teamNameKey, teamName)
ctx = context.WithValue(ctx, teamIDKey, teamID)
ctx = context.WithValue(ctx, isAdminKey, isAdmin)
}
isSystem, found := h.userContextReader.GetSystem(r)
if found {
ctx = context.WithValue(ctx, isSystemKey, isSystem)
}
h.handler.ServeHTTP(w, r.WithContext(ctx))
}