func TestFlatIntegrity(t *testing.T) { j := New(dataRaw, crypto.SigningMethodRS512) b, err := j.Flat(rsaPriv) if err != nil { t.Error(err) } var jj struct { Payload json.RawMessage `json:"payload"` sigHead } if err := json.Unmarshal(b, &jj); err != nil { t.Error(err) } got, err := jose.DecodeEscaped(jj.Payload) if err != nil { t.Error(err) } if !bytes.Equal(got, dataSerialized) { Error(t, dataSerialized, got) } }
// UnmarshalJSON implements json.Unmarshaler for signature. func (s *Signature) UnmarshalJSON(b []byte) error { dec, err := jose.DecodeEscaped(b) if err != nil { return err } *s = Signature(dec) return nil }
// MarshalJSON implements json.Unmarshaler for payload. func (p *payload) UnmarshalJSON(b []byte) error { b2, err := jose.DecodeEscaped(b) if err != nil { return err } if p.u != nil { err := p.u.UnmarshalJSON(b2) p.v = p.u return err } return json.Unmarshal(b2, &p.v) }
// UnmarshalJSON implements json.Unmarshaler for Claims. func (c *Claims) UnmarshalJSON(b []byte) error { if b == nil { return nil } b, err := jose.DecodeEscaped(b) if err != nil { return err } // Since json.Unmarshal calls UnmarshalJSON, // calling json.Unmarshal on *p would be infinitely recursive // A temp variable is needed because &map[string]interface{}(*p) is // invalid Go. tmp := map[string]interface{}(*c) if err = json.Unmarshal(b, &tmp); err != nil { return err } *c = Claims(tmp) return nil }