-
Notifications
You must be signed in to change notification settings - Fork 349
/
jwt.go
182 lines (153 loc) · 3.67 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
/*
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 (
"encoding/base64"
"encoding/json"
"net/http"
"strings"
"github.com/zalando/skipper/predicates"
"github.com/zalando/skipper/routing"
)
const (
authHeaderName = "Authorization"
authHeaderPrefix = "Bearer "
)
type roleMatchType int
const (
matchJWTPayloadAnyKV roleMatchType = iota
matchJWTPayloadAllKV
matchUnknown
matchJWTPayloadAllKVName = "JWTPayloadAllKV"
matchJWTPayloadAnyKVName = "JWTPayloadAnyKV"
matchUnkown = "unkown"
)
type (
spec struct{ typ roleMatchType }
predicateAny struct {
kv map[string]string
}
predicateAll struct {
kv map[string]string
}
)
func NewJWTPayloadAnyKV() routing.PredicateSpec {
return &spec{typ: matchJWTPayloadAnyKV}
}
func NewJWTPayloadAllKV() routing.PredicateSpec {
return &spec{typ: matchJWTPayloadAllKV}
}
func (s *spec) Name() string {
switch s.typ {
case matchJWTPayloadAllKV:
return matchJWTPayloadAllKVName
case matchJWTPayloadAnyKV:
return matchJWTPayloadAnyKVName
}
return matchUnkown
}
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]string)
var k, v string
for i := range args {
if s, ok := args[i].(string); ok {
switch i % 2 {
case 0:
k = s
kv[k] = ""
case 1:
v = s
kv[k] = v
}
} else {
return nil, predicates.ErrInvalidPredicateParameters
}
}
switch s.typ {
case matchJWTPayloadAllKV:
return &predicateAll{kv: kv}, nil
case matchJWTPayloadAnyKV:
return &predicateAny{kv: kv}, nil
}
return nil, predicates.ErrInvalidPredicateParameters
}
func (p *predicateAll) Match(r *http.Request) bool {
ahead := r.Header.Get(authHeaderName)
if !strings.HasPrefix(ahead, authHeaderPrefix) {
return false
}
fields := strings.FieldsFunc(ahead, func(r rune) bool {
return r == []rune(".")[0]
})
if len(fields) != 3 {
return false
}
sDec, err := base64.RawURLEncoding.DecodeString(fields[1])
if err != nil {
return false
}
var h map[string]interface{}
err = json.Unmarshal(sDec, &h)
if err != nil {
return false
}
return allMatch(p.kv, h)
}
func (p *predicateAny) Match(r *http.Request) bool {
ahead := r.Header.Get(authHeaderName)
if !strings.HasPrefix(ahead, authHeaderPrefix) {
return false
}
fields := strings.FieldsFunc(ahead, func(r rune) bool {
return r == []rune(".")[0]
})
if len(fields) != 3 {
return false
}
sDec, err := base64.RawURLEncoding.DecodeString(fields[1])
if err != nil {
return false
}
var h map[string]interface{}
err = json.Unmarshal(sDec, &h)
if err != nil {
return false
}
return anyMatch(p.kv, h)
}
func allMatch(kv map[string]string, h map[string]interface{}) bool {
for k, v := range kv {
if vh, ok := h[k]; !ok {
return false
} else {
if s, ok2 := vh.(string); !ok2 || v != s {
return false
}
}
}
return true
}
func anyMatch(kv map[string]string, h map[string]interface{}) bool {
for k, v := range kv {
if vh, ok := h[k]; ok {
if s, ok2 := vh.(string); ok2 && v == s {
return true
}
}
}
return false
}