finish session getting code
This commit is contained in:
@@ -5,3 +5,4 @@ import "errors"
|
|||||||
var ErrSessionNotFound = errors.New("session not found")
|
var ErrSessionNotFound = errors.New("session not found")
|
||||||
var ErrSessionAlreadyExists = errors.New("session already exists")
|
var ErrSessionAlreadyExists = errors.New("session already exists")
|
||||||
var ErrSessionExpired = errors.New("session expired")
|
var ErrSessionExpired = errors.New("session expired")
|
||||||
|
var ErrSessionBackend = errors.New("session backend")
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package session
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"time"
|
||||||
|
|
||||||
"astraltech.xyz/accountmanager/src/logging"
|
"astraltech.xyz/accountmanager/src/logging"
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
@@ -45,19 +47,25 @@ func (m *RedisStore) Create(sessionID string, session *SessionData) (err error)
|
|||||||
func (m *RedisStore) Get(sessionID string) (*SessionData, error) {
|
func (m *RedisStore) Get(sessionID string) (*SessionData, error) {
|
||||||
hashed := hashSession(sessionID)
|
hashed := hashSession(sessionID)
|
||||||
|
|
||||||
_, err := m.client.Get(m.ctx, hashed).Result()
|
data, err := m.client.Get(m.ctx, hashed).Bytes()
|
||||||
if err == redis.Nil {
|
if err == redis.Nil {
|
||||||
return nil, ErrSessionNotFound
|
return nil, ErrSessionNotFound
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
logging.Error(err.Error())
|
logging.Error(err.Error())
|
||||||
|
return nil, ErrSessionBackend
|
||||||
}
|
}
|
||||||
|
|
||||||
// if time.Now().After(data.ExpiresAt) {
|
var session_data SessionData
|
||||||
// _ = m.Delete(sessionID) // ignore error
|
if err := json.Unmarshal(data, &session_data); err != nil {
|
||||||
// return nil, ErrSessionExpired
|
logging.Error(err.Error())
|
||||||
// }
|
return nil, ErrSessionBackend
|
||||||
// copy := *data
|
}
|
||||||
return nil, nil
|
|
||||||
|
if time.Now().After(session_data.ExpiresAt) {
|
||||||
|
_ = m.Delete(sessionID)
|
||||||
|
return nil, ErrSessionBackend
|
||||||
|
}
|
||||||
|
return &session_data, nil
|
||||||
}
|
}
|
||||||
func (m *RedisStore) Update(sessionID string, session *SessionData) error {
|
func (m *RedisStore) Update(sessionID string, session *SessionData) error {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ package session
|
|||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type SessionData struct {
|
type SessionData struct {
|
||||||
UserID string
|
UserID string `json:"userid"`
|
||||||
CSRFToken string
|
CSRFToken string `json:"csrftoken"`
|
||||||
ExpiresAt time.Time
|
ExpiresAt time.Time `json:"expiresat"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SessionStore interface {
|
type SessionStore interface {
|
||||||
|
|||||||
Reference in New Issue
Block a user