Files
archie/internal/infrastructure/oidc/flow_test.go
T
sam c53b2fee60 feat(webui): let a person sign in and act as themselves
The last gap in the identity boundary: enabling a provider used to lock browsers
out, because nothing could turn a sign-in into the credential the dashboard already
verifies. Now a browser completes the authorization code flow with PKCE and the
identity reaching the request context is the person who signed in.

The audience is requested AT THE AUTHORIZATION ENDPOINT, not only at the token
endpoint. Infra measured that a provider asked otherwise returns the audience empty,
and this dashboard then refuses a perfectly valid token -- a failure that reads as a
signing fault and is actually a missing query parameter. A test asserts the
parameter is present, so it cannot quietly go missing.

The flow is the libraries': discovery, the code exchange, PKCE generation and the
challenge transformation are go-oidc and x/oauth2, so archie writes no protocol. The
token returned by the exchange is verified through the SAME Verifier a presented
token goes through, and the identity is then resolved through the same rule -- one
Resolve now shared by Authenticate and the callback, so a browser sign-in cannot be
accepted on terms a presented token would be refused on.

Archie still issues no session. The callback stores the PROVIDER's token in an
HttpOnly cookie and every later request re-verifies it, so what archie holds cannot
outlive the provider's own decision. The callback checks the state it issued, and
expires the state and PKCE verifier cookies on use.

The sign-in routes mount on the bypass mux because they are how a caller becomes
authenticated; gating them behind the credential they obtain would make signing in
impossible. The client secret is never a flag: -oidc-client-secret-env names the
environment variable holding it, so it cannot reach a process listing or a log line.
No client id means no sign-in route and token verification is unaffected, which is
what an agent-only deployment wants.

Evidence, produced by driv·ng the flow rather than the seam:
internal/webui/api_tasks_actor_test.go's TestBrowserSignInRecordsTheSignedInPerson
walks sign-in -> callback -> cookie -> action and asserts the recorded actor is the
signed-in person with human_approved. TestCallbackRefusesAMismatchedState asserts a
replayed state is refused before any credential is verified.
internal/infrastructure/oidc/flow_test.go runs the real flow against a test
provider's own authorize and token endpoints, asserting the audience parameter, the
PKCE challenge, a successful exchange, and a refused wrong verifier.

Verified: task check exit 0 on this tree.
2026-09-22 14:59:03 +10:00

198 lines
7.0 KiB
Go

package oidc
import (
"context"
"encoding/json"
"net/http"
"net/url"
"testing"
"golang.org/x/oauth2"
)
// TestAuthCodeURLRequestsTheAudienceAtTheAuthorizationEndpoint pins the trap infra
// measured: a provider that is asked for the audience only at the token endpoint
// returns a token with an empty audience, and the audience check then refuses a
// perfectly valid token. The fix is this query parameter, so a test fails if it
// ever goes missing.
func TestAuthCodeURLRequestsTheAudienceAtTheAuthorizationEndpoint(t *testing.T) {
p := newProvider(t)
flow, err := NewFlow(context.Background(), Config{Issuer: p.server.URL, Audience: testAudience},
"archie-web", "secret", p.server.URL+"/oauth2/callback")
if err != nil {
t.Fatalf("NewFlow() error = %v", err)
}
raw, verifier := flow.AuthCodeURL("state-1")
parsed, err := url.Parse(raw)
if err != nil {
t.Fatalf("AuthCodeURL() returned an unparseable URL: %v", err)
}
query := parsed.Query()
if got := query.Get("audience"); got != testAudience {
t.Fatalf("audience = %q, want %q", got, testAudience)
}
if query.Get("code_challenge") == "" || query.Get("code_challenge_method") != "S256" {
t.Fatalf("PKCE is not on the authorization request: %v", query)
}
if verifier == "" {
t.Fatal("AuthCodeURL() returned no PKCE verifier for the callback")
}
}
func TestNewFlowRefusesAnIncompleteConfiguration(t *testing.T) {
p := newProvider(t)
complete := Config{Issuer: p.server.URL, Audience: testAudience}
tests := []struct {
name string
cfg Config
clientID string
redirectURL string
}{
{"no client id", complete, "", p.server.URL + "/oauth2/callback"},
{"no redirect URL", complete, "archie-web", ""},
{"no audience", Config{Issuer: p.server.URL}, "archie-web", p.server.URL + "/oauth2/callback"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if _, err := NewFlow(context.Background(), tc.cfg, tc.clientID, "secret", tc.redirectURL); err == nil {
t.Fatal("NewFlow() accepted a configuration it must refuse")
}
})
}
}
// TestExchangeCompletesARealAuthorizationCodeFlow drives the flow against the test
// provider's own authorize and token endpoints, so the code exchange and PKCE are
// exercised rather than stubbed.
func TestExchangeCompletesARealAuthorizationCodeFlow(t *testing.T) {
p := newProvider(t)
redirect := "http://dashboard.example/oauth2/callback"
flow, err := NewFlow(context.Background(), Config{Issuer: p.server.URL, Audience: testAudience},
"archie-web", "secret", redirect)
if err != nil {
t.Fatalf("NewFlow() error = %v", err)
}
authURL, verifier := flow.AuthCodeURL("state-2")
code := p.authorize(t, authURL, "state-2")
session, err := flow.Exchange(context.Background(), code, verifier)
if err != nil {
t.Fatalf("Exchange() error = %v", err)
}
if session.Credential.Subject.Subject != "a150ab4b-0000-0000-0000-000000000001" {
t.Fatalf("subject = %q, want the person's provider subject", session.Credential.Subject.Subject)
}
if session.Credential.Subject.Issuer != p.server.URL {
t.Fatalf("issuer = %q, want %q", session.Credential.Subject.Issuer, p.server.URL)
}
if session.Token == "" {
t.Fatal("Exchange() returned no token to present on later requests")
}
}
// TestExchangeRefusesAWrongPKCEVerifier: the provider rejects the exchange, and
// archie must surface that as a refusal rather than a session.
func TestExchangeRefusesAWrongPKCEVerifier(t *testing.T) {
p := newProvider(t)
flow, err := NewFlow(context.Background(), Config{Issuer: p.server.URL, Audience: testAudience},
"archie-web", "secret", "http://dashboard.example/oauth2/callback")
if err != nil {
t.Fatalf("NewFlow() error = %v", err)
}
authURL, _ := flow.AuthCodeURL("state-3")
code := p.authorize(t, authURL, "state-3")
if _, err := flow.Exchange(context.Background(), code, "not-the-verifier"); err == nil {
t.Fatal("Exchange() accepted a wrong PKCE verifier")
}
}
// authorize walks a browser's side of the flow: it asks the provider's
// authorization endpoint and returns the code the provider sends back.
func (p *provider) authorize(t *testing.T, authURL, wantState string) string {
t.Helper()
request, err := http.NewRequestWithContext(context.Background(), http.MethodGet, authURL, nil)
if err != nil {
t.Fatalf("build authorize request: %v", err)
}
client := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}
response, err := client.Do(request)
if err != nil {
t.Fatalf("authorize: %v", err)
}
defer func() { _ = response.Body.Close() }()
location := response.Header.Get("Location")
if location == "" {
t.Fatalf("authorize returned %d with no redirect", response.StatusCode)
}
parsed, err := url.Parse(location)
if err != nil {
t.Fatalf("authorize redirect is unparseable: %v", err)
}
if got := parsed.Query().Get("state"); got != wantState {
t.Fatalf("state = %q, want %q", got, wantState)
}
return parsed.Query().Get("code")
}
// authorizeEndpoint is a faithful authorization endpoint: it requires the
// audience and the PKCE challenge, and it only issues a code for a verifier it
// later receives.
func (p *provider) authorizeEndpoint(t *testing.T) http.HandlerFunc {
t.Helper()
return func(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
if query.Get("audience") == "" {
http.Error(w, "audience is required at the authorization endpoint", http.StatusBadRequest)
return
}
challenge := query.Get("code_challenge")
if challenge == "" || query.Get("code_challenge_method") != "S256" {
http.Error(w, "PKCE is required", http.StatusBadRequest)
return
}
code := "code-" + query.Get("state")
p.pending.Store(code, challenge)
location := query.Get("redirect_uri") + "?code=" + url.QueryEscape(code) + "&state=" + url.QueryEscape(query.Get("state"))
http.Redirect(w, r, location, http.StatusSeeOther)
}
}
// tokenEndpoint trades a code for a signed access token, checking the PKCE
// verifier against the challenge the authorization endpoint recorded.
func (p *provider) tokenEndpoint(t *testing.T) http.HandlerFunc {
t.Helper()
return func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, "bad form", http.StatusBadRequest)
return
}
code := r.PostForm.Get("code")
challenge, ok := p.pending.LoadAndDelete(code)
if !ok {
http.Error(w, "unknown code", http.StatusBadRequest)
return
}
want, ok := challenge.(string)
if !ok || pkceChallenge(r.PostForm.Get("code_verifier")) != want {
http.Error(w, "PKCE verification failed", http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"access_token": p.personToken(t),
"token_type": "Bearer",
"expires_in": 3600,
"scope": "openid profile email",
})
}
}
// pkceChallenge computes the challenge the way the client library does, so the
// provider's check is the library's own transformation rather than archie's.
func pkceChallenge(verifier string) string {
return oauth2.S256ChallengeFromVerifier(verifier)
}