I am trying to get access token from azure using msal node and need to follow service principle with certificate. Currently i am using key-vault url to read certificate. My references doc is https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/samples/msal-node-samples/auth-code-key-vault/index.js
const msal = require('@azure/msal-node');
const { DefaultAzureCredential } = require('@azure/identity');
const { CertificateClient } = require('@azure/keyvault-certificates');
const { SecretClient } = require('@azure/keyvault-secrets');
const getazureToken = async () => {
const credential = new DefaultAzureCredential();
const client = new CertificateClient(config.keyVaultUrl, credential);
const secretClient = new SecretClient(config.keyVaultUrl, credential);
const certResponse = await client.getCertificate(config.certificateName);
const thumbprint = certResponse.properties.x509Thumbprint.toString('hex');
const secretResponse = await secretClient.getSecret(config.certificateName);
const privateKey = secretResponse.value;
await msalApp(thumbprint, privateKey);
};
async function msalApp(thumbprint, privateKey) {
// Before running the sample, you will need to replace the values in the config
const msalConfig = {
auth: {
clientId: config.azureClientId,
authority: `${config.authorityUri}${config.tenantId}/`,
clientCertificate: {
thumbprint,
privateKey,
},
},
system: {
loggerOptions: {
loggerCallback(loglevel, message, containsPii) {
console.log('loglevel', loglevel, message);
},
piiLoggingEnabled: false,
logLevel: msal.LogLevel.Verbose,
},
},
};
// Create msal application object
const cca = new msal.ConfidentialClientApplication(msalConfig);
const authCodeUrlParameters = {
scopes: config.scope,
};
cca
.acquireTokenByClientCredential(authCodeUrlParameters)
.then((response) => {
console.log('==========> response', response);
})
.catch((error) =>
console.log('error------------->', JSON.stringify(error))
);
}
I have .pfx file of certificate too. If it can be helpful.