Skip to content

Commit

Permalink
fix: jwt-decode can not decode token contains emoji
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudingcity committed Dec 7, 2020
1 parent 637c403 commit 9042ead
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pkg/jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ func Decode(token string) (string, error) {
return "", errors.New("jwt decode: invalid JWT structure")
}

header, err := base64.RawStdEncoding.DecodeString(s[0])
header, err := base64.RawURLEncoding.DecodeString(s[0])
if err != nil {
return "", errors.New("jwt decode: base64 decode header failed")
}

payload, err := base64.RawStdEncoding.DecodeString(s[1])
payload, err := base64.RawURLEncoding.DecodeString(s[1])
if err != nil {
return "", errors.New("jwt decode: base64 decode payload failed")
}
Expand Down
23 changes: 18 additions & 5 deletions pkg/jwt/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,24 @@ import (

func TestDecode(t *testing.T) {
t.Run("success", func(t *testing.T) {
token := `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c`
got, err := Decode(token)
want := `{"header":{"alg":"HS256","typ":"JWT"},"payload":{"name":"John Doe","sub":"1234567890","iat":1516239022},"signature":"SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"}`
assert.NoError(t, err)
assert.JSONEq(t, want, got)
tests := []struct {
token string
want string
}{
{
token: `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c`,
want: `{"header":{"alg":"HS256","typ":"JWT"},"payload":{"name":"John Doe","sub":"1234567890","iat":1516239022},"signature":"SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"}`,
},
{
token: `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9l8J-OgyIsImlhdCI6MTUxNjIzOTAyMn0.LJEPu1YqnXWREF3ooCnu-jIKS-fesGumsPC4xPjZ68Y`,
want: `{"header":{"alg":"HS256","typ":"JWT"},"payload":{"name":"John Doe🎃","sub":"1234567890","iat":1516239022},"signature":"LJEPu1YqnXWREF3ooCnu-jIKS-fesGumsPC4xPjZ68Y"}`,
},
}
for _, test := range tests {
got, err := Decode(test.token)
assert.NoError(t, err)
assert.JSONEq(t, test.want, got)
}
})

t.Run("failed", func(t *testing.T) {
Expand Down

0 comments on commit 9042ead

Please sign in to comment.