How to build a library that can be used in various types of applications (executables, web, etc.) using msal-node?

24 Views Asked by At

I'm developing a library for authentication using @azure/msal-node, however, for each type of application, there is a specific class. For instance, ConfidentialClientApps for web app, web API, service/daemon. PublicClientApps for desktop app, browserless API, mobile app. Is there a way to develop an application that encompasses various types of applications?

I'm testing this code, but it's not comprehensive enough to encompass non-web applications.

class MicrosoftUserSigninController {
  async handle(request: Request, response: Response): Promise<void> {

    const confidentialClientConfig = {
      auth: {
        clientId: process.env.CLIENT_ID,
        authority: process.env.AUTHORITY,
        clientSecret: process.env.CLIENT_SECRET,
        redirectUri: process.env.REDIRECT_URI,
        scopes: ['user.read'],
        state: state,
      },
    };

    const confidentialClientApplication =
      new msal.ConfidentialClientApplication(confidentialClientConfig);

    const authCodeRequest = {
      redirectUri: confidentialClientConfig.auth.redirectUri,
      authority: confidentialClientConfig.auth.authority,
      scopes: confidentialClientConfig.auth.scopes,
      state: confidentialClientConfig.auth.state,
    };

    const getAuthCode = async (authority, scopes, state, res) => {
      console.log('Auth code: ');
      authCodeRequest.authority = authority;
      authCodeRequest.scopes = scopes;
      authCodeRequest.state = state;

      try {
        const authCodeUrl = await confidentialClientApplication.getAuthCodeUrl(
          authCodeRequest
        );
        console.log(`URL: ${authCodeUrl}`);
        res.redirect(authCodeUrl);
      } catch (error) {
        console.error('Error:', error);
        res.status(500).send(error.message || 'Error');
      }
    };

    await getAuthCode(
      confidentialClientConfig.auth.authority,
      confidentialClientConfig.auth.scopes,
      confidentialClientConfig.auth.state,
      response
    );

    const tokenRequest = {
      //code: code,
    
      redirectUri: confidentialClientConfig.auth.redirectUri,
      scopes: confidentialClientConfig.auth.scopes,
    };

    confidentialClientApplication
      .acquireTokenByCode(tokenRequest)
      .then((response) => {
        console.log('\nResponse: \n:', response);
      })
      .catch((error) => {
        console.log(error);
      });
  }
}
0

There are 0 best solutions below