2020-09-29 15:11:20 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2022-05-18 15:20:08 +00:00
|
|
|
"fmt"
|
2020-09-29 15:11:20 +00:00
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
"github.com/google/uuid"
|
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-05-17 13:59:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
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.
|
2022-05-31 17:00:41 +00:00
|
|
|
Get(owner user.ID, tokenID []byte) *storage.PrivateToken
|
2022-03-21 14:56:56 +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
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
// SessionInfo groups information about NeoFS Object session
|
|
|
|
// which is reflected in KeyStorage.
|
|
|
|
type SessionInfo struct {
|
|
|
|
// Session unique identifier.
|
|
|
|
ID uuid.UUID
|
|
|
|
|
|
|
|
// Session issuer.
|
|
|
|
Owner user.ID
|
|
|
|
}
|
|
|
|
|
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.
|
2022-05-18 15:20:08 +00:00
|
|
|
func (s *KeyStorage) GetKey(info *SessionInfo) (*ecdsa.PrivateKey, error) {
|
|
|
|
if info != nil {
|
|
|
|
binID, err := info.ID.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("marshal ID: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
pToken := s.tokenStore.Get(info.Owner, binID)
|
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
|
|
|
|
}
|