2020-09-29 15:11:20 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/session"
|
2020-09-29 15:11:20 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/session/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
// KeyStorage represents private key storage of the local node.
|
|
|
|
type KeyStorage struct {
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
tokenStore *storage.TokenStore
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewKeyStorage creates, initializes and returns new KeyStorage instance.
|
|
|
|
func NewKeyStorage(localKey *ecdsa.PrivateKey, tokenStore *storage.TokenStore) *KeyStorage {
|
|
|
|
return &KeyStorage{
|
|
|
|
key: localKey,
|
|
|
|
tokenStore: tokenStore,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
return pToken.SessionKey(), nil
|
2020-09-29 15:11:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.key, nil
|
|
|
|
}
|