Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
In JavaScript, how can we use the Microsoft Authentication Library to request a JWT token for a client connection with an an integration user?

We have a microservice, composed in JavaScript, which needs to consume a second microservice. The second microservice requires the consuming application to provide a JWT token which claims

  "roles": [
    "FooBar.Read"
  ],

for permission to use the service.

Rather than reinvent the wheel when calling Azure Active Directory to obtain and cache the token, we'd like to make use of the Microsoft Authentication Library node package.

I think we probably want to use the acquireTokenSilent() method of the ConfidentialClientApplication, but I'm not entirely clear how to create the request.

I've created this module:

import msal from '@azure/msal-node';
import {cachePlugin} from 'token-cache';


const confidentialClient = new msal.ConfidentialClientApplication({
    auth: {
        authority: `${process.env.AZURE_ACTIVE_DIRECTORY_AUTHORITY_URI}/${process.env.AZURE_ACTIVE_DIRECTORY_TENANT_ID}`,
        clientId: process.env.AZURE_ACTIVE_DIRECTORY_CLIENT_ID,
        clientSecret: process.env.AZURE_ACTIVE_DIRECTORY_CLIENT_SECRET,
        knownAuthorities: [],
    },
    cache: {
        cachePlugin,
    },
    system: {
        loggerOptions: {
            loggerCallback(loglevel, message) {
                console.log(message);
            },
            piiLoggingEnabled: false,
            logLevel: msal.LogLevel.Verbose,
        },
    },
});


const silentFlowRequest = {
    account: {
        tenantId: process.env.AZURE_ACTIVE_DIRECTORY_TENANT_ID,
        username: process.env.AZURE_ACTIVE_DIRECTORY_USERNAME,
        password: process.env.AZURE_ACTIVE_DIRECTORY_PASSWORD,
    },
    scopes: [process.env.AZURE_ACTIVE_DIRECTORY_EMPLOYEE_MANAGEMENT_SCOPE]
};

async function acquireToken() {
    try {
        return await  confidentialClient.acquireTokenSilent(silentFlowRequest)
    }
    catch (error) {
        console.error(error);
    }
}

module.exports = {
    acquireToken
};

However, I expect it to fail because Intell-J tells me:

Argument type {scopes: string[], account: {password: string, tenantId: string, username: string}} is not assignable to parameter type SilentFlowRequest

What is the correct way to do this?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Thanks for reaching out to us, please follow the doc - https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/wiki/AcquireTokenSilentAsync-using-a-cached-token#recommended-pattern-to-acquire-a-token .

hope this will help you.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use the MSAL's client credentials grant using a client secret in order to acquire tokens for your web API. We have a code sample with a fairly explanatory README here.

The client credentials grant first acquires a token (through ConfidentialClientApplicaiton.acquireTokenByClientCredentials) making a network request to AzureAD. Once the token is acquired, it is cached automatically by MSAL and subsequent calls will return the same token from the cache until it expires, at which point MSAL will refresh the token for you.

 // Create msal application object
 const confidentialClientApplication = new msal.ConfidentialClientApplication(
    {
      authOptions: {
          clientId: "<ENTER_CLIENT_ID>",
          authority: "https://login.microsoftonline.com/<ENTER_TENANT_ID>",
          clientSecret: "<ENTER_CLIENT_SECRET>"
        }
    });
  
  // Acquire tokens

function getClientCredentialsToken(confidentialClientApplication, scopes) {
    // With client credentials flows permissions need to be granted in the portal by a tenant administrator. 
    // The scope is always in the format "<resource>/.default"
    const clientCredentialRequest = {
        scopes: scopes
    };

    return confidentialClientApplication
        .acquireTokenByClientCredential(clientCredentialRequest)
        .then((response) => {
            // Handle response
        }).catch((error) => {
            // Handle error
        });
}

Essentially, you create a client secret on the Azure Portal and then place it in your MSAL configuration. This secret is used in place of user credentials, allowing your application to authenticate with AzureAD and acquire tokens without any user interaction.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda