-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9729749
commit b8ca1c0
Showing
6 changed files
with
108 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,39 @@ | ||
package jwt | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"encoding/json" | ||
"strings" | ||
) | ||
|
||
func decode(token string) (dh map[string]interface{}, dp map[string]interface{}, err error) { | ||
splited := strings.Split(token, ".") | ||
func decode(token []byte) (header map[string]interface{}, payload map[string]interface{}, err error) { | ||
segments := bytes.Split(token, periodBytes) | ||
|
||
if len(splited) != 3 { | ||
if len(segments) != 3 { | ||
return nil, nil, ErrInvalidToken | ||
} | ||
|
||
h, err := base64.StdEncoding.DecodeString(splited[0]) | ||
|
||
if err != nil { | ||
if header, err = decodeSegment(segments[0]); err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
if err := json.Unmarshal(h, dh); err != nil { | ||
if payload, err = decodeSegment(segments[1]); err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
p, err := base64.StdEncoding.DecodeString(splited[1]) | ||
return header, payload, nil | ||
} | ||
|
||
func decodeSegment(segment []byte) (m map[string]interface{}, err error) { | ||
s, err := base64.StdEncoding.DecodeString(string(segment)) | ||
|
||
if err != nil { | ||
return nil, nil, err | ||
return nil, err | ||
} | ||
|
||
if err := json.Unmarshal(p, dp); err != nil { | ||
return nil, nil, err | ||
if err := json.Unmarshal(s, &m); err != nil { | ||
return nil, err | ||
} | ||
|
||
return dh, dp, nil | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package jwt | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDecode(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
t.Run("Should return ErrInvalidToken when token is invalid", func(t *testing.T) { | ||
_, _, err := decode([]byte("a.b")) | ||
|
||
assert.Equal(ErrInvalidToken, err) | ||
}) | ||
|
||
t.Run("Should return origin header and payload", func(t *testing.T) { | ||
custom := map[string]interface{}{ | ||
"test1k": "test1v", | ||
"test2k": float64(234), | ||
} | ||
|
||
opt := &SignOption{ | ||
Algorithm: HS256, | ||
Issuer: "testIssuer", | ||
Subject: "tsetSubject", | ||
Audience: "testAudience", | ||
ExpiresIn: time.Minute, | ||
} | ||
|
||
signed, err := Sign(custom, "key", opt) | ||
|
||
header, payload, err := decode(signed) | ||
|
||
assert.Nil(err) | ||
assert.Equal(2, len(header)) | ||
assert.Equal(6, len(payload)) | ||
assert.Equal(string(HS256), header["alg"]) | ||
assert.Equal("JWT", header["typ"]) | ||
assert.Equal(opt.Subject, payload["sub"]) | ||
assert.Equal(opt.Issuer, payload["iss"]) | ||
assert.Equal(opt.Audience, payload["aud"]) | ||
assert.Equal(float64(60), payload["exp"]) | ||
assert.Equal(custom["test1k"], payload["test1k"]) | ||
assert.Equal(custom["test2k"], payload["test2k"]) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters