Debian Codegraph @

pkg/authentication/authenticator/audagnostic.go

raw
1 /*
2 Copyright 2018 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 authenticator
18
19 import (
20 "context"
21 "fmt"
22 "net/http"
23 )
24
25 func authenticate(ctx context.Context, implicitAuds Audiences, authenticate func() (*Response, bool, error)) (*Response, bool, error) {
26 targetAuds, ok := AudiencesFrom(ctx)
27 // We can remove this once api audiences is never empty. That will probably
28 // be N releases after TokenRequest is GA.
29 if !ok {
30 return authenticate()
31 }
32 auds := implicitAuds.Intersect(targetAuds)
33 if len(auds) == 0 {
34 return nil, false, nil
35 }
36 resp, ok, err := authenticate()
37 if err != nil || !ok {
38 return nil, false, err
39 }
40 if len(resp.Audiences) > 0 {
41 // maybe the authenticator was audience aware after all.
42 return nil, false, fmt.Errorf("audience agnostic authenticator wrapped an authenticator that returned audiences: %q", resp.Audiences)
43 }
44 resp.Audiences = auds
45 return resp, true, nil
46 }
47
48 type audAgnosticRequestAuthenticator struct {
49 implicit Audiences
50 delegate Request
51 }
52
53 var _ = Request(&audAgnosticRequestAuthenticator{})
54
55 func (a *audAgnosticRequestAuthenticator) AuthenticateRequest(req *http.Request) (*Response, bool, error) {
56 return authenticate(req.Context(), a.implicit, func() (*Response, bool, error) {
57 return a.delegate.AuthenticateRequest(req)
58 })
59 }
60
61 // WrapAudienceAgnosticRequest wraps an audience agnostic request authenticator
62 // to restrict its accepted audiences to a set of implicit audiences.
63 func WrapAudienceAgnosticRequest(implicit Audiences, delegate Request) Request {
64 return &audAgnosticRequestAuthenticator{
65 implicit: implicit,
66 delegate: delegate,
67 }
68 }
69
70 type audAgnosticTokenAuthenticator struct {
71 implicit Audiences
72 delegate Token
73 }
74
75 var _ = Token(&audAgnosticTokenAuthenticator{})
76
77 func (a *audAgnosticTokenAuthenticator) AuthenticateToken(ctx context.Context, tok string) (*Response, bool, error) {
78 return authenticate(ctx, a.implicit, func() (*Response, bool, error) {
79 return a.delegate.AuthenticateToken(ctx, tok)
80 })
81 }
82
83 // WrapAudienceAgnosticToken wraps an audience agnostic token authenticator to
84 // restrict its accepted audiences to a set of implicit audiences.
85 func WrapAudienceAgnosticToken(implicit Audiences, delegate Token) Token {
86 return &audAgnosticTokenAuthenticator{
87 implicit: implicit,
88 delegate: delegate,
89 }
90 }