2021-07-14 18:43:19 +00:00
|
|
|
package session
|
|
|
|
|
2021-10-27 17:45:38 +00:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
2021-07-14 18:43:19 +00:00
|
|
|
|
|
|
|
type InMemoryStore struct {
|
2021-10-27 17:45:38 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
store map[string]map[string]string
|
|
|
|
socialLoginState map[string]string
|
2021-07-14 18:43:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 17:45:38 +00:00
|
|
|
func (c *InMemoryStore) AddToken(userId, accessToken, refreshToken string) {
|
2021-07-14 18:43:19 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
// 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
|
|
|
c.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-10-27 17:45:38 +00:00
|
|
|
func (c *InMemoryStore) DeleteUserSession(userId string) {
|
2021-07-14 18:43:19 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
delete(c.store, userId)
|
|
|
|
c.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-12-17 15:55:07 +00:00
|
|
|
func (c *InMemoryStore) DeleteVerificationRequest(userId, accessToken string) {
|
2021-10-27 17:45:38 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
delete(c.store[userId], accessToken)
|
|
|
|
c.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-07-14 18:43:19 +00:00
|
|
|
func (c *InMemoryStore) ClearStore() {
|
|
|
|
c.mu.Lock()
|
2021-10-27 17:45:38 +00:00
|
|
|
c.store = map[string]map[string]string{}
|
2021-07-14 18:43:19 +00:00
|
|
|
c.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-10-27 17:45:38 +00:00
|
|
|
func (c *InMemoryStore) GetToken(userId, accessToken string) string {
|
2021-07-14 18:43:19 +00:00
|
|
|
token := ""
|
|
|
|
c.mu.Lock()
|
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
|
|
|
}
|
|
|
|
c.mu.Unlock()
|
|
|
|
|
|
|
|
return token
|
|
|
|
}
|
2021-10-27 17:45:38 +00:00
|
|
|
|
|
|
|
func (c *InMemoryStore) SetSocialLoginState(key, state string) {
|
|
|
|
c.mu.Lock()
|
|
|
|
c.socialLoginState[key] = state
|
|
|
|
c.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *InMemoryStore) GetSocialLoginState(key string) string {
|
|
|
|
state := ""
|
|
|
|
if stateVal, ok := c.socialLoginState[key]; ok {
|
|
|
|
state = stateVal
|
|
|
|
}
|
|
|
|
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *InMemoryStore) RemoveSocialLoginState(key string) {
|
|
|
|
c.mu.Lock()
|
|
|
|
delete(c.socialLoginState, key)
|
|
|
|
c.mu.Unlock()
|
|
|
|
}
|