forked from TrueCloudLab/frostfs-node
[#57] services/object: Implement private key storage
Implement storage that provides access to local node key and session keys through session token. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
2da323c4b9
commit
be322835af
1 changed files with 41 additions and 0 deletions
41
pkg/services/object/util/key.go
Normal file
41
pkg/services/object/util/key.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
|
||||
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/services/session/storage"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// 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.
|
||||
func (s *KeyStorage) GetKey(token *token.SessionToken) (*ecdsa.PrivateKey, error) {
|
||||
if token != nil {
|
||||
pToken := s.tokenStore.Get(token.OwnerID(), token.ID())
|
||||
if pToken == nil {
|
||||
return nil, errors.Wrapf(storage.ErrNotFound, "(%T) could not get session key", s)
|
||||
}
|
||||
|
||||
return pToken.SessionKey(), nil
|
||||
}
|
||||
|
||||
return s.key, nil
|
||||
}
|
Loading…
Reference in a new issue