From fdeab96f74da4a5f7e08329133192737795d9ec9 Mon Sep 17 00:00:00 2001 From: Vojtech Vitek Date: Wed, 17 Jan 2018 22:14:14 -0500 Subject: [PATCH] Support jwtauth.Claims in FromContext(), fixes panic (#25) --- jwtauth.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/jwtauth.go b/jwtauth.go index f3c2721..250066f 100644 --- a/jwtauth.go +++ b/jwtauth.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "net/http" "strings" "time" @@ -191,11 +192,14 @@ func FromContext(ctx context.Context) (*jwt.Token, Claims, error) { var claims Claims if token != nil { - tokenClaims, ok := token.Claims.(jwt.MapClaims) - if !ok { - panic("jwtauth: expecting jwt.MapClaims") + switch tokenClaims := token.Claims.(type) { + case Claims: + // Nop. + case jwt.MapClaims: + claims = Claims(tokenClaims) + default: + panic(fmt.Sprintf("jwtauth: unknown type of Claims: %T", token.Claims)) } - claims = Claims(tokenClaims) } else { claims = Claims{} }