2020-09-29 15:11:20 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
|
2021-10-25 12:19:50 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
2022-03-21 14:56:56 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/session/storage"
|
2022-03-17 06:36:09 +00:00
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
2022-03-21 14:56:56 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
2020-09-29 15:11:20 +00:00
|
|
|
)
|
|
|
|
|
2022-03-21 14:56:56 +00:00
|
|
|
// SessionSource is an interface tha provides
|
|
|
|
// access to node's actual (not expired) session
|
|
|
|
// tokens.
|
|
|
|
type SessionSource interface {
|
|
|
|
// Get must return non-expired private token that
|
|
|
|
// corresponds with passed owner and tokenID. If
|
|
|
|
// token has not been created, has been expired
|
|
|
|
// of it is impossible to get information about the
|
|
|
|
// token Get must return nil.
|
|
|
|
Get(owner *owner.ID, tokenID []byte) *storage.PrivateToken
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:11:20 +00:00
|
|
|
// KeyStorage represents private key storage of the local node.
|
|
|
|
type KeyStorage struct {
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
2022-03-21 14:56:56 +00:00
|
|
|
tokenStore SessionSource
|
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.
|
2022-03-21 14:56:56 +00:00
|
|
|
func NewKeyStorage(localKey *ecdsa.PrivateKey, tokenStore SessionSource, 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() {
|
2022-03-17 06:36:09 +00:00
|
|
|
var errExpired apistatus.SessionTokenExpired
|
|
|
|
|
|
|
|
return nil, errExpired
|
2021-10-25 12:19:50 +00:00
|
|
|
}
|
2020-12-02 23:45:25 +00:00
|
|
|
return pToken.SessionKey(), nil
|
2020-09-29 15:11:20 +00:00
|
|
|
}
|
2022-03-17 06:36:09 +00:00
|
|
|
|
|
|
|
var errNotFound apistatus.SessionTokenNotFound
|
|
|
|
|
|
|
|
return nil, errNotFound
|
2020-09-29 15:11:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.key, nil
|
|
|
|
}
|