Vibe-guided bskyoauth and custom repo example code in Golang 馃 probably not safe to use in prod
1package bskyoauth
2
3import (
4 "time"
5
6 "github.com/shindakun/bskyoauth/internal/session"
7)
8
9// MemorySessionStore is an in-memory implementation of SessionStore with automatic expiration.
10// This is a wrapper around internal/session.MemoryStore to maintain backward compatibility.
11type MemorySessionStore struct {
12 store *session.MemoryStore
13}
14
15// NewMemorySessionStore creates a new in-memory session store with default TTL (30 days).
16// Sessions automatically expire and are cleaned up every 5 minutes.
17func NewMemorySessionStore() *MemorySessionStore {
18 // Set the logger for the internal session package
19 session.SetLogger(Logger)
20 return &MemorySessionStore{
21 store: session.NewMemoryStore(),
22 }
23}
24
25// NewMemorySessionStoreWithTTL creates a new in-memory session store with custom TTL and cleanup interval.
26// ttl: how long sessions remain valid before expiring
27// cleanupInterval: how often the cleanup goroutine runs to remove expired sessions
28func NewMemorySessionStoreWithTTL(ttl, cleanupInterval time.Duration) *MemorySessionStore {
29 // Set the logger for the internal session package
30 session.SetLogger(Logger)
31 return &MemorySessionStore{
32 store: session.NewMemoryStoreWithTTL(ttl, cleanupInterval),
33 }
34}
35
36// Get retrieves a session by ID.
37// Returns ErrSessionNotFound if the session doesn't exist or has expired.
38func (m *MemorySessionStore) Get(sessionID string) (*Session, error) {
39 sess, err := m.store.Get(sessionID)
40 if err != nil {
41 return nil, err
42 }
43 return sess.(*Session), nil
44}
45
46// Set stores a session with the given ID.
47// The session will expire after the configured TTL (default: 30 days).
48func (m *MemorySessionStore) Set(sessionID string, session *Session) error {
49 return m.store.Set(sessionID, session)
50}
51
52// Delete removes a session by ID.
53func (m *MemorySessionStore) Delete(sessionID string) error {
54 return m.store.Delete(sessionID)
55}
56
57// Stop gracefully stops the cleanup goroutine.
58// Call this when shutting down the application to avoid goroutine leaks.
59func (m *MemorySessionStore) Stop() {
60 m.store.Stop()
61}
62
63// GenerateSessionID creates a cryptographically secure random session ID.
64func GenerateSessionID() string {
65 return session.GenerateSessionID()
66}