/
headers.go
34 lines (30 loc) · 913 Bytes
/
headers.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
package middleware
import (
"context"
"net/http"
"github.com/RedHatInsights/chrome-service-backend/rest/util"
"github.com/sirupsen/logrus"
)
func ParseHeaders(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
header := r.Header.Get(util.XRHIDENTITY)
ctx := r.Context()
if header == "" {
errString := "Missing authentication"
w.WriteHeader(http.StatusForbidden)
w.Write([]byte(errString))
logrus.Errorf("missing the %s header", util.XRHIDENTITY)
return
} else {
identity, err := util.ParseXRHIdentityHeader(header)
if err != nil {
logrus.Errorln("Error parsing X-RH-IDENTITY header: ", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Internal server error"))
return
}
ctx = context.WithValue(ctx, util.IDENTITY_CTX_KEY, identity)
}
next.ServeHTTP(w, r.WithContext(ctx))
})
}