diff --git a/parse.go b/parse.go index 94cc980..ac37562 100644 --- a/parse.go +++ b/parse.go @@ -69,7 +69,7 @@ func ParseAndVerify(raw []byte, verifier Verifier) (*Token, error) { if token.Header().Algorithm != verifier.Algorithm() { return nil, ErrAlgorithmMismatch } - if err := verifier.Verify(token.payload, token.signature); err != nil { + if err := verifier.Verify(token.Payload(), token.Signature()); err != nil { return nil, err } return token, nil diff --git a/parse_test.go b/parse_test.go index 62ac140..8edff6e 100644 --- a/parse_test.go +++ b/parse_test.go @@ -5,17 +5,48 @@ import ( ) func TestParseString(t *testing.T) { - f := func(got string, header Header, payload, signature string) { + f := func(token string, header Header, payload, signature string) { t.Helper() - _, err := ParseString(got) - if err == nil { - t.Error("got nil want nil") + tk, err := ParseString(token) + if err != nil { + t.Errorf("want nil, got %#v", err) + } + if tk.Header() != header { + t.Errorf("want %#v, got %#v", header, tk.Header()) + } + headerStr := toBase64(headerString(header)) + if string(tk.RawHeader()) != headerStr { + t.Errorf("want %#v, got %#v", headerStr, string(tk.RawHeader())) + } + if string(tk.Payload()) != payload { + t.Errorf("want %#v, got %#v", payload, string(tk.Payload())) + } + sign := toBase64(string(tk.Signature())) + if sign != signature { + t.Errorf("want %#v, got %#v", signature, sign) } } - // TODO - f(``, Header{}, "", "") + f( + `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJqdXN0IGFuIGlkIiwiYXVkIjoiYXVkaWVuY2UifQ.t5oEdZGp0Qbth7lo5fZlV_o4-r9gMoYBSktXbarjWoo`, + Header{ + Algorithm: HS256, + Type: "JWT", + }, + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJqdXN0IGFuIGlkIiwiYXVkIjoiYXVkaWVuY2UifQ", + "t5oEdZGp0Qbth7lo5fZlV_o4-r9gMoYBSktXbarjWoo", + ) + f( + `eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCIsImN0eSI6InRva2VuIn0.eyJqdGkiOiJqdXN0IGFuIGlkIiwiYXVkIjoiYXVkaWVuY2UifQ.t5oEdZGp0Qbth7lo5fZlV_o4-r9gMoYBSktXbarjWoo`, + Header{ + Algorithm: HS512, + Type: "JWT", + ContentType: "token", + }, + "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCIsImN0eSI6InRva2VuIn0.eyJqdGkiOiJqdXN0IGFuIGlkIiwiYXVkIjoiYXVkaWVuY2UifQ", + "t5oEdZGp0Qbth7lo5fZlV_o4-r9gMoYBSktXbarjWoo", + ) } func TestParseMalformed(t *testing.T) { @@ -35,3 +66,8 @@ func TestParseMalformed(t *testing.T) { f(`x/z.ab_c.xyz`) f(`ab_c.xyz.xyz`) } + +func headerString(header Header) string { + raw, _ := header.MarshalJSON() + return string(raw) +}