2019-11-18 13:34:06 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2020-04-29 09:39:41 +00:00
|
|
|
type mapTokenStore struct {
|
2019-11-18 13:34:06 +00:00
|
|
|
*sync.RWMutex
|
|
|
|
|
2020-04-29 08:52:05 +00:00
|
|
|
tokens map[TokenID]PrivateToken
|
2019-11-18 13:34:06 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 09:39:41 +00:00
|
|
|
// NewMapTokenStore creates new PrivateTokenStore instance.
|
|
|
|
//
|
|
|
|
// The elements of the instance are stored in the map.
|
|
|
|
func NewMapTokenStore() PrivateTokenStore {
|
|
|
|
return &mapTokenStore{
|
2019-11-18 13:34:06 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 09:39:41 +00:00
|
|
|
// Store adds passed token to the map.
|
|
|
|
//
|
|
|
|
// Resulting error is always nil.
|
|
|
|
func (s *mapTokenStore) Store(id TokenID, token PrivateToken) error {
|
2019-11-18 13:34:06 +00:00
|
|
|
s.Lock()
|
2020-04-29 09:39:41 +00:00
|
|
|
s.tokens[id] = token
|
2019-11-18 13:34:06 +00:00
|
|
|
s.Unlock()
|
|
|
|
|
2020-04-29 09:39:41 +00:00
|
|
|
return nil
|
2019-11-18 13:34:06 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 09:39:41 +00:00
|
|
|
// Fetch returns the map element corresponding to the given key.
|
|
|
|
//
|
|
|
|
// Returns ErrPrivateTokenNotFound is there is no element in map.
|
|
|
|
func (s *mapTokenStore) Fetch(id TokenID) (PrivateToken, error) {
|
2019-11-18 13:34:06 +00:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
2020-04-29 09:39:41 +00:00
|
|
|
t, ok := s.tokens[id]
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrPrivateTokenNotFound
|
|
|
|
}
|
2019-11-18 13:34:06 +00:00
|
|
|
|
2020-04-29 09:39:41 +00:00
|
|
|
return t, nil
|
2019-11-18 13:34:06 +00:00
|
|
|
}
|