Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TS2339 compile error when using both BearerAuth and OAuth2Auth for authentication #2186

Closed
yufeih opened this issue Jan 4, 2024 · 4 comments · Fixed by #2195
Closed

TS2339 compile error when using both BearerAuth and OAuth2Auth for authentication #2186

yufeih opened this issue Jan 4, 2024 · 4 comments · Fixed by #2195
Assignees
Labels

Comments

@yufeih
Copy link

yufeih commented Jan 4, 2024

@azure-tools/typespec-ts version: 0.19.0

When using both BearerAuth and OAuth2Auth, the generated code results in TS2339 error.

TypeSpec file:

@useAuth([BearerAuth, OAuth2Auth<[{
    type: OAuth2FlowType.clientCredentials;
    tokenUrl: "https://login.microsoftonline.com/common/oauth2/v2.0/token";
    refreshUrl: "https://login.microsoftonline.com/common/oauth2/v2.0/token";
    scopes: ["foo/.default"];
}]>])
namespace MyService;

Generated code:

export default function createClient(
  credentials: TokenCredential | KeyCredential,
  options: ClientOptions = {}
): MyServiceClient {
   //...

  const client = getClient(baseUrl, credentials, options) as MyServiceClient;

  client.pipeline.addPolicy({
    name: "customKeyCredentialPolicy",
    async sendRequest(request, next) {
      request.headers.set("Authorization", "bearer " + credentials.key);  // <--- TS2339
      return next(request);
    },
  });
  return client;
}

Error:

Property 'key' does not exist on type 'TokenCredential | KeyCredential'.
  Property 'key' does not exist on type 'TokenCredential'.ts(2339)
@MaryGao
Copy link
Member

MaryGao commented Jan 4, 2024

Thanks for reporting and this would be a codegen bug and the customKeyCredentialPolicy should only be added when the credential is KeyCredential. I will work on the fix.

@MaryGao
Copy link
Member

MaryGao commented Jan 9, 2024

@yufeih I'd like to confirm with you one detail, for BearerAuth regarding the word bearer in Authorization header do we(server side) care about it is capital case or lower case?

@yufeih
Copy link
Author

yufeih commented Jan 10, 2024

@yufeih I'd like to confirm with you one detail, for BearerAuth regarding the word bearer in Authorization header do we(server side) care about it is capital case or lower case?

Our server should treat is as case insensitive, it seems Bearer is more appropriate. Here is the codegen for C#:

        private const string AuthorizationApiKeyPrefix = "Bearer";

@MaryGao
Copy link
Member

MaryGao commented Jan 10, 2024

Thanks for the info! Yes, generally Bearer is the convention and server side should treat as case insensitive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants