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.
62 lines
2.8 KiB
Go
62 lines
2.8 KiB
Go
package archieui
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/identity"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/oidc"
|
|
)
|
|
|
|
// dashboardAuthenticator builds the dashboard's credential check from the
|
|
// endpoint references the process was given, or returns nil when none were.
|
|
//
|
|
// It is built here rather than in the command because cmd/ parses flags and
|
|
// nothing else, and it takes two URLs rather than a configuration struct because
|
|
// this process is deliberately kept unaware of the config file's shape -- see the
|
|
// note above Options. Issuer and audience are exactly the endpoint references
|
|
// Options already carries for the Gateway and the State Store.
|
|
//
|
|
// A nil result means no provider is configured, and the shared token stays the
|
|
// gate: an instance that never configured one keeps working, and its requests are
|
|
// recorded unattributed rather than credited to a human.
|
|
func dashboardAuthenticator(ctx context.Context, opts Options, subjects identity.SubjectResolver, log *slog.Logger) (func(context.Context, string) (identity.Identity, error), error) {
|
|
if strings.TrimSpace(opts.OidcIssuer) == "" {
|
|
return nil, nil
|
|
}
|
|
verifier, err := oidc.New(ctx, oidc.Config{Issuer: opts.OidcIssuer, Audience: opts.OidcAudience})
|
|
if err != nil {
|
|
// Failing here rather than serving anyway: a dashboard that cannot verify
|
|
// a credential would silently fall back to a gate that names nobody, and
|
|
// an operator who configured a provider must not get the unauthenticated
|
|
// behaviour instead.
|
|
return nil, err
|
|
}
|
|
log.Info("dashboard authenticates provider credentials", "issuer", opts.OidcIssuer, "audience", opts.OidcAudience)
|
|
return func(ctx context.Context, credential string) (identity.Identity, error) {
|
|
value, _, err := identity.Authenticate(ctx, subjects, verifier, credential)
|
|
return value, err
|
|
}, nil
|
|
}
|
|
|
|
// dashboardLoginFlow builds the browser sign-in flow from endpoint references and
|
|
// the operator's registration, or returns nil when none is configured.
|
|
//
|
|
// The secret is read from the environment variable the operator named and never
|
|
// logged, printed or stored. A client id is optional: an instance that only
|
|
// accepts agent tokens configures none, and then there is simply no sign-in
|
|
// route -- token verification is unaffected.
|
|
func dashboardLoginFlow(ctx context.Context, opts Options) (identity.LoginFlow, error) {
|
|
if strings.TrimSpace(opts.OidcIssuer) == "" || strings.TrimSpace(opts.OidcClientID) == "" {
|
|
return nil, nil
|
|
}
|
|
secret := ""
|
|
if name := strings.TrimSpace(opts.OidcClientSecretEnv); name != "" {
|
|
secret = os.Getenv(name)
|
|
}
|
|
return oidc.NewFlow(ctx, oidc.Config{Issuer: opts.OidcIssuer, Audience: opts.OidcAudience},
|
|
opts.OidcClientID, secret, opts.OidcRedirectURL)
|
|
}
|