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

report diagnostics on unsupported auth #5496

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/http-client-csharp/emitter/src/lib/logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

import { NoTarget, Program, Tracer } from "@typespec/compiler";
import { DiagnosticTarget, NoTarget, Program, Tracer } from "@typespec/compiler";
import { getTracer, reportDiagnostic } from "./lib.js";
import { LoggerLevel } from "./log-level.js";

Expand Down Expand Up @@ -63,19 +63,19 @@ export class Logger {
}
}

warn(message: string): void {
warn(message: string, target?: DiagnosticTarget | typeof NoTarget): void {
reportDiagnostic(this.program, {
code: "general-warning",
format: { message: message },
target: NoTarget,
target: target ?? NoTarget,
});
}

error(message: string): void {
error(message: string, target?: DiagnosticTarget | typeof NoTarget): void {
reportDiagnostic(this.program, {
code: "general-error",
format: { message: message },
target: NoTarget,
target: target ?? NoTarget,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,37 +47,49 @@ function processAuthType(credentialType: SdkCredentialType): InputAuth | undefin
const scheme = credentialType.scheme;
switch (scheme.type) {
case "apiKey":
return { ApiKey: { Name: scheme.name } } as InputAuth;
if (scheme.in !== "header") {
Logger.getInstance().warn(
`Only header is supported for ApiKey auth method. ${scheme.in} is not supported.`,
Copy link
Contributor

@jorgerangel-msft jorgerangel-msft Jan 7, 2025

Choose a reason for hiding this comment

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

How about: Only header is supported for ApiKey authentication. ${scheme.in} is not supported. ?

credentialType.__raw,
);
return undefined;
}
return { ApiKey: { Name: scheme.name, In: scheme.in } } as InputAuth;
case "oauth2":
return processOAuth2(scheme);
case "http":
{
const schemeOrApiKeyPrefix = scheme.scheme;
switch (schemeOrApiKeyPrefix) {
case "basic":
Logger.getInstance().warn(
`${schemeOrApiKeyPrefix} auth method is currently not supported.`,
);
return undefined;
case "bearer":
return {
ApiKey: {
Name: "Authorization",
Prefix: "Bearer",
},
} as InputAuth;
default:
return {
ApiKey: {
Name: "Authorization",
Prefix: schemeOrApiKeyPrefix,
},
} as InputAuth;
}
case "http": {
const schemeOrApiKeyPrefix = scheme.scheme;
switch (schemeOrApiKeyPrefix) {
case "basic":
Logger.getInstance().warn(
`${schemeOrApiKeyPrefix} auth method is currently not supported.`,
credentialType.__raw,
);
return undefined;
case "bearer":
return {
ApiKey: {
Name: "Authorization",
In: "header",
Prefix: "Bearer",
},
};
default:
return {
ApiKey: {
Name: "Authorization",
In: "header",
Prefix: schemeOrApiKeyPrefix,
},
};
}
break;
}
default:
throw new Error(`un-supported authentication scheme ${scheme.type}`);
Logger.getInstance().error(
`un-supported authentication scheme ${scheme.type}`,
credentialType.__raw,
);
return undefined;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@

export interface InputApiKeyAuth {
Name: string;
In: ApiKeyLocation;
Prefix?: string;
}

export type ApiKeyLocation = "header"; // | "query" | "cookie"; // we do not support query or cookie yet
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@
"$id": "18",
"ApiKey": {
"$id": "19",
"Name": "x-ms-api-key"
"Name": "x-ms-api-key",
"In": "header"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
"ApiKey": {
"$id": "19",
"Name": "Authorization",
"In": "header",
"Prefix": "SharedAccessKey"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@
"$id": "12",
"ApiKey": {
"$id": "13",
"Name": "x-ms-api-key"
"Name": "x-ms-api-key",
"In": "header"
},
"OAuth2": {
"$id": "14",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3684,7 +3684,8 @@
"$id": "348",
"ApiKey": {
"$id": "349",
"Name": "my-api-key"
"Name": "my-api-key",
"In": "header"
}
}
}
Loading