Successfully fetch login status

This commit is contained in:
2023-10-27 01:17:30 +02:00
parent d5af575e74
commit 6498a8bd06
8 changed files with 181 additions and 35 deletions

View File

@@ -3,6 +3,7 @@ package redis
import (
"context"
"encoding/json"
"fmt"
"git.thequux.com/thequux/ipasso/backend"
"github.com/redis/go-redis/v9"
"net/url"
@@ -59,12 +60,12 @@ type RedisBackend struct {
var putSessionScript = redis.NewScript(`
local skey = KEYS[1]
local ckey = KEYS[2]
local session = ARGS[1]
local sexp = ARGS[2]
local cache = ARGS[3]
local clifetime = ARGS[4]
local session = ARGV[1]
local sexp = ARGV[2]
local cache = ARGV[3]
local clifetime = ARGV[4]
if redis.call("GET", skey) != "" then
if redis.call("GET", skey) ~= "" then
return false
else
redis.call("SET", skey, session, "EXAT", sexp)
@@ -103,29 +104,32 @@ func (r *RedisBackend) PutSession(ctx context.Context, session backend.Session,
func (r *RedisBackend) GetSession(ctx context.Context, id string) (backend.Session, *backend.SessionCache, error) {
var session backend.Session
var cache backend.SessionCache
var cachep = &cache
result, err := r.rdb.MGet(ctx, sessionKey(id), scacheKey(id)).Result()
if err != nil {
return session, nil, err
}
v, ok := result[0].([]byte)
fmt.Printf("Result: %#v\n", result)
v, ok := result[0].(string)
if !ok {
return backend.Session{}, nil, backend.ErrBackendData
}
if err = json.Unmarshal(v, &session); err != nil {
if err = json.Unmarshal([]byte(v), &session); err != nil {
return backend.Session{}, nil, err
}
v, ok = result[1].([]byte)
if !ok {
return backend.Session{}, nil, backend.ErrBackendData
}
if err = json.Unmarshal(v, &cache); err != nil {
return backend.Session{}, nil, err
v, ok = result[1].(string)
if ok {
if err = json.Unmarshal([]byte(v), &cache); err != nil {
return backend.Session{}, nil, err
}
} else {
cachep = nil
}
return session, &cache, nil
return session, cachep, nil
}
func (r *RedisBackend) EndSession(ctx context.Context, id string) {