2020-09-29 15:11:20 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2021-10-25 12:10:13 +00:00
|
|
|
"errors"
|
2020-09-29 15:11:20 +00:00
|
|
|
|
2021-10-25 12:19:50 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
2020-09-29 15:11:20 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/session/storage"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
2020-09-29 15:11:20 +00:00
|
|
|
)
|
|
|
|
|
2021-10-25 12:19:50 +00:00
|
|
|
var (
|
|
|
|
errNoSessionToken = errors.New("session token does not exist")
|
|
|
|
errSessionTokenExpired = errors.New("session token has been expired")
|
|
|
|
)
|
2021-10-25 12:10:13 +00:00
|
|
|
|
2020-09-29 15:11:20 +00:00
|
|
|
// KeyStorage represents private key storage of the local node.
|
|
|
|
type KeyStorage struct {
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
tokenStore *storage.TokenStore
|
2021-10-25 12:19:50 +00:00
|
|
|
|
|
|
|
networkState netmap.State
|
2020-09-29 15:11:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewKeyStorage creates, initializes and returns new KeyStorage instance.
|
2021-10-25 12:19:50 +00:00
|
|
|
func NewKeyStorage(localKey *ecdsa.PrivateKey, tokenStore *storage.TokenStore, net netmap.State) *KeyStorage {
|
2020-09-29 15:11:20 +00:00
|
|
|
return &KeyStorage{
|
2021-10-25 12:19:50 +00:00
|
|
|
key: localKey,
|
|
|
|
tokenStore: tokenStore,
|
|
|
|
networkState: net,
|
2020-09-29 15:11:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetKey returns private key of the node.
|
|
|
|
//
|
|
|
|
// If token is not nil, session private key is returned.
|
|
|
|
// Otherwise, node private key is returned.
|
2021-05-31 11:03:17 +00:00
|
|
|
func (s *KeyStorage) GetKey(token *session.Token) (*ecdsa.PrivateKey, error) {
|
2020-09-29 15:11:20 +00:00
|
|
|
if token != nil {
|
|
|
|
pToken := s.tokenStore.Get(token.OwnerID(), token.ID())
|
2020-12-02 23:45:25 +00:00
|
|
|
if pToken != nil {
|
2021-10-25 12:19:50 +00:00
|
|
|
if pToken.ExpiredAt() <= s.networkState.CurrentEpoch() {
|
|
|
|
return nil, errSessionTokenExpired
|
|
|
|
}
|
2020-12-02 23:45:25 +00:00
|
|
|
return pToken.SessionKey(), nil
|
2020-09-29 15:11:20 +00:00
|
|
|
}
|
2021-10-25 12:10:13 +00:00
|
|
|
return nil, errNoSessionToken
|
2020-09-29 15:11:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.key, nil
|
|
|
|
}
|