Skip to content

Authserver DCR: discovery function and config structs (Phase 2, Steps 2a/2b/2e) #5037

Closed

Description

@tgrunnagle

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 of pkg/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.
  • ErrRegistrationEndpointMissing sentinel returned when the winning response omits registration_endpoint.
  • No pkg/networking import (leaf-package invariant).

New file pkg/oauthproto/discovery_test.go:

  • Table-driven: one httptest.NewServer per discovery path, issuer mismatch error, missing registration_endpoint case.

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 ClientID AND nil DCRConfig (both missing).
  • Reject non-empty ClientID AND non-nil DCRConfig (both set).
  • Reject DCRConfig with both DiscoveryURL and RegistrationEndpoint set.
  • Reject DCRConfig with neither DiscoveryURL nor RegistrationEndpoint set.
  • 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.go exports FetchAuthorizationServerMetadata(ctx context.Context, issuer string, client *http.Client) (*AuthorizationServerMetadata, error) (client == nil builds 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 == issuer enforced (RFC 8414 §3.3); mismatch returns a descriptive error.
  • Bounded per-call timeout (5–10 s) regardless of caller's context deadline.
  • ErrRegistrationEndpointMissing returned when registration_endpoint is absent from winning response.
  • go list -deps ./pkg/oauthproto/... does not list github.com/stacklok/toolhive/pkg/networking.
  • Unit test: each of the three discovery paths populates the returned metadata (one httptest.NewServer per path).
  • Unit test: issuer mismatch returns the RFC 8414 §3.3 error.
  • Unit test: missing registration_endpoint surfaces ErrRegistrationEndpointMissing.

Step 2b

  • DCRUpstreamConfig struct added to pkg/authserver/config.go with DiscoveryURL, RegistrationEndpoint, InitialAccessTokenFile, InitialAccessTokenEnvVar, SoftwareID, SoftwareStatement fields (JSON/YAML tags, with doc comments).
  • OAuth2UpstreamRunConfig gains DCRConfig *DCRUpstreamConfig field with doc comment.

Step 2e

  • OAuth2Config.Validate() rejects empty ClientID AND nil DCRConfig (both missing).
  • OAuth2Config.Validate() rejects non-empty ClientID AND non-nil DCRConfig (both set).
  • OAuth2Config.Validate() rejects DCRConfig with both DiscoveryURL and RegistrationEndpoint set.
  • OAuth2Config.Validate() rejects DCRConfig with neither DiscoveryURL nor RegistrationEndpoint set.
  • 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-check all pass.
  • All new .go files have SPDX 2-line header (task license-fix catches 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 with t.Cleanup(server.Close).
  • Plain testing + testify (not Ginkgo) — matches existing unit tests in pkg/oauthproto/ and pkg/authserver/.
  • SPDX header required on all new files.

Metadata

Metadata

Assignees

No one assigned

    Labels

    authenhancementgo

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions