2022-01-22 19:54:41 +00:00
|
|
|
package sessionstore
|
2021-07-14 18:43:19 +00:00
|
|
|
|
2021-10-27 17:45:38 +00:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
2021-07-14 18:43:19 +00:00
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// InMemoryStore is a simple in-memory store for sessions.
|
2021-07-14 18:43:19 +00:00
|
|
|
type InMemoryStore struct {
|
2022-01-17 06:02:13 +00:00
|
|
|
mutex sync.Mutex
|
2021-10-27 17:45:38 +00:00
|
|
|
store map[string]map[string]string
|
|
|
|
socialLoginState map[string]string
|
2021-07-14 18:43:19 +00:00
|
|
|
}
|
|
|
|
|
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()
|
2021-07-14 18:43:19 +00:00
|
|
|
// delete sessions > 500 // not recommended for production
|
|
|
|
if len(c.store) >= 500 {
|
2021-10-27 17:45:38 +00:00
|
|
|
c.store = map[string]map[string]string{}
|
2021-07-14 18:43:19 +00:00
|
|
|
}
|
2021-10-27 17:45:38 +00:00
|
|
|
// check if entry exists in map
|
|
|
|
_, exists := c.store[userId]
|
|
|
|
if exists {
|
|
|
|
tempMap := c.store[userId]
|
|
|
|
tempMap[accessToken] = refreshToken
|
|
|
|
c.store[userId] = tempMap
|
|
|
|
} else {
|
|
|
|
tempMap := map[string]string{
|
|
|
|
accessToken: refreshToken,
|
|
|
|
}
|
|
|
|
c.store[userId] = tempMap
|
|
|
|
}
|
2021-07-14 18:43:19 +00:00
|
|
|
}
|
|
|
|
|
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()
|
2021-07-14 18:43:19 +00:00
|
|
|
delete(c.store, 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()
|
2021-10-27 17:45:38 +00:00
|
|
|
delete(c.store[userId], accessToken)
|
|
|
|
}
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// ClearStore clears the in-memory store.
|
2021-07-14 18:43:19 +00:00
|
|
|
func (c *InMemoryStore) ClearStore() {
|
2022-01-17 06:02:13 +00:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
2021-10-27 17:45:38 +00:00
|
|
|
c.store = map[string]map[string]string{}
|
2021-07-14 18:43:19 +00:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2021-07-14 18:43:19 +00:00
|
|
|
token := ""
|
2021-10-27 17:45:38 +00:00
|
|
|
if sessionMap, ok := c.store[userId]; ok {
|
|
|
|
if val, ok := sessionMap[accessToken]; ok {
|
|
|
|
token = val
|
|
|
|
}
|
2021-07-14 18:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return token
|
|
|
|
}
|
2021-10-27 17:45:38 +00:00
|
|
|
|
2022-01-22 19:54:41 +00:00
|
|
|
// 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()
|
|
|
|
|
|
|
|
sessionMap, ok := c.store[userId]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return sessionMap
|
|
|
|
}
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// SetSocialLoginState sets the social login state in the in-memory store.
|
2021-10-27 17:45:38 +00:00
|
|
|
func (c *InMemoryStore) SetSocialLoginState(key, state string) {
|
2022-01-17 06:02:13 +00:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2021-10-27 17:45:38 +00:00
|
|
|
c.socialLoginState[key] = state
|
|
|
|
}
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// GetSocialLoginState gets the social login state from the in-memory store.
|
2021-10-27 17:45:38 +00:00
|
|
|
func (c *InMemoryStore) GetSocialLoginState(key string) string {
|
2022-01-17 06:02:13 +00:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2021-10-27 17:45:38 +00:00
|
|
|
state := ""
|
|
|
|
if stateVal, ok := c.socialLoginState[key]; ok {
|
|
|
|
state = stateVal
|
|
|
|
}
|
|
|
|
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// RemoveSocialLoginState removes the social login state from the in-memory store.
|
2021-10-27 17:45:38 +00:00
|
|
|
func (c *InMemoryStore) RemoveSocialLoginState(key string) {
|
2022-01-17 06:02:13 +00:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2021-10-27 17:45:38 +00:00
|
|
|
delete(c.socialLoginState, key)
|
|
|
|
}
|