38 lines
1.1 KiB
JavaScript
38 lines
1.1 KiB
JavaScript
import { DefaultAzureCredential, ClientSecretCredential, DeviceCodeCredential } from "@azure/identity";
|
|
|
|
export async function getCredential(credentialType, options) {
|
|
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}`);
|
|
}
|
|
}
|