/
audience.go
165 lines (132 loc) · 4.15 KB
/
audience.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2019 Aporeto Inc.
// 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 tokenutils
import (
"fmt"
"strings"
"go.aporeto.io/elemental"
)
// AudienceAny represents any operation, identity or namespace.
const AudienceAny = "*"
// UnsecureAudience extracts the audience list from a token string without
// verifying its validity. Only use or trust this after proper validation.
func UnsecureAudience(token string, modelManager elemental.ModelManager) (AudiencesList, error) {
claims, err := UnsecureClaimsMap(token)
if err != nil {
return nil, fmt.Errorf("unable to extract audience: %s", err)
}
audStr, ok := claims["aud"].(string)
if !ok || audStr == "" {
return nil, nil
}
return ParseAudience(audStr, modelManager)
}
// ParseAudience parses the audience string and returns an AudiencesList.
func ParseAudience(audString string, modelManager elemental.ModelManager) (AudiencesList, error) {
// TODO: In order to not invalidate all
// currently issued tokens, if the audience
// doesn't start with the correct prefix
// we assume there is no audience.
// This must be removed after a little while.
if !strings.HasPrefix(audString, "aud:") {
return nil, nil
}
auds := strings.Split(audString, ";")
out := make(AudiencesList, len(auds))
var parts []string
var err error
for i, a := range auds {
parts = strings.Split(a, ":")
if len(parts) != 4 {
return nil, fmt.Errorf("invalid audience '%s': invalid number of parts", a)
}
// Validate operation
ops := strings.Split(parts[1], ",")
for _, o := range ops {
if o == AudienceAny {
continue
}
if _, err = elemental.ParseOperation(o); err != nil {
return nil, fmt.Errorf("invalid audience '%s': %s", a, err)
}
}
// Validate identity
idents := strings.Split(parts[2], ",")
for _, ident := range idents {
if ident == AudienceAny {
continue
}
if modelManager.IdentityFromCategory(ident).IsEmpty() {
return nil, fmt.Errorf("invalid audience '%s': invalid identity '%s'", a, ident)
}
}
out[i] = Audience{
Operations: ops,
Identities: idents,
Namespaces: strings.Split(parts[3], ","),
}
}
return out, nil
}
// AudiencesList is a list of audiences.
type AudiencesList []Audience
// Verify verifies at least one audience in the list is valid for the given operation, identity and namespace.
func (a AudiencesList) Verify(operation elemental.Operation, identity elemental.Identity, namespace string) bool {
for _, item := range a {
if item.Verify(operation, identity, namespace) {
return true
}
}
return false
}
func (a AudiencesList) String() string {
parts := make([]string, len(a))
for i, item := range a {
parts[i] = item.String()
}
return strings.Join(parts, ";")
}
// Audience represents a parsed audience string.
type Audience struct {
Operations []string
Identities []string
Namespaces []string
}
// Verify verifies the audience is valid for the given operation, identity and namespace.
func (a Audience) Verify(operation elemental.Operation, identity elemental.Identity, namespace string) bool {
var operationOK, identityOK, namespaceOK bool
for _, o := range a.Operations {
if o == AudienceAny || o == string(operation) {
operationOK = true
break
}
}
for _, i := range a.Identities {
if i == AudienceAny || i == identity.Category {
identityOK = true
break
}
}
for _, n := range a.Namespaces {
if n == AudienceAny || n == namespace {
namespaceOK = true
break
}
}
return operationOK && identityOK && namespaceOK
}
func (a Audience) String() string {
return fmt.Sprintf("aud:%s:%s:%s",
strings.Join(a.Operations, ","),
strings.Join(a.Identities, ","),
strings.Join(a.Namespaces, ","),
)
}