fix: session invalidation
This commit is contained in:
@@ -2,24 +2,23 @@ package inmemory
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/authorizerdev/authorizer/server/memorystore/providers/inmemory/stores"
|
||||
)
|
||||
|
||||
type provider struct {
|
||||
mutex sync.Mutex
|
||||
sessionStore map[string]map[string]string
|
||||
stateStore map[string]string
|
||||
envStore *EnvStore
|
||||
sessionStore *stores.SessionStore
|
||||
stateStore *stores.StateStore
|
||||
envStore *stores.EnvStore
|
||||
}
|
||||
|
||||
// NewInMemoryStore returns a new in-memory store.
|
||||
func NewInMemoryProvider() (*provider, error) {
|
||||
return &provider{
|
||||
mutex: sync.Mutex{},
|
||||
sessionStore: map[string]map[string]string{},
|
||||
stateStore: map[string]string{},
|
||||
envStore: &EnvStore{
|
||||
mutex: sync.Mutex{},
|
||||
store: map[string]interface{}{},
|
||||
},
|
||||
envStore: stores.NewEnvStore(),
|
||||
sessionStore: stores.NewSessionStore(),
|
||||
stateStore: stores.NewStateStore(),
|
||||
}, nil
|
||||
}
|
||||
|
@@ -3,46 +3,44 @@ package inmemory
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/authorizerdev/authorizer/server/constants"
|
||||
)
|
||||
|
||||
// ClearStore clears the in-memory store.
|
||||
func (c *provider) ClearStore() error {
|
||||
if os.Getenv("ENV") != constants.TestEnv {
|
||||
c.mutex.Lock()
|
||||
defer c.mutex.Unlock()
|
||||
}
|
||||
c.sessionStore = map[string]map[string]string{}
|
||||
|
||||
// SetUserSession sets the user session
|
||||
func (c *provider) SetUserSession(userId, key, token string) error {
|
||||
c.sessionStore.Set(userId, key, token)
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetUserSessions returns all the user session token from the in-memory store.
|
||||
func (c *provider) GetUserSessions(userId string) map[string]string {
|
||||
res := map[string]string{}
|
||||
for k, v := range c.stateStore {
|
||||
split := strings.Split(v, "@")
|
||||
if split[1] == userId {
|
||||
res[k] = split[0]
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
// GetAllUserSessions returns all the user sessions token from the in-memory store.
|
||||
func (c *provider) GetAllUserSessions(userId string) (map[string]string, error) {
|
||||
data := c.sessionStore.GetAll(userId)
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// DeleteAllUserSession deletes all the user sessions from in-memory store.
|
||||
func (c *provider) DeleteAllUserSession(userId string) error {
|
||||
// GetUserSession returns value for given session token
|
||||
func (c *provider) GetUserSession(userId, sessionToken string) (string, error) {
|
||||
return c.sessionStore.Get(userId, sessionToken), nil
|
||||
}
|
||||
|
||||
// DeleteAllUserSessions deletes all the user sessions from in-memory store.
|
||||
func (c *provider) DeleteAllUserSessions(userId string) error {
|
||||
if os.Getenv("ENV") != constants.TestEnv {
|
||||
c.mutex.Lock()
|
||||
defer c.mutex.Unlock()
|
||||
}
|
||||
sessions := c.GetUserSessions(userId)
|
||||
for k := range sessions {
|
||||
c.RemoveState(k)
|
||||
}
|
||||
c.sessionStore.RemoveAll(userId)
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteUserSession deletes the user session from the in-memory store.
|
||||
func (c *provider) DeleteUserSession(userId, sessionToken string) error {
|
||||
if os.Getenv("ENV") != constants.TestEnv {
|
||||
c.mutex.Lock()
|
||||
defer c.mutex.Unlock()
|
||||
}
|
||||
c.sessionStore.Remove(userId, sessionToken)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -52,29 +50,19 @@ func (c *provider) SetState(key, state string) error {
|
||||
c.mutex.Lock()
|
||||
defer c.mutex.Unlock()
|
||||
}
|
||||
c.stateStore[key] = state
|
||||
c.stateStore.Set(key, state)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetState gets the state from the in-memory store.
|
||||
func (c *provider) GetState(key string) (string, error) {
|
||||
state := ""
|
||||
if stateVal, ok := c.stateStore[key]; ok {
|
||||
state = stateVal
|
||||
}
|
||||
|
||||
return state, nil
|
||||
return c.stateStore.Get(key), nil
|
||||
}
|
||||
|
||||
// RemoveState removes the state from the in-memory store.
|
||||
func (c *provider) RemoveState(key string) error {
|
||||
if os.Getenv("ENV") != constants.TestEnv {
|
||||
c.mutex.Lock()
|
||||
defer c.mutex.Unlock()
|
||||
}
|
||||
delete(c.stateStore, key)
|
||||
|
||||
c.stateStore.Remove(key)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package inmemory
|
||||
package stores
|
||||
|
||||
import (
|
||||
"os"
|
||||
@@ -13,6 +13,14 @@ type EnvStore struct {
|
||||
store map[string]interface{}
|
||||
}
|
||||
|
||||
// NewEnvStore create a new env store
|
||||
func NewEnvStore() *EnvStore {
|
||||
return &EnvStore{
|
||||
mutex: sync.Mutex{},
|
||||
store: make(map[string]interface{}),
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateEnvStore to update the whole env store object
|
||||
func (e *EnvStore) UpdateStore(store map[string]interface{}) {
|
||||
if os.Getenv("ENV") != constants.TestEnv {
|
@@ -0,0 +1,67 @@
|
||||
package stores
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/authorizerdev/authorizer/server/constants"
|
||||
)
|
||||
|
||||
// SessionStore struct to store the env variables
|
||||
type SessionStore struct {
|
||||
mutex sync.Mutex
|
||||
store map[string]map[string]string
|
||||
}
|
||||
|
||||
// NewSessionStore create a new session store
|
||||
func NewSessionStore() *SessionStore {
|
||||
return &SessionStore{
|
||||
mutex: sync.Mutex{},
|
||||
store: make(map[string]map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
// Get returns the value of the key in state store
|
||||
func (s *SessionStore) Get(key, subKey string) string {
|
||||
return s.store[key][subKey]
|
||||
}
|
||||
|
||||
// Set sets the value of the key in state store
|
||||
func (s *SessionStore) Set(key string, subKey, value string) {
|
||||
if os.Getenv("ENV") != constants.TestEnv {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
}
|
||||
if _, ok := s.store[key]; !ok {
|
||||
s.store[key] = make(map[string]string)
|
||||
}
|
||||
s.store[key][subKey] = value
|
||||
}
|
||||
|
||||
// RemoveAll all values for given key
|
||||
func (s *SessionStore) RemoveAll(key string) {
|
||||
if os.Getenv("ENV") != constants.TestEnv {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
}
|
||||
delete(s.store, key)
|
||||
}
|
||||
|
||||
// Remove value for given key and subkey
|
||||
func (s *SessionStore) Remove(key, subKey string) {
|
||||
if os.Getenv("ENV") != constants.TestEnv {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
}
|
||||
if _, ok := s.store[key]; ok {
|
||||
delete(s.store[key], subKey)
|
||||
}
|
||||
}
|
||||
|
||||
// Get all the values for given key
|
||||
func (s *SessionStore) GetAll(key string) map[string]string {
|
||||
if _, ok := s.store[key]; !ok {
|
||||
s.store[key] = make(map[string]string)
|
||||
}
|
||||
return s.store[key]
|
||||
}
|
46
server/memorystore/providers/inmemory/stores/state_store.go
Normal file
46
server/memorystore/providers/inmemory/stores/state_store.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package stores
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/authorizerdev/authorizer/server/constants"
|
||||
)
|
||||
|
||||
// StateStore struct to store the env variables
|
||||
type StateStore struct {
|
||||
mutex sync.Mutex
|
||||
store map[string]string
|
||||
}
|
||||
|
||||
// NewStateStore create a new state store
|
||||
func NewStateStore() *StateStore {
|
||||
return &StateStore{
|
||||
mutex: sync.Mutex{},
|
||||
store: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
// Get returns the value of the key in state store
|
||||
func (s *StateStore) Get(key string) string {
|
||||
return s.store[key]
|
||||
}
|
||||
|
||||
// Set sets the value of the key in state store
|
||||
func (s *StateStore) Set(key string, value string) {
|
||||
if os.Getenv("ENV") != constants.TestEnv {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
}
|
||||
s.store[key] = value
|
||||
}
|
||||
|
||||
// Remove removes the key from state store
|
||||
func (s *StateStore) Remove(key string) {
|
||||
if os.Getenv("ENV") != constants.TestEnv {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
}
|
||||
|
||||
delete(s.store, key)
|
||||
}
|
Reference in New Issue
Block a user