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.
97 lines
3.7 KiB
Go
97 lines
3.7 KiB
Go
package oidc
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
gooidc "github.com/coreos/go-oidc/v3/oidc"
|
|
"golang.org/x/oauth2"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/identity"
|
|
)
|
|
|
|
// Flow runs the authorization code flow with PKCE, so a person signs in at the
|
|
// provider and the dashboard learns who they are without archie ever seeing a
|
|
// password, holding a session, or writing any of the protocol itself: discovery,
|
|
// the code exchange, PKCE and token verification are the libraries'.
|
|
type Flow struct {
|
|
verifier *Verifier
|
|
oauth oauth2.Config
|
|
audience string
|
|
}
|
|
|
|
// NewFlow discovers the provider and builds the browser flow. ClientID and the
|
|
// secret are the provider's registration for this dashboard; the secret is read
|
|
// from wherever the operator keeps it and is never logged or stored.
|
|
func NewFlow(ctx context.Context, cfg Config, clientID, clientSecret, redirectURL string) (*Flow, error) {
|
|
issuer := strings.TrimSpace(cfg.Issuer)
|
|
audience := strings.TrimSpace(cfg.Audience)
|
|
if issuer == "" {
|
|
return nil, fmt.Errorf("oidc: issuer is required")
|
|
}
|
|
if audience == "" {
|
|
return nil, fmt.Errorf("oidc: audience is required")
|
|
}
|
|
if strings.TrimSpace(clientID) == "" {
|
|
return nil, fmt.Errorf("oidc: client id is required for the browser flow")
|
|
}
|
|
if strings.TrimSpace(redirectURL) == "" {
|
|
return nil, fmt.Errorf("oidc: redirect URL is required for the browser flow")
|
|
}
|
|
provider, err := gooidc.NewProvider(ctx, issuer)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("oidc: discover %q: %w", issuer, err)
|
|
}
|
|
return &Flow{
|
|
verifier: &Verifier{verifier: provider.Verifier(&gooidc.Config{ClientID: audience})},
|
|
oauth: oauth2.Config{
|
|
ClientID: clientID,
|
|
ClientSecret: clientSecret,
|
|
Endpoint: provider.Endpoint(),
|
|
RedirectURL: redirectURL,
|
|
// openid is what makes the provider return a subject at all; profile
|
|
// and email are what make a name available for a display.
|
|
Scopes: []string{gooidc.ScopeOpenID, "profile", "email"},
|
|
},
|
|
audience: audience,
|
|
}, nil
|
|
}
|
|
|
|
// AuthCodeURL returns the provider URL to send a browser to, and the PKCE
|
|
// verifier the callback must present with the code.
|
|
//
|
|
// The audience is requested HERE, at the authorization endpoint, and not only at
|
|
// the token endpoint: a provider that returns the audience empty otherwise mints
|
|
// a token this dashboard must refuse, and the refusal looks like a signing fault
|
|
// rather than a missing request parameter. The verifier is generated by the
|
|
// library, not by archie.
|
|
func (f *Flow) AuthCodeURL(state string) (string, string) {
|
|
codeVerifier := oauth2.GenerateVerifier()
|
|
url := f.oauth.AuthCodeURL(state,
|
|
oauth2.S256ChallengeOption(codeVerifier),
|
|
oauth2.SetAuthURLParam("audience", f.audience),
|
|
)
|
|
return url, codeVerifier
|
|
}
|
|
|
|
// Exchange completes the flow: it trades the code for the provider's tokens and
|
|
// verifies the access token through the same Verifier a presented token goes
|
|
// through. There is no second verification path here, so a token obtained by the
|
|
// browser cannot be accepted on terms a presented token would be refused on.
|
|
func (f *Flow) Exchange(ctx context.Context, code, codeVerifier string) (identity.ProviderSession, error) {
|
|
token, err := f.oauth.Exchange(ctx, strings.TrimSpace(code), oauth2.VerifierOption(codeVerifier))
|
|
if err != nil {
|
|
return identity.ProviderSession{}, fmt.Errorf("oidc: exchange code: %w", err)
|
|
}
|
|
credential, err := f.verifier.Verify(ctx, token.AccessToken)
|
|
if err != nil {
|
|
return identity.ProviderSession{}, err
|
|
}
|
|
expires := token.Expiry
|
|
if credential.Expires.Before(expires) || expires.IsZero() {
|
|
expires = credential.Expires
|
|
}
|
|
return identity.ProviderSession{Credential: credential, Token: token.AccessToken, Expires: expires}, nil
|
|
}
|