johnandersen777.bsky.social/socialweb-computer-console
console.socialweb.computer: kcp, kubectl and ssh compiled to wasm in a browser tab with a terminal; a kcp fork
0
0
/*
Copyright 2022 The kcp Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package filters
import (
"net/http"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apiserver/pkg/authentication/authenticator"
authenticatorunion "k8s.io/apiserver/pkg/authentication/request/union"
genericapifilters "k8s.io/apiserver/pkg/endpoints/filters"
"k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/klog/v2"
"github.com/kcp-dev/kcp/pkg/authentication"
)
// WithOptionalAuthentication creates a handler that authenticates a request
// if a ClientCert is presented but passes through to the next handler if one is
// not. It will also call the authenticator present in the request context, if
// any, to support per-workspace authentication.
// When additionalAuthMethods is true we also attempt to authenticate even when
// no client cert is detected in the request.
func WithOptionalAuthentication(handler, failed http.Handler, auth authenticator.Request, additionalAuthMethods bool) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// Do not accidentally modify the variables in the scope of WithOptionalAuthentication().
var (
authenticator = auth
additionalAuth = additionalAuthMethods
)
// If an authenticator for the workspace exists,
// it offers an alternative way to authenticate.
reqAuthenticator, ok := authentication.WorkspaceAuthenticatorFrom(req.Context())
if ok {
if auth != nil {
authenticator = authenticatorunion.New(auth, reqAuthenticator)
} else {
authenticator = reqAuthenticator
}
additionalAuth = true
}
if authenticator == nil {
handler.ServeHTTP(w, req)
return
}
if (req.TLS == nil || len(req.TLS.PeerCertificates) == 0) && !additionalAuth {
handler.ServeHTTP(w, req)
return
}
resp, ok, err := authenticator.AuthenticateRequest(req)
if err != nil || !ok {
if err != nil {
logger := klog.FromContext(req.Context())
logger.Error(err, "Unable to authenticate the request")
}
failed.ServeHTTP(w, req)
return
}
req = req.WithContext(request.WithUser(req.Context(), resp.User))
handler.ServeHTTP(w, req)
})
}
func NewUnauthorizedHandler() http.Handler {
scheme := runtime.NewScheme()
metav1.AddToGroupVersion(scheme, schema.GroupVersion{Group: "", Version: "v1"})
codecs := serializer.NewCodecFactory(scheme)
return genericapifilters.Unauthorized(codecs)
}