Feat/multiple session (#64)
* fix: disable windows build * feat: add ability to handle multiple sessions
This commit is contained in:
@@ -1,41 +1,88 @@
|
||||
package session
|
||||
|
||||
import "sync"
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type InMemoryStore struct {
|
||||
mu sync.Mutex
|
||||
store map[string]string
|
||||
mu sync.Mutex
|
||||
store map[string]map[string]string
|
||||
socialLoginState map[string]string
|
||||
}
|
||||
|
||||
func (c *InMemoryStore) AddToken(userId, token string) {
|
||||
func (c *InMemoryStore) AddToken(userId, accessToken, refreshToken string) {
|
||||
c.mu.Lock()
|
||||
// delete sessions > 500 // not recommended for production
|
||||
if len(c.store) >= 500 {
|
||||
c.store = make(map[string]string)
|
||||
c.store = map[string]map[string]string{}
|
||||
}
|
||||
c.store[userId] = token
|
||||
// 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
|
||||
}
|
||||
|
||||
log.Println(c.store)
|
||||
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
func (c *InMemoryStore) DeleteToken(userId string) {
|
||||
func (c *InMemoryStore) DeleteUserSession(userId string) {
|
||||
c.mu.Lock()
|
||||
delete(c.store, userId)
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
func (c *InMemoryStore) ClearStore() {
|
||||
func (c *InMemoryStore) DeleteToken(userId, accessToken string) {
|
||||
c.mu.Lock()
|
||||
c.store = make(map[string]string)
|
||||
delete(c.store[userId], accessToken)
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
func (c *InMemoryStore) GetToken(userId string) string {
|
||||
func (c *InMemoryStore) ClearStore() {
|
||||
c.mu.Lock()
|
||||
c.store = map[string]map[string]string{}
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
func (c *InMemoryStore) GetToken(userId, accessToken string) string {
|
||||
token := ""
|
||||
c.mu.Lock()
|
||||
if val, ok := c.store[userId]; ok {
|
||||
token = val
|
||||
if sessionMap, ok := c.store[userId]; ok {
|
||||
if val, ok := sessionMap[accessToken]; ok {
|
||||
token = val
|
||||
}
|
||||
}
|
||||
c.mu.Unlock()
|
||||
|
||||
return token
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@ package session
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
@@ -12,20 +13,29 @@ type RedisStore struct {
|
||||
store *redis.Client
|
||||
}
|
||||
|
||||
func (c *RedisStore) AddToken(userId, token string) {
|
||||
err := c.store.Set(c.ctx, "authorizer_"+userId, token, 0).Err()
|
||||
func (c *RedisStore) AddToken(userId, accessToken, refreshToken string) {
|
||||
err := c.store.HMSet(c.ctx, "authorizer_"+userId, map[string]string{
|
||||
accessToken: refreshToken,
|
||||
}).Err()
|
||||
if err != nil {
|
||||
log.Fatalln("Error saving redis token:", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *RedisStore) DeleteToken(userId string) {
|
||||
func (c *RedisStore) DeleteUserSession(userId string) {
|
||||
err := c.store.Del(c.ctx, "authorizer_"+userId).Err()
|
||||
if err != nil {
|
||||
log.Fatalln("Error deleting redis token:", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *RedisStore) DeleteToken(userId, accessToken string) {
|
||||
err := c.store.HDel(c.ctx, "authorizer_"+userId, accessToken).Err()
|
||||
if err != nil {
|
||||
log.Fatalln("Error deleting redis token:", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *RedisStore) ClearStore() {
|
||||
err := c.store.Del(c.ctx, "authorizer_*").Err()
|
||||
if err != nil {
|
||||
@@ -33,11 +43,38 @@ func (c *RedisStore) ClearStore() {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *RedisStore) GetToken(userId string) string {
|
||||
func (c *RedisStore) GetToken(userId, accessToken string) string {
|
||||
token := ""
|
||||
token, err := c.store.Get(c.ctx, "authorizer_"+userId).Result()
|
||||
res, err := c.store.HMGet(c.ctx, "authorizer_"+userId, accessToken).Result()
|
||||
if err != nil {
|
||||
log.Println("Error getting token from redis store:", err)
|
||||
}
|
||||
if len(res) > 0 && res[0] != nil {
|
||||
token = fmt.Sprintf("%v", res[0])
|
||||
}
|
||||
return token
|
||||
}
|
||||
|
||||
func (c *RedisStore) SetSocialLoginState(key, state string) {
|
||||
err := c.store.Set(c.ctx, key, state, 0).Err()
|
||||
if err != nil {
|
||||
log.Fatalln("Error saving redis token:", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *RedisStore) GetSocialLoginState(key string) string {
|
||||
state := ""
|
||||
state, err := c.store.Get(c.ctx, key).Result()
|
||||
if err != nil {
|
||||
log.Println("Error getting token from redis store:", err)
|
||||
}
|
||||
|
||||
return state
|
||||
}
|
||||
|
||||
func (c *RedisStore) RemoveSocialLoginState(key string) {
|
||||
err := c.store.Del(c.ctx, key).Err()
|
||||
if err != nil {
|
||||
log.Fatalln("Error deleting redis token:", err)
|
||||
}
|
||||
}
|
||||
|
@@ -15,30 +15,42 @@ type SessionStore struct {
|
||||
|
||||
var SessionStoreObj SessionStore
|
||||
|
||||
func SetToken(userId, token string) {
|
||||
func SetToken(userId, accessToken, refreshToken string) {
|
||||
// TODO: Set session information in db for all the sessions that gets generated
|
||||
// it should async go function
|
||||
|
||||
if SessionStoreObj.RedisMemoryStoreObj != nil {
|
||||
SessionStoreObj.RedisMemoryStoreObj.AddToken(userId, token)
|
||||
SessionStoreObj.RedisMemoryStoreObj.AddToken(userId, accessToken, refreshToken)
|
||||
}
|
||||
if SessionStoreObj.InMemoryStoreObj != nil {
|
||||
SessionStoreObj.InMemoryStoreObj.AddToken(userId, token)
|
||||
SessionStoreObj.InMemoryStoreObj.AddToken(userId, accessToken, refreshToken)
|
||||
}
|
||||
}
|
||||
|
||||
func DeleteToken(userId string) {
|
||||
func DeleteToken(userId, accessToken string) {
|
||||
if SessionStoreObj.RedisMemoryStoreObj != nil {
|
||||
SessionStoreObj.RedisMemoryStoreObj.DeleteToken(userId)
|
||||
SessionStoreObj.RedisMemoryStoreObj.DeleteToken(userId, accessToken)
|
||||
}
|
||||
if SessionStoreObj.InMemoryStoreObj != nil {
|
||||
SessionStoreObj.InMemoryStoreObj.DeleteToken(userId)
|
||||
SessionStoreObj.InMemoryStoreObj.DeleteToken(userId, accessToken)
|
||||
}
|
||||
}
|
||||
|
||||
func GetToken(userId string) string {
|
||||
func DeleteUserSession(userId string) {
|
||||
if SessionStoreObj.RedisMemoryStoreObj != nil {
|
||||
return SessionStoreObj.RedisMemoryStoreObj.GetToken(userId)
|
||||
SessionStoreObj.RedisMemoryStoreObj.DeleteUserSession(userId)
|
||||
}
|
||||
if SessionStoreObj.InMemoryStoreObj != nil {
|
||||
return SessionStoreObj.InMemoryStoreObj.GetToken(userId)
|
||||
SessionStoreObj.InMemoryStoreObj.DeleteUserSession(userId)
|
||||
}
|
||||
}
|
||||
|
||||
func GetToken(userId, accessToken string) string {
|
||||
if SessionStoreObj.RedisMemoryStoreObj != nil {
|
||||
return SessionStoreObj.RedisMemoryStoreObj.GetToken(userId, accessToken)
|
||||
}
|
||||
if SessionStoreObj.InMemoryStoreObj != nil {
|
||||
return SessionStoreObj.InMemoryStoreObj.GetToken(userId, accessToken)
|
||||
}
|
||||
|
||||
return ""
|
||||
@@ -53,6 +65,35 @@ func ClearStore() {
|
||||
}
|
||||
}
|
||||
|
||||
func SetSocailLoginState(key, state string) {
|
||||
if SessionStoreObj.RedisMemoryStoreObj != nil {
|
||||
SessionStoreObj.RedisMemoryStoreObj.SetSocialLoginState(key, state)
|
||||
}
|
||||
if SessionStoreObj.InMemoryStoreObj != nil {
|
||||
SessionStoreObj.InMemoryStoreObj.SetSocialLoginState(key, state)
|
||||
}
|
||||
}
|
||||
|
||||
func GetSocailLoginState(key string) string {
|
||||
if SessionStoreObj.RedisMemoryStoreObj != nil {
|
||||
return SessionStoreObj.RedisMemoryStoreObj.GetSocialLoginState(key)
|
||||
}
|
||||
if SessionStoreObj.InMemoryStoreObj != nil {
|
||||
return SessionStoreObj.InMemoryStoreObj.GetSocialLoginState(key)
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func RemoveSocialLoginState(key string) {
|
||||
if SessionStoreObj.RedisMemoryStoreObj != nil {
|
||||
SessionStoreObj.RedisMemoryStoreObj.RemoveSocialLoginState(key)
|
||||
}
|
||||
if SessionStoreObj.InMemoryStoreObj != nil {
|
||||
SessionStoreObj.InMemoryStoreObj.RemoveSocialLoginState(key)
|
||||
}
|
||||
}
|
||||
|
||||
func InitSession() {
|
||||
if constants.REDIS_URL != "" {
|
||||
log.Println("Using redis store to save sessions")
|
||||
@@ -75,7 +116,8 @@ func InitSession() {
|
||||
} else {
|
||||
log.Println("Using in memory store to save sessions")
|
||||
SessionStoreObj.InMemoryStoreObj = &InMemoryStore{
|
||||
store: make(map[string]string),
|
||||
store: map[string]map[string]string{},
|
||||
socialLoginState: map[string]string{},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user