Debian Codegraph @

pkg/authentication/request/union/union.go

raw
1 /*
2 Copyright 2014 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package union
18
19 import (
20 "net/http"
21
22 utilerrors "k8s.io/apimachinery/pkg/util/errors"
23 "k8s.io/apiserver/pkg/authentication/authenticator"
24 )
25
26 // unionAuthRequestHandler authenticates requests using a chain of authenticator.Requests
27 type unionAuthRequestHandler struct {
28 // Handlers is a chain of request authenticators to delegate to
29 Handlers []authenticator.Request
30 // FailOnError determines whether an error returns short-circuits the chain
31 FailOnError bool
32 }
33
34 // New returns a request authenticator that validates credentials using a chain of authenticator.Request objects.
35 // The entire chain is tried until one succeeds. If all fail, an aggregate error is returned.
36 func New(authRequestHandlers ...authenticator.Request) authenticator.Request {
37 if len(authRequestHandlers) == 1 {
38 return authRequestHandlers[0]
39 }
40 return &unionAuthRequestHandler{Handlers: authRequestHandlers, FailOnError: false}
41 }
42
43 // NewFailOnError returns a request authenticator that validates credentials using a chain of authenticator.Request objects.
44 // The first error short-circuits the chain.
45 func NewFailOnError(authRequestHandlers ...authenticator.Request) authenticator.Request {
46 if len(authRequestHandlers) == 1 {
47 return authRequestHandlers[0]
48 }
49 return &unionAuthRequestHandler{Handlers: authRequestHandlers, FailOnError: true}
50 }
51
52 // AuthenticateRequest authenticates the request using a chain of authenticator.Request objects.
53 func (authHandler *unionAuthRequestHandler) AuthenticateRequest(req *http.Request) (*authenticator.Response, bool, error) {
54 var errlist []error
55 for _, currAuthRequestHandler := range authHandler.Handlers {
56 resp, ok, err := currAuthRequestHandler.AuthenticateRequest(req)
57 if err != nil {
58 if authHandler.FailOnError {
59 return resp, ok, err
60 }
61 errlist = append(errlist, err)
62 continue
63 }
64
65 if ok {
66 return resp, ok, err
67 }
68 }
69
70 return nil, false, utilerrors.NewAggregate(errlist)
71 }