← Back to Pull requests

feat(auth): protect HttpApi endpoints with OAuth scope requirements #271

Open
unknown opened this pull request with 496 lines diff across 6 files
View on GitHub
unknown unknown commented on Oct 25, 2026 13:02 UTC
Author

oauth.middleware(scopes) protects HttpRouter routes such as MCP, but an HttpApi shared with browser clients cannot declare the scopes its groups and endpoints need. Its generated clients see untyped 401 and 403 responses, and consent or scope pages cannot read scope descriptions from the API.

OAuthScope gains browser-safe declarations, and the authorization server gains securityLayer(api):

  • OAuthScope.Required annotates a group or endpoint with { allOf }, { anyOf }, or both. Entries are branded scopes or { name, description }. An endpoint's requirement replaces its group's, and { allOf: [] } accepts any valid access.

  • OAuthScope.RequireAccess is bearer security middleware. Generated clients need no implementation and decode failures as HttpApiError.Unauthorized, Forbidden, and ServiceUnavailable.

  • OAuthScope.catalog(api) lists described scopes in declaration order.

  • oauth.securityLayer(api) implements RequireAccess with the verification deadline, failure reporting, WWW-Authenticate challenges, and CurrentAccess of oauth.middleware. It fails with ConfigurationError when a protected endpoint has no valid requirement, an annotated endpoint lacks the middleware (for example .middleware before a later .add), a requirement names an undeclared scope, or the HttpApi itself is annotated.
  • const Read = OAuthScope.make("documents:read");
    const AllDocuments = OAuthScope.make("documents");
    const oauth = OAuthServer.make("documents", { scopes: [Read, AllDocuments] });
    
    export const Api = HttpApi.make("documents").add(
      HttpApiGroup.make("documents")
        .add(HttpApiEndpoint.get("list", "/documents", { success: DocumentList }))
        .annotate(OAuthScope.Required, {
          anyOf: [{ name: Read, description: "Read your documents" }, AllDocuments],
        })
        .middleware(OAuthScope.RequireAccess),
    );
    
    const ApiLive = HttpApiBuilder.layer(Api).pipe(
      Layer.provide(DocumentsLive),
      Layer.provide(oauth.securityLayer(Api)),
    );
    

    oauth.middleware responses are unchanged; both paths now share the verification classification and challenge builder. 503 stays a raw no-store response because its cause is already reported, and failing with HttpApiError.ServiceUnavailable would report it again through the HttpApi builder. Challenges advertise the allOf scopes and the first anyOf scope.

    Out of scope: a Layer for resource servers that verify tokens with makeAccessTokenVerifier.

    <!-- codesmith:footer -->
    ---
    <a href="https://app.blacksmith.sh/yielded-dev/codesmith/auth/pr/271?autoLogin=true&ref=codesmith_pr_footer"><picture><source media="(prefers-color-scheme: dark)" srcset="https://pr-comments-assets.blacksmith.sh/codesmith/view-with-codesmith-dark-v3.svg"><source media="(prefers-color-scheme: light)" srcset="https://pr-comments-assets.blacksmith.sh/codesmith/view-with-codesmith-light-v3.svg"><img alt="View with [code]smith" src="https://pr-comments-assets.blacksmith.sh/codesmith/view-with-codesmith-dark-v3-3x.png" width="162" height="28"></picture></a> <a href="https://backend.blacksmith.sh/track/enable-autofix?expires=1795525378&installation_model_id=440452&pr_number=271&ref=codesmith_pr_footer&repository=yielded-dev%2Fauth&return_to=https%3A%2F%2Fgithub.com%2Fyielded-dev%2Fauth%2Fpull%2F271&signature=605a128bbb57c367f45c35953a0ddee4f2dc9058e7d5286a49a054380c306a3a"><picture><source media="(prefers-color-scheme: dark)" srcset="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-light.svg"><img alt="Autofix with [code]smith" src="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-dark-3x.png" width="190" height="28"></picture></a>
    <sup>Need help on this PR? Tag <code>@codesmith-bot</code> with what you need.</sup>

    <!-- codesmith:autofix:disabled -->
    <!-- /codesmith:footer -->

    Diff: 496 lines (+372 / -124) across 6 files Reviewer: github-actions[bot] Review duration: 6m
    All Vouch review confidence checks passed
    Residual risk 0.52 = Change Risk (0.71) × (1 − Review Confidence (0.27)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.71 · 496 lines diff across 6 files
    0.71
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 48% · Reviewer familiarity: 35%
    26%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.371
    Shallow Review Depth shallow_review_depth
    0.344
    Sensitive Paths sensitive_paths
    0.071
    Real PR Conversation Timeline
    1 comments
    "Review active on #271"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: medium confidence: 26.9% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 70.6% (HIGH) · Higher = larger blast radius
    70.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 26.9% · Lower = rushed or shallow review
    26.9%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 51.6% · Moderate risk
    51.6%