mirror of
https://github.com/niusmallnan/steve.git
synced 2025-07-01 17:01:54 +00:00
Adding user authentication failed context value
This commit is contained in:
parent
dbf9ef88ce
commit
7b5a48f871
@ -33,6 +33,8 @@ var ExistingContext = ToMiddleware(AuthenticatorFunc(func(req *http.Request) (us
|
|||||||
return user, ok, nil
|
return user, ok, nil
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
const CattleAuthFailed = "X-API-Cattle-Auth-Failed"
|
||||||
|
|
||||||
type Authenticator interface {
|
type Authenticator interface {
|
||||||
Authenticate(req *http.Request) (user.Info, bool, error)
|
Authenticate(req *http.Request) (user.Info, bool, error)
|
||||||
}
|
}
|
||||||
@ -144,6 +146,7 @@ func ToMiddleware(auth Authenticator) Middleware {
|
|||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||||
info, ok, err := auth.Authenticate(req)
|
info, ok, err := auth.Authenticate(req)
|
||||||
|
ctx := req.Context()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
info = &user.DefaultInfo{
|
info = &user.DefaultInfo{
|
||||||
Name: "system:cattle:error",
|
Name: "system:cattle:error",
|
||||||
@ -153,6 +156,7 @@ func ToMiddleware(auth Authenticator) Middleware {
|
|||||||
"system:cattle:error",
|
"system:cattle:error",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
ctx = request.WithValue(ctx, CattleAuthFailed, "true")
|
||||||
} else if !ok {
|
} else if !ok {
|
||||||
info = &user.DefaultInfo{
|
info = &user.DefaultInfo{
|
||||||
Name: "system:unauthenticated",
|
Name: "system:unauthenticated",
|
||||||
@ -162,8 +166,8 @@ func ToMiddleware(auth Authenticator) Middleware {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ctx = request.WithUser(ctx, info)
|
||||||
|
|
||||||
ctx := request.WithUser(req.Context(), info)
|
|
||||||
req = req.WithContext(ctx)
|
req = req.WithContext(ctx)
|
||||||
next.ServeHTTP(rw, req)
|
next.ServeHTTP(rw, req)
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user