2019-11-18 13:34:06 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/rand"
|
|
|
|
"testing"
|
|
|
|
|
2020-01-30 13:32:50 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api/refs"
|
2019-11-18 13:34:06 +00:00
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testClient struct {
|
|
|
|
*ecdsa.PrivateKey
|
|
|
|
OwnerID OwnerID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testClient) Sign(data []byte) ([]byte, error) {
|
|
|
|
return crypto.Sign(c.PrivateKey, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestClient(t *testing.T) *testClient {
|
|
|
|
key, err := ecdsa.GenerateKey(defaultCurve(), rand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
owner, err := refs.NewOwnerID(&key.PublicKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return &testClient{PrivateKey: key, OwnerID: owner}
|
|
|
|
}
|
|
|
|
|
|
|
|
func signToken(t *testing.T, token *PToken, c *testClient) {
|
|
|
|
require.NotNil(t, token)
|
2020-01-17 09:39:00 +00:00
|
|
|
token.SetPublicKeys(&c.PublicKey)
|
2019-11-18 13:34:06 +00:00
|
|
|
|
|
|
|
signH, err := c.Sign(token.Header.PublicKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, signH)
|
|
|
|
|
|
|
|
// data is not yet signed
|
2020-01-17 09:39:00 +00:00
|
|
|
keys := UnmarshalPublicKeys(&token.Token)
|
|
|
|
require.False(t, token.Verify(keys...))
|
2019-11-18 13:34:06 +00:00
|
|
|
|
|
|
|
signT, err := c.Sign(token.verificationData())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, signT)
|
|
|
|
|
|
|
|
token.AddSignatures(signH, signT)
|
2020-01-17 09:39:00 +00:00
|
|
|
require.True(t, token.Verify(keys...))
|
2019-11-18 13:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTokenStore(t *testing.T) {
|
|
|
|
s := NewSimpleStore()
|
|
|
|
|
|
|
|
oid, err := refs.NewObjectID()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
c := newTestClient(t)
|
|
|
|
require.NotNil(t, c)
|
2020-01-17 11:56:51 +00:00
|
|
|
pk := [][]byte{crypto.MarshalPublicKey(&c.PublicKey)}
|
2019-11-18 13:34:06 +00:00
|
|
|
|
|
|
|
// create new token
|
2020-01-17 11:56:51 +00:00
|
|
|
token := s.New(TokenParams{
|
2020-01-30 13:32:50 +00:00
|
|
|
ObjectID: []ObjectID{oid},
|
|
|
|
OwnerID: c.OwnerID,
|
2020-01-17 11:56:51 +00:00
|
|
|
PublicKeys: pk,
|
|
|
|
})
|
2019-11-18 13:34:06 +00:00
|
|
|
signToken(t, token, c)
|
|
|
|
|
|
|
|
// check that it can be fetched
|
|
|
|
t1 := s.Fetch(token.ID)
|
|
|
|
require.NotNil(t, t1)
|
|
|
|
require.Equal(t, token, t1)
|
|
|
|
|
|
|
|
// create and sign another token by the same client
|
2020-01-17 11:56:51 +00:00
|
|
|
t1 = s.New(TokenParams{
|
2020-01-30 13:32:50 +00:00
|
|
|
ObjectID: []ObjectID{oid},
|
|
|
|
OwnerID: c.OwnerID,
|
|
|
|
PublicKeys: pk,
|
|
|
|
})
|
2020-01-17 11:56:51 +00:00
|
|
|
|
2019-11-18 13:34:06 +00:00
|
|
|
signToken(t, t1, c)
|
|
|
|
|
|
|
|
data := []byte{1, 2, 3}
|
|
|
|
sign, err := t1.SignData(data)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Error(t, token.Header.VerifyData(data, sign))
|
|
|
|
|
|
|
|
sign, err = token.SignData(data)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, token.Header.VerifyData(data, sign))
|
|
|
|
|
|
|
|
s.Remove(token.ID)
|
|
|
|
require.Nil(t, s.Fetch(token.ID))
|
|
|
|
require.NotNil(t, s.Fetch(t1.ID))
|
|
|
|
}
|