| 1 |
/* |
| 2 |
Copyright 2017 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 |
"context" |
| 21 |
|
| 22 |
utilerrors "k8s.io/apimachinery/pkg/util/errors" |
| 23 |
"k8s.io/apiserver/pkg/authentication/authenticator" |
| 24 |
) |
| 25 |
|
| 26 |
// unionAuthTokenHandler authenticates tokens using a chain of authenticator.Token objects |
| 27 |
type unionAuthTokenHandler struct { |
| 28 |
// Handlers is a chain of request authenticators to delegate to |
| 29 |
Handlers []authenticator.Token |
| 30 |
// FailOnError determines whether an error returns short-circuits the chain |
| 31 |
FailOnError bool |
| 32 |
} |
| 33 |
|
| 34 |
// New returns a token authenticator that validates credentials using a chain of authenticator.Token objects. |
| 35 |
// The entire chain is tried until one succeeds. If all fail, an aggregate error is returned. |
| 36 |
func New(authTokenHandlers ...authenticator.Token) authenticator.Token { |
| 37 |
if len(authTokenHandlers) == 1 { |
| 38 |
return authTokenHandlers[0] |
| 39 |
} |
| 40 |
return &unionAuthTokenHandler{Handlers: authTokenHandlers, FailOnError: false} |
| 41 |
} |
| 42 |
|
| 43 |
// NewFailOnError returns a token authenticator that validates credentials using a chain of authenticator.Token objects. |
| 44 |
// The first error short-circuits the chain. |
| 45 |
func NewFailOnError(authTokenHandlers ...authenticator.Token) authenticator.Token { |
| 46 |
if len(authTokenHandlers) == 1 { |
| 47 |
return authTokenHandlers[0] |
| 48 |
} |
| 49 |
return &unionAuthTokenHandler{Handlers: authTokenHandlers, FailOnError: true} |
| 50 |
} |
| 51 |
|
| 52 |
// AuthenticateToken authenticates the token using a chain of authenticator.Token objects. |
| 53 |
func (authHandler *unionAuthTokenHandler) AuthenticateToken(ctx context.Context, token string) (*authenticator.Response, bool, error) { |
| 54 |
var errlist []error |
| 55 |
for _, currAuthRequestHandler := range authHandler.Handlers { |
| 56 |
info, ok, err := currAuthRequestHandler.AuthenticateToken(ctx, token) |
| 57 |
if err != nil { |
| 58 |
if authHandler.FailOnError { |
| 59 |
return info, ok, err |
| 60 |
} |
| 61 |
errlist = append(errlist, err) |
| 62 |
continue |
| 63 |
} |
| 64 |
|
| 65 |
if ok { |
| 66 |
return info, ok, err |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return nil, false, utilerrors.NewAggregate(errlist) |
| 71 |
} |