v2/service: Fill SessionToken uni-structure

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-08-17 16:14:30 +03:00 committed by Stanislav Bogatyrev
parent ae2e951b77
commit 57dcf01f6b
2 changed files with 340 additions and 5 deletions

View file

@ -1,6 +1,8 @@
package service package service
import ( import (
"fmt"
"github.com/nspcc-dev/neofs-api-go/v2/acl" "github.com/nspcc-dev/neofs-api-go/v2/acl"
"github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/nspcc-dev/neofs-api-go/v2/refs"
service "github.com/nspcc-dev/neofs-api-go/v2/service/grpc" 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 { func SessionTokenToGRPCMessage(t *SessionToken) *service.SessionToken {
// TODO: fill me if t == nil {
return 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 { func SessionTokenFromGRPCMessage(m *service.SessionToken) *SessionToken {
// TODO: fill me if m == nil {
return nil return nil
}
t := new(SessionToken)
t.SetBody(
SessionTokenBodyFromGRPCMessage(m.GetBody()),
)
t.SetSignature(
SignatureFromGRPCMessage(m.GetSignature()),
)
return t
} }
func BearerTokenToGRPCMessage(t *BearerToken) *service.BearerToken { func BearerTokenToGRPCMessage(t *BearerToken) *service.BearerToken {
@ -509,3 +537,145 @@ func ResponseHeadersFromGRPC(
ResponseVerificationHeaderFromGRPCMessage(src.GetVerifyHeader()), 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
}

View file

@ -21,8 +21,34 @@ type TokenLifetime struct {
exp, nbf, iat uint64 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 { type SessionToken struct {
// TODO: fill me body *SessionTokenBody
sig *Signature
} }
type BearerTokenBody struct { type BearerTokenBody struct {
@ -79,6 +105,17 @@ type ResponseMetaHeader struct {
origin *ResponseMetaHeader origin *ResponseMetaHeader
} }
const (
ObjectVerbUnknown ObjectSessionVerb = iota
ObjectVerbPut
ObjectVerbGet
ObjectVerbHead
ObjectVerbSearch
ObjectVerbDelete
ObjectVerbRange
ObjectVerbRangeHash
)
func (s *Signature) GetKey() []byte { func (s *Signature) GetKey() []byte {
if s != nil { if s != nil {
return s.key return s.key
@ -586,3 +623,131 @@ func (r *ResponseMetaHeader) SetOrigin(v *ResponseMetaHeader) {
r.origin = v 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
}
}