Feat/dashboard (#105)

This commit is contained in:
Lakhan Samani
2022-01-17 11:32:13 +05:30
committed by GitHub
parent 7ce96367a3
commit f1b4141367
120 changed files with 3381 additions and 3044 deletions

View File

@@ -4,14 +4,17 @@ import (
"sync"
)
// InMemoryStore is a simple in-memory store for sessions.
type InMemoryStore struct {
mu sync.Mutex
mutex sync.Mutex
store map[string]map[string]string
socialLoginState map[string]string
}
func (c *InMemoryStore) AddToken(userId, accessToken, refreshToken string) {
c.mu.Lock()
// 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
if len(c.store) >= 500 {
c.store = map[string]map[string]string{}
@@ -28,48 +31,57 @@ func (c *InMemoryStore) AddToken(userId, accessToken, refreshToken string) {
}
c.store[userId] = tempMap
}
c.mu.Unlock()
}
func (c *InMemoryStore) DeleteUserSession(userId string) {
c.mu.Lock()
// DeleteAllUserSession deletes all the user sessions from in-memory store.
func (c *InMemoryStore) DeleteAllUserSession(userId string) {
c.mutex.Lock()
defer c.mutex.Unlock()
delete(c.store, userId)
c.mu.Unlock()
}
func (c *InMemoryStore) DeleteVerificationRequest(userId, accessToken string) {
c.mu.Lock()
// DeleteUserSession deletes the particular user session from in-memory store.
func (c *InMemoryStore) DeleteUserSession(userId, accessToken string) {
c.mutex.Lock()
defer c.mutex.Unlock()
delete(c.store[userId], accessToken)
c.mu.Unlock()
}
// ClearStore clears the in-memory store.
func (c *InMemoryStore) ClearStore() {
c.mu.Lock()
c.mutex.Lock()
defer c.mutex.Unlock()
c.store = map[string]map[string]string{}
c.mu.Unlock()
}
func (c *InMemoryStore) GetToken(userId, accessToken string) string {
// 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 := ""
c.mu.Lock()
if sessionMap, ok := c.store[userId]; ok {
if val, ok := sessionMap[accessToken]; ok {
token = val
}
}
c.mu.Unlock()
return token
}
// SetSocialLoginState sets the social login state in the in-memory store.
func (c *InMemoryStore) SetSocialLoginState(key, state string) {
c.mu.Lock()
c.mutex.Lock()
defer c.mutex.Unlock()
c.socialLoginState[key] = state
c.mu.Unlock()
}
// GetSocialLoginState gets the social login state from the in-memory store.
func (c *InMemoryStore) GetSocialLoginState(key string) string {
c.mutex.Lock()
defer c.mutex.Unlock()
state := ""
if stateVal, ok := c.socialLoginState[key]; ok {
state = stateVal
@@ -78,8 +90,10 @@ func (c *InMemoryStore) GetSocialLoginState(key string) string {
return state
}
// RemoveSocialLoginState removes the social login state from the in-memory store.
func (c *InMemoryStore) RemoveSocialLoginState(key string) {
c.mu.Lock()
c.mutex.Lock()
defer c.mutex.Unlock()
delete(c.socialLoginState, key)
c.mu.Unlock()
}

View File

@@ -13,7 +13,8 @@ type RedisStore struct {
store *redis.Client
}
func (c *RedisStore) AddToken(userId, accessToken, refreshToken string) {
// AddUserSession adds the user session to redis
func (c *RedisStore) AddUserSession(userId, accessToken, refreshToken string) {
err := c.store.HMSet(c.ctx, "authorizer_"+userId, map[string]string{
accessToken: refreshToken,
}).Err()
@@ -22,20 +23,23 @@ func (c *RedisStore) AddToken(userId, accessToken, refreshToken string) {
}
}
func (c *RedisStore) DeleteUserSession(userId string) {
// DeleteAllUserSession deletes all the user session from redis
func (c *RedisStore) DeleteAllUserSession(userId string) {
err := c.store.Del(c.ctx, "authorizer_"+userId).Err()
if err != nil {
log.Fatalln("Error deleting redis token:", err)
}
}
func (c *RedisStore) DeleteVerificationRequest(userId, accessToken string) {
// DeleteUserSession deletes the particular user session from redis
func (c *RedisStore) DeleteUserSession(userId, accessToken string) {
err := c.store.HDel(c.ctx, "authorizer_"+userId, accessToken).Err()
if err != nil {
log.Fatalln("Error deleting redis token:", err)
}
}
// ClearStore clears the redis store for authorizer related tokens
func (c *RedisStore) ClearStore() {
err := c.store.Del(c.ctx, "authorizer_*").Err()
if err != nil {
@@ -43,7 +47,8 @@ func (c *RedisStore) ClearStore() {
}
}
func (c *RedisStore) GetToken(userId, accessToken string) string {
// GetUserSession returns the user session token from the redis store.
func (c *RedisStore) GetUserSession(userId, accessToken string) string {
token := ""
res, err := c.store.HMGet(c.ctx, "authorizer_"+userId, accessToken).Result()
if err != nil {
@@ -55,6 +60,7 @@ func (c *RedisStore) GetToken(userId, accessToken string) string {
return token
}
// SetSocialLoginState sets the social login state in redis store.
func (c *RedisStore) SetSocialLoginState(key, state string) {
err := c.store.Set(c.ctx, key, state, 0).Err()
if err != nil {
@@ -62,6 +68,7 @@ func (c *RedisStore) SetSocialLoginState(key, state string) {
}
}
// GetSocialLoginState gets the social login state from redis store.
func (c *RedisStore) GetSocialLoginState(key string) string {
state := ""
state, err := c.store.Get(c.ctx, key).Result()
@@ -72,6 +79,7 @@ func (c *RedisStore) GetSocialLoginState(key string) string {
return state
}
// RemoveSocialLoginState removes the social login state from redis store.
func (c *RedisStore) RemoveSocialLoginState(key string) {
err := c.store.Del(c.ctx, key).Err()
if err != nil {

View File

@@ -5,57 +5,65 @@ import (
"log"
"github.com/authorizerdev/authorizer/server/constants"
"github.com/authorizerdev/authorizer/server/envstore"
"github.com/go-redis/redis/v8"
)
// SessionStore is a struct that defines available session stores
// If redis store is available, higher preference is given to that store.
// Else in memory store is used.
type SessionStore struct {
InMemoryStoreObj *InMemoryStore
RedisMemoryStoreObj *RedisStore
}
// SessionStoreObj is a global variable that holds the
// reference to various session store instances
var SessionStoreObj SessionStore
func SetToken(userId, accessToken, refreshToken string) {
// TODO: Set session information in db for all the sessions that gets generated
// it should async go function
// SetUserSession sets the user session in the session store
func SetUserSession(userId, accessToken, refreshToken string) {
if SessionStoreObj.RedisMemoryStoreObj != nil {
SessionStoreObj.RedisMemoryStoreObj.AddToken(userId, accessToken, refreshToken)
SessionStoreObj.RedisMemoryStoreObj.AddUserSession(userId, accessToken, refreshToken)
}
if SessionStoreObj.InMemoryStoreObj != nil {
SessionStoreObj.InMemoryStoreObj.AddToken(userId, accessToken, refreshToken)
SessionStoreObj.InMemoryStoreObj.AddUserSession(userId, accessToken, refreshToken)
}
}
func DeleteVerificationRequest(userId, accessToken string) {
// DeleteUserSession deletes the particular user session from the session store
func DeleteUserSession(userId, accessToken string) {
if SessionStoreObj.RedisMemoryStoreObj != nil {
SessionStoreObj.RedisMemoryStoreObj.DeleteVerificationRequest(userId, accessToken)
SessionStoreObj.RedisMemoryStoreObj.DeleteUserSession(userId, accessToken)
}
if SessionStoreObj.InMemoryStoreObj != nil {
SessionStoreObj.InMemoryStoreObj.DeleteVerificationRequest(userId, accessToken)
SessionStoreObj.InMemoryStoreObj.DeleteUserSession(userId, accessToken)
}
}
func DeleteUserSession(userId string) {
// DeleteAllSessions deletes all the sessions from the session store
func DeleteAllUserSession(userId string) {
if SessionStoreObj.RedisMemoryStoreObj != nil {
SessionStoreObj.RedisMemoryStoreObj.DeleteUserSession(userId)
SessionStoreObj.RedisMemoryStoreObj.DeleteAllUserSession(userId)
}
if SessionStoreObj.InMemoryStoreObj != nil {
SessionStoreObj.InMemoryStoreObj.DeleteUserSession(userId)
SessionStoreObj.InMemoryStoreObj.DeleteAllUserSession(userId)
}
}
func GetToken(userId, accessToken string) string {
// GetUserSession returns the user session from the session store
func GetUserSession(userId, accessToken string) string {
if SessionStoreObj.RedisMemoryStoreObj != nil {
return SessionStoreObj.RedisMemoryStoreObj.GetToken(userId, accessToken)
return SessionStoreObj.RedisMemoryStoreObj.GetUserSession(userId, accessToken)
}
if SessionStoreObj.InMemoryStoreObj != nil {
return SessionStoreObj.InMemoryStoreObj.GetToken(userId, accessToken)
return SessionStoreObj.InMemoryStoreObj.GetUserSession(userId, accessToken)
}
return ""
}
// ClearStore clears the session store for authorizer tokens
func ClearStore() {
if SessionStoreObj.RedisMemoryStoreObj != nil {
SessionStoreObj.RedisMemoryStoreObj.ClearStore()
@@ -65,6 +73,7 @@ func ClearStore() {
}
}
// SetSocialLoginState sets the social login state in the session store
func SetSocailLoginState(key, state string) {
if SessionStoreObj.RedisMemoryStoreObj != nil {
SessionStoreObj.RedisMemoryStoreObj.SetSocialLoginState(key, state)
@@ -74,6 +83,7 @@ func SetSocailLoginState(key, state string) {
}
}
// GetSocialLoginState returns the social login state from the session store
func GetSocailLoginState(key string) string {
if SessionStoreObj.RedisMemoryStoreObj != nil {
return SessionStoreObj.RedisMemoryStoreObj.GetSocialLoginState(key)
@@ -85,6 +95,7 @@ func GetSocailLoginState(key string) string {
return ""
}
// RemoveSocialLoginState removes the social login state from the session store
func RemoveSocialLoginState(key string) {
if SessionStoreObj.RedisMemoryStoreObj != nil {
SessionStoreObj.RedisMemoryStoreObj.RemoveSocialLoginState(key)
@@ -94,10 +105,11 @@ func RemoveSocialLoginState(key string) {
}
}
// InitializeSessionStore initializes the SessionStoreObj based on environment variables
func InitSession() {
if constants.EnvData.REDIS_URL != "" {
if envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyRedisURL).(string) != "" {
log.Println("using redis store to save sessions")
opt, err := redis.ParseURL(constants.EnvData.REDIS_URL)
opt, err := redis.ParseURL(envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyRedisURL).(string))
if err != nil {
log.Fatalln("Error parsing redis url:", err)
}