- pluggable per-domain Repository interfaces; PostgreSQL and SQLite impls - SQLite on modernc.org/sqlite (WAL, busy_timeout, foreign_keys, immediate tx) - separate migrations/sqlite and db.MigrateSQLite; store.NewPostgres/NewSQLite - cmd/reconcile compares object storage with the journal (report, then --delete) - storage.Backend.List and photos.Repository.AllStorageKeys - DB_BACKEND/SQLITE_PATH and STORAGE_BACKEND/FS_ROOT selectable; local defaults (sqlite ./galo.db, fs ./data) - Makefile reconcile/reconcile-delete; docs and .env.example updated
112 lines
3.5 KiB
Go
112 lines
3.5 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"errors"
|
|
"time"
|
|
|
|
"shared-album/internal/models"
|
|
"shared-album/internal/sqliteutil"
|
|
)
|
|
|
|
// SQLiteStore is the SQLite implementation of Repository.
|
|
type SQLiteStore struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func NewSQLiteStore(db *sql.DB) *SQLiteStore { return &SQLiteStore{db: db} }
|
|
|
|
func (s *SQLiteStore) CreateUser(ctx context.Context, u *models.User) error {
|
|
_, err := s.db.ExecContext(ctx,
|
|
`INSERT INTO users (id, email, password_hash, name, avatar_url)
|
|
VALUES ($1, $2, $3, $4, $5)`,
|
|
u.ID, u.Email, u.PasswordHash, u.Name, nullIfEmpty(u.AvatarURL))
|
|
return err
|
|
}
|
|
|
|
func (s *SQLiteStore) GetUserByEmail(ctx context.Context, email string) (*models.User, error) {
|
|
return s.scanUser(s.db.QueryRowContext(ctx,
|
|
`SELECT id, email, password_hash, name, COALESCE(avatar_url, ''),
|
|
COALESCE(preferences, '{}'), created_at
|
|
FROM users WHERE lower(email) = lower($1)`, email))
|
|
}
|
|
|
|
func (s *SQLiteStore) GetUserByID(ctx context.Context, id string) (*models.User, error) {
|
|
return s.scanUser(s.db.QueryRowContext(ctx,
|
|
`SELECT id, email, password_hash, name, COALESCE(avatar_url, ''),
|
|
COALESCE(preferences, '{}'), created_at
|
|
FROM users WHERE id = $1`, id))
|
|
}
|
|
|
|
func (s *SQLiteStore) scanUser(row *sql.Row) (*models.User, error) {
|
|
var u models.User
|
|
var prefs, createdAt string
|
|
if err := row.Scan(&u.ID, &u.Email, &u.PasswordHash, &u.Name, &u.AvatarURL, &prefs, &createdAt); err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
u.CreatedAt = sqliteutil.ParseTime(createdAt)
|
|
if prefs != "" && prefs != "null" {
|
|
_ = json.Unmarshal([]byte(prefs), &u.Preferences)
|
|
}
|
|
return &u, nil
|
|
}
|
|
|
|
func (s *SQLiteStore) UpdatePreferences(ctx context.Context, userID string, raw []byte) error {
|
|
res, err := s.db.ExecContext(ctx,
|
|
`UPDATE users SET preferences = $2 WHERE id = $1`, userID, string(raw))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if n, _ := res.RowsAffected(); n == 0 {
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *SQLiteStore) CreateRefreshToken(ctx context.Context, t *models.RefreshToken) error {
|
|
_, err := s.db.ExecContext(ctx,
|
|
`INSERT INTO refresh_tokens (id, user_id, token_hash, expires_at)
|
|
VALUES ($1, $2, $3, $4)`,
|
|
t.ID, t.UserID, t.TokenHash, sqliteutil.FormatTime(t.ExpiresAt))
|
|
return err
|
|
}
|
|
|
|
func (s *SQLiteStore) GetRefreshToken(ctx context.Context, hash string) (*models.RefreshToken, error) {
|
|
var t models.RefreshToken
|
|
var expiresAt, createdAt string
|
|
var revokedAt sql.NullString
|
|
err := s.db.QueryRowContext(ctx,
|
|
`SELECT id, user_id, token_hash, expires_at, revoked_at, created_at
|
|
FROM refresh_tokens WHERE token_hash = $1`, hash).
|
|
Scan(&t.ID, &t.UserID, &t.TokenHash, &expiresAt, &revokedAt, &createdAt)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
t.ExpiresAt = sqliteutil.ParseTime(expiresAt)
|
|
t.CreatedAt = sqliteutil.ParseTime(createdAt)
|
|
t.RevokedAt = sqliteutil.ParseNullTime(revokedAt)
|
|
return &t, nil
|
|
}
|
|
|
|
func (s *SQLiteStore) RevokeRefreshToken(ctx context.Context, id string) error {
|
|
_, err := s.db.ExecContext(ctx,
|
|
`UPDATE refresh_tokens SET revoked_at = $2 WHERE id = $1 AND revoked_at IS NULL`,
|
|
id, sqliteutil.FormatTime(time.Now()))
|
|
return err
|
|
}
|
|
|
|
func (s *SQLiteStore) RevokeAllUserTokens(ctx context.Context, userID string) error {
|
|
_, err := s.db.ExecContext(ctx,
|
|
`UPDATE refresh_tokens SET revoked_at = $2 WHERE user_id = $1 AND revoked_at IS NULL`,
|
|
userID, sqliteutil.FormatTime(time.Now()))
|
|
return err
|
|
}
|