authorizer/server/sessionstore/in_memory_session.go

113 lines
2.7 KiB
Go
Raw Normal View History

package sessionstore
import (
"sync"
)
2022-01-17 06:02:13 +00:00
// InMemoryStore is a simple in-memory store for sessions.
type InMemoryStore struct {
2022-02-28 15:56:49 +00:00
mutex sync.Mutex
sessionStore map[string]map[string]string
stateStore map[string]string
}
2022-01-17 06:02:13 +00:00
// AddUserSession adds a user session to the in-memory store.
func (c *InMemoryStore) AddUserSession(userId, accessToken, refreshToken string) {
c.mutex.Lock()
defer c.mutex.Unlock()
// delete sessions > 500 // not recommended for production
2022-02-28 15:56:49 +00:00
if len(c.sessionStore) >= 500 {
c.sessionStore = map[string]map[string]string{}
}
// check if entry exists in map
2022-02-28 15:56:49 +00:00
_, exists := c.sessionStore[userId]
if exists {
2022-02-28 15:56:49 +00:00
tempMap := c.sessionStore[userId]
tempMap[accessToken] = refreshToken
2022-02-28 15:56:49 +00:00
c.sessionStore[userId] = tempMap
} else {
tempMap := map[string]string{
accessToken: refreshToken,
}
2022-02-28 15:56:49 +00:00
c.sessionStore[userId] = tempMap
}
}
2022-01-17 06:02:13 +00:00
// DeleteAllUserSession deletes all the user sessions from in-memory store.
func (c *InMemoryStore) DeleteAllUserSession(userId string) {
c.mutex.Lock()
defer c.mutex.Unlock()
2022-02-28 15:56:49 +00:00
delete(c.sessionStore, userId)
}
2022-01-17 06:02:13 +00:00
// DeleteUserSession deletes the particular user session from in-memory store.
func (c *InMemoryStore) DeleteUserSession(userId, accessToken string) {
c.mutex.Lock()
defer c.mutex.Unlock()
2022-02-28 15:56:49 +00:00
delete(c.sessionStore[userId], accessToken)
}
2022-01-17 06:02:13 +00:00
// ClearStore clears the in-memory store.
func (c *InMemoryStore) ClearStore() {
2022-01-17 06:02:13 +00:00
c.mutex.Lock()
defer c.mutex.Unlock()
2022-02-28 15:56:49 +00:00
c.sessionStore = map[string]map[string]string{}
}
2022-01-17 06:02:13 +00:00
// GetUserSession returns the user session token from the in-memory store.
func (c *InMemoryStore) GetUserSession(userId, accessToken string) string {
// c.mutex.Lock()
// defer c.mutex.Unlock()
token := ""
2022-02-28 15:56:49 +00:00
if sessionMap, ok := c.sessionStore[userId]; ok {
if val, ok := sessionMap[accessToken]; ok {
token = val
}
}
return token
}
// GetUserSessions returns all the user session token from the in-memory store.
func (c *InMemoryStore) GetUserSessions(userId string) map[string]string {
// c.mutex.Lock()
// defer c.mutex.Unlock()
2022-02-28 15:56:49 +00:00
sessionMap, ok := c.sessionStore[userId]
if !ok {
return nil
}
return sessionMap
}
2022-02-28 15:56:49 +00:00
// SetState sets the state in the in-memory store.
func (c *InMemoryStore) SetState(key, state string) {
2022-01-17 06:02:13 +00:00
c.mutex.Lock()
defer c.mutex.Unlock()
2022-02-28 15:56:49 +00:00
c.stateStore[key] = state
}
2022-02-28 15:56:49 +00:00
// GetState gets the state from the in-memory store.
func (c *InMemoryStore) GetState(key string) string {
2022-01-17 06:02:13 +00:00
c.mutex.Lock()
defer c.mutex.Unlock()
state := ""
2022-02-28 15:56:49 +00:00
if stateVal, ok := c.stateStore[key]; ok {
state = stateVal
}
return state
}
2022-02-28 15:56:49 +00:00
// RemoveState removes the state from the in-memory store.
func (c *InMemoryStore) RemoveState(key string) {
2022-01-17 06:02:13 +00:00
c.mutex.Lock()
defer c.mutex.Unlock()
2022-02-28 15:56:49 +00:00
delete(c.stateStore, key)
}