c275f4ff4c
Lands the bottom three layers of Phase 1: P1-08 internal/api: protocol_version + envelope + every WS message shape from spec.md §6.2 (Hello, Heartbeat, Job*, Schedule*, etc). Wire-format tests pin the JSON shape so a rename here breaks tests instead of silently breaking the agent. P1-02 + P1-03 internal/store: SQLite via modernc.org/sqlite, embed.FS + a tiny version table for hand-rolled migrations. 0001_initial.sql covers every table from spec.md §5 plus enrollment_tokens and host_schedule_version. Typed accessors for users / sessions / enrollment / audit. WAL + foreign_keys + busy_timeout on by default. P1-06 internal/crypto: XChaCha20-Poly1305 AEAD wrapper with per-message random nonce. Key file lifecycle (generate + refuse-to-overwrite, load with size validation). Optional additionalData binds ciphertext to the row that owns it. P1-04 internal/auth (partial — passwords + tokens; sessions middleware lands with the HTTP handlers): argon2id following RFC 9106 (64 MiB / t=3 / p=4 / 32B), constant-time verify. HashToken stores SHA-256 of session/agent/enrollment tokens so a stolen DB doesn't hand over credentials. Build floor moves to Go 1.25 (modernc.org/sqlite v1.50+ requires it); CI + Dockerfile + README updated. Markdown lint diagnostics on tasks.md cleared. All packages tested. ~70 new tests pass in <1s. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
88 lines
2.6 KiB
Go
88 lines
2.6 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// CreateUser inserts a new user. The caller is responsible for
|
|
// generating an ID (typically a ULID) and hashing the password.
|
|
func (s *Store) CreateUser(ctx context.Context, u User) error {
|
|
_, err := s.db.ExecContext(ctx,
|
|
`INSERT INTO users (id, username, password_hash, role, created_at)
|
|
VALUES (?, ?, ?, ?, ?)`,
|
|
u.ID, u.Username, u.PasswordHash, string(u.Role), u.CreatedAt.UTC().Format(time.RFC3339Nano))
|
|
if err != nil {
|
|
return fmt.Errorf("store: create user: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetUserByUsername looks up a user by their (case-sensitive) username.
|
|
// Returns ErrNotFound if no row matches.
|
|
func (s *Store) GetUserByUsername(ctx context.Context, username string) (*User, error) {
|
|
row := s.db.QueryRowContext(ctx,
|
|
`SELECT id, username, password_hash, role, created_at, last_login_at
|
|
FROM users WHERE username = ?`, username)
|
|
return scanUser(row)
|
|
}
|
|
|
|
// GetUserByID looks up a user by id. Returns ErrNotFound on miss.
|
|
func (s *Store) GetUserByID(ctx context.Context, id string) (*User, error) {
|
|
row := s.db.QueryRowContext(ctx,
|
|
`SELECT id, username, password_hash, role, created_at, last_login_at
|
|
FROM users WHERE id = ?`, id)
|
|
return scanUser(row)
|
|
}
|
|
|
|
// CountUsers returns the total number of user rows. The first-run
|
|
// bootstrap uses this to detect a fresh install.
|
|
func (s *Store) CountUsers(ctx context.Context) (int, error) {
|
|
var n int
|
|
if err := s.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM users`).Scan(&n); err != nil {
|
|
return 0, fmt.Errorf("store: count users: %w", err)
|
|
}
|
|
return n, nil
|
|
}
|
|
|
|
// MarkUserLogin records a successful authentication.
|
|
func (s *Store) MarkUserLogin(ctx context.Context, id string, when time.Time) error {
|
|
_, err := s.db.ExecContext(ctx,
|
|
`UPDATE users SET last_login_at = ? WHERE id = ?`,
|
|
when.UTC().Format(time.RFC3339Nano), id)
|
|
if err != nil {
|
|
return fmt.Errorf("store: mark login: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func scanUser(row *sql.Row) (*User, error) {
|
|
var u User
|
|
var role string
|
|
var lastLogin sql.NullString
|
|
var created string
|
|
if err := row.Scan(&u.ID, &u.Username, &u.PasswordHash, &role, &created, &lastLogin); err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, fmt.Errorf("store: scan user: %w", err)
|
|
}
|
|
u.Role = Role(role)
|
|
t, err := time.Parse(time.RFC3339Nano, created)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("store: parse created_at: %w", err)
|
|
}
|
|
u.CreatedAt = t
|
|
if lastLogin.Valid {
|
|
t, err := time.Parse(time.RFC3339Nano, lastLogin.String)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("store: parse last_login_at: %w", err)
|
|
}
|
|
u.LastLoginAt = &t
|
|
}
|
|
return &u, nil
|
|
}
|