session: implement function for receiving session public key bytes

After recent changes PrivateToken cannot directly return public key
bytes. In order to provide this ability, this commit implements
a function over PrivateToken interface.
This commit is contained in:
Leonard Lyubich 2020-05-18 13:14:18 +03:00
parent af28735ca6
commit 291d512840
3 changed files with 49 additions and 1 deletions

View file

@ -13,3 +13,7 @@ const ErrNilGPRCClientConn = internal.Error("gRPC client connection is nil")
// ErrPrivateTokenNotFound is returned when addressed private token was // ErrPrivateTokenNotFound is returned when addressed private token was
// not found in storage. // not found in storage.
const ErrPrivateTokenNotFound = internal.Error("private token not found") const ErrPrivateTokenNotFound = internal.Error("private token not found")
// ErrNilPrivateToken is returned by functions that expect a non-nil
// PrivateToken, but received nil.
const ErrNilPrivateToken = internal.Error("private token is nil")

View file

@ -4,6 +4,8 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"crypto/elliptic" "crypto/elliptic"
"crypto/rand" "crypto/rand"
crypto "github.com/nspcc-dev/neofs-crypto"
) )
type pToken struct { type pToken struct {
@ -28,7 +30,24 @@ func NewPrivateToken(validUntil uint64) (PrivateToken, error) {
}, nil }, nil
} }
// PrivateKey returns a binary representation of the session public key. // PublicSessionToken returns a binary representation of session public key.
//
// If passed PrivateToken is nil, ErrNilPrivateToken returns.
// If passed PrivateToken carries nil private key, crypto.ErrEmptyPrivateKey returns.
func PublicSessionToken(pToken PrivateToken) ([]byte, error) {
if pToken == nil {
return nil, ErrNilPrivateToken
}
sk := pToken.PrivateKey()
if sk == nil {
return nil, crypto.ErrEmptyPrivateKey
}
return crypto.MarshalPublicKey(&sk.PublicKey), nil
}
// PrivateKey is a session private key getter.
func (t *pToken) PrivateKey() *ecdsa.PrivateKey { func (t *pToken) PrivateKey() *ecdsa.PrivateKey {
return t.sessionKey return t.sessionKey
} }

View file

@ -3,6 +3,7 @@ package session
import ( import (
"testing" "testing"
crypto "github.com/nspcc-dev/neofs-crypto"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -49,3 +50,27 @@ func TestPrivateTokenKey_SetTokenID(t *testing.T) {
require.Equal(t, tokenID, s.token) require.Equal(t, tokenID, s.token)
} }
func TestPublicSessionToken(t *testing.T) {
var err error
// nil PrivateToken
_, err = PublicSessionToken(nil)
require.EqualError(t, err, ErrNilPrivateToken.Error())
// empty private key
var pToken PrivateToken = new(pToken)
_, err = PublicSessionToken(pToken)
require.EqualError(t, err, crypto.ErrEmptyPrivateKey.Error())
// correct PrivateToken
pToken, err = NewPrivateToken(0)
require.NoError(t, err)
key := pToken.PrivateKey()
require.NotNil(t, key)
res, err := PublicSessionToken(pToken)
require.NoError(t, err)
require.Equal(t, res, crypto.MarshalPublicKey(&key.PublicKey))
}