From ddde7de56ad901fda3599d098b67122e8f38227b Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Sun, 25 Mar 2018 19:31:06 +0300 Subject: [PATCH] Add support for numbers inside context variables Initially made to fix JWT exp claim https://github.com/TykTechnologies/tyk/issues/1568 --- mw_jwt.go | 10 +++++----- mw_openid.go | 26 ++------------------------ mw_url_rewrite.go | 4 ++++ 3 files changed, 11 insertions(+), 29 deletions(-) diff --git a/mw_jwt.go b/mw_jwt.go index 665235fc6b77..ee68a2a3eaea 100644 --- a/mw_jwt.go +++ b/mw_jwt.go @@ -263,7 +263,7 @@ func (k *JWTMiddleware) processCentralisedJWT(r *http.Request, token *jwt.Token) ctxSetSession(r, &session) ctxSetAuthToken(r, sessionID) } - k.setContextVars(r, token) + ctxSetJWTContextVars(k.Spec, r, token) return nil, 200 } else if k.Spec.JWTPolicyFieldName != "" { // extract policy ID from JWT token @@ -310,7 +310,7 @@ func (k *JWTMiddleware) processCentralisedJWT(r *http.Request, token *jwt.Token) ctxSetSession(r, &session) ctxSetAuthToken(r, sessionID) } - k.setContextVars(r, token) + ctxSetJWTContextVars(k.Spec, r, token) return nil, 200 } @@ -340,7 +340,7 @@ func (k *JWTMiddleware) processOneToOneTokenMap(r *http.Request, token *jwt.Toke log.Debug("Raw key ID found.") ctxSetSession(r, &session) ctxSetAuthToken(r, tykId) - k.setContextVars(r, token) + ctxSetJWTContextVars(k.Spec, r, token) return nil, 200 } @@ -443,9 +443,9 @@ func (k *JWTMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Request, _ return errors.New("Key not authorized"), 403 } -func (k *JWTMiddleware) setContextVars(r *http.Request, token *jwt.Token) { +func ctxSetJWTContextVars(s *APISpec, r *http.Request, token *jwt.Token) { // Flatten claims and add to context - if !k.Spec.EnableContextVars { + if !s.EnableContextVars { return } if cnt := ctxGetData(r); cnt != nil { diff --git a/mw_openid.go b/mw_openid.go index 63d0a9b9959a..9ae13aab3448 100644 --- a/mw_openid.go +++ b/mw_openid.go @@ -205,7 +205,7 @@ func (k *OpenIDMW) ProcessRequest(w http.ResponseWriter, r *http.Request, _ inte ctxSetSession(r, &session) ctxSetAuthToken(r, sessionID) } - k.setContextVars(r, token) + ctxSetJWTContextVars(k.Spec, r, token) return nil, 200 } @@ -221,26 +221,4 @@ func (k *OpenIDMW) reportLoginFailure(tykId string, r *http.Request) { // Report in health check reportHealthValue(k.Spec, KeyFailure, "1") -} - -func (k *OpenIDMW) setContextVars(r *http.Request, token *jwt.Token) { - if !k.Spec.EnableContextVars { - return - } - // Flatten claims and add to context - cnt := ctxGetData(r) - if cnt == nil { - return - } - claimPrefix := "jwt_claims_" - - for claimName, claimValue := range token.Claims.(jwt.MapClaims) { - claim := claimPrefix + claimName - cnt[claim] = claimValue - } - - // Key data - cnt["token"] = ctxGetAuthToken(r) - - ctxSetData(r, cnt) -} +} \ No newline at end of file diff --git a/mw_url_rewrite.go b/mw_url_rewrite.go index 983483e1dfa5..5ca593e743b2 100644 --- a/mw_url_rewrite.go +++ b/mw_url_rewrite.go @@ -224,6 +224,10 @@ func valToStr(v interface{}) string { switch x := v.(type) { case string: s = x + case float64: + s = strconv.FormatFloat(x, 'f', -1, 32) + case int64: + s = strconv.FormatInt(x, 10) case []string: s = strings.Join(x, ",") // Remove empty start