Adds JWS encode and decode

It also adds interfaces Signer and HS256 and RS256 implementation.
This commit is contained in:
2015-08-18 16:11:56 +02:00
parent 4eb3bdaa13
commit 00c5853b45
6 changed files with 591 additions and 25 deletions

View File

@@ -1,6 +1,7 @@
package jwt
import (
"encoding/base64"
"testing"
. "gopkg.in/check.v1"
@@ -16,19 +17,20 @@ var _ = Suite(&Base64Suite{})
type dataAndEncode struct {
data []byte
encoded string
encoded []byte
}
func (s *Base64Suite) TestEncodeWithNoTrailing(c *C) {
data := []dataAndEncode{
{[]byte{0, 0, 0}, "AAAA"}, //With trailing Should be AAAA
{[]byte{0, 0}, "AAA"}, //With trailing Should be AAA=
{[]byte{0}, "AA"}, //With trailing Should be AA==
{[]byte{0, 0, 0}, []byte("AAAA")}, //With trailing Should be AAAA
{[]byte{0, 0}, []byte("AAA")}, //With trailing Should be AAA=
{[]byte{0}, []byte("AA")}, //With trailing Should be AA==
}
for _, d := range data {
res := Base64Encode(d.data)
c.Check(res, Equals, d.encoded)
res := make([]byte, Base64EncodedBufferLen(len(d.data)))
base64.URLEncoding.Encode(res, d.data)
c.Check(res[:Base64EncodedStrippedLen(len(d.data))], DeepEquals, d.encoded)
}
}
@@ -41,11 +43,12 @@ func (s *Base64Suite) TestDecodeWithNoTrailing(c *C) {
}
for encoded, expected := range data {
res, err := Base64Decode(encoded)
res := make([]byte, Base64DecodedLenFromStripped(len(encoded)))
err := Base64Decode(res, []byte(encoded))
if c.Check(err, IsNil) == false {
continue
}
c.Check(res, DeepEquals, expected)
c.Check(res[:Base64DecodedStrippedLen(len(encoded))], DeepEquals, expected)
}
}
@@ -56,8 +59,8 @@ func (s *Base64Suite) TestDetectBadFormat(c *C) {
}
for encoded, errorMatches := range data {
res, err := Base64Decode(encoded)
res := make([]byte, Base64DecodedLenFromStripped(len(encoded)))
err := Base64Decode(res, []byte(encoded))
c.Check(err, ErrorMatches, errorMatches)
c.Check(res, IsNil)
}
}