Repository navigation
Closed
Labels
Description
Context
Part of #4978 (Phase 2 of the DCR story, #4976). This sub-issue covers the purely additive, leaf-package and config-surface work — no behavior change, no CRD changes.
Depends on: #4977 (pkg/oauthproto rename must land first).
Followed by: Sub-issue B (credential store + resolver).
Scope
Step 2a — FetchAuthorizationServerMetadata (pkg/oauthproto/discovery.go)
Add to the existing pkg/oauthproto/discovery.go:
FetchAuthorizationServerMetadata(ctx context.Context, issuer string, client *http.Client) (*AuthorizationServerMetadata, error)implementing a three-path RFC 8414 / OIDC Discovery fallback. Mirror the plumbing ofpkg/auth/discovery/discovery.go:745(FetchResourceMetadata): bounded HTTP client (5–10 s),Accept: application/json,io.LimitReader, content-type check, issuer equality check per RFC 8414 §3.3.ErrRegistrationEndpointMissingsentinel returned when the winning response omitsregistration_endpoint.- No
pkg/networkingimport (leaf-package invariant).
New file pkg/oauthproto/discovery_test.go:
- Table-driven: one
httptest.NewServerper discovery path, issuer mismatch error, missingregistration_endpointcase.
Step 2b — DCRUpstreamConfig struct (pkg/authserver/config.go)
Add adjacent to OAuth2UpstreamRunConfig:
type DCRUpstreamConfig struct {
DiscoveryURL string `json:"discovery_url,omitempty" yaml:"discovery_url,omitempty"`
RegistrationEndpoint string `json:"registration_endpoint,omitempty" yaml:"registration_endpoint,omitempty"`
InitialAccessTokenFile string `json:"initial_access_token_file,omitempty" yaml:"initial_access_token_file,omitempty"`
InitialAccessTokenEnvVar string `json:"initial_access_token_env_var,omitempty" yaml:"initial_access_token_env_var,omitempty"`
SoftwareID string `json:"software_id,omitempty" yaml:"software_id,omitempty"`
SoftwareStatement string `json:"software_statement,omitempty" yaml:"software_statement,omitempty"`
}Add DCRConfig *DCRUpstreamConfig field to OAuth2UpstreamRunConfig with doc comment.
Step 2e — OAuth2Config.Validate() (pkg/authserver/config.go)
Extend the existing validator:
- Reject empty
ClientIDAND nilDCRConfig(both missing). - Reject non-empty
ClientIDAND non-nilDCRConfig(both set). - Reject
DCRConfigwith bothDiscoveryURLandRegistrationEndpointset. - Reject
DCRConfigwith neitherDiscoveryURLnorRegistrationEndpointset. - Do NOT touch
CommonOAuthConfig.Validate().
Extend pkg/authserver/config_test.go with table-driven cases for all four ClientID × DCRConfig combinations plus the exactly-one-of rule.
Acceptance Criteria
Step 2a
-
pkg/oauthproto/discovery.goexportsFetchAuthorizationServerMetadata(ctx context.Context, issuer string, client *http.Client) (*AuthorizationServerMetadata, error)(client == nilbuilds a default bounded client). - Three-path fallback tried in order, first 200-with-clean-JSON wins: (1) RFC 8414 path-insertion
{origin}/.well-known/oauth-authorization-server{path}; (2) OIDC Discovery{issuer}/.well-known/openid-configuration; (3) bare RFC 8414{origin}/.well-known/oauth-authorization-server. - After decode,
metadata.issuer == issuerenforced (RFC 8414 §3.3); mismatch returns a descriptive error. - Bounded per-call timeout (5–10 s) regardless of caller's context deadline.
-
ErrRegistrationEndpointMissingreturned whenregistration_endpointis absent from winning response. -
go list -deps ./pkg/oauthproto/...does not listgithub.com/stacklok/toolhive/pkg/networking. - Unit test: each of the three discovery paths populates the returned metadata (one
httptest.NewServerper path). - Unit test: issuer mismatch returns the RFC 8414 §3.3 error.
- Unit test: missing
registration_endpointsurfacesErrRegistrationEndpointMissing.
Step 2b
-
DCRUpstreamConfigstruct added topkg/authserver/config.gowithDiscoveryURL,RegistrationEndpoint,InitialAccessTokenFile,InitialAccessTokenEnvVar,SoftwareID,SoftwareStatementfields (JSON/YAML tags, with doc comments). -
OAuth2UpstreamRunConfiggainsDCRConfig *DCRUpstreamConfigfield with doc comment.
Step 2e
-
OAuth2Config.Validate()rejects emptyClientIDAND nilDCRConfig(both missing). -
OAuth2Config.Validate()rejects non-emptyClientIDAND non-nilDCRConfig(both set). -
OAuth2Config.Validate()rejectsDCRConfigwith bothDiscoveryURLandRegistrationEndpointset. -
OAuth2Config.Validate()rejectsDCRConfigwith neitherDiscoveryURLnorRegistrationEndpointset. -
CommonOAuthConfig.Validate()is unchanged (verified by diff). - Unit tests: table-driven covering all four ClientID × DCRConfig combinations and the exactly-one-of rule.
Cross-cutting
-
task build,task test,task lint-fix,task license-checkall pass. - All new
.gofiles have SPDX 2-line header (task license-fixcatches omissions).
Patterns & References
- Template for
FetchAuthorizationServerMetadata:pkg/auth/discovery/discovery.go:745(FetchResourceMetadata) — same timeout, LimitReader, content-type check, required-field validation pattern. - RFC 8414 §3.1 path-insertion precedent:
pkg/auth/oauth/oidc.go:253(buildWellKnownURLs) — the OAuth well-known URL is{base}/.well-known/oauth-authorization-server/{tenant}, not{base}/{tenant}/.well-known/…. - Unit test HTTP mock style:
pkg/auth/oauth/dynamic_registration_test.go:1-80(minimal per-request httptest pattern). Register teardown witht.Cleanup(server.Close). - Plain
testing+testify(not Ginkgo) — matches existing unit tests inpkg/oauthproto/andpkg/authserver/. - SPDX header required on all new files.
Activity
Add authserver DCR discovery and config surface
Add authserver DCR discovery and config surface
Add authserver DCR discovery and config surface
Add authserver DCR discovery and config surface
Add authserver DCR discovery and config surface
Add authserver DCR discovery and config surface (#5041)