forked from TrueCloudLab/frostfs-api-go
[#150] sdk/token: Add owner ID
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
73220620c5
commit
0259a06783
4 changed files with 104 additions and 34 deletions
|
@ -1034,6 +1034,8 @@ func (c Client) attachV2SessionToken(opts callOptions, hdr *v2session.RequestMet
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
token := opts.session.ToV2()
|
||||||
|
|
||||||
opCtx := new(v2session.ObjectSessionContext)
|
opCtx := new(v2session.ObjectSessionContext)
|
||||||
opCtx.SetAddress(info.addr)
|
opCtx.SetAddress(info.addr)
|
||||||
opCtx.SetVerb(info.verb)
|
opCtx.SetVerb(info.verb)
|
||||||
|
@ -1043,15 +1045,11 @@ func (c Client) attachV2SessionToken(opts callOptions, hdr *v2session.RequestMet
|
||||||
lt.SetNbf(info.nbf)
|
lt.SetNbf(info.nbf)
|
||||||
lt.SetExp(info.exp)
|
lt.SetExp(info.exp)
|
||||||
|
|
||||||
body := new(v2session.SessionTokenBody)
|
body := token.GetBody()
|
||||||
body.SetID(opts.session.ID())
|
|
||||||
body.SetSessionKey(opts.session.SessionKey())
|
body.SetSessionKey(opts.session.SessionKey())
|
||||||
body.SetContext(opCtx)
|
body.SetContext(opCtx)
|
||||||
body.SetLifetime(lt)
|
body.SetLifetime(lt)
|
||||||
|
|
||||||
token := new(v2session.SessionToken)
|
|
||||||
token.SetBody(body)
|
|
||||||
|
|
||||||
signWrapper := signature.StableMarshalerWrapper{SM: token.GetBody()}
|
signWrapper := signature.StableMarshalerWrapper{SM: token.GetBody()}
|
||||||
err := signer.SignDataWithHandler(c.key, signWrapper, func(key []byte, sig []byte) {
|
err := signer.SignDataWithHandler(c.key, signWrapper, func(key []byte, sig []byte) {
|
||||||
sessionTokenSignature := new(v2refs.Signature)
|
sessionTokenSignature := new(v2refs.Signature)
|
||||||
|
|
|
@ -70,10 +70,10 @@ func (c Client) createSessionV2(ctx context.Context, expiration uint64, opts ...
|
||||||
return nil, errors.New("malformed response body")
|
return nil, errors.New("malformed response body")
|
||||||
}
|
}
|
||||||
|
|
||||||
sessionToken, err := token.CreateSessionToken(body.GetID(), body.GetSessionKey())
|
sessionToken := token.NewSessionToken()
|
||||||
if err != nil {
|
sessionToken.SetID(body.GetID())
|
||||||
return nil, errors.Wrap(err, "malformed response body")
|
sessionToken.SetSessionKey(body.GetSessionKey())
|
||||||
}
|
sessionToken.SetOwnerID(ownerID)
|
||||||
|
|
||||||
return sessionToken, nil
|
return sessionToken, nil
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -1,40 +1,70 @@
|
||||||
package token
|
package token
|
||||||
|
|
||||||
import (
|
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 {
|
type SessionToken session.SessionToken
|
||||||
id uuid.UUID
|
|
||||||
pubKey []byte
|
func NewSessionTokenFromV2(tV2 *session.SessionToken) *SessionToken {
|
||||||
|
return (*SessionToken)(tV2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateSessionToken(id, pub []byte) (*SessionToken, error) {
|
func NewSessionToken() *SessionToken {
|
||||||
var tokenID uuid.UUID
|
return NewSessionTokenFromV2(new(session.SessionToken))
|
||||||
|
}
|
||||||
|
|
||||||
err := tokenID.UnmarshalBinary(id)
|
func (t *SessionToken) ToV2() *session.SessionToken {
|
||||||
if err != nil {
|
return (*session.SessionToken)(t)
|
||||||
return nil, err
|
}
|
||||||
|
|
||||||
|
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))
|
setter(body)
|
||||||
copy(key[:], pub)
|
|
||||||
|
|
||||||
return &SessionToken{
|
|
||||||
id: tokenID,
|
|
||||||
pubKey: key,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SessionToken) SessionKey() []byte {
|
func (t *SessionToken) ID() []byte {
|
||||||
return s.pubKey
|
return (*session.SessionToken)(t).
|
||||||
|
GetBody().
|
||||||
|
GetID()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SessionToken) ID() []byte {
|
func (t *SessionToken) SetID(v []byte) {
|
||||||
data, err := s.id.MarshalBinary()
|
t.setBodyField(func(body *session.SessionTokenBody) {
|
||||||
if err != nil {
|
body.SetID(v)
|
||||||
panic(err) // must never panic
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return data
|
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
42
pkg/token/session_test.go
Normal 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())
|
||||||
|
}
|
Loading…
Reference in a new issue