[#150] sdk/token: Add owner ID

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-09-17 18:04:04 +03:00 committed by Stanislav Bogatyrev
parent 73220620c5
commit 0259a06783
4 changed files with 104 additions and 34 deletions

View file

@ -1034,6 +1034,8 @@ func (c Client) attachV2SessionToken(opts callOptions, hdr *v2session.RequestMet
return nil
}
token := opts.session.ToV2()
opCtx := new(v2session.ObjectSessionContext)
opCtx.SetAddress(info.addr)
opCtx.SetVerb(info.verb)
@ -1043,15 +1045,11 @@ func (c Client) attachV2SessionToken(opts callOptions, hdr *v2session.RequestMet
lt.SetNbf(info.nbf)
lt.SetExp(info.exp)
body := new(v2session.SessionTokenBody)
body.SetID(opts.session.ID())
body := token.GetBody()
body.SetSessionKey(opts.session.SessionKey())
body.SetContext(opCtx)
body.SetLifetime(lt)
token := new(v2session.SessionToken)
token.SetBody(body)
signWrapper := signature.StableMarshalerWrapper{SM: token.GetBody()}
err := signer.SignDataWithHandler(c.key, signWrapper, func(key []byte, sig []byte) {
sessionTokenSignature := new(v2refs.Signature)

View file

@ -70,10 +70,10 @@ func (c Client) createSessionV2(ctx context.Context, expiration uint64, opts ...
return nil, errors.New("malformed response body")
}
sessionToken, err := token.CreateSessionToken(body.GetID(), body.GetSessionKey())
if err != nil {
return nil, errors.Wrap(err, "malformed response body")
}
sessionToken := token.NewSessionToken()
sessionToken.SetID(body.GetID())
sessionToken.SetSessionKey(body.GetSessionKey())
sessionToken.SetOwnerID(ownerID)
return sessionToken, nil
default:

View file

@ -1,40 +1,70 @@
package token
import (
"github.com/google/uuid"
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
"github.com/nspcc-dev/neofs-api-go/v2/session"
)
type SessionToken struct {
id uuid.UUID
pubKey []byte
type SessionToken session.SessionToken
func NewSessionTokenFromV2(tV2 *session.SessionToken) *SessionToken {
return (*SessionToken)(tV2)
}
func CreateSessionToken(id, pub []byte) (*SessionToken, error) {
var tokenID uuid.UUID
func NewSessionToken() *SessionToken {
return NewSessionTokenFromV2(new(session.SessionToken))
}
err := tokenID.UnmarshalBinary(id)
if err != nil {
return nil, err
func (t *SessionToken) ToV2() *session.SessionToken {
return (*session.SessionToken)(t)
}
func (t *SessionToken) setBodyField(setter func(*session.SessionTokenBody)) {
token := (*session.SessionToken)(t)
body := token.GetBody()
if body == nil {
body = new(session.SessionTokenBody)
token.SetBody(body)
}
key := make([]byte, len(pub))
copy(key[:], pub)
return &SessionToken{
id: tokenID,
pubKey: key,
}, nil
setter(body)
}
func (s SessionToken) SessionKey() []byte {
return s.pubKey
func (t *SessionToken) ID() []byte {
return (*session.SessionToken)(t).
GetBody().
GetID()
}
func (s SessionToken) ID() []byte {
data, err := s.id.MarshalBinary()
if err != nil {
panic(err) // must never panic
}
return data
func (t *SessionToken) SetID(v []byte) {
t.setBodyField(func(body *session.SessionTokenBody) {
body.SetID(v)
})
}
func (t *SessionToken) OwnerID() *owner.ID {
return owner.NewIDFromV2(
(*session.SessionToken)(t).
GetBody().
GetOwnerID(),
)
}
func (t *SessionToken) SetOwnerID(v *owner.ID) {
t.setBodyField(func(body *session.SessionTokenBody) {
body.SetOwnerID(v.ToV2())
})
}
func (t *SessionToken) SessionKey() []byte {
return (*session.SessionToken)(t).
GetBody().
GetSessionKey()
}
func (t *SessionToken) SetSessionKey(v []byte) {
t.setBodyField(func(body *session.SessionTokenBody) {
body.SetSessionKey(v)
})
}

42
pkg/token/session_test.go Normal file
View file

@ -0,0 +1,42 @@
package token
import (
"crypto/rand"
"testing"
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
"github.com/stretchr/testify/require"
)
func TestSessionToken_SetID(t *testing.T) {
token := NewSessionToken()
id := []byte{1, 2, 3}
token.SetID(id)
require.Equal(t, id, token.ID())
}
func TestSessionToken_SetOwnerID(t *testing.T) {
token := NewSessionToken()
w := new(owner.NEO3Wallet)
_, err := rand.Read(w.Bytes())
require.NoError(t, err)
ownerID := owner.NewID()
ownerID.SetNeo3Wallet(w)
token.SetOwnerID(ownerID)
require.Equal(t, ownerID, token.OwnerID())
}
func TestSessionToken_SetSessionKey(t *testing.T) {
token := NewSessionToken()
key := []byte{1, 2, 3}
token.SetSessionKey(key)
require.Equal(t, key, token.SessionKey())
}