Debian Codegraph @

pkg/authentication/authenticator/audiences.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 "context"
20
21 // Audiences is a container for the Audiences of a token.
22 type Audiences []string
23
24 // The key type is unexported to prevent collisions
25 type key int
26
27 const (
28 // audiencesKey is the context key for request audiences.
29 audiencesKey key = iota
30 )
31
32 // WithAudiences returns a context that stores a request's expected audiences.
33 func WithAudiences(ctx context.Context, auds Audiences) context.Context {
34 return context.WithValue(ctx, audiencesKey, auds)
35 }
36
37 // AudiencesFrom returns a request's expected audiences stored in the request context.
38 func AudiencesFrom(ctx context.Context) (Audiences, bool) {
39 auds, ok := ctx.Value(audiencesKey).(Audiences)
40 return auds, ok
41 }
42
43 // Has checks if Audiences contains a specific audiences.
44 func (a Audiences) Has(taud string) bool {
45 for _, aud := range a {
46 if aud == taud {
47 return true
48 }
49 }
50 return false
51 }
52
53 // Intersect intersects Audiences with a target Audiences and returns all
54 // elements in both.
55 func (a Audiences) Intersect(tauds Audiences) Audiences {
56 selected := Audiences{}
57 for _, taud := range tauds {
58 if a.Has(taud) {
59 selected = append(selected, taud)
60 }
61 }
62 return selected
63 }