From 0806a2b588cf91e30b984b6cf9a19f19288d05fe Mon Sep 17 00:00:00 2001 From: Slawomir Koszewski Date: Tue, 3 Feb 2026 21:50:39 +0100 Subject: [PATCH] Add support for ClientSecret and DeviceCode credentials in getCredential function --- src/azure.js | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/azure.js b/src/azure.js index 31f56cd..cf3793d 100644 --- a/src/azure.js +++ b/src/azure.js @@ -1,12 +1,42 @@ -// auth.js +// azure.js import http from "node:http"; import { URL } from "node:url"; import open from "open"; import crypto from "node:crypto"; -import { PublicClientApplication } from "@azure/msal-node"; import fs from "node:fs"; import path from "node:path"; import os from "node:os"; +import { PublicClientApplication, ConfidentialClientApplication } from "@azure/msal-node"; +import { DefaultAzureCredential, ClientSecretCredential, DeviceCodeCredential } from "@azure/identity"; + +export async function getCredential(credentialType, options) { + switch (credentialType) { + case "default": + return new DefaultAzureCredential(); + 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 "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}`); + } +} function fileCachePlugin(cachePath) { return { @@ -135,4 +165,4 @@ export async function loginInteractive({ tenantId, clientId, scopes }) { } }); }); -} \ No newline at end of file +}