f4ca0059dc
The web UI signs in with a username and password and holds a session cookie; the TUI was the only client still needing an API key pasted into a config file. It now asks for the same credentials on a form at start. What is kept between runs is the session token, not the password, in session.json under the config directory, mode 0600 and keyed by server URL so one server's token is never offered to another. It resumes on the next start; the server's sessions last 30 days and slide with use. L signs out, which ends the session on the server and deletes the saved one even if the server cannot be reached. The client attaches the cookie by hand instead of using a cookie jar: the server marks it Secure behind https, and a jar drops a Secure cookie it is given over plain http, which would break a local server for no reason. It sends no Authorization header at all, since the server judges a request carrying one on that alone and never falls back to the cookie. Writes go through the server's cross-origin guard, which lets a client that sends neither Origin nor Sec-Fetch-Site through; checked against a real v0.20.1 server for both reads and writes. A 401 from anything means the session is gone (expired, ended from the web UI, or the account disabled), so the TUI returns to the form with the reason, forgets the saved token, and drops what the last session loaded rather than showing it to whoever signs in next. A 403 is a permission and leaves the session alone. The refresh timer is started once, so signing out and in does not leave two running. An account with no password cannot sign in, and the server answers it exactly like a wrong password, so the form's message says a password must be set first. Users created only for API access hit this. Breaking: api_key in config.yaml is no longer used. It is not an error to leave it there; the form says it is ignored. API keys still exist on the server and k in Users still manages them.
62 lines
1.6 KiB
Go
62 lines
1.6 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func writeConfig(t *testing.T, body string) {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
t.Setenv("XDG_CONFIG_HOME", dir)
|
|
if err := os.MkdirAll(filepath.Join(dir, "terdut-tui"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "terdut-tui", "config.yaml"), []byte(body), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestLoad_TeamIsOptional(t *testing.T) {
|
|
writeConfig(t, "server_url: https://terdut.example.com\n")
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatalf("load: %v", err)
|
|
}
|
|
if cfg.Team != "" {
|
|
t.Errorf("expected no default team, got %q", cfg.Team)
|
|
}
|
|
|
|
writeConfig(t, "server_url: https://terdut.example.com\nteam: Ops\n")
|
|
cfg, err = Load()
|
|
if err != nil {
|
|
t.Fatalf("load: %v", err)
|
|
}
|
|
if cfg.Team != "Ops" {
|
|
t.Errorf("expected team Ops, got %q", cfg.Team)
|
|
}
|
|
}
|
|
|
|
// Signing in replaced the API key, so a config that has only a server URL is
|
|
// complete, and one that still carries an api_key is noted rather than refused.
|
|
func TestLoad_NoAPIKeyNeeded(t *testing.T) {
|
|
writeConfig(t, "server_url: https://terdut.example.com\nusername: niklas\n")
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatalf("load: %v", err)
|
|
}
|
|
if cfg.Username != "niklas" || cfg.LegacyAPIKey {
|
|
t.Errorf("unexpected config %+v", cfg)
|
|
}
|
|
|
|
writeConfig(t, "server_url: https://terdut.example.com\napi_key: old\n")
|
|
cfg, err = Load()
|
|
if err != nil {
|
|
t.Fatalf("a leftover api_key must not stop the TUI starting: %v", err)
|
|
}
|
|
if !cfg.LegacyAPIKey {
|
|
t.Error("expected the leftover api_key to be noted")
|
|
}
|
|
}
|