package store import ( "context" "database/sql" "errors" "strings" "time" ) func splitRecovery(s string) []string { if strings.TrimSpace(s) == "" { return nil } return strings.Split(s, ",") } func joinRecovery(codes []string) string { return strings.Join(codes, ",") } // CreateSession persists a new login session. func (s *Store) CreateSession(ctx context.Context, sess *Session) error { _, err := s.db.ExecContext(ctx, `INSERT INTO sessions(id, user_id, ip, ua, csrf, created_at, expires_at, last_seen) VALUES(?,?,?,?,?,?,?,?)`, sess.ID, sess.UserID, sess.IP, sess.UA, sess.CSRF, sess.CreatedAt, sess.ExpiresAt, sess.CreatedAt) return err } // SessionByID returns a session ignoring expiry (the caller checks it). func (s *Store) SessionByID(ctx context.Context, id string) (*Session, error) { var sess Session err := s.db.QueryRowContext(ctx, `SELECT id, user_id, ip, ua, csrf, created_at, expires_at, last_seen FROM sessions WHERE id = ?`, id). Scan(&sess.ID, &sess.UserID, &sess.IP, &sess.UA, &sess.CSRF, &sess.CreatedAt, &sess.ExpiresAt, &sess.LastSeen) if errors.Is(err, sql.ErrNoRows) { return nil, ErrNotFound } if err != nil { return nil, err } return &sess, nil } // TouchSession extends a session after authenticated activity. func (s *Store) TouchSession(ctx context.Context, id string, expiresAt int64) error { _, err := s.db.ExecContext(ctx, `UPDATE sessions SET last_seen = ?, expires_at = ? WHERE id = ?`, time.Now().Unix(), expiresAt, id) return err } // DeleteSession removes a session. func (s *Store) DeleteSession(ctx context.Context, id string) error { _, err := s.db.ExecContext(ctx, `DELETE FROM sessions WHERE id = ?`, id) return err } // DeleteOtherSessions invalidates every session of a user except keep. func (s *Store) DeleteOtherSessions(ctx context.Context, userID int64, keep string) (int64, error) { res, err := s.db.ExecContext(ctx, `DELETE FROM sessions WHERE user_id = ? AND id <> ?`, userID, keep) if err != nil { return 0, err } return res.RowsAffected() } // PurgeExpiredSessions drops stale sessions. func (s *Store) PurgeExpiredSessions(ctx context.Context) (int64, error) { res, err := s.db.ExecContext(ctx, `DELETE FROM sessions WHERE expires_at < ?`, time.Now().Unix()) if err != nil { return 0, err } return res.RowsAffected() } // RecordLoginAttempt appends one authentication attempt. func (s *Store) RecordLoginAttempt(ctx context.Context, ip, username string, ok bool) error { v := 0 if ok { v = 1 } _, err := s.db.ExecContext(ctx, `INSERT INTO login_attempts(ip, username, success, at) VALUES(?,?,?,?)`, ip, username, v, time.Now().Unix()) return err } // CountRecentFailures counts failed logins from an IP inside the window. func (s *Store) CountRecentFailures(ctx context.Context, ip string, window time.Duration) (int, error) { since := time.Now().Add(-window).Unix() var n int err := s.db.QueryRowContext(ctx, `SELECT COUNT(1) FROM login_attempts WHERE ip = ? AND success = 0 AND at >= ?`, ip, since).Scan(&n) return n, err } // CleanupAttempts removes attempts older than 24h. func (s *Store) CleanupAttempts(ctx context.Context) (int64, error) { res, err := s.db.ExecContext(ctx, `DELETE FROM login_attempts WHERE at < ?`, time.Now().Add(-24*time.Hour).Unix()) if err != nil { return 0, err } return res.RowsAffected() }