Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding user authentication failed context value #56

Merged
merged 1 commit into from
Aug 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{
Copy link
Contributor

@cbron cbron Aug 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So all this PR does is add a single key/val to the context if the authentication failed. But then below if authentication worked but user is unauthenticated we don't care, because they are using a rancher token ? Nvm, read as unathorized.

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