Skip to content

Commit

Permalink
Merge pull request #42 from xmidt-org/fix-jwt-parser
Browse files Browse the repository at this point in the history
Added map function
  • Loading branch information
johnabass authored Sep 17, 2019
2 parents a60b791 + 6984bc0 commit 5058f64
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
6 changes: 4 additions & 2 deletions basculehttp/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"net/textproto"
"strings"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/goph/emperror"
"github.com/xmidt-org/bascule"
)

Expand Down Expand Up @@ -67,7 +69,7 @@ func (c *constructor) decorate(next http.Handler) http.Handler {
ctx := request.Context()
token, err := tf.ParseAndValidate(ctx, request, key, authorization[i+len(c.headerDelimiter):])
if err != nil {
c.error(logger, ParseFailed, authorization, err)
c.error(logger, ParseFailed, authorization, emperror.Wrap(err, "failed to parse and validate token"))
WriteResponse(response, http.StatusForbidden, err)
return
}
Expand All @@ -91,7 +93,7 @@ func (c *constructor) decorate(next http.Handler) http.Handler {
}

func (c *constructor) error(logger bascule.Logger, e ErrorResponseReason, auth string, err error) {
logger.Log(level.Key(), level.ErrorValue(), bascule.ErrorKey, err.Error(), "auth", auth)
log.With(logger, emperror.Context(err)...).Log(level.Key(), level.ErrorValue(), bascule.ErrorKey, err.Error(), "auth", auth)
c.onErrorResponse(e, err)
}

Expand Down
10 changes: 7 additions & 3 deletions basculehttp/tokenFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,20 @@ func (btf BearerTokenFactory) ParseAndValidate(ctx context.Context, _ *http.Requ
return nil, ErrorInvalidToken
}

claims, ok := jwsToken.Claims.(*jwt.MapClaims)
claims, ok := jwsToken.Claims.(*bascule.ClaimsWithLeeway)
if !ok {
return nil, emperror.Wrap(ErrorUnexpectedClaims, "failed to parse JWS")
}

payload := bascule.Attributes(*claims)
claimsMap, err := claims.GetMap()
if err != nil {
return nil, emperror.WrapWith(err, "failed to get map of claims", "claims struct", claims)
}
payload := bascule.Attributes(claimsMap)

principal, ok := payload[jwtPrincipalKey].(string)
if !ok {
return nil, ErrorUnexpectedPrincipal
return nil, emperror.WrapWith(ErrorUnexpectedPrincipal, "failed to get and convert principal", "principal", payload[jwtPrincipalKey], "payload", payload)
}

return bascule.NewToken("jwt", principal, payload), nil
Expand Down
11 changes: 11 additions & 0 deletions jws.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bascule

import (
"encoding/json"
"errors"

jwt "github.com/dgrijalva/jwt-go"
Expand Down Expand Up @@ -65,3 +66,13 @@ func (c *ClaimsWithLeeway) Valid() error {

return vErr
}

func (c *ClaimsWithLeeway) UnmarshalJSON(data []byte) error {
c.MapClaims = make(jwt.MapClaims) // just to be sure it's clean before each unmarshal
return json.Unmarshal(data, &c.MapClaims)
}

// GetMap returns a map of string to interfaces of the values in the ClaimsWithLeeway
func (c *ClaimsWithLeeway) GetMap() (map[string]interface{}, error) {
return c.MapClaims, nil
}

0 comments on commit 5058f64

Please sign in to comment.