From b5b0400496868587bef58509fa4e39ef4f2b1b2d Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 22 Sep 2026 15:02:27 +1000 Subject: [PATCH] fix(webui): send a refused browser to sign in, and drop an unused method Two loose ends from the sign-in work, both mine. A browser that cannot authenticate now goes to the provider when this instance has a sign-in flow, instead of being shown the shared-token paste page -- a page that cannot help it, because the token it would paste is not what this instance accepts. A refusal that signing in again cannot fix, a suspended identity, is still answered rather than redirected: sending someone back to a provider that already authenticated them would hide the reason they were refused. ProviderSession.Expired is removed. Nothing called it, and it must not: expiry is the verifier's check, and a second opinion about whether a token is still good is exactly the sort of near-duplicate that drifts from the authority it copies. The browser path re-runs the flow when verification refuses, which is one rule rather than two. Verified: task check exit 0 on this tree. --- internal/domain/identity/credential.go | 7 --- internal/webui/auth.go | 20 ++++++-- internal/webui/auth_identity_test.go | 64 ++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 11 deletions(-) diff --git a/internal/domain/identity/credential.go b/internal/domain/identity/credential.go index 7030f1b78..2576292cd 100644 --- a/internal/domain/identity/credential.go +++ b/internal/domain/identity/credential.go @@ -85,13 +85,6 @@ type ProviderSession struct { Expires time.Time } -// Expired reports whether a session's token is past the point of being worth -// presenting, so a caller can send the browser through the flow again instead of -// letting every request fail verification. -func (s ProviderSession) Expired(now time.Time) bool { - return !s.Expires.IsZero() && !now.Before(s.Expires) -} - var ( // ErrNoCredential means the request presented nothing to authenticate with. ErrNoCredential = errors.New("no credential presented") diff --git a/internal/webui/auth.go b/internal/webui/auth.go index 69cf23e87..e41a76342 100644 --- a/internal/webui/auth.go +++ b/internal/webui/auth.go @@ -32,10 +32,13 @@ const ( loginStateCookie = "archie_login_state" loginVerifierCookie = "archie_login_verifier" loginCookieMaxAge = 600 - // loginRoute and loginCallbackRoute must match the redirect URI registered at - // the provider: the provider returns the browser to the path it was given. - loginRoute = "GET /oauth2/login" - loginCallbackRoute = "GET /oauth2/callback" + // loginPath is where a browser signs in, and the path a refused browser is + // sent to. loginCallbackPath must match the redirect URI registered at the + // provider: the provider returns the browser to the path it was given. + loginPath = "/oauth2/login" + loginCallbackPath = "/oauth2/callback" + loginRoute = "GET " + loginPath + loginCallbackRoute = "GET " + loginCallbackPath ) // IsLoopback reports whether a listen address is reachable only from this @@ -324,6 +327,15 @@ func (s *Server) refuseUnidentified(w http.ResponseWriter, r *http.Request, err w.Header().Set("WWW-Authenticate", `Bearer realm="archie"`) } if !allowed && wantsDocument(r) { + // A browser that cannot authenticate is sent to sign in, when this + // instance has a sign-in flow: re-presenting a credential is the only + // thing that helps, and a shared-token paste page would be meaningless + // here. A refusal the caller cannot fix by signing in again -- a + // suspended identity -- is answered rather than redirected. + if s.Login != nil { + http.Redirect(w, r, loginPath, http.StatusSeeOther) + return + } s.authPage(w, reason) return } diff --git a/internal/webui/auth_identity_test.go b/internal/webui/auth_identity_test.go index 69f62e157..5ea974f75 100644 --- a/internal/webui/auth_identity_test.go +++ b/internal/webui/auth_identity_test.go @@ -162,3 +162,67 @@ func TestSharedTokenStillGatesAnInstanceWithNoProvider(t *testing.T) { t.Fatalf("status = %d, want %d", recorder.Code, http.StatusOK) } } + +// TestRefusedBrowserIsSentToSignIn: with a sign-in flow configured, a browser that +// cannot authenticate belongs at the provider, not on a shared-token paste page +// that cannot help it. A refusal signing in again cannot fix -- a suspended +// identity -- is answered instead of redirected. +func TestRefusedBrowserIsSentToSignIn(t *testing.T) { + tests := []struct { + name string + authErr error + login identity.LoginFlow + wantStatus int + wantLogin bool + }{ + { + name: "no credential, sign-in available", + authErr: identity.ErrCredentialRejected, + login: stubFlow{}, + wantStatus: http.StatusSeeOther, + wantLogin: true, + }, + { + name: "no credential, no sign-in flow", + authErr: identity.ErrCredentialRejected, + wantStatus: http.StatusUnauthorized, + }, + { + name: "suspended identity is told, not redirected", + authErr: identity.ErrIdentityInactive, + login: stubFlow{}, + wantStatus: http.StatusForbidden, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + s := &Server{ + Login: tc.login, + Authenticate: func(context.Context, string) (identity.Identity, error) { + return identity.Identity{}, tc.authErr + }, + } + handler := s.requireToken(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { + t.Error("an unauthenticated request reached the handler") + })) + + request := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/", nil) + request.Header.Set("Authorization", "Bearer a-token") + request.Header.Set("Accept", "text/html") + recorder := httptest.NewRecorder() + handler.ServeHTTP(recorder, request) + + if recorder.Code != tc.wantStatus { + t.Fatalf("status = %d, want %d", recorder.Code, tc.wantStatus) + } + location := recorder.Header().Get("Location") + if tc.wantLogin && location != loginPath { + t.Fatalf("Location = %q, want %q", location, loginPath) + } + if !tc.wantLogin && location != "" { + t.Fatalf("a refusal that signing in cannot fix redirected to %q", location) + } + }) + } +}