frostfs-node/pkg/services/object/util/key.go
Evgenii Stratonikov 95893927aa *: replace neofs-api-go with neofs-sdk-go
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2021-11-12 17:29:09 +03:00

53 lines
1.4 KiB
Go

package util
import (
"crypto/ecdsa"
"errors"
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
"github.com/nspcc-dev/neofs-node/pkg/services/session/storage"
"github.com/nspcc-dev/neofs-sdk-go/session"
)
var (
// todo(alexvanin): should be a part of status API
errNoSessionToken = errors.New("session token does not exist")
errSessionTokenExpired = errors.New("session token has been expired")
)
// KeyStorage represents private key storage of the local node.
type KeyStorage struct {
key *ecdsa.PrivateKey
tokenStore *storage.TokenStore
networkState netmap.State
}
// NewKeyStorage creates, initializes and returns new KeyStorage instance.
func NewKeyStorage(localKey *ecdsa.PrivateKey, tokenStore *storage.TokenStore, net netmap.State) *KeyStorage {
return &KeyStorage{
key: localKey,
tokenStore: tokenStore,
networkState: net,
}
}
// GetKey returns private key of the node.
//
// If token is not nil, session private key is returned.
// Otherwise, node private key is returned.
func (s *KeyStorage) GetKey(token *session.Token) (*ecdsa.PrivateKey, error) {
if token != nil {
pToken := s.tokenStore.Get(token.OwnerID(), token.ID())
if pToken != nil {
if pToken.ExpiredAt() <= s.networkState.CurrentEpoch() {
return nil, errSessionTokenExpired
}
return pToken.SessionKey(), nil
}
return nil, errNoSessionToken
}
return s.key, nil
}