forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmw_jwt.go
735 lines (622 loc) · 21.1 KB
/
mw_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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
package gateway
import (
"crypto/md5"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"net/http"
"strings"
"time"
jwt "github.com/dgrijalva/jwt-go"
cache "github.com/pmylund/go-cache"
"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/user"
)
type JWTMiddleware struct {
BaseMiddleware
}
const (
KID = "kid"
SUB = "sub"
HMACSign = "hmac"
RSASign = "rsa"
ECDSASign = "ecdsa"
)
func (k *JWTMiddleware) Name() string {
return "JWTMiddleware"
}
func (k *JWTMiddleware) EnabledForSpec() bool {
return k.Spec.EnableJWT
}
var JWKCache *cache.Cache
type JWK struct {
Alg string `json:"alg"`
Kty string `json:"kty"`
Use string `json:"use"`
X5c []string `json:"x5c"`
N string `json:"n"`
E string `json:"e"`
KID string `json:"kid"`
X5t string `json:"x5t"`
}
type JWKs struct {
Keys []JWK `json:"keys"`
}
func (k *JWTMiddleware) getSecretFromURL(url, kid, keyType string) ([]byte, error) {
// Implement a cache
if JWKCache == nil {
k.Logger().Debug("Creating JWK Cache")
JWKCache = cache.New(240*time.Second, 30*time.Second)
}
var jwkSet JWKs
cachedJWK, found := JWKCache.Get(k.Spec.APIID)
if !found {
// Get the JWK
k.Logger().Debug("Pulling JWK")
resp, err := http.Get(url)
if err != nil {
k.Logger().WithError(err).Error("Failed to get resource URL")
return nil, err
}
defer resp.Body.Close()
// Decode it
if err := json.NewDecoder(resp.Body).Decode(&jwkSet); err != nil {
k.Logger().WithError(err).Error("Failed to decode body JWK")
return nil, err
}
// Cache it
k.Logger().Debug("Caching JWK")
JWKCache.Set(k.Spec.APIID, jwkSet, cache.DefaultExpiration)
} else {
jwkSet = cachedJWK.(JWKs)
}
k.Logger().Debug("Checking JWKs...")
for _, val := range jwkSet.Keys {
if val.KID != kid || strings.ToLower(val.Kty) != strings.ToLower(keyType) {
continue
}
if len(val.X5c) > 0 {
// Use the first cert only
decodedCert, err := base64.StdEncoding.DecodeString(val.X5c[0])
if err != nil {
return nil, err
}
k.Logger().Debug("Found cert! Replying...")
k.Logger().Debug("Cert was: ", string(decodedCert))
return decodedCert, nil
}
return nil, errors.New("no certificates in JWK")
}
return nil, errors.New("No matching KID could be found")
}
func (k *JWTMiddleware) getIdentityFromToken(token *jwt.Token) (string, error) {
// Check which claim is used for the id - kid or sub header
// If is not supposed to ignore KID - will use this as ID if not empty
if !k.Spec.APIDefinition.JWTSkipKid {
if tykId, idFound := token.Header[KID].(string); idFound {
k.Logger().Debug("Found: ", tykId)
return tykId, nil
}
}
// In case KID was empty or was set to ignore KID ==> Will try to get the Id from JWTIdentityBaseField or fallback to 'sub'
tykId, err := k.getUserIdFromClaim(token.Claims.(jwt.MapClaims))
return tykId, err
}
func (k *JWTMiddleware) getSecretToVerifySignature(r *http.Request, token *jwt.Token) ([]byte, error) {
config := k.Spec.APIDefinition
// Check for central JWT source
if config.JWTSource != "" {
// Is it a URL?
if httpScheme.MatchString(config.JWTSource) {
secret, err := k.getSecretFromURL(config.JWTSource, token.Header[KID].(string), k.Spec.JWTSigningMethod)
if err != nil {
return nil, err
}
return secret, nil
}
// If not, return the actual value
decodedCert, err := base64.StdEncoding.DecodeString(config.JWTSource)
if err != nil {
return nil, err
}
// Is decoded url too?
if httpScheme.MatchString(string(decodedCert)) {
secret, err := k.getSecretFromURL(string(decodedCert), token.Header[KID].(string), k.Spec.JWTSigningMethod)
if err != nil {
return nil, err
}
return secret, nil
}
return decodedCert, nil // Returns the decoded secret
}
// If we are here, there's no central JWT source
// Get the ID from the token (in KID header or configured claim or SUB claim)
tykId, err := k.getIdentityFromToken(token)
if err != nil {
return nil, err
}
// Couldn't base64 decode the kid, so lets try it raw
k.Logger().Debug("Getting key: ", tykId)
session, rawKeyExists := k.CheckSessionAndIdentityForValidKey(tykId, r)
if !rawKeyExists {
return nil, errors.New("token invalid, key not found")
}
return []byte(session.JWTData.Secret), nil
}
func (k *JWTMiddleware) getPolicyIDFromToken(claims jwt.MapClaims) (string, bool) {
policyID, foundPolicy := claims[k.Spec.JWTPolicyFieldName].(string)
if !foundPolicy {
k.Logger().Error("Could not identify a policy to apply to this token from field")
return "", false
}
if policyID == "" {
k.Logger().Error("Policy field has empty value")
return "", false
}
return policyID, true
}
func (k *JWTMiddleware) getBasePolicyID(r *http.Request, claims jwt.MapClaims) (policyID string, found bool) {
if k.Spec.JWTPolicyFieldName != "" {
policyID, found = k.getPolicyIDFromToken(claims)
return
} else if k.Spec.JWTClientIDBaseField != "" {
clientID, clientIDFound := claims[k.Spec.JWTClientIDBaseField].(string)
if !clientIDFound {
k.Logger().Error("Could not identify a policy to apply to this token from field")
return
}
// Check for a regular token that matches this client ID
clientSession, exists := k.CheckSessionAndIdentityForValidKey(clientID, r)
if !exists {
return
}
pols := clientSession.PolicyIDs()
if len(pols) < 1 {
return
}
// Use the policy from the client ID
return pols[0], true
}
return
}
func (k *JWTMiddleware) getUserIdFromClaim(claims jwt.MapClaims) (string, error) {
var userId string
var found = false
if k.Spec.JWTIdentityBaseField != "" {
if userId, found = claims[k.Spec.JWTIdentityBaseField].(string); found {
if len(userId) > 0 {
k.Logger().WithField("userId", userId).Debug("Found User Id in Base Field")
return userId, nil
}
message := "found an empty user ID in predefined base field claim " + k.Spec.JWTIdentityBaseField
k.Logger().Error(message)
return "", errors.New(message)
}
if !found {
k.Logger().WithField("Base Field", k.Spec.JWTIdentityBaseField).Warning("Base Field claim not found, trying to find user ID in 'sub' claim.")
}
}
if userId, found = claims[SUB].(string); found {
if len(userId) > 0 {
k.Logger().WithField("userId", userId).Debug("Found User Id in 'sub' claim")
return userId, nil
}
message := "found an empty user ID in sub claim"
k.Logger().Error(message)
return "", errors.New(message)
}
message := "no suitable claims for user ID were found"
k.Logger().Error(message)
return "", errors.New(message)
}
func getScopeFromClaim(claims jwt.MapClaims, scopeClaimName string) []string {
// get claim with scopes and turn it into slice of strings
if scope, found := claims[scopeClaimName].(string); found {
return strings.Split(scope, " ") // by standard is space separated list of values
}
// claim with scopes is optional so return nothing if it is not present
return nil
}
func mapScopeToPolicies(mapping map[string]string, scope []string) []string {
polIDs := []string{}
// add all policies matched from scope-policy mapping
policiesToApply := map[string]bool{}
for _, scopeItem := range scope {
if policyID, ok := mapping[scopeItem]; ok {
policiesToApply[policyID] = true
}
}
for id := range policiesToApply {
polIDs = append(polIDs, id)
}
return polIDs
}
// processCentralisedJWT Will check a JWT token centrally against the secret stored in the API Definition.
func (k *JWTMiddleware) processCentralisedJWT(r *http.Request, token *jwt.Token) (error, int) {
k.Logger().Debug("JWT authority is centralised")
claims := token.Claims.(jwt.MapClaims)
baseFieldData, err := k.getUserIdFromClaim(claims)
if err != nil {
k.reportLoginFailure("[NOT FOUND]", r)
return err, http.StatusForbidden
}
// Generate a virtual token
data := []byte(baseFieldData)
keyID := fmt.Sprintf("%x", md5.Sum(data))
sessionID := generateToken(k.Spec.OrgID, keyID)
updateSession := false
k.Logger().Debug("JWT Temporary session ID is: ", sessionID)
session, exists := k.CheckSessionAndIdentityForValidKey(sessionID, r)
isDefaultPol := false
basePolicyID := ""
foundPolicy := false
if !exists {
// Create it
k.Logger().Debug("Key does not exist, creating")
// We need a base policy as a template, either get it from the token itself OR a proxy client ID within Tyk
basePolicyID, foundPolicy = k.getBasePolicyID(r, claims)
if !foundPolicy {
if len(k.Spec.JWTDefaultPolicies) == 0 {
k.reportLoginFailure(baseFieldData, r)
return errors.New("key not authorized: no matching policy found"), http.StatusForbidden
} else {
isDefaultPol = true
basePolicyID = k.Spec.JWTDefaultPolicies[0]
}
}
session, err = generateSessionFromPolicy(basePolicyID,
k.Spec.OrgID,
true)
// If base policy is one of the defaults, apply other ones as well
if isDefaultPol {
for _, pol := range k.Spec.JWTDefaultPolicies {
if !contains(session.ApplyPolicies, pol) {
session.ApplyPolicies = append(session.ApplyPolicies, pol)
}
}
}
if err := k.ApplyPolicies(&session); err != nil {
return errors.New("failed to create key: " + err.Error()), http.StatusInternalServerError
}
if err != nil {
k.reportLoginFailure(baseFieldData, r)
k.Logger().Error("Could not find a valid policy to apply to this token!")
return errors.New("key not authorized: no matching policy"), http.StatusForbidden
}
//override session expiry with JWT if longer lived
if f, ok := claims["exp"].(float64); ok {
if int64(f)-session.Expires > 0 {
session.Expires = int64(f)
}
}
session.MetaData = map[string]interface{}{"TykJWTSessionID": sessionID}
session.Alias = baseFieldData
// Update the session in the session manager in case it gets called again
updateSession = true
k.Logger().Debug("Policy applied to key")
} else {
// extract policy ID from JWT token
basePolicyID, foundPolicy = k.getBasePolicyID(r, claims)
if !foundPolicy {
if len(k.Spec.JWTDefaultPolicies) == 0 {
k.reportLoginFailure(baseFieldData, r)
return errors.New("key not authorized: no matching policy found"), http.StatusForbidden
} else {
isDefaultPol = true
basePolicyID = k.Spec.JWTDefaultPolicies[0]
}
}
// check if we received a valid policy ID in claim
policiesMu.RLock()
policy, ok := policiesByID[basePolicyID]
policiesMu.RUnlock()
if !ok {
k.reportLoginFailure(baseFieldData, r)
k.Logger().Error("Policy ID found is invalid!")
return errors.New("key not authorized: no matching policy"), http.StatusForbidden
}
// check if token for this session was switched to another valid policy
pols := session.PolicyIDs()
if len(pols) == 0 {
k.reportLoginFailure(baseFieldData, r)
k.Logger().Error("No policies for the found session. Failing Request.")
return errors.New("key not authorized: no matching policy found"), http.StatusForbidden
}
defaultPolicyListChanged := false
if isDefaultPol {
// check a policy is removed/added from/to default policies
for _, pol := range session.PolicyIDs() {
if !contains(k.Spec.JWTDefaultPolicies, pol) && basePolicyID != pol {
defaultPolicyListChanged = true
}
}
for _, defPol := range k.Spec.JWTDefaultPolicies {
if !contains(session.PolicyIDs(), defPol) {
defaultPolicyListChanged = true
}
}
}
if !contains(pols, basePolicyID) || defaultPolicyListChanged {
if policy.OrgID != k.Spec.OrgID {
k.reportLoginFailure(baseFieldData, r)
k.Logger().Error("Policy ID found is invalid (wrong ownership)!")
return errors.New("key not authorized: no matching policy"), http.StatusForbidden
}
// apply new policy to session and update session
updateSession = true
session.SetPolicies(basePolicyID)
if isDefaultPol {
for _, pol := range k.Spec.JWTDefaultPolicies {
if !contains(session.ApplyPolicies, pol) {
session.ApplyPolicies = append(session.ApplyPolicies, pol)
}
}
}
if err := k.ApplyPolicies(&session); err != nil {
k.reportLoginFailure(baseFieldData, r)
k.Logger().WithError(err).Error("Could not apply new policy to session")
return errors.New("key not authorized: could not apply new policy"), http.StatusForbidden
}
}
//override session expiry with JWT if longer lived
if f, ok := claims["exp"].(float64); ok {
if int64(f)-session.Expires > 0 {
session.Expires = int64(f)
updateSession = true
}
}
}
// apply policies from scope if scope-to-policy mapping is specified for this API
if len(k.Spec.JWTScopeToPolicyMapping) != 0 {
scopeClaimName := k.Spec.JWTScopeClaimName
if scopeClaimName == "" {
scopeClaimName = "scope"
}
if scope := getScopeFromClaim(claims, scopeClaimName); scope != nil {
polIDs := []string{
basePolicyID, // add base policy as a first one
}
// // If specified, scopes should not use default policy
if isDefaultPol {
polIDs = []string{}
}
// add all policies matched from scope-policy mapping
mappedPolIDs := mapScopeToPolicies(k.Spec.JWTScopeToPolicyMapping, scope)
polIDs = append(polIDs, mappedPolIDs...)
// check if we need to update session
if !updateSession {
updateSession = !session.PoliciesEqualTo(polIDs)
}
session.SetPolicies(polIDs...)
// multiple policies assigned to a key, check if it is applicable
if err := k.ApplyPolicies(&session); err != nil {
k.reportLoginFailure(baseFieldData, r)
k.Logger().WithError(err).Error("Could not several policies from scope-claim mapping to JWT to session")
return errors.New("key not authorized: could not apply several policies"), http.StatusForbidden
}
}
}
k.Logger().Debug("Key found")
switch k.Spec.BaseIdentityProvidedBy {
case apidef.JWTClaim, apidef.UnsetAuth:
ctxSetSession(r, &session, sessionID, updateSession)
if updateSession {
SessionCache.Set(session.KeyHash(), session, cache.DefaultExpiration)
}
}
ctxSetJWTContextVars(k.Spec, r, token)
return nil, http.StatusOK
}
func (k *JWTMiddleware) reportLoginFailure(tykId string, r *http.Request) {
// Fire Authfailed Event
AuthFailed(k, r, tykId)
// Report in health check
reportHealthValue(k.Spec, KeyFailure, "1")
}
func (k *JWTMiddleware) processOneToOneTokenMap(r *http.Request, token *jwt.Token) (error, int) {
// Get the ID from the token
tykId, err := k.getIdentityFromToken(token)
if err != nil {
k.reportLoginFailure(tykId, r)
return err, http.StatusNotFound
}
k.Logger().Debug("Using raw key ID: ", tykId)
session, exists := k.CheckSessionAndIdentityForValidKey(tykId, r)
if !exists {
k.reportLoginFailure(tykId, r)
return errors.New("Key not authorized"), http.StatusForbidden
}
k.Logger().Debug("Raw key ID found.")
ctxSetSession(r, &session, tykId, false)
ctxSetJWTContextVars(k.Spec, r, token)
return nil, http.StatusOK
}
// getAuthType overrides BaseMiddleware.getAuthType.
func (k *JWTMiddleware) getAuthType() string {
return jwtType
}
func (k *JWTMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Request, _ interface{}) (error, int) {
if ctxGetRequestStatus(r) == StatusOkAndIgnore {
return nil, http.StatusOK
}
logger := k.Logger()
var tykId string
rawJWT, config := k.getAuthToken(k.getAuthType(), r)
if rawJWT == "" {
// No header value, fail
logger.Info("Attempted access with malformed header, no JWT auth header found.")
log.Debug("Looked in: ", config.AuthHeaderName)
log.Debug("Raw data was: ", rawJWT)
log.Debug("Headers are: ", r.Header)
k.reportLoginFailure(tykId, r)
return errors.New("Authorization field missing"), http.StatusBadRequest
}
// enable bearer token format
rawJWT = stripBearer(rawJWT)
// Use own validation logic, see below
parser := &jwt.Parser{SkipClaimsValidation: true}
// Verify the token
token, err := parser.Parse(rawJWT, func(token *jwt.Token) (interface{}, error) {
// Don't forget to validate the alg is what you expect:
switch k.Spec.JWTSigningMethod {
case HMACSign:
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v and not HMAC signature", token.Header["alg"])
}
case RSASign:
if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v and not RSA signature", token.Header["alg"])
}
case ECDSASign:
if _, ok := token.Method.(*jwt.SigningMethodECDSA); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v and not ECDSA signature", token.Header["alg"])
}
default:
logger.Warning("No signing method found in API Definition, defaulting to HMAC signature")
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
}
val, err := k.getSecretToVerifySignature(r, token)
if err != nil {
k.Logger().WithError(err).Error("Couldn't get token")
return nil, err
}
switch k.Spec.JWTSigningMethod {
case RSASign, ECDSASign:
key, err := ParseRSAPublicKey(val)
if err != nil {
logger.WithError(err).Error("Failed to decode JWT key")
return nil, errors.New("Failed to decode JWT key")
}
return key, nil
default:
return val, nil
}
})
if err == nil && token.Valid {
if jwtErr := k.timeValidateJWTClaims(token.Claims.(jwt.MapClaims)); jwtErr != nil {
return errors.New("Key not authorized: " + jwtErr.Error()), http.StatusUnauthorized
}
// Token is valid - let's move on
// Are we mapping to a central JWT Secret?
if k.Spec.JWTSource != "" {
return k.processCentralisedJWT(r, token)
}
// No, let's try one-to-one mapping
return k.processOneToOneTokenMap(r, token)
}
logger.Info("Attempted JWT access with non-existent key.")
k.reportLoginFailure(tykId, r)
if err != nil {
logger.WithError(err).Error("JWT validation error")
return errors.New("Key not authorized:" + err.Error()), http.StatusForbidden
}
return errors.New("Key not authorized"), http.StatusForbidden
}
func ParseRSAPublicKey(data []byte) (interface{}, error) {
input := data
block, _ := pem.Decode(data)
if block != nil {
input = block.Bytes
}
var pub interface{}
var err error
pub, err = x509.ParsePKIXPublicKey(input)
if err != nil {
cert, err0 := x509.ParseCertificate(input)
if err0 != nil {
return nil, err0
}
pub = cert.PublicKey
err = nil
}
return pub, err
}
func (k *JWTMiddleware) timeValidateJWTClaims(c jwt.MapClaims) *jwt.ValidationError {
vErr := new(jwt.ValidationError)
now := time.Now().Unix()
// The claims below are optional, by default, so if they are set to the
// default value in Go, let's not fail the verification for them.
if !c.VerifyExpiresAt(now-int64(k.Spec.JWTExpiresAtValidationSkew), false) {
vErr.Inner = errors.New("token has expired")
vErr.Errors |= jwt.ValidationErrorExpired
}
if c.VerifyIssuedAt(now+int64(k.Spec.JWTIssuedAtValidationSkew), false) == false {
vErr.Inner = errors.New("token used before issued")
vErr.Errors |= jwt.ValidationErrorIssuedAt
}
if c.VerifyNotBefore(now+int64(k.Spec.JWTNotBeforeValidationSkew), false) == false {
vErr.Inner = errors.New("token is not valid yet")
vErr.Errors |= jwt.ValidationErrorNotValidYet
}
if vErr.Errors == 0 {
return nil
}
return vErr
}
func ctxSetJWTContextVars(s *APISpec, r *http.Request, token *jwt.Token) {
// Flatten claims and add to context
if !s.EnableContextVars {
return
}
if cnt := ctxGetData(r); cnt != nil {
claimPrefix := "jwt_claims_"
for claimName, claimValue := range token.Header {
claim := claimPrefix + claimName
cnt[claim] = claimValue
}
for claimName, claimValue := range token.Claims.(jwt.MapClaims) {
claim := claimPrefix + claimName
cnt[claim] = claimValue
}
// Key data
cnt["token"] = ctxGetAuthToken(r)
ctxSetData(r, cnt)
}
}
func generateSessionFromPolicy(policyID, orgID string, enforceOrg bool) (user.SessionState, error) {
policiesMu.RLock()
policy, ok := policiesByID[policyID]
policiesMu.RUnlock()
session := user.SessionState{}
if !ok {
return session, errors.New("Policy not found")
}
// Check ownership, policy org owner must be the same as API,
// otherwise youcould overwrite a session key with a policy from a different org!
if enforceOrg {
if policy.OrgID != orgID {
log.Error("Attempting to apply policy from different organisation to key, skipping")
return session, errors.New("Key not authorized: no matching policy")
}
} else {
// Org isn;t enforced, so lets use the policy baseline
orgID = policy.OrgID
}
session.SetPolicies(policyID)
session.OrgID = orgID
session.Allowance = policy.Rate // This is a legacy thing, merely to make sure output is consistent. Needs to be purged
session.Rate = policy.Rate
session.Per = policy.Per
session.ThrottleInterval = policy.ThrottleInterval
session.ThrottleRetryLimit = policy.ThrottleRetryLimit
session.MaxQueryDepth = policy.MaxQueryDepth
session.QuotaMax = policy.QuotaMax
session.QuotaRenewalRate = policy.QuotaRenewalRate
session.AccessRights = make(map[string]user.AccessDefinition)
for apiID, access := range policy.AccessRights {
session.AccessRights[apiID] = access
}
session.HMACEnabled = policy.HMACEnabled
session.EnableHTTPSignatureValidation = policy.EnableHTTPSignatureValidation
session.IsInactive = policy.IsInactive
session.Tags = policy.Tags
if policy.KeyExpiresIn > 0 {
session.Expires = time.Now().Unix() + policy.KeyExpiresIn
}
return session, nil
}