Skip to content

Commit

Permalink
Merge pull request #56 from MbolotSuse/auth
Browse files Browse the repository at this point in the history
Adding user authentication failed context value
  • Loading branch information
cbron authored Aug 5, 2022
2 parents dbf9ef8 + 7b5a48f commit 647cba2
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/auth/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ var ExistingContext = ToMiddleware(AuthenticatorFunc(func(req *http.Request) (us
return user, ok, nil
}))

const CattleAuthFailed = "X-API-Cattle-Auth-Failed"

type Authenticator interface {
Authenticate(req *http.Request) (user.Info, bool, error)
}
Expand Down Expand Up @@ -144,6 +146,7 @@ func ToMiddleware(auth Authenticator) Middleware {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
info, ok, err := auth.Authenticate(req)
ctx := req.Context()
if err != nil {
info = &user.DefaultInfo{
Name: "system:cattle:error",
Expand All @@ -153,6 +156,7 @@ func ToMiddleware(auth Authenticator) Middleware {
"system:cattle:error",
},
}
ctx = request.WithValue(ctx, CattleAuthFailed, "true")
} else if !ok {
info = &user.DefaultInfo{
Name: "system:unauthenticated",
Expand All @@ -162,8 +166,8 @@ func ToMiddleware(auth Authenticator) Middleware {
},
}
}
ctx = request.WithUser(ctx, info)

ctx := request.WithUser(req.Context(), info)
req = req.WithContext(ctx)
next.ServeHTTP(rw, req)
})
Expand Down

0 comments on commit 647cba2

Please sign in to comment.