Skip to content

Commit 4bb9650

Browse files
committed
Revert atomic changes to utilize boolean
1 parent edf4019 commit 4bb9650

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

parser_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ var setPaddingTestData = []struct {
439439
name string
440440
tokenString string
441441
claims jwt.Claims
442-
paddedDecode uint64
442+
paddedDecode bool
443443
signingMethod jwt.SigningMethod
444444
keyfunc jwt.Keyfunc
445445
valid bool
@@ -448,7 +448,7 @@ var setPaddingTestData = []struct {
448448
name: "Validated non-padded token with padding disabled",
449449
tokenString: "",
450450
claims: jwt.MapClaims{"foo": "paddedbar"},
451-
paddedDecode: jwt.DisablePadding,
451+
paddedDecode: false,
452452
signingMethod: jwt.SigningMethodRS256,
453453
keyfunc: defaultKeyFunc,
454454
valid: true,
@@ -457,7 +457,7 @@ var setPaddingTestData = []struct {
457457
name: "Validated non-padded token with padding enabled",
458458
tokenString: "",
459459
claims: jwt.MapClaims{"foo": "paddedbar"},
460-
paddedDecode: jwt.AllowPadding,
460+
paddedDecode: true,
461461
signingMethod: jwt.SigningMethodRS256,
462462
keyfunc: defaultKeyFunc,
463463
valid: true,
@@ -466,7 +466,7 @@ var setPaddingTestData = []struct {
466466
name: "Error for padded token with padding disabled",
467467
tokenString: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJwYWRkZWRiYXIifQ==.20kGGJaYekGTRFf8b0TwhuETcR8lv5z2363X5jf7G1yTWVTwOmte5Ii8L8_OQbYwPoiVHmZY6iJPbt_DhCN42AeFY74BcsUhR-BVrYUVhKK0RppuzEcSlILDNeQsJDLEL035CPm1VO6Jrgk7enQPIctVxUesRgswP71OpGvJxy3j1k_J8p0WzZvRZTe1D_2Misa0UDGwnEIHhmr97fIpMSZjFxlcygQw8QN34IHLHIXMaTY1eiCf4CCr6rOS9wUeu7P3CPkmFq9XhxBT_LLCmIMhHnxP5x27FUJE_JZlfek0MmARcrhpsZS2sFhHAiWrjxjOE27jkDtv1nEwn65wMw==",
468468
claims: jwt.MapClaims{"foo": "paddedbar"},
469-
paddedDecode: jwt.DisablePadding,
469+
paddedDecode: false,
470470
signingMethod: jwt.SigningMethodRS256,
471471
keyfunc: defaultKeyFunc,
472472
valid: false,
@@ -475,7 +475,7 @@ var setPaddingTestData = []struct {
475475
name: "Validated padded token with padding enabled",
476476
tokenString: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJwYWRkZWRiYXIifQ==.20kGGJaYekGTRFf8b0TwhuETcR8lv5z2363X5jf7G1yTWVTwOmte5Ii8L8_OQbYwPoiVHmZY6iJPbt_DhCN42AeFY74BcsUhR-BVrYUVhKK0RppuzEcSlILDNeQsJDLEL035CPm1VO6Jrgk7enQPIctVxUesRgswP71OpGvJxy3j1k_J8p0WzZvRZTe1D_2Misa0UDGwnEIHhmr97fIpMSZjFxlcygQw8QN34IHLHIXMaTY1eiCf4CCr6rOS9wUeu7P3CPkmFq9XhxBT_LLCmIMhHnxP5x27FUJE_JZlfek0MmARcrhpsZS2sFhHAiWrjxjOE27jkDtv1nEwn65wMw==",
477477
claims: jwt.MapClaims{"foo": "paddedbar"},
478-
paddedDecode: jwt.AllowPadding,
478+
paddedDecode: true,
479479
signingMethod: jwt.SigningMethodRS256,
480480
keyfunc: defaultKeyFunc,
481481
valid: true,
@@ -512,7 +512,7 @@ func TestSetPadding(t *testing.T) {
512512
}
513513

514514
})
515-
jwt.SetDecodePadding(jwt.DisablePadding)
515+
jwt.SetDecodePadding(false)
516516

517517
}
518518
}

token.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,17 @@ import (
44
"encoding/base64"
55
"encoding/json"
66
"strings"
7-
"sync/atomic"
87
"time"
98
)
109

11-
const (
12-
DisablePadding uint64 = 0 // Utilizes RawURLEncoding
13-
AllowPadding uint64 = 1 // Utilizes URLEncoding
14-
15-
)
16-
17-
var decodePaddingAllowed uint64
10+
var decodePaddingAllowed bool
1811

1912
// SetDecodePadding will switch the codec used for encoding/decoding JWTs respectively. Note that the JWS RFC7515
2013
// states that the tokens will utilize a Base64url encoding with no padding. Unfortunately, some implementations
2114
// of JWT are producing non-standard tokens, and thus require support for decoding. Note that this is a global
22-
// variable, and updating it will change the behavior on a package level.
23-
func SetDecodePadding(setPadding uint64) {
24-
atomic.SwapUint64(&decodePaddingAllowed, setPadding)
15+
// variable, and updating it will change the behavior on a package level, and is also NOT go-routine safe.
16+
func SetDecodePadding(setPadding bool) {
17+
decodePaddingAllowed = setPadding
2518
}
2619

2720
// TimeFunc provides the current time when parsing token to validate "exp" claim (expiration time).
@@ -129,7 +122,7 @@ func EncodeSegment(seg []byte) string {
129122
// Deprecated: In a future release, we will demote this function to a non-exported function, since it
130123
// should only be used internally
131124
func DecodeSegment(seg string) ([]byte, error) {
132-
if atomic.LoadUint64(&decodePaddingAllowed) == AllowPadding {
125+
if decodePaddingAllowed {
133126
if l := len(seg) % 4; l > 0 {
134127
seg += strings.Repeat("=", 4-l)
135128
}

0 commit comments

Comments
 (0)