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"
|
2020-04-28 10:09:18 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/service"
|
2019-11-18 13:34:06 +00:00
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
|
|
|
)
|
|
|
|
|
|
|
|
type simpleStore struct {
|
|
|
|
*sync.RWMutex
|
|
|
|
|
|
|
|
tokens map[TokenID]*PToken
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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),
|
|
|
|
tokens: make(map[TokenID]*PToken),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns new token with specified parameters.
|
|
|
|
func (s *simpleStore) New(p TokenParams) *PToken {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
t := &PToken{
|
|
|
|
mtx: new(sync.Mutex),
|
|
|
|
Token: Token{
|
2020-04-28 10:09:18 +00:00
|
|
|
Token_Info: service.Token_Info{
|
|
|
|
ID: tid,
|
|
|
|
OwnerID: p.OwnerID,
|
|
|
|
Verb: p.Verb,
|
|
|
|
Address: p.Address,
|
|
|
|
Created: p.FirstEpoch,
|
|
|
|
ValidUntil: p.LastEpoch,
|
|
|
|
SessionKey: crypto.MarshalPublicKey(&key.PublicKey),
|
|
|
|
},
|
2019-11-18 13:34:06 +00:00
|
|
|
},
|
|
|
|
PrivateKey: key,
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Lock()
|
|
|
|
s.tokens[t.ID] = t
|
|
|
|
s.Unlock()
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch tries to fetch a token with specified id.
|
|
|
|
func (s *simpleStore) Fetch(id TokenID) *PToken {
|
|
|
|
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()
|
|
|
|
}
|