-
Notifications
You must be signed in to change notification settings - Fork 54
/
authentication_middleware.go
73 lines (59 loc) · 2.05 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
package handlers
import (
"net/http"
"code.cloudfoundry.org/korifi/api/apierrors"
"code.cloudfoundry.org/korifi/api/authorization"
"code.cloudfoundry.org/korifi/api/correlation"
ctrl "sigs.k8s.io/controller-runtime"
"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(authInfoParser AuthInfoParser, identityProvider IdentityProvider) *AuthenticationMiddleware {
return &AuthenticationMiddleware{
logger: ctrl.Log.WithName("AuthenticationMiddleware"),
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
}
ctx := r.Context()
logger := correlation.AddCorrelationIDToLogger(ctx, a.logger)
authInfo, err := a.authInfoParser.Parse(r.Header.Get(headers.Authorization))
if err != nil {
logger.Info("failed to parse auth info", "err", err)
presentError(logger, w, err)
return
}
r = r.WithContext(authorization.NewContext(r.Context(), &authInfo))
_, err = a.identityProvider.GetIdentity(r.Context(), authInfo)
if err != nil {
presentError(logger, w, apierrors.LogAndReturn(logger, err, "failed to get identity"))
return
}
next.ServeHTTP(w, r)
})
}
func (a *AuthenticationMiddleware) isUnauthenticatedEndpoint(p string) bool {
_, authNotRequired := a.unauthenticatedEndpoints[p]
return authNotRequired
}