feat: TOTP 2FA + diverse Verbesserungen

2FA (TOTP):
- backend/api/auth.go: zweistufiger Login-Flow mit Pending-Token (5min),
  Endpunkte /2fa/validate, /2fa/setup, /2fa/confirm, /2fa/disable
- backend/db/users.go: GetUserByID, SetTOTPSecret, EnableTOTP, DisableTOTP
- backend/db/postgres.go: Migration totp_secret + totp_enabled Spalten
- backend/models/user.go: TOTPEnabled/TOTPSecret Felder, neue Request-Typen
- backend/main.go: neue Routen registriert (/2fa/validate ohne Auth, rest geschuetzt)
- backend/go.mod+sum: github.com/pquerna/otp hinzugefuegt
- frontend/src/pages/Login.jsx: zweistufiger Login (Passwort -> TOTP)
- frontend/src/pages/SettingsPage.jsx: TwoFactorSection mit Setup/Deaktivieren
- frontend/src/stores/auth.js: validateTOTP Action
- frontend/src/api/client.js: setupTOTP, confirmTOTP, disableTOTP, validateTOTP

Weitere Aenderungen (unstaged -> jetzt committed):
- frontend/src/components/ScriptModal.jsx: neu
- frontend/src/components/AgentPanel.jsx: Erweiterungen
- frontend/src/components/ProxmoxPanel.jsx: Erweiterungen
- agent-linux/ws/handler.go: Erweiterungen
- backend/api/scheduler.go + tasks.go: Erweiterungen
- backend/db/tasks.go + models/task.go: Erweiterungen
This commit is contained in:
cynfo3000
2026-03-08 19:17:43 +01:00
parent c76868e806
commit 10684dfc0a
19 changed files with 926 additions and 98 deletions
+6
View File
@@ -128,6 +128,10 @@ func (d *Database) migrate() error {
return fmt.Errorf("Schema anlegen: %w", err)
}
// TOTP 2FA Spalten
d.db.Exec("ALTER TABLE users ADD COLUMN IF NOT EXISTS totp_secret TEXT")
d.db.Exec("ALTER TABLE users ADD COLUMN IF NOT EXISTS totp_enabled BOOLEAN NOT NULL DEFAULT false")
// Agents um customer_id erweitern
d.db.Exec("ALTER TABLE agents ADD COLUMN IF NOT EXISTS customer_id INTEGER REFERENCES customers(id)")
d.db.Exec("CREATE INDEX IF NOT EXISTS idx_agents_customer ON agents(customer_id)")
@@ -266,6 +270,8 @@ func (d *Database) migrate() error {
d.db.Exec(`ALTER TABLE tasks ADD COLUMN IF NOT EXISTS last_run TIMESTAMPTZ`)
d.db.Exec(`ALTER TABLE tasks ADD COLUMN IF NOT EXISTS next_run TIMESTAMPTZ`)
d.db.Exec(`ALTER TABLE tasks ADD COLUMN IF NOT EXISTS run_count INT DEFAULT 0`)
d.db.Exec(`ALTER TABLE tasks ADD COLUMN IF NOT EXISTS script TEXT`)
d.db.Exec(`ALTER TABLE tasks ADD COLUMN IF NOT EXISTS script_timeout INT DEFAULT 300`)
d.db.Exec(`CREATE INDEX IF NOT EXISTS idx_tasks_active_next_run ON tasks(active, next_run) WHERE active = TRUE`)
// Retention Policy (90 Tage)
+22 -7
View File
@@ -62,9 +62,10 @@ func CalculateNextRun(scheduleType, scheduleTime string, weekday, monthday *int,
func (d *Database) CreateTask(agentID, action, scheduledAt, webhookURL, createdBy string,
name, scheduleType, scheduleTime string, weekday, monthday *int,
reboot, healthCheck bool, healthCheckTimeout int, securityOnly bool, active bool) (*models.Task, error) {
reboot, healthCheck bool, healthCheckTimeout int, securityOnly bool, active bool,
script string, scriptTimeout int) (*models.Task, error) {
validActions := map[string]bool{"backup": true, "update": true}
validActions := map[string]bool{"backup": true, "update": true, "zpoolscrub": true, "script": true}
if !validActions[action] {
return nil, fmt.Errorf("ungueltige Aktion: %s", action)
}
@@ -103,16 +104,22 @@ func (d *Database) CreateTask(agentID, action, scheduledAt, webhookURL, createdB
nextRun = CalculateNextRun(scheduleType, scheduleTime, weekday, monthday, now)
}
if scriptTimeout <= 0 {
scriptTimeout = 300
}
var taskID int
err := d.db.QueryRow(`
INSERT INTO tasks (agent_id, name, action, scheduled_at, webhook_url, created_by,
schedule_type, schedule_time, schedule_weekday, schedule_monthday,
reboot, health_check, health_check_timeout, security_only, active, next_run)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)
reboot, health_check, health_check_timeout, security_only, active, next_run,
script, script_timeout)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18)
RETURNING id
`, agentID, name, action, schedTime, webhookURL, createdBy,
scheduleType, scheduleTime, weekday, monthday,
reboot, healthCheck, healthCheckTimeout, securityOnly, active, nextRun).Scan(&taskID)
reboot, healthCheck, healthCheckTimeout, securityOnly, active, nextRun,
script, scriptTimeout).Scan(&taskID)
if err != nil {
return nil, fmt.Errorf("Task erstellen: %w", err)
}
@@ -130,7 +137,8 @@ func scanTask(scan func(dest ...interface{}) error) (*models.Task, error) {
var name, scheduleType, scheduleTime sql.NullString
var scheduleWeekday, scheduleMonthday sql.NullInt32
var reboot, healthCheck, securityOnly, active sql.NullBool
var healthCheckTimeout, runCount sql.NullInt32
var healthCheckTimeout, runCount, scriptTimeout sql.NullInt32
var script sql.NullString
err := scan(
&t.ID, &t.AgentID, &agentName, &customerName, &customerNumber,
@@ -139,6 +147,7 @@ func scanTask(scan func(dest ...interface{}) error) (*models.Task, error) {
&createdBy, &createdAt,
&scheduleType, &scheduleTime, &scheduleWeekday, &scheduleMonthday,
&reboot, &healthCheck, &healthCheckTimeout, &securityOnly, &active, &lastRun, &nextRun, &runCount,
&script, &scriptTimeout,
)
if err != nil {
return nil, err
@@ -171,6 +180,11 @@ func scanTask(scan func(dest ...interface{}) error) (*models.Task, error) {
t.HealthCheckTimeout = int(healthCheckTimeout.Int32)
}
t.SecurityOnly = securityOnly.Valid && securityOnly.Bool
t.Script = script.String
t.ScriptTimeout = 300
if scriptTimeout.Valid && scriptTimeout.Int32 > 0 {
t.ScriptTimeout = int(scriptTimeout.Int32)
}
t.Active = !active.Valid || active.Bool
t.RunCount = int(runCount.Int32)
@@ -215,7 +229,8 @@ const taskSelectCols = `t.id, t.agent_id, COALESCE(a.name, ''), COALESCE(c.name,
t.result, t.webhook_url, t.webhook_sent, t.webhook_response,
t.created_by, t.created_at,
t.schedule_type, t.schedule_time, t.schedule_weekday, t.schedule_monthday,
t.reboot, t.health_check, t.health_check_timeout, COALESCE(t.security_only, FALSE), t.active, t.last_run, t.next_run, t.run_count`
t.reboot, t.health_check, t.health_check_timeout, COALESCE(t.security_only, FALSE), t.active, t.last_run, t.next_run, t.run_count,
COALESCE(t.script, ''), COALESCE(t.script_timeout, 300)`
const taskFromJoin = `FROM tasks t
LEFT JOIN agents a ON a.id = t.agent_id
+44 -2
View File
@@ -27,10 +27,11 @@ func (d *Database) CreateUser(username, password, displayName, role string) (*mo
func (d *Database) GetUserByUsername(username string) (*models.User, error) {
var u models.User
var lastLogin sql.NullTime
var totpSecret sql.NullString
err := d.db.QueryRow(
"SELECT id, username, password_hash, display_name, role, created_at, last_login FROM users WHERE username = $1",
"SELECT id, username, password_hash, display_name, role, totp_enabled, totp_secret, created_at, last_login FROM users WHERE username = $1",
username,
).Scan(&u.ID, &u.Username, &u.PasswordHash, &u.DisplayName, &u.Role, &u.CreatedAt, &lastLogin)
).Scan(&u.ID, &u.Username, &u.PasswordHash, &u.DisplayName, &u.Role, &u.TOTPEnabled, &totpSecret, &u.CreatedAt, &lastLogin)
if err == sql.ErrNoRows {
return nil, nil
}
@@ -40,9 +41,50 @@ func (d *Database) GetUserByUsername(username string) (*models.User, error) {
if lastLogin.Valid {
u.LastLogin = &lastLogin.Time
}
if totpSecret.Valid {
u.TOTPSecret = totpSecret.String
}
return &u, nil
}
func (d *Database) GetUserByID(id int) (*models.User, error) {
var u models.User
var lastLogin sql.NullTime
var totpSecret sql.NullString
err := d.db.QueryRow(
"SELECT id, username, password_hash, display_name, role, totp_enabled, totp_secret, created_at, last_login FROM users WHERE id = $1",
id,
).Scan(&u.ID, &u.Username, &u.PasswordHash, &u.DisplayName, &u.Role, &u.TOTPEnabled, &totpSecret, &u.CreatedAt, &lastLogin)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
if lastLogin.Valid {
u.LastLogin = &lastLogin.Time
}
if totpSecret.Valid {
u.TOTPSecret = totpSecret.String
}
return &u, nil
}
func (d *Database) SetTOTPSecret(userID int, secret string) error {
_, err := d.db.Exec("UPDATE users SET totp_secret = $1, totp_enabled = false WHERE id = $2", secret, userID)
return err
}
func (d *Database) EnableTOTP(userID int) error {
_, err := d.db.Exec("UPDATE users SET totp_enabled = true WHERE id = $1", userID)
return err
}
func (d *Database) DisableTOTP(userID int) error {
_, err := d.db.Exec("UPDATE users SET totp_enabled = false, totp_secret = NULL WHERE id = $1", userID)
return err
}
func (d *Database) GetUsers() ([]models.User, error) {
rows, err := d.db.Query("SELECT id, username, display_name, role, created_at, last_login FROM users ORDER BY username")
if err != nil {