diff --git a/TODO.md b/TODO.md index 252a3c9..6c542c3 100644 --- a/TODO.md +++ b/TODO.md @@ -16,7 +16,8 @@ (2026-11-20, first consumer yaemp, replacing go-oidc/oauth2): `Web.PublicIssuer` (the browser reaches the provider under another origin), `Client.AllowInsecure` (http off loopback, test installations - only), `email_verified` true only as JSON boolean. + only), `email_verified` true only as JSON boolean; `Callback` checks + the cookie and state before it contacts the provider (v0.5.1). - [ ] **yaiam: web refresh.** `Web` has no refresh for a confidential client (Client.Refresh sends no secret); add when an application keeps access tokens beyond the sign-in. diff --git a/yaiam/web.go b/yaiam/web.go index 5159706..4d69afb 100644 --- a/yaiam/web.go +++ b/yaiam/web.go @@ -251,10 +251,8 @@ func (w *Web) Callback(ctx context.Context, rw http.ResponseWriter, r *http.Requ if err := w.check(); err != nil { return nil, err } - m, err := w.discover(ctx) - if err != nil { - return nil, err - } + // The browser's cookie and the state first: a callback of no sign-in + // of this browser never reaches the provider. var c signInCookie cookie, cerr := r.Cookie(w.cookieName()) http.SetCookie(rw, &http.Cookie{Name: w.cookieName(), Value: "", Path: "/", MaxAge: -1, HttpOnly: true, Secure: w.secure(), @@ -282,6 +280,10 @@ func (w *Web) Callback(ctx context.Context, rw http.ResponseWriter, r *http.Requ if code == "" { return nil, errors.New("yaiam: the callback has no code") } + m, err := w.discover(ctx) + if err != nil { + return nil, err + } tok, idToken, err := w.exchange(ctx, m, code, c.Verifier) if err != nil { return nil, err diff --git a/yaiam/web_test.go b/yaiam/web_test.go index 48d3d0c..7eafe80 100644 --- a/yaiam/web_test.go +++ b/yaiam/web_test.go @@ -411,3 +411,14 @@ func TestAllowInsecure(t *testing.T) { t.Error("http redirect URL off loopback taken without AllowInsecure") } } + +// TestWebCallbackBeforeProvider: a callback without this browser's +// sign-in is refused before the provider is asked (here: unreachable). +func TestWebCallbackBeforeProvider(t *testing.T) { + w := &Web{Client: Client{Issuer: "http://127.0.0.1:9/t/default", ClientID: "web"}, RedirectURL: "http://localhost:8097/callback"} + for _, cookie := range []*http.Cookie{nil, {Name: "yaiam_signin", Value: "!!"}} { + if _, _, err := callback(w, "code=c&state=s", cookie); !errors.Is(err, ErrSignInState) { + t.Errorf("cookie %v: %v", cookie, err) + } + } +}