diff --git a/pkg/token/session.go b/pkg/token/session.go index d9e1c47e..834110b3 100644 --- a/pkg/token/session.go +++ b/pkg/token/session.go @@ -76,3 +76,47 @@ func (t *SessionToken) Signature() *pkg.Signature { GetSignature(), ) } + +// Marshal marshals SessionToken into a protobuf binary form. +// +// Buffer is allocated when the argument is empty. +// Otherwise, the first buffer is used. +func (t *SessionToken) Marshal(bs ...[]byte) ([]byte, error) { + var buf []byte + if len(bs) > 0 { + buf = bs[0] + } + + return (*session.SessionToken)(t). + StableMarshal(buf) +} + +// Unmarshal unmarshals protobuf binary representation of SessionToken. +func (t *SessionToken) Unmarshal(data []byte) error { + tV2 := new(session.SessionToken) + if err := tV2.Unmarshal(data); err != nil { + return err + } + + *t = *NewSessionTokenFromV2(tV2) + + return nil +} + +// MarshalJSON encodes SessionToken to protobuf JSON format. +func (t *SessionToken) MarshalJSON() ([]byte, error) { + return (*session.SessionToken)(t). + MarshalJSON() +} + +// UnmarshalJSON decodes SessionToken from protobuf JSON format. +func (t *SessionToken) UnmarshalJSON(data []byte) error { + tV2 := new(session.SessionToken) + if err := tV2.UnmarshalJSON(data); err != nil { + return err + } + + *t = *NewSessionTokenFromV2(tV2) + + return nil +} diff --git a/pkg/token/session_test.go b/pkg/token/session_test.go index f155bc92..8a399ae7 100644 --- a/pkg/token/session_test.go +++ b/pkg/token/session_test.go @@ -40,3 +40,28 @@ func TestSessionToken_SetSessionKey(t *testing.T) { require.Equal(t, key, token.SessionKey()) } + +func TestSessionTokenEncoding(t *testing.T) { + tok := NewSessionToken() + tok.SetID([]byte("id")) + + t.Run("binary", func(t *testing.T) { + data, err := tok.Marshal() + require.NoError(t, err) + + tok2 := NewSessionToken() + require.NoError(t, tok2.Unmarshal(data)) + + require.Equal(t, tok, tok2) + }) + + t.Run("json", func(t *testing.T) { + data, err := tok.MarshalJSON() + require.NoError(t, err) + + tok2 := NewSessionToken() + require.NoError(t, tok2.UnmarshalJSON(data)) + + require.Equal(t, tok, tok2) + }) +} diff --git a/v2/session/json.go b/v2/session/json.go index d192b657..ec24b584 100644 --- a/v2/session/json.go +++ b/v2/session/json.go @@ -64,3 +64,23 @@ func (t *SessionTokenBody) UnmarshalJSON(data []byte) error { return nil } + +func (t *SessionToken) MarshalJSON() ([]byte, error) { + return protojson.MarshalOptions{ + EmitUnpopulated: true, + }.Marshal( + SessionTokenToGRPCMessage(t), + ) +} + +func (t *SessionToken) UnmarshalJSON(data []byte) error { + msg := new(session.SessionToken) + + if err := protojson.Unmarshal(data, msg); err != nil { + return err + } + + *t = *SessionTokenFromGRPCMessage(msg) + + return nil +} diff --git a/v2/session/json_test.go b/v2/session/json_test.go index 10eda4e1..eee4878c 100644 --- a/v2/session/json_test.go +++ b/v2/session/json_test.go @@ -42,3 +42,15 @@ func TestSessionTokenBodyJSON(t *testing.T) { require.Equal(t, b, b2) } + +func TestSessionTokenJSON(t *testing.T) { + tok := generateSessionToken("id") + + data, err := tok.MarshalJSON() + require.NoError(t, err) + + tok2 := new(session.SessionToken) + require.NoError(t, tok2.UnmarshalJSON(data)) + + require.Equal(t, tok, tok2) +} diff --git a/v2/session/marshal.go b/v2/session/marshal.go index 437147d2..5b21964f 100644 --- a/v2/session/marshal.go +++ b/v2/session/marshal.go @@ -417,6 +417,17 @@ func (t *SessionToken) StableSize() (size int) { return size } +func (t *SessionToken) Unmarshal(data []byte) error { + m := new(session.SessionToken) + if err := goproto.Unmarshal(data, m); err != nil { + return err + } + + *t = *SessionTokenFromGRPCMessage(m) + + return nil +} + func (r *RequestMetaHeader) StableMarshal(buf []byte) ([]byte, error) { if r == nil { return []byte{}, nil diff --git a/v2/session/marshal_test.go b/v2/session/marshal_test.go index 1dc5397e..5d257a57 100644 --- a/v2/session/marshal_test.go +++ b/v2/session/marshal_test.go @@ -104,16 +104,14 @@ func TestSessionTokenBody_StableMarshal(t *testing.T) { func TestSessionToken_StableMarshal(t *testing.T) { sessionTokenFrom := generateSessionToken("Session Token") - transport := new(grpc.SessionToken) t.Run("non empty", func(t *testing.T) { wire, err := sessionTokenFrom.StableMarshal(nil) require.NoError(t, err) - err = goproto.Unmarshal(wire, transport) - require.NoError(t, err) + sessionTokenTo := new(session.SessionToken) + require.NoError(t, sessionTokenTo.Unmarshal(wire)) - sessionTokenTo := session.SessionTokenFromGRPCMessage(transport) require.Equal(t, sessionTokenFrom, sessionTokenTo) }) }