forked from sanderPostma/traefik-validate-jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
244 lines (217 loc) · 6.19 KB
/
middleware.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package traefik_validate_jwt
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/MicahParks/keyfunc"
"github.com/golang-jwt/jwt/v4"
"io"
"log"
"net/http"
"strings"
"time"
)
type TenantIdFilter struct {
TenantId string `json:"tenantId,omitempty"`
AppIds []string `json:"appIds,omitempty"`
}
// Config the plugin configuration.
type Config struct {
IsTest bool
TenantIdFilters []TenantIdFilter `json:"filters,omitempty"`
}
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{}
}
// Middleware a Middleware plugin.
type Middleware struct {
next http.Handler
name string
filters []TenantIdFilter
keyFuncOptions keyfunc.Options
jwksURIMap map[string]string
jwksMap map[string]*keyfunc.JWKS
isTest bool
}
// New created a new Middleware plugin.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
if len(config.TenantIdFilters) == 0 && !config.IsTest {
return nil, errors.New(fmt.Sprintln("no filters could be found, jwt-validator not created. config:", config))
}
m := &Middleware{
next: next,
name: name,
filters: config.TenantIdFilters,
jwksURIMap: map[string]string{},
jwksMap: map[string]*keyfunc.JWKS{},
isTest: config.IsTest,
keyFuncOptions: buildKeyFuncOptions(),
}
return m, nil
}
func (m *Middleware) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if m.isTest { // Skip Yaegi test because I don't think it can log on to our Azure app registration and get a valid bearer token
m.next.ServeHTTP(rw, req)
return
}
defer func() {
if err := recover(); err != nil {
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte(fmt.Sprint("500 - panic occurred:", err)))
}
}()
// Assert input
if req.RemoteAddr == "" {
rw.WriteHeader(http.StatusUnauthorized)
rw.Write([]byte(fmt.Sprint("500 - RemoteAddr is empty.")))
return
}
tokenString := req.Header.Get("Authorization")
if tokenString == "" {
rw.WriteHeader(http.StatusUnauthorized)
rw.Write([]byte(fmt.Sprint("401 - Authorization header is empty or non-existent.")))
return
}
if !strings.HasPrefix(tokenString, "Bearer ") {
rw.WriteHeader(http.StatusUnauthorized)
rw.Write([]byte(fmt.Sprint("401 - Authorization header must be a 'Bearer' token")))
return
}
tokenString = tokenString[7:]
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
jwks, i, err := m.getJwks(token)
if err != nil {
return i, err
}
return jwks.Keyfunc(token)
})
if err != nil {
rw.WriteHeader(http.StatusUnauthorized)
rw.Write([]byte(fmt.Sprint("401 - Token could not be parsed:", err)))
return
}
// Check if the token is valid
if !token.Valid {
rw.WriteHeader(http.StatusUnauthorized)
rw.Write([]byte(fmt.Sprint("401 - Token is invalid", token)))
return
}
claims := token.Claims.(jwt.MapClaims)
expTime := time.Unix(int64(claims["exp"].(float64)), 0)
if time.Now().UTC().After(expTime) {
rw.WriteHeader(http.StatusUnauthorized)
rw.Write([]byte("401 - Token has expired"))
return
}
tenantIdFound := false
appIdFound := false
tenantIdClaim := claims["tid"].(string)
appIdClaim, ok := claims["appid"].(string)
if !ok {
appIdClaim, ok = claims["aud"].(string)
if !ok {
rw.WriteHeader(http.StatusBadRequest)
rw.Write([]byte(fmt.Sprint("400 - Can't get the application id from the JWT claims")))
return
}
appIdClaim = extractAppId(appIdClaim)
}
for _, filter := range m.filters {
if filter.TenantId == tenantIdClaim {
tenantIdFound = true
for _, appId := range filter.AppIds {
if appId == appIdClaim {
appIdFound = true
break
}
}
break
}
}
if !tenantIdFound {
rw.WriteHeader(http.StatusUnauthorized)
rw.Write([]byte(fmt.Sprint("401 - No match found for tenant id ", tenantIdClaim)))
return
}
if !appIdFound {
rw.WriteHeader(http.StatusUnauthorized)
rw.Write([]byte(fmt.Sprintf("401 - No match found for app id %s. Name: %s", appIdClaim, m.name)))
return
}
if m.next != nil {
m.next.ServeHTTP(rw, req)
} else {
log.Println("Request authorized")
}
}
func (m *Middleware) getJwks(token *jwt.Token) (*keyfunc.JWKS, interface{}, error) {
// Get the jwksURL from the idp claim, else try iss
jwksURL, ok := m.getJwksUrl(token, "idp")
if !ok {
jwksURL, ok = m.getJwksUrl(token, "iss")
if !ok {
return nil, nil, fmt.Errorf("could not determine openid-configuration from idp or iss claims")
}
}
jwks, found := m.jwksMap[jwksURL]
if !found {
// Create jwks handler instance
var err error
jwks, err = keyfunc.Get(jwksURL, m.keyFuncOptions)
if err != nil {
return nil, nil, fmt.Errorf("Failed to create JWKS from resource at the given URL.\nError: %s", err.Error())
}
m.jwksMap[jwksURL] = jwks
}
return jwks, nil, nil
}
func (m *Middleware) getJwksUrl(token *jwt.Token, claimName string) (string, bool) {
jwksURI, found := m.jwksURIMap[claimName]
if found {
return jwksURI, true
}
authServerClaim, ok := token.Claims.(jwt.MapClaims)[claimName].(string)
if !ok {
return "", false
}
// FIXME this is expensive when the first claim exists but does not work and the second does
openIdConfigUrl := fmt.Sprintf("%s/.well-known/openid-configuration", authServerClaim)
openIdResp, err := http.Get(openIdConfigUrl)
if err != nil {
return "", false
}
defer openIdResp.Body.Close()
openIdBody, err := io.ReadAll(openIdResp.Body)
if err != nil {
return "", false
}
var jsonData map[string]interface{}
if err := json.Unmarshal(openIdBody, &jsonData); err != nil {
return "", false
}
jwksURI, ok = jsonData["jwks_uri"].(string)
if ok {
m.jwksURIMap[claimName] = jwksURI
}
return jwksURI, ok
}
func extractAppId(input string) string {
if strings.Contains(input, "://") {
return strings.Split(input, "://")[1]
}
return input
}
func buildKeyFuncOptions() keyfunc.Options {
return keyfunc.Options{
Ctx: context.Background(),
RefreshErrorHandler: func(err error) {
log.Printf("There was an error with the jwt.Keyfunc\nError: %s", err.Error())
},
RefreshInterval: 0,
RefreshRateLimit: time.Minute * 5,
RefreshTimeout: time.Second * 10,
RefreshUnknownKID: true,
}
}