Skip to content

Commit 7cc0ea7

Browse files
committed
Revert atomic changes to utilize boolean
1 parent edf4019 commit 7cc0ea7

File tree

2 files changed

+36
-43
lines changed

2 files changed

+36
-43
lines changed

parser_test.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -436,49 +436,49 @@ func TestParser_ParseUnverified(t *testing.T) {
436436
}
437437

438438
var setPaddingTestData = []struct {
439-
name string
440-
tokenString string
441-
claims jwt.Claims
442-
paddedDecode uint64
439+
name string
440+
tokenString string
441+
claims jwt.Claims
442+
paddedDecode bool
443443
signingMethod jwt.SigningMethod
444-
keyfunc jwt.Keyfunc
445-
valid bool
444+
keyfunc jwt.Keyfunc
445+
valid bool
446446
}{
447447
{
448-
name: "Validated non-padded token with padding disabled",
449-
tokenString: "",
450-
claims: jwt.MapClaims{"foo": "paddedbar"},
451-
paddedDecode: jwt.DisablePadding,
448+
name: "Validated non-padded token with padding disabled",
449+
tokenString: "",
450+
claims: jwt.MapClaims{"foo": "paddedbar"},
451+
paddedDecode: false,
452452
signingMethod: jwt.SigningMethodRS256,
453-
keyfunc: defaultKeyFunc,
454-
valid: true,
453+
keyfunc: defaultKeyFunc,
454+
valid: true,
455455
},
456456
{
457-
name: "Validated non-padded token with padding enabled",
458-
tokenString: "",
459-
claims: jwt.MapClaims{"foo": "paddedbar"},
460-
paddedDecode: jwt.AllowPadding,
457+
name: "Validated non-padded token with padding enabled",
458+
tokenString: "",
459+
claims: jwt.MapClaims{"foo": "paddedbar"},
460+
paddedDecode: true,
461461
signingMethod: jwt.SigningMethodRS256,
462-
keyfunc: defaultKeyFunc,
463-
valid: true,
462+
keyfunc: defaultKeyFunc,
463+
valid: true,
464464
},
465465
{
466-
name: "Error for padded token with padding disabled",
467-
tokenString: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJwYWRkZWRiYXIifQ==.20kGGJaYekGTRFf8b0TwhuETcR8lv5z2363X5jf7G1yTWVTwOmte5Ii8L8_OQbYwPoiVHmZY6iJPbt_DhCN42AeFY74BcsUhR-BVrYUVhKK0RppuzEcSlILDNeQsJDLEL035CPm1VO6Jrgk7enQPIctVxUesRgswP71OpGvJxy3j1k_J8p0WzZvRZTe1D_2Misa0UDGwnEIHhmr97fIpMSZjFxlcygQw8QN34IHLHIXMaTY1eiCf4CCr6rOS9wUeu7P3CPkmFq9XhxBT_LLCmIMhHnxP5x27FUJE_JZlfek0MmARcrhpsZS2sFhHAiWrjxjOE27jkDtv1nEwn65wMw==",
468-
claims: jwt.MapClaims{"foo": "paddedbar"},
469-
paddedDecode: jwt.DisablePadding,
466+
name: "Error for padded token with padding disabled",
467+
tokenString: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJwYWRkZWRiYXIifQ==.20kGGJaYekGTRFf8b0TwhuETcR8lv5z2363X5jf7G1yTWVTwOmte5Ii8L8_OQbYwPoiVHmZY6iJPbt_DhCN42AeFY74BcsUhR-BVrYUVhKK0RppuzEcSlILDNeQsJDLEL035CPm1VO6Jrgk7enQPIctVxUesRgswP71OpGvJxy3j1k_J8p0WzZvRZTe1D_2Misa0UDGwnEIHhmr97fIpMSZjFxlcygQw8QN34IHLHIXMaTY1eiCf4CCr6rOS9wUeu7P3CPkmFq9XhxBT_LLCmIMhHnxP5x27FUJE_JZlfek0MmARcrhpsZS2sFhHAiWrjxjOE27jkDtv1nEwn65wMw==",
468+
claims: jwt.MapClaims{"foo": "paddedbar"},
469+
paddedDecode: false,
470470
signingMethod: jwt.SigningMethodRS256,
471-
keyfunc: defaultKeyFunc,
472-
valid: false,
471+
keyfunc: defaultKeyFunc,
472+
valid: false,
473473
},
474474
{
475-
name: "Validated padded token with padding enabled",
476-
tokenString: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJwYWRkZWRiYXIifQ==.20kGGJaYekGTRFf8b0TwhuETcR8lv5z2363X5jf7G1yTWVTwOmte5Ii8L8_OQbYwPoiVHmZY6iJPbt_DhCN42AeFY74BcsUhR-BVrYUVhKK0RppuzEcSlILDNeQsJDLEL035CPm1VO6Jrgk7enQPIctVxUesRgswP71OpGvJxy3j1k_J8p0WzZvRZTe1D_2Misa0UDGwnEIHhmr97fIpMSZjFxlcygQw8QN34IHLHIXMaTY1eiCf4CCr6rOS9wUeu7P3CPkmFq9XhxBT_LLCmIMhHnxP5x27FUJE_JZlfek0MmARcrhpsZS2sFhHAiWrjxjOE27jkDtv1nEwn65wMw==",
477-
claims: jwt.MapClaims{"foo": "paddedbar"},
478-
paddedDecode: jwt.AllowPadding,
475+
name: "Validated padded token with padding enabled",
476+
tokenString: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJwYWRkZWRiYXIifQ==.20kGGJaYekGTRFf8b0TwhuETcR8lv5z2363X5jf7G1yTWVTwOmte5Ii8L8_OQbYwPoiVHmZY6iJPbt_DhCN42AeFY74BcsUhR-BVrYUVhKK0RppuzEcSlILDNeQsJDLEL035CPm1VO6Jrgk7enQPIctVxUesRgswP71OpGvJxy3j1k_J8p0WzZvRZTe1D_2Misa0UDGwnEIHhmr97fIpMSZjFxlcygQw8QN34IHLHIXMaTY1eiCf4CCr6rOS9wUeu7P3CPkmFq9XhxBT_LLCmIMhHnxP5x27FUJE_JZlfek0MmARcrhpsZS2sFhHAiWrjxjOE27jkDtv1nEwn65wMw==",
477+
claims: jwt.MapClaims{"foo": "paddedbar"},
478+
paddedDecode: true,
479479
signingMethod: jwt.SigningMethodRS256,
480-
keyfunc: defaultKeyFunc,
481-
valid: true,
480+
keyfunc: defaultKeyFunc,
481+
valid: true,
482482
},
483483
}
484484

@@ -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)