feat(auth): store sessions in redis with sliding and absolute expiry
Tokens are only kept as SHA-256 hashes. A per-user index allows ending all sessions of an account at once. Closes #1 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
ccc0fe58b8
commit
dae55d8c18
2 files changed
+274
No files matched your search
@@ -0,0 +1,100 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func testSessions(t *testing.T) (*Sessions, *time.Time) {
|
||||
t.Helper()
|
||||
url := os.Getenv("UPTIME_TEST_REDIS_URL")
|
||||
if url == "" {
|
||||
t.Skip("UPTIME_TEST_REDIS_URL not set")
|
||||
}
|
||||
opts, err := redis.ParseURL(url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rdb := redis.NewClient(opts)
|
||||
t.Cleanup(func() { _ = rdb.Close() })
|
||||
now := time.Now()
|
||||
s := NewSessions(rdb, time.Hour, 24*time.Hour)
|
||||
s.now = func() time.Time { return now }
|
||||
return s, &now
|
||||
}
|
||||
|
||||
func TestSessionRoundTrip(t *testing.T) {
|
||||
s, _ := testSessions(t)
|
||||
ctx := context.Background()
|
||||
user, org := uuid.New(), uuid.New()
|
||||
|
||||
created, err := s.Create(ctx, user, org)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := s.Get(ctx, created.Token)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.UserID != user || got.OrgID != org || got.CSRF != created.CSRF {
|
||||
t.Fatalf("mismatch: %+v vs %+v", got, created)
|
||||
}
|
||||
if !got.CheckCSRF(created.CSRF) || got.CheckCSRF("") || got.CheckCSRF("x") {
|
||||
t.Fatal("csrf check wrong")
|
||||
}
|
||||
if err := s.Delete(ctx, got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.Get(ctx, created.Token); !errors.Is(err, ErrNoSession) {
|
||||
t.Fatalf("deleted session still valid: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionAbsoluteLifetime(t *testing.T) {
|
||||
s, now := testSessions(t)
|
||||
ctx := context.Background()
|
||||
sess, err := s.Create(ctx, uuid.New(), uuid.Nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
*now = now.Add(25 * time.Hour)
|
||||
if _, err := s.Get(ctx, sess.Token); !errors.Is(err, ErrNoSession) {
|
||||
t.Fatalf("expired session accepted: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteAllForUser(t *testing.T) {
|
||||
s, _ := testSessions(t)
|
||||
ctx := context.Background()
|
||||
user := uuid.New()
|
||||
a, _ := s.Create(ctx, user, uuid.Nil)
|
||||
b, _ := s.Create(ctx, user, uuid.Nil)
|
||||
other, _ := s.Create(ctx, uuid.New(), uuid.Nil)
|
||||
|
||||
if err := s.DeleteAllForUser(ctx, user); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tok := range []string{a.Token, b.Token} {
|
||||
if _, err := s.Get(ctx, tok); !errors.Is(err, ErrNoSession) {
|
||||
t.Fatal("session survived DeleteAllForUser")
|
||||
}
|
||||
}
|
||||
if _, err := s.Get(ctx, other.Token); err != nil {
|
||||
t.Fatalf("unrelated session removed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTokenNotStoredInPlain(t *testing.T) {
|
||||
s, _ := testSessions(t)
|
||||
ctx := context.Background()
|
||||
sess, _ := s.Create(ctx, uuid.New(), uuid.Nil)
|
||||
if n, _ := s.rdb.Exists(ctx, "session:"+sess.Token).Result(); n != 0 {
|
||||
t.Fatal("raw token used as redis key")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user