session: change PrivateToken interface methods
This commit replaces PublicKey() and SignData() methods of PrivateToken with PrivateKey() in order to have the ability to sign data with session key using service package functions.
This commit is contained in:
parent
ba27e296c3
commit
af28735ca6
3 changed files with 7 additions and 39 deletions
|
@ -4,8 +4,6 @@ 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 {
|
||||||
|
@ -30,14 +28,9 @@ func NewPrivateToken(validUntil uint64) (PrivateToken, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sign signs data with session private key.
|
// PrivateKey returns a binary representation of the session public key.
|
||||||
func (t *pToken) Sign(data []byte) ([]byte, error) {
|
func (t *pToken) PrivateKey() *ecdsa.PrivateKey {
|
||||||
return crypto.Sign(t.sessionKey, data)
|
return t.sessionKey
|
||||||
}
|
|
||||||
|
|
||||||
// PublicKey returns a binary representation of the session public key.
|
|
||||||
func (t *pToken) PublicKey() []byte {
|
|
||||||
return crypto.MarshalPublicKey(&t.sessionKey.PublicKey)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *pToken) Expired(epoch uint64) bool {
|
func (t *pToken) Expired(epoch uint64) bool {
|
||||||
|
|
|
@ -1,35 +1,16 @@
|
||||||
package session
|
package session
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
crypto "github.com/nspcc-dev/neofs-crypto"
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPrivateToken(t *testing.T) {
|
func TestPToken_PrivateKey(t *testing.T) {
|
||||||
// create new private token
|
// create new private token
|
||||||
pToken, err := NewPrivateToken(0)
|
pToken, err := NewPrivateToken(0)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, pToken.PrivateKey())
|
||||||
// generate data to sign
|
|
||||||
data := make([]byte, 10)
|
|
||||||
_, err = rand.Read(data)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
// sign data via private token
|
|
||||||
sig, err := pToken.Sign(data)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
// check signature
|
|
||||||
require.NoError(t,
|
|
||||||
crypto.Verify(
|
|
||||||
crypto.UnmarshalPublicKey(pToken.PublicKey()),
|
|
||||||
data,
|
|
||||||
sig,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPToken_Expired(t *testing.T) {
|
func TestPToken_Expired(t *testing.T) {
|
||||||
|
|
|
@ -10,14 +10,8 @@ import (
|
||||||
|
|
||||||
// PrivateToken is an interface of session private part.
|
// PrivateToken is an interface of session private part.
|
||||||
type PrivateToken interface {
|
type PrivateToken interface {
|
||||||
// PublicKey must return a binary representation of session public key.
|
// PrivateKey must return session private key.
|
||||||
PublicKey() []byte
|
PrivateKey() *ecdsa.PrivateKey
|
||||||
|
|
||||||
// Sign must return the signature of passed data.
|
|
||||||
//
|
|
||||||
// Resulting signature must be verified by crypto.Verify function
|
|
||||||
// with the session public key.
|
|
||||||
Sign([]byte) ([]byte, error)
|
|
||||||
|
|
||||||
// Expired must return true if and only if private token is expired in the given epoch number.
|
// Expired must return true if and only if private token is expired in the given epoch number.
|
||||||
Expired(uint64) bool
|
Expired(uint64) bool
|
||||||
|
|
Loading…
Reference in a new issue