Sign in as a user instead of with an API key
CI / test (push) Successful in 12s
Release / test (push) Successful in 5s
Release / binaries (push) Successful in 12s

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.
This commit is contained in:
Niklas Ye
2026-09-23 22:15:14 +02:00
parent 496e7b6d90
commit f4ca0059dc
13 changed files with 985 additions and 37 deletions
+95 -1
View File
@@ -10,6 +10,7 @@ import (
"time"
"git.ryuvia.com/niklas/terdut-tui/internal/api"
"git.ryuvia.com/niklas/terdut-tui/internal/session"
"git.ryuvia.com/niklas/terdut-tui/internal/theme"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
@@ -52,6 +53,14 @@ const (
modeAPIKeyReveal
modeAPIKeyRevokeByID
modePasswordSet
modeLogin
)
// Fields of the sign-in form, in tab order.
const (
loginUsername = iota
loginPassword
loginFieldCount
)
// Fields of the set-password form, in tab order.
@@ -115,6 +124,9 @@ type connectedMsg struct {
me api.Me
}
type connectErrMsg struct{ err error }
type loginDoneMsg struct{}
type loginErrMsg struct{ err error }
type logoutDoneMsg struct{}
type incidentsFetchedMsg struct{ incidents []api.Incident }
type archivedIncidentsFetchedMsg struct{ incidents []api.Incident }
type incidentActionDoneMsg struct {
@@ -188,6 +200,7 @@ type Model struct {
client *api.Client
serverURL string
refreshInterval time.Duration
theme theme.Theme // kept to build a fresh model when signing out
activeSection section
mode mode
@@ -203,6 +216,17 @@ type Model struct {
meID int64
isAdmin bool
// Sign-in. Until the server accepts a session the TUI is in modeLogin;
// loginNote is a line the form shows above the fields (why we are here), and
// ticking says the refresh timer is already running, so signing in again
// after signing out does not start a second one.
loginInputs [loginFieldCount]textinput.Model
loginFocus int
loggingIn bool
loginErr string
loginNote string
ticking bool
// Connection & dashboard
connected bool
loading bool
@@ -365,6 +389,18 @@ func NewModel(client *api.Client, serverURL string, refreshInterval time.Duratio
pwIn[i].CharLimit = 72 // bcrypt's limit; the server refuses longer
}
var loginIn [loginFieldCount]textinput.Model
for i, placeholder := range [loginFieldCount]string{"username", "password"} {
loginIn[i] = textinput.New()
loginIn[i].Placeholder = placeholder
loginIn[i].CharLimit = 72 // bcrypt's limit, and the server refuses longer passwords
}
loginIn[loginPassword].EchoMode = textinput.EchoPassword
loginIn[loginPassword].EchoCharacter = '•'
for i := range loginIn {
loginIn[i] = st.Input(loginIn[i])
}
for _, in := range []*textinput.Model{
&noteIn, &snoozeIn, &usernameIn, &emailIn, &topicIn, &keyNameIn, &revokeIn,
} {
@@ -385,12 +421,20 @@ func NewModel(client *api.Client, serverURL string, refreshInterval time.Duratio
}
window := today.AddDate(0, 0, -(weekday - 1))
startMode := modeDashboard
if client != nil && !client.HasSession() {
startMode = modeLogin
loginIn[loginUsername].Focus()
}
return Model{
client: client,
serverURL: serverURL,
refreshInterval: refreshInterval,
theme: th,
activeSection: sectionIncidents,
mode: modeDashboard,
mode: startMode,
loginInputs: loginIn,
loading: true,
incidentFilter: "",
alertFilter: "firing",
@@ -424,7 +468,25 @@ func (m Model) WithDefaultTeam(team string) Model {
return m
}
// WithLogin prefills the sign-in form's username and sets a note shown above it.
func (m Model) WithLogin(username, note string) Model {
m.loginInputs[loginUsername].SetValue(username)
m.loginNote = note
if username != "" && m.mode == modeLogin {
// The name is known, so the only thing left to type is the password.
m.loginInputs[loginUsername].Blur()
m.loginFocus = loginPassword
m.loginInputs[loginPassword].Focus()
}
return m
}
// Init tries the saved session, if there is one; otherwise the sign-in form is
// already showing and there is nothing to do until it is submitted.
func (m Model) Init() tea.Cmd {
if m.mode == modeLogin {
return nil
}
return connectCmd(m.client)
}
@@ -975,6 +1037,38 @@ func connectCmd(client *api.Client) tea.Cmd {
}
}
// loginCmd signs in and saves the session, so the next run can resume it. Failing
// to save is not failing to sign in: the session works for this run either way.
func loginCmd(client *api.Client, serverURL, username, password string) tea.Cmd {
return func() tea.Msg {
token, err := client.Login(username, password)
if err != nil {
return loginErrMsg{err}
}
_ = session.Save(serverURL, token)
return loginDoneMsg{}
}
}
// logoutCmd ends the session on the server and deletes the saved one. The saved
// copy goes even when the server cannot be reached, because the person asked to
// be signed out and a token left on disk would say otherwise.
func logoutCmd(client *api.Client) tea.Cmd {
return func() tea.Msg {
_ = client.Logout()
_ = session.Clear()
return logoutDoneMsg{}
}
}
// forgetSessionCmd drops a saved session the server no longer honours.
func forgetSessionCmd() tea.Cmd {
return func() tea.Msg {
_ = session.Clear()
return nil
}
}
func fetchIncidentsCmd(client *api.Client, teamID int64, filter string) tea.Cmd {
return func() tea.Msg {
status, snoozed := incidentQuery(filter)