/
authentication_middleware.go
74 lines (60 loc) · 2.23 KB
/
authentication_middleware.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package apis
import (
"net/http"
"regexp"
"code.cloudfoundry.org/cf-k8s-controllers/api/authorization"
"github.com/go-http-utils/headers"
"github.com/go-logr/logr"
)
type AuthenticationMiddleware struct {
logger logr.Logger
authInfoParser AuthInfoParser
identityProvider IdentityProvider
unauthenticatedEndpoints map[string]interface{}
}
//counterfeiter:generate -o fake -fake-name AuthInfoParser . AuthInfoParser
type AuthInfoParser interface {
Parse(authHeader string) (authorization.Info, error)
}
func NewAuthenticationMiddleware(logger logr.Logger, authInfoParser AuthInfoParser, identityProvider IdentityProvider) *AuthenticationMiddleware {
return &AuthenticationMiddleware{
logger: logger,
authInfoParser: authInfoParser,
identityProvider: identityProvider,
unauthenticatedEndpoints: map[string]interface{}{
"/": struct{}{},
"/v3": struct{}{},
"/api/v1/info": struct{}{},
},
}
}
func (a *AuthenticationMiddleware) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if a.isUnauthenticatedEndpoint(r.URL.Path) {
next.ServeHTTP(w, r)
return
}
authInfo, err := a.authInfoParser.Parse(r.Header.Get(headers.Authorization))
if err != nil {
a.logger.Error(err, "failed to parse auth info")
presentError(w, err)
return
}
r = r.WithContext(authorization.NewContext(r.Context(), &authInfo))
_, err = a.identityProvider.GetIdentity(r.Context(), authInfo)
if err != nil {
a.logger.Error(err, "failed to get identity")
presentError(w, err)
return
}
next.ServeHTTP(w, r)
})
}
// Eventually we will want to authenticate the log-cache read endpoint and go back to using the simple
// unauthenticatedEndpoints map. For now though we need to do a more complicated regexp match against
// the path since the log-cache read endpoint includes an arbitrary guid as part of its path
var logCacheReadEndpointRegexp = regexp.MustCompile(`\/api\/v1\/read\/[0-9a-fA-F\-]*$`)
func (a *AuthenticationMiddleware) isUnauthenticatedEndpoint(p string) bool {
_, authNotRequired := a.unauthenticatedEndpoints[p]
return authNotRequired || logCacheReadEndpointRegexp.MatchString(p)
}