Skip to content

Commit 50dca70

Browse files
committed
Update Decode Segment and add test case to parser
1 parent 5838617 commit 50dca70

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

parser_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ var jwtTestData = []struct {
5757
nil,
5858
jwt.SigningMethodRS256,
5959
},
60+
{
61+
"basic with padding",
62+
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJwYWRkZWRiYXIifQ==.20kGGJaYekGTRFf8b0TwhuETcR8lv5z2363X5jf7G1yTWVTwOmte5Ii8L8_OQbYwPoiVHmZY6iJPbt_DhCN42AeFY74BcsUhR-BVrYUVhKK0RppuzEcSlILDNeQsJDLEL035CPm1VO6Jrgk7enQPIctVxUesRgswP71OpGvJxy3j1k_J8p0WzZvRZTe1D_2Misa0UDGwnEIHhmr97fIpMSZjFxlcygQw8QN34IHLHIXMaTY1eiCf4CCr6rOS9wUeu7P3CPkmFq9XhxBT_LLCmIMhHnxP5x27FUJE_JZlfek0MmARcrhpsZS2sFhHAiWrjxjOE27jkDtv1nEwn65wMw==", defaultKeyFunc,
63+
jwt.MapClaims{"foo": "paddedbar"},
64+
true,
65+
0,
66+
nil,
67+
jwt.SigningMethodRS256,
68+
},
6069
{
6170
"basic expired",
6271
"", // autogen

token.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,17 @@ func ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc, options
104104
// Deprecated: In a future release, we will demote this function to a non-exported function, since it
105105
// should only be used internally
106106
func EncodeSegment(seg []byte) string {
107-
return strings.TrimRight(base64.RawURLEncoding.EncodeToString(seg), "=")
107+
return base64.RawURLEncoding.EncodeToString(seg)
108108
}
109109

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

119-
return base64.URLEncoding.DecodeString(seg)
119+
return base64.RawURLEncoding.DecodeString(seg)
120120
}

0 commit comments

Comments
 (0)