Skip to content
This repository was archived by the owner on Apr 22, 2024. It is now read-only.

Commit 81ddea8

Browse files
authored
Merge pull request #18 from henderjon/master
Algorithm.Sign() was pre-encoding the signature; the rest of the lib then double encoded them
2 parents 0c02049 + 2479b53 commit 81ddea8

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

algorithms.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ func (a *Algorithm) write(data []byte) (int, error) {
4141
}
4242

4343
// Sign signs the token with the given hash, and key
44-
func (a *Algorithm) Sign(unsignedToken string) (string, error) {
44+
func (a *Algorithm) Sign(unsignedToken string) ([]byte, error) {
4545
_, err := a.write([]byte(unsignedToken))
4646
if err != nil {
47-
return "", errors.Wrap(err, "Unable to write to HMAC-SHA256")
47+
return nil, errors.Wrap(err, "Unable to write to HMAC-SHA256")
4848
}
4949

50-
encodedToken := base64.RawURLEncoding.EncodeToString(a.sum(nil))
50+
encodedToken := a.sum(nil)
5151
a.reset()
5252

5353
return encodedToken, nil

jwt_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ func RunTest(t *testing.T, command func(Algorithm)) {
2424
func TestEncodeAndValidateToken(t *testing.T) {
2525
RunTest(t, func(algorithm Algorithm) {
2626
payload := NewClaim()
27-
payload.SetTime("nbf", time.Now().Add(time.Duration(-1) * time.Hour))
28-
payload.SetTime("exp", time.Now().Add(time.Duration(100) * time.Hour))
27+
payload.SetTime("nbf", time.Now().Add(time.Duration(-1)*time.Hour))
28+
payload.SetTime("exp", time.Now().Add(time.Duration(100)*time.Hour))
2929

3030
token, err := algorithm.Encode(payload)
3131
if err != nil {
@@ -90,7 +90,7 @@ func TestVerifyTokenNbf(t *testing.T) {
9090
RunTest(t, func(algorithm Algorithm) {
9191

9292
payload := NewClaim()
93-
payload.SetTime("nbf", time.Now().Add(time.Duration(1) * time.Hour))
93+
payload.SetTime("nbf", time.Now().Add(time.Duration(1)*time.Hour))
9494

9595
err := json.Unmarshal([]byte(`{"sub":"1234567890","name":"John Doe","admin":true}`), &payload)
9696
if err != nil {
@@ -120,3 +120,14 @@ func TestDecodeMalformedToken(t *testing.T) {
120120
}
121121
})
122122
}
123+
124+
func TestValidateExternalToken(t *testing.T) {
125+
token := "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImp0aSI6ImZmNzJkMWM5LTMzMTktNGIyOS04YjlhLWU1OThkNGJhNDRlZCJ9.eyJpc3MiOiJodHRwOi8vbG9jYWwuaG9zdC5jb20iLCJhdWQiOiJodHRwOi8vbG9jYWwuaG9zdC5jb20iLCJqdGkiOiJmZjcyZDFjOS0zMzE5LTRiMjktOGI5YS1lNTk4ZDRiYTQ0ZWQiLCJpYXQiOjE1MTkzMjc2NDYsIm5iZiI6MTUxOTMyNzY1MCwiZXhwIjoxNjQwMzkwNDAwfQ.ASo8eiekkwZ7on43S9n697x-SqmdehY680GetK_KqpI"
126+
127+
algorithm := HmacSha256("this-needs-a-test")
128+
129+
err := algorithm.Validate(token)
130+
if err != nil {
131+
t.Fatal(err)
132+
}
133+
}

0 commit comments

Comments
 (0)