diff --git a/v2/service/convert.go b/v2/service/convert.go index e5786ed4..91b173bf 100644 --- a/v2/service/convert.go +++ b/v2/service/convert.go @@ -1,6 +1,8 @@ package service import ( + "fmt" + "github.com/nspcc-dev/neofs-api-go/v2/acl" "github.com/nspcc-dev/neofs-api-go/v2/refs" service "github.com/nspcc-dev/neofs-api-go/v2/service/grpc" @@ -59,13 +61,39 @@ func XHeaderFromGRPCMessage(m *service.XHeader) *XHeader { } func SessionTokenToGRPCMessage(t *SessionToken) *service.SessionToken { - // TODO: fill me - return nil + if t == nil { + return nil + } + + m := new(service.SessionToken) + + m.SetBody( + SessionTokenBodyToGRPCMessage(t.GetBody()), + ) + + m.SetSignature( + SignatureToGRPCMessage(t.GetSignature()), + ) + + return m } func SessionTokenFromGRPCMessage(m *service.SessionToken) *SessionToken { - // TODO: fill me - return nil + if m == nil { + return nil + } + + t := new(SessionToken) + + t.SetBody( + SessionTokenBodyFromGRPCMessage(m.GetBody()), + ) + + t.SetSignature( + SignatureFromGRPCMessage(m.GetSignature()), + ) + + return t } func BearerTokenToGRPCMessage(t *BearerToken) *service.BearerToken { @@ -509,3 +537,145 @@ func ResponseHeadersFromGRPC( ResponseVerificationHeaderFromGRPCMessage(src.GetVerifyHeader()), ) } + +func ObjectSessionVerbToGRPCField(v ObjectSessionVerb) service.ObjectServiceContext_Verb { + switch v { + case ObjectVerbPut: + return service.ObjectServiceContext_PUT + case ObjectVerbGet: + return service.ObjectServiceContext_GET + case ObjectVerbHead: + return service.ObjectServiceContext_HEAD + case ObjectVerbSearch: + return service.ObjectServiceContext_SEARCH + case ObjectVerbDelete: + return service.ObjectServiceContext_DELETE + case ObjectVerbRange: + return service.ObjectServiceContext_RANGE + case ObjectVerbRangeHash: + return service.ObjectServiceContext_RANGEHASH + default: + return service.ObjectServiceContext_VERB_UNSPECIFIED + } +} + +func ObjectSessionVerbFromGRPCField(v service.ObjectServiceContext_Verb) ObjectSessionVerb { + switch v { + case service.ObjectServiceContext_PUT: + return ObjectVerbPut + case service.ObjectServiceContext_GET: + return ObjectVerbGet + case service.ObjectServiceContext_HEAD: + return ObjectVerbHead + case service.ObjectServiceContext_SEARCH: + return ObjectVerbSearch + case service.ObjectServiceContext_DELETE: + return ObjectVerbDelete + case service.ObjectServiceContext_RANGE: + return ObjectVerbRange + case service.ObjectServiceContext_RANGEHASH: + return ObjectVerbRangeHash + default: + return ObjectVerbUnknown + } +} + +func ObjectSessionContextToGRPCMessage(c *ObjectSessionContext) *service.ObjectServiceContext { + if c == nil { + return nil + } + + m := new(service.ObjectServiceContext) + + m.SetVerb( + ObjectSessionVerbToGRPCField(c.GetVerb()), + ) + + m.SetAddress( + refs.AddressToGRPCMessage(c.GetAddress()), + ) + + return m +} + +func ObjectSessionContextFromGRPCMessage(m *service.ObjectServiceContext) *ObjectSessionContext { + if m == nil { + return nil + } + + c := new(ObjectSessionContext) + + c.SetVerb( + ObjectSessionVerbFromGRPCField(m.GetVerb()), + ) + + c.SetAddress( + refs.AddressFromGRPCMessage(m.GetAddress()), + ) + + return c +} + +func SessionTokenBodyToGRPCMessage(t *SessionTokenBody) *service.SessionToken_Body { + if t == nil { + return nil + } + + m := new(service.SessionToken_Body) + + switch v := t.GetContext(); t := v.(type) { + case nil: + case *ObjectSessionContext: + m.SetObjectServiceContext( + ObjectSessionContextToGRPCMessage(t), + ) + default: + panic(fmt.Sprintf("unknown session context %T", t)) + } + + m.SetId(t.GetID()) + + m.SetOwnerId( + refs.OwnerIDToGRPCMessage(t.GetOwnerID()), + ) + + m.SetLifetime( + TokenLifetimeToGRPCMessage(t.GetLifetime()), + ) + + m.SetSessionKey(t.GetSessionKey()) + + return m +} + +func SessionTokenBodyFromGRPCMessage(m *service.SessionToken_Body) *SessionTokenBody { + if m == nil { + return nil + } + + t := new(SessionTokenBody) + + switch v := m.GetContext().(type) { + case nil: + case *service.SessionToken_Body_ObjectService: + t.SetContext( + ObjectSessionContextFromGRPCMessage(v.ObjectService), + ) + default: + panic(fmt.Sprintf("unknown session context %T", v)) + } + + t.SetID(m.GetId()) + + t.SetOwnerID( + refs.OwnerIDFromGRPCMessage(m.GetOwnerId()), + ) + + t.SetLifetime( + TokenLifetimeFromGRPCMessage(m.GetLifetime()), + ) + + t.SetSessionKey(m.GetSessionKey()) + + return t +} diff --git a/v2/service/service.go b/v2/service/service.go index 89df919f..61e33cb8 100644 --- a/v2/service/service.go +++ b/v2/service/service.go @@ -21,8 +21,34 @@ type TokenLifetime struct { exp, nbf, iat uint64 } +type ObjectSessionVerb uint32 + +type ObjectSessionContext struct { + verb ObjectSessionVerb + + addr *refs.Address +} + +type SessionTokenContext interface { + sessionTokenContext() +} + +type SessionTokenBody struct { + id []byte + + ownerID *refs.OwnerID + + lifetime *TokenLifetime + + sessionKey []byte + + ctx SessionTokenContext +} + type SessionToken struct { - // TODO: fill me + body *SessionTokenBody + + sig *Signature } type BearerTokenBody struct { @@ -79,6 +105,17 @@ type ResponseMetaHeader struct { origin *ResponseMetaHeader } +const ( + ObjectVerbUnknown ObjectSessionVerb = iota + ObjectVerbPut + ObjectVerbGet + ObjectVerbHead + ObjectVerbSearch + ObjectVerbDelete + ObjectVerbRange + ObjectVerbRangeHash +) + func (s *Signature) GetKey() []byte { if s != nil { return s.key @@ -586,3 +623,131 @@ func (r *ResponseMetaHeader) SetOrigin(v *ResponseMetaHeader) { r.origin = v } } + +func (c *ObjectSessionContext) sessionTokenContext() {} + +func (c *ObjectSessionContext) GetVerb() ObjectSessionVerb { + if c != nil { + return c.verb + } + + return ObjectVerbUnknown +} + +func (c *ObjectSessionContext) SetVerb(v ObjectSessionVerb) { + if c != nil { + c.verb = v + } +} + +func (c *ObjectSessionContext) GetAddress() *refs.Address { + if c != nil { + return c.addr + } + + return nil +} + +func (c *ObjectSessionContext) SetAddress(v *refs.Address) { + if c != nil { + c.addr = v + } +} + +func (t *SessionTokenBody) GetID() []byte { + if t != nil { + return t.id + } + + return nil +} + +func (t *SessionTokenBody) SetID(v []byte) { + if t != nil { + t.id = v + } +} + +func (t *SessionTokenBody) GetOwnerID() *refs.OwnerID { + if t != nil { + return t.ownerID + } + + return nil +} + +func (t *SessionTokenBody) SetOwnerID(v *refs.OwnerID) { + if t != nil { + t.ownerID = v + } +} + +func (t *SessionTokenBody) GetLifetime() *TokenLifetime { + if t != nil { + return t.lifetime + } + + return nil +} + +func (t *SessionTokenBody) SetLifetime(v *TokenLifetime) { + if t != nil { + t.lifetime = v + } +} + +func (t *SessionTokenBody) GetSessionKey() []byte { + if t != nil { + return t.sessionKey + } + + return nil +} + +func (t *SessionTokenBody) SetSessionKey(v []byte) { + if t != nil { + t.sessionKey = v + } +} + +func (t *SessionTokenBody) GetContext() SessionTokenContext { + if t != nil { + return t.ctx + } + + return nil +} + +func (t *SessionTokenBody) SetContext(v SessionTokenContext) { + if t != nil { + t.ctx = v + } +} + +func (t *SessionToken) GetBody() *SessionTokenBody { + if t != nil { + return t.body + } + + return nil +} + +func (t *SessionToken) SetBody(v *SessionTokenBody) { + if t != nil { + t.body = v + } +} + +func (t *SessionToken) GetSignature() *Signature { + if t != nil { + return t.sig + } + + return nil +} + +func (t *SessionToken) SetSignature(v *Signature) { + if t != nil { + t.sig = v + } +}