Skip to content

Commit

Permalink
Update Decode Segment and add test case to parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ajermaky committed Oct 31, 2021
1 parent 5838617 commit d7f2c46
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 10 additions & 0 deletions parser_test.go
Expand Up @@ -57,6 +57,16 @@ var jwtTestData = []struct {
nil,
jwt.SigningMethodRS256,
},
{
"basic with padding",
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJwYWRkZWRiYXIifQ==.20kGGJaYekGTRFf8b0TwhuETcR8lv5z2363X5jf7G1yTWVTwOmte5Ii8L8_OQbYwPoiVHmZY6iJPbt_DhCN42AeFY74BcsUhR-BVrYUVhKK0RppuzEcSlILDNeQsJDLEL035CPm1VO6Jrgk7enQPIctVxUesRgswP71OpGvJxy3j1k_J8p0WzZvRZTe1D_2Misa0UDGwnEIHhmr97fIpMSZjFxlcygQw8QN34IHLHIXMaTY1eiCf4CCr6rOS9wUeu7P3CPkmFq9XhxBT_LLCmIMhHnxP5x27FUJE_JZlfek0MmARcrhpsZS2sFhHAiWrjxjOE27jkDtv1nEwn65wMw==",
defaultKeyFunc,
jwt.MapClaims{"foo": "paddedbar"},
true,
0,
nil,
jwt.SigningMethodRS256,
},
{
"basic expired",
"", // autogen
Expand Down
8 changes: 4 additions & 4 deletions token.go
Expand Up @@ -104,17 +104,17 @@ func ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc, options
// Deprecated: In a future release, we will demote this function to a non-exported function, since it
// should only be used internally
func EncodeSegment(seg []byte) string {
return strings.TrimRight(base64.RawURLEncoding.EncodeToString(seg), "=")
return base64.RawURLEncoding.EncodeToString(seg)
}

// DecodeSegment decodes a JWT specific base64url encoding with padding stripped
//
// Deprecated: In a future release, we will demote this function to a non-exported function, since it
// should only be used internally
func DecodeSegment(seg string) ([]byte, error) {
if l := len(seg) % 4; l > 0 {
seg += strings.Repeat("=", 4-l)
if strings.Contains(seg, "=") {
return base64.URLEncoding.DecodeString(seg)
}

return base64.URLEncoding.DecodeString(seg)
return base64.RawURLEncoding.DecodeString(seg)
}

0 comments on commit d7f2c46

Please sign in to comment.