Skip to content

[Bug][http-client-csharp] NoAuth with OAuth2Auth emits Client that still requires credentials #9811

@pvaneck

Description

@pvaneck

Describe the bug

In some scenarios, a service can support both authenticated and unauthenticated requests.

If I have a service namespace with the following:

@useAuth(
  NoAuth | OAuth2Auth<[
    {
      type: OAuth2FlowType.implicit,
      authorizationUrl: "https://login.microsoftonline.com/common/oauth2/authorize",
      scopes: ["https://monitor.azure.com/.default"],
    }
  ]>
)

The http-client-csharp emitter will emit a client where TokenCredential is required. Example constructor:

public ApplicationInsightsClient(Uri endpoint, TokenCredential credential, ApplicationInsightsClientOptions options)
{
    Argument.AssertNotNull(endpoint, nameof(endpoint));
    Argument.AssertNotNull(credential, nameof(credential));

    options ??= new ApplicationInsightsClientOptions();

    _endpoint = endpoint;
    _tokenCredential = credential;
    Pipeline = HttpPipelineBuilder.Build(options, new HttpPipelinePolicy[] { new BearerTokenAuthenticationPolicy(_tokenCredential, AuthorizationScopes) });
    _apiVersion = options.Version;
    ClientDiagnostics = new ClientDiagnostics(options, true);
}

The emitter will also give the warning: main.tsp:28:11 - warning @typespec/http-client-csharp/unsupported-auth: un-supported authentication scheme noAuth

Can we make it so that the credential doesn't need to be specified for unauthenticated requests? Similar issue in the python emitter: #9238

Reproduction

Using the http-client-csharp emitter with:

@useAuth(
  NoAuth | OAuth2Auth<[
    {
      type: OAuth2FlowType.implicit,
      authorizationUrl: "https://login.microsoftonline.com/common/oauth2/authorize",
      scopes: ["https://monitor.azure.com/.default"],
    }
  ]>
)
@service(#{ title: "Azure Monitor Exporter" })
@versioned(Versions)
@server(
  "{host}/{apiVersion}",
  "Azure MonitorOpenTelemetry Exporter Service",
  {
    /** Application Insights' Breeze endpoint. */
    host: url = "https://dc.services.visualstudio.com",

    /** The service API version. */
    apiVersion: Versions,
  }
)
namespace AzureMonitorExporter;

Checklist

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingemitter:client:csharpIssue for the C# client emitter: @typespec/http-client-csharp

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions