/
jwt.go
212 lines (183 loc) · 4.63 KB
/
jwt.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
Package auth implements custom predicates to match based on content
of the HTTP Authorization header.
This predicate can be used to match a route based on data in the 2nd
part of a JWT token, for example based on the issuer.
Examples:
// one key value pair has to match
example1: JWTPayloadAnyKV("iss", "https://accounts.google.com", "email", "skipper-router@googlegroups.com")
-> "http://example.org/";
// all key value pairs have to match
example2: * && JWTPayloadAllKV("iss", "https://accounts.google.com", "email", "skipper-router@googlegroups.com")
-> "http://example.org/";
*/
package auth
import (
"net/http"
"regexp"
"strings"
"github.com/zalando/skipper/jwt"
"github.com/zalando/skipper/predicates"
"github.com/zalando/skipper/routing"
)
const (
authHeaderName = "Authorization"
authHeaderPrefix = "Bearer "
)
type (
matchBehavior int
matchMode int
)
type valueMatcher interface {
Match(jwtValue string) bool
}
const (
matchBehaviorAll matchBehavior = iota
matchBehaviorAny
matchModeExact matchMode = iota
matchModeRegexp
)
type (
spec struct {
name string
matchBehavior matchBehavior
matchMode matchMode
}
predicate struct {
kv map[string][]valueMatcher
matchBehavior matchBehavior
}
exactMatcher struct {
expected string
}
regexMatcher struct {
regexp *regexp.Regexp
}
)
func NewJWTPayloadAnyKV() routing.PredicateSpec {
return &spec{
name: predicates.JWTPayloadAnyKVName,
matchBehavior: matchBehaviorAny,
matchMode: matchModeExact,
}
}
func NewJWTPayloadAllKV() routing.PredicateSpec {
return &spec{
name: predicates.JWTPayloadAllKVName,
matchBehavior: matchBehaviorAll,
matchMode: matchModeExact,
}
}
func NewJWTPayloadAnyKVRegexp() routing.PredicateSpec {
return &spec{
name: predicates.JWTPayloadAnyKVRegexpName,
matchBehavior: matchBehaviorAny,
matchMode: matchModeRegexp,
}
}
func NewJWTPayloadAllKVRegexp() routing.PredicateSpec {
return &spec{
name: predicates.JWTPayloadAllKVRegexpName,
matchBehavior: matchBehaviorAll,
matchMode: matchModeRegexp,
}
}
func (s *spec) Name() string {
return s.name
}
func (s *spec) Create(args []interface{}) (routing.Predicate, error) {
if len(args) == 0 || len(args)%2 != 0 {
return nil, predicates.ErrInvalidPredicateParameters
}
kv := make(map[string][]valueMatcher)
for i := 0; i < len(args); i += 2 {
key, keyOk := args[i].(string)
value, valueOk := args[i+1].(string)
if !keyOk || !valueOk {
return nil, predicates.ErrInvalidPredicateParameters
}
var matcher valueMatcher
switch s.matchMode {
case matchModeExact:
matcher = exactMatcher{expected: value}
case matchModeRegexp:
re, err := regexp.Compile(value)
if err != nil {
return nil, predicates.ErrInvalidPredicateParameters
}
matcher = regexMatcher{regexp: re}
default:
return nil, predicates.ErrInvalidPredicateParameters
}
kv[key] = append(kv[key], matcher)
}
return &predicate{
kv: kv,
matchBehavior: s.matchBehavior,
}, nil
}
func (m exactMatcher) Match(jwtValue string) bool {
return jwtValue == m.expected
}
func (m regexMatcher) Match(jwtValue string) bool {
return m.regexp.MatchString(jwtValue)
}
func (p *predicate) Match(r *http.Request) bool {
ahead := r.Header.Get(authHeaderName)
tv := strings.TrimPrefix(ahead, authHeaderPrefix)
if tv == ahead {
return false
}
token, err := jwt.Parse(tv)
if err != nil {
return false
}
switch p.matchBehavior {
case matchBehaviorAll:
return allMatch(p.kv, token.Claims)
case matchBehaviorAny:
return anyMatch(p.kv, token.Claims)
default:
return false
}
}
func stringValue(payload map[string]interface{}, key string) (string, bool) {
if value, ok := payload[key]; ok {
result, ok := value.(string)
return result, ok
}
return "", false
}
func allMatch(expected map[string][]valueMatcher, payload map[string]interface{}) bool {
if len(expected) > len(payload) {
return false
}
for key, expectedValues := range expected {
payloadValue, ok := stringValue(payload, key)
if !ok {
return false
}
// expectedValues is expected to be a slice of one value
for _, expectedValue := range expectedValues {
if !expectedValue.Match(payloadValue) {
return false
}
}
}
return true
}
func anyMatch(expected map[string][]valueMatcher, payload map[string]interface{}) bool {
if len(expected) == 0 {
return true
}
for key, expectedValues := range expected {
if payloadValue, ok := stringValue(payload, key); ok {
for _, expectedValue := range expectedValues {
if expectedValue.Match(payloadValue) {
return true
}
}
}
}
return false
}