2019-11-18 13:34:06 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
|
|
|
"sync"
|
|
|
|
|
2020-03-31 07:05:26 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/refs"
|
2019-11-18 13:34:06 +00:00
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
|
|
|
)
|
|
|
|
|
|
|
|
type simpleStore struct {
|
|
|
|
*sync.RWMutex
|
|
|
|
|
2020-04-29 08:52:05 +00:00
|
|
|
tokens map[TokenID]PrivateToken
|
2019-11-18 13:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO get curve from neofs-crypto
|
|
|
|
func defaultCurve() elliptic.Curve {
|
|
|
|
return elliptic.P256()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSimpleStore creates simple token storage
|
|
|
|
func NewSimpleStore() TokenStore {
|
|
|
|
return &simpleStore{
|
|
|
|
RWMutex: new(sync.RWMutex),
|
2020-04-29 08:52:05 +00:00
|
|
|
tokens: make(map[TokenID]PrivateToken),
|
2019-11-18 13:34:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns new token with specified parameters.
|
2020-04-29 08:52:05 +00:00
|
|
|
func (s *simpleStore) New(p TokenParams) PrivateToken {
|
2019-11-18 13:34:06 +00:00
|
|
|
tid, err := refs.NewUUID()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := ecdsa.GenerateKey(defaultCurve(), rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.FirstEpoch > p.LastEpoch || p.OwnerID.Empty() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-28 15:40:21 +00:00
|
|
|
token := new(Token)
|
|
|
|
token.SetID(tid)
|
|
|
|
token.SetOwnerID(p.OwnerID)
|
|
|
|
token.SetVerb(p.Verb)
|
|
|
|
token.SetAddress(p.Address)
|
|
|
|
token.SetCreationEpoch(p.FirstEpoch)
|
|
|
|
token.SetExpirationEpoch(p.LastEpoch)
|
|
|
|
token.SetSessionKey(crypto.MarshalPublicKey(&key.PublicKey))
|
|
|
|
|
2020-04-29 08:52:05 +00:00
|
|
|
t := &pToken{
|
|
|
|
sessionKey: key,
|
2019-11-18 13:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.Lock()
|
2020-04-28 15:40:21 +00:00
|
|
|
s.tokens[tid] = t
|
2019-11-18 13:34:06 +00:00
|
|
|
s.Unlock()
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch tries to fetch a token with specified id.
|
2020-04-29 08:52:05 +00:00
|
|
|
func (s *simpleStore) Fetch(id TokenID) PrivateToken {
|
2019-11-18 13:34:06 +00:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
|
|
|
return s.tokens[id]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes token with id from store.
|
|
|
|
func (s *simpleStore) Remove(id TokenID) {
|
|
|
|
s.Lock()
|
|
|
|
delete(s.tokens, id)
|
|
|
|
s.Unlock()
|
|
|
|
}
|