Migrated to TypeScript.
This commit is contained in:
50
src/azure/client-auth.ts
Normal file
50
src/azure/client-auth.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import { DefaultAzureCredential, ClientSecretCredential, DeviceCodeCredential } from "@azure/identity";
|
||||
|
||||
type CredentialType = "d" | "default" | "cs" | "clientSecret" | "dc" | "deviceCode";
|
||||
|
||||
type CredentialOptions = {
|
||||
tenantId?: string;
|
||||
clientId?: string;
|
||||
clientSecret?: string;
|
||||
};
|
||||
|
||||
export async function getCredential(
|
||||
credentialType: CredentialType,
|
||||
options: CredentialOptions,
|
||||
): Promise<DefaultAzureCredential | ClientSecretCredential | DeviceCodeCredential> {
|
||||
switch (credentialType) {
|
||||
case "d":
|
||||
case "default":
|
||||
return new DefaultAzureCredential();
|
||||
case "cs":
|
||||
case "clientSecret":
|
||||
if (!options.tenantId || !options.clientId || !options.clientSecret) {
|
||||
throw new Error(
|
||||
"tenantId, clientId, and clientSecret are required for ClientSecretCredential",
|
||||
);
|
||||
}
|
||||
return new ClientSecretCredential(
|
||||
options.tenantId,
|
||||
options.clientId,
|
||||
options.clientSecret,
|
||||
);
|
||||
case "dc":
|
||||
case "deviceCode":
|
||||
if (!options.tenantId || !options.clientId) {
|
||||
throw new Error(
|
||||
"tenantId and clientId are required for DeviceCodeCredential",
|
||||
);
|
||||
}
|
||||
return new DeviceCodeCredential({
|
||||
tenantId: options.tenantId,
|
||||
clientId: options.clientId,
|
||||
userPromptCallback: (info) => {
|
||||
console.log(info.message);
|
||||
},
|
||||
});
|
||||
default:
|
||||
throw new Error(`Unsupported credential type: ${credentialType}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user