Merge pull request #69 from nspcc-dev/new-fields-getters-setters

New fields getters setters
This commit is contained in:
Evgeniy Kulikov 2020-04-28 17:42:53 +03:00 committed by GitHub
commit 091f953ebf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 274 additions and 0 deletions

View file

@ -37,6 +37,18 @@ type (
OwnerID chain.WalletAddress OwnerID chain.WalletAddress
) )
// OwnerIDContainer is an interface of the container of an OwnerID value.
type OwnerIDContainer interface {
GetOwnerID() OwnerID
SetOwnerID(OwnerID)
}
// AddressContainer is an interface of the container of object address value.
type AddressContainer interface {
GetAddress() Address
SetAddress(Address)
}
const ( const (
// UUIDSize contains size of UUID. // UUIDSize contains size of UUID.
UUIDSize = 16 UUIDSize = 16

View file

@ -25,6 +25,9 @@ type (
// VersionHeader allows get or set version of protocol request // VersionHeader allows get or set version of protocol request
VersionHeader VersionHeader
// RawHeader allows to get and set raw option of request
RawHeader
} }
// EpochHeader interface gives possibility to get or set epoch in RPC Requests. // EpochHeader interface gives possibility to get or set epoch in RPC Requests.
@ -39,6 +42,12 @@ type (
SetVersion(uint32) SetVersion(uint32)
} }
// RawHeader is an interface of the container of a boolean Raw value
RawHeader interface {
GetRaw() bool
SetRaw(bool)
}
// TTLCondition is closure, that allows to validate request with ttl. // TTLCondition is closure, that allows to validate request with ttl.
TTLCondition func(ttl uint32) error TTLCondition func(ttl uint32) error
) )
@ -77,6 +86,11 @@ func (m *RequestMetaHeader) SetTTL(v uint32) { m.TTL = v }
// SetEpoch sets Epoch to RequestMetaHeader. // SetEpoch sets Epoch to RequestMetaHeader.
func (m *RequestMetaHeader) SetEpoch(v uint64) { m.Epoch = v } func (m *RequestMetaHeader) SetEpoch(v uint64) { m.Epoch = v }
// SetRaw is a Raw field setter.
func (m *RequestMetaHeader) SetRaw(raw bool) {
m.Raw = raw
}
// ResetMeta returns current value and sets RequestMetaHeader to empty value. // ResetMeta returns current value and sets RequestMetaHeader to empty value.
func (m *RequestMetaHeader) ResetMeta() RequestMetaHeader { func (m *RequestMetaHeader) ResetMeta() RequestMetaHeader {
cp := *m cp := *m

View file

@ -102,3 +102,13 @@ func TestRequestMetaHeader_SetVersion(t *testing.T) {
m.SetVersion(version) m.SetVersion(version)
require.Equal(t, version, m.GetVersion()) require.Equal(t, version, m.GetVersion())
} }
func TestRequestMetaHeader_SetRaw(t *testing.T) {
m := new(RequestMetaHeader)
m.SetRaw(true)
require.True(t, m.GetRaw())
m.SetRaw(false)
require.False(t, m.GetRaw())
}

125
service/token.go Normal file
View file

@ -0,0 +1,125 @@
package service
import (
"github.com/nspcc-dev/neofs-api-go/refs"
)
// VerbContainer is an interface of the container of a token verb value.
type VerbContainer interface {
GetVerb() Token_Info_Verb
SetVerb(Token_Info_Verb)
}
// TokenIDContainer is an interface of the container of a token ID value.
type TokenIDContainer interface {
GetID() TokenID
SetID(TokenID)
}
// CreationEpochContainer is an interface of the container of a creation epoch number.
type CreationEpochContainer interface {
CreationEpoch() uint64
SetCreationEpoch(uint64)
}
// ExpirationEpochContainer is an interface of the container of an expiration epoch number.
type ExpirationEpochContainer interface {
ExpirationEpoch() uint64
SetExpirationEpoch(uint64)
}
// SessionKeyContainer is an interface of the container of session key bytes.
type SessionKeyContainer interface {
GetSessionKey() []byte
SetSessionKey([]byte)
}
// SignatureContainer is an interface of the container of signature bytes.
type SignatureContainer interface {
GetSignature() []byte
SetSignature([]byte)
}
// SessionTokenInfo is an interface that determines the information scope of session token.
type SessionTokenInfo interface {
TokenIDContainer
refs.OwnerIDContainer
VerbContainer
refs.AddressContainer
CreationEpochContainer
ExpirationEpochContainer
SessionKeyContainer
}
// SessionToken is an interface of token information and signature pair.
type SessionToken interface {
SessionTokenInfo
SignatureContainer
}
var _ SessionToken = (*Token)(nil)
// GetID is an ID field getter.
func (m Token_Info) GetID() TokenID {
return m.ID
}
// SetID is an ID field setter.
func (m *Token_Info) SetID(id TokenID) {
m.ID = id
}
// GetOwnerID is an OwnerID field getter.
func (m Token_Info) GetOwnerID() OwnerID {
return m.OwnerID
}
// SetOwnerID is an OwnerID field setter.
func (m *Token_Info) SetOwnerID(id OwnerID) {
m.OwnerID = id
}
// SetVerb is a Verb field setter.
func (m *Token_Info) SetVerb(verb Token_Info_Verb) {
m.Verb = verb
}
// GetAddress is an Address field getter.
func (m Token_Info) GetAddress() Address {
return m.Address
}
// SetAddress is an Address field setter.
func (m *Token_Info) SetAddress(addr Address) {
m.Address = addr
}
// CreationEpoch is a Created field getter.
func (m Token_Info) CreationEpoch() uint64 {
return m.Created
}
// SetCreationEpoch is a Created field setter.
func (m *Token_Info) SetCreationEpoch(e uint64) {
m.Created = e
}
// ExpirationEpoch is a ValidUntil field getter.
func (m Token_Info) ExpirationEpoch() uint64 {
return m.ValidUntil
}
// SetExpirationEpoch is a ValidUntil field setter.
func (m *Token_Info) SetExpirationEpoch(e uint64) {
m.ValidUntil = e
}
// SetSessionKey is a SessionKey field setter.
func (m *Token_Info) SetSessionKey(key []byte) {
m.SessionKey = key
}
// SetSignature is a Signature field setter.
func (m *Token) SetSignature(sig []byte) {
m.Signature = sig
}

88
service/token_test.go Normal file
View file

@ -0,0 +1,88 @@
package service
import (
"crypto/rand"
"testing"
"github.com/nspcc-dev/neofs-api-go/refs"
"github.com/stretchr/testify/require"
)
func TestTokenGettersSetters(t *testing.T) {
var tok SessionToken = new(Token)
{ // ID
id, err := refs.NewUUID()
require.NoError(t, err)
tok.SetID(id)
require.Equal(t, id, tok.GetID())
}
{ // OwnerID
ownerID := OwnerID{}
_, err := rand.Read(ownerID[:])
require.NoError(t, err)
tok.SetOwnerID(ownerID)
require.Equal(t, ownerID, tok.GetOwnerID())
}
{ // Verb
verb := Token_Info_Verb(3)
tok.SetVerb(verb)
require.Equal(t, verb, tok.GetVerb())
}
{ // Address
addr := Address{}
_, err := rand.Read(addr.CID[:])
require.NoError(t, err)
_, err = rand.Read(addr.ObjectID[:])
require.NoError(t, err)
tok.SetAddress(addr)
require.Equal(t, addr, tok.GetAddress())
}
{ // Created
e := uint64(5)
tok.SetCreationEpoch(e)
require.Equal(t, e, tok.CreationEpoch())
}
{ // ValidUntil
e := uint64(5)
tok.SetExpirationEpoch(e)
require.Equal(t, e, tok.ExpirationEpoch())
}
{ // SessionKey
key := make([]byte, 10)
_, err := rand.Read(key)
require.NoError(t, err)
tok.SetSessionKey(key)
require.Equal(t, key, tok.GetSessionKey())
}
{ // Signature
sig := make([]byte, 10)
_, err := rand.Read(sig)
require.NoError(t, err)
tok.SetSignature(sig)
require.Equal(t, sig, tok.GetSignature())
}
}

View file

@ -27,6 +27,12 @@ type (
SetOwner(*ecdsa.PublicKey, []byte) SetOwner(*ecdsa.PublicKey, []byte)
GetLastPeer() (*ecdsa.PublicKey, error) GetLastPeer() (*ecdsa.PublicKey, error)
} }
// TokenHeader is an interface of the container of a Token pointer value
TokenHeader interface {
GetToken() *Token
SetToken(*Token)
}
) )
const ( const (
@ -97,6 +103,11 @@ func (m *RequestVerificationHeader) GetLastPeer() (*ecdsa.PublicKey, error) {
} }
} }
// SetToken is a Token field setter.
func (m *RequestVerificationHeader) SetToken(token *Token) {
m.Token = token
}
func newSignature(key *ecdsa.PrivateKey, data []byte) (*RequestVerificationHeader_Signature, error) { func newSignature(key *ecdsa.PrivateKey, data []byte) (*RequestVerificationHeader_Signature, error) {
sign, err := crypto.Sign(key, data) sign, err := crypto.Sign(key, data)
if err != nil { if err != nil {

View file

@ -188,3 +188,17 @@ func TestVerifyAndSignRequestHeaderWithoutCloning(t *testing.T) {
require.Contains(t, buf.String(), "proto: don't know how to copy") require.Contains(t, buf.String(), "proto: don't know how to copy")
} }
func TestRequestVerificationHeader_SetToken(t *testing.T) {
id, err := refs.NewUUID()
require.NoError(t, err)
token := new(Token)
token.ID = id
h := new(RequestVerificationHeader)
h.SetToken(token)
require.Equal(t, token, h.GetToken())
}