msal-token
Small wrapper around msal-node for getting tokens from Microsoft (with certificate or secret)
Install
npm i @vestfoldfylke/msal-token
Prerequisities
- An app registration with permissions to whatever your scope is
- A secret or a certificate with in PFX format (can e.g. be created in an azure keyvault) added on the app registration (secrets and certificates tab)
Create a secret
- Go to app registrations "secrets and certificates"
- Create a new secret
- Store the secret somewhere safe (you won't see it again after creation)
Add certificate to app registration
- Go to app registrations "secrets and certificates"
- Upload the certficate
- Copy the thumbprint of the certificate
Usage
const { getAccessToken } = require('@vestfoldfylke/msal-token')
const authConfigSecret = {
clientId: CLIENT_ID,
tenantId: TENANT_ID,
clientSecret: CLIENT_SECRET,
scopes: TOKEN_SCOPES
}
const authConfigPfx = {
clientId: CLIENT_ID,
tenantId: TENANT_ID,
thumbprint: THUMBPRINT,
pfxcert: PFX_CERT_AS_BASE64,
privateKeyPassphrase: PFX_PRIVATE_KEY_PASSPHRASE || null,
scopes: TOKEN_SCOPES
}
const token = await getAccessToken(authConfigSecret || authConfigPfx)