Skip to content

Resolve Custom JWT auth provider to the registered Bearer scheme - #3821

Open
Souvik Ghosh (souvikghosh04) wants to merge 2 commits into
mainfrom
bugfix/3541-custom-auth-oauth-scheme
Open

Souvik Ghosh (souvikghosh04) wants to merge 2 commits into
mainfrom
bugfix/3541-custom-auth-oauth-scheme

Conversation

@souvikghosh04

Copy link
Copy Markdown
Contributor

Summary

Fixes #3541.

Requests fail with Custom (and any non out‑of‑box JWT) authentication provider:

System.InvalidOperationException: No authentication handler is registered for the scheme
'OAuthAuthentication'. The registered schemes are: StaticWebAppsAuthentication,
AppServiceAuthentication, Bearer, SimulatorAuthentication.

This affects normal REST/GraphQL requests as well as the /api/openapi (Swagger) endpoint.

Root cause

An internal authentication‑scheme name mismatch:

  • For a JWT‑configured provider (anything that isn't EasyAuth / Simulator / Unauthenticated —
    e.g. Custom), DAB registers the JWT bearer handler under
    JwtBearerDefaults.AuthenticationScheme ("Bearer") in both
    ConfigureAuthentication (production) and ConfigureAuthenticationV2 (development,
    AddJwtBearer() → "Bearer").
  • At request time, ClientRoleHeaderAuthenticationMiddleware.ResolveConfiguredAuthNScheme
    returned GenericOAuthDefaults.AUTHENTICATIONSCHEME ("OAuthAuthentication") for Custom —
    a scheme that is never registered as a handler anywhere in the codebase.
  • httpContext.AuthenticateAsync("OAuthAuthentication") therefore throws.

AzureAD / EntraID worked only because they explicitly returned the matching "Bearer"
scheme. Custom is equally a JWT‑bearer provider and must resolve to the same registered scheme.

Fix

  • ResolveConfiguredAuthNScheme now resolves every JWT‑configured provider (AzureAD, EntraID,
    and any custom provider such as Custom) to JwtBearerDefaults.AuthenticationScheme,
    matching the handler registration.
  • Removed the now‑unused GenericOAuthDefaults class (its "OAuthAuthentication" constant was
    never registered as a handler and was the sole source of the mismatch).

Tests

  • Added JwtTokenAuthenticationUnitTests.TestValidToken_JwtConfiguredProviders, a regression
    test that validates a JWT through the middleware for Custom, AzureAD, and EntraID
    providers and asserts the request is authenticated (HTTP 200). Prior to the fix, the Custom
    case threw No authentication handler is registered for the scheme 'OAuthAuthentication'.
  • All 15 tests in JwtTokenAuthenticationUnitTests pass; dotnet format --verify-no-changes
    is clean on the changed files.

Custom (and any non out-of-box JWT) auth providers registered the JWT handler under the Bearer scheme but ClientRoleHeaderAuthenticationMiddleware resolved the request scheme to the unregistered 'OAuthAuthentication', causing AuthenticateAsync to throw 'No authentication handler is registered for the scheme OAuthAuthentication' on every REST/GraphQL/OpenAPI request. Resolve all JWT-configured providers to JwtBearerDefaults.AuthenticationScheme and remove the now-unused GenericOAuthDefaults. Adds regression test covering Custom/AzureAD/EntraID.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Removing the public GenericOAuthDefaults type may break consumers; retain an obsolete compatibility shim.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity

Open (1)
What changed in this PR

Fixes JWT authentication for custom providers by resolving them to the registered Bearer scheme.

Changes:

  • Aligns JWT provider resolution with the registered bearer handler.
  • Adds regression tests for Custom, AzureAD, and EntraID.
  • Removes the unused OAuth scheme definition.
File Description
src/​Service.Tests/​Authentication/​JwtTokenAuthenticationUnitTests.cs Adds JWT provider regression coverage.
src/​Core/​AuthenticationHelpers/​GenericOAuthDefaults.cs Removes the obsolete OAuth scheme definition.
src/​Core/​AuthenticationHelpers/​ClientRoleHeaderAuthenticationMiddleware.cs Maps JWT providers to the registered bearer scheme.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines 201 to +205
{
// Changing this value is a breaking change because non-out of box
// authentication provider names supplied in dab-config.json indicate
// that JWT bearer authentication should be used.
return GenericOAuthDefaults.AUTHENTICATIONSCHEME;
// Every non-EasyAuth/Simulator/Unauthenticated provider (AzureAD, EntraID, and any
// custom OAuth/JWT provider such as "Custom") is authenticated via JWT bearer. The JWT
// handler is always registered under JwtBearerDefaults.AuthenticationScheme ("Bearer")
// in Startup's ConfigureAuthentication/ConfigureAuthenticationV2, so the resolved scheme

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed this as well, but I dont think it is an issue that will arise, as I doubt anyone it taking dependencies on this. We could document it however just so that it is made clear.

@souvikghosh04 Souvik Ghosh (souvikghosh04) moved this from In Progress to Review In Progress in Data API builder Sep 21, 2026

@aaronburtle aaronburtle left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, might want to document the change in pubic facing type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Review In Progress

Development

Successfully merging this pull request may close these issues.

[Bug]: Swagger/OpenAPI fails with Custom Auth in DAB 1.7.93: No authentication handler is registered for OAuthAuthentication

4 participants