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

@ -4,6 +4,8 @@ import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
crypto "github.com/nspcc-dev/neofs-crypto"
)
type pToken struct {
@ -28,7 +30,24 @@ func NewPrivateToken(validUntil uint64) (PrivateToken, error) {
}, 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 {
return t.sessionKey
}