question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Special characters in passwords using PublicClientApplication.acquireTokenByUsernamePassword() in Node JS

See original GitHub issue

Core Library

MSAL Node (@azure/msal-node)

Core Library Version

1.4.0

Wrapper Library

Not Applicable

Wrapper Library Version

Nest JS

Description

I have integrated Azure AD authentication into an existing Node JS application built with the Nest JS framework for a Web application used by college students. As we need to collect additional data about students from Azure AD, I implemented authentication via user name and password. This works well for most users. However, some have special characters in their password. So far we have identified the characters & and + as problematic. I can assure you that these characters are posted intact as far as the initial call to PublicClientApplication.acquireTokenByUsernamePassword() :

The same characters are accepted by the server’s local authentication system using the Passport module. Where two-factor authentication is required, the frontend Web app does provide a client-side solution via a popup triggered by a remote instance of the MSAL library. This does enable students to enter any character in their passwords, but we’d rather avoid this scenario and have the same user journey for all users, whether using Azure AD authentication or other means. I have run tests to ensure the original string matches the one sent to acquireTokenByUsernamePassword() and it does. I have tried to escape these characters with backslashes to no avail and do not see any hints in your documentation. Please note this is not a browser issue as the same happens via Postman.

I have upgraded to @azure/msal-node to version 1.4.0. We initially deployed version 1.1.0 last year.

Any help or pointers, e.g. via configuration options, would be much appreciated.

Error Message

50126 - [2021-12-14 21:37:35Z]: AADSTS50126: Error validating credentials due to invalid username or password.\r\nTrace ID: 885a428b-91b5-4c94-aacc-082bbfdf2000\r\nCorrelation ID: 0567c418-a6cc-4c0e-9994-747317a5a929\r\nTimestamp: 2021-12-14 21:37:35Z - Correlation ID: 0567c418-a6cc-4c0e-9994-747317a5a929 - Trace ID: 885a428b-91b5-4c94-aacc-082bbfdf2000

Msal Logs

N/A

MSAL Configuration

const cachePlugin = {
  beforeCacheAccess,
  afterCacheAccess,
};

const msalConfig = {
  auth: {
    clientId: azureAd.auth.clientId,
    authority: azureAd.auth.authority,
  },
  cache: {
    cachePlugin,
  },
};

Relevant Code Snippets

const usernamePasswordRequest = {
      scopes: ['user.read'],
      username,
      password,
    };
    await pca
      .acquireTokenByUsernamePassword(usernamePasswordRequest)
      .then(response => {
        data.message = 'acquired token by password grant';
        data.result = response;
      }).catch(error =>{ ....})

Reproduction Steps

Enter email and password. Post data to the server. Decide authentication method based on email type. If using Azure AD, authenticate email and password via MSAL-node library Once authenticated, fetch additional data from Azure AD.

Expected Behavior

Should work in the same way as college portals where the students can log in with their email and password.

Identity Provider

Azure AD / MSA

Browsers Affected (Select all that apply)

None (Server)

Regression

No response

Source

External (Customer)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
ahaengglicommented, Dec 15, 2021

hi @neilg63 I had the same problem today with the @azure/Identity (v2.0.1) of “UsernamePasswordCredential”. In v1.5.1 this was working:

const aIdentity = require("@azure/identity");
let credential = new aIdentity.UsernamePasswordCredential(
        AZURE_TENANTID,
        AZURE_APP_ID,
        username,
        password
    );

However, with v2.0.1 it won’t. Presumably it’s the same problem as it seems, they switched form adal to msal. As a workaround, I used encodeURIComponent() for the password:

const aIdentity = require("@azure/identity");
let credential = new aIdentity.UsernamePasswordCredential(
        AZURE_TENANTID,
        AZURE_APP_ID,
        username,
        encodeURIComponent(password)
    );

Maybe you can also (temporarily) use js’ encodeURIComponent function?

0reactions
msftbot[bot]commented, Jun 6, 2022

🎉This issue was addressed in #4807, which has now been successfully released as @azure/msal-common@v6.4.0.🎉

We recommend upgrading to the latest version of @azure/msal-browser or @azure/msal-node to take advantage of this change.

Handy links:

Read more comments on GitHub >

github_iconTop Results From Across the Web

PublicClientApplication.AcquireTokenByUsernamePassword ...
Non-interactive request to acquire a security token from the authority, via Username/Password Authentication. See https://aka.ms/msal-net-up for details.
Read more >
Bcrypt not consistently comparing passwords in Node.js
Bcrypt not consistently comparing passwords in Node.js ... There are no special characters, the plainTextPassword is 'test' and the hash is ...
Read more >
Is it more secure to not allow passwords with special ...
My Gmail account got hacked last year which led me to develop a complex password system that includes special characters.
Read more >
PublicClientApplication | microsoft-authentication-libraries-for-js
Defined in msal-node/src/client/PublicClientApplication.ts:23 ... Acquires tokens with password grant by exchanging client applications username and ...
Read more >
How to Generate a Random Password using JavaScript
Approach 1: Make a string consist of Alphabets(lowercase and uppercase), Numbers and Special Characters. the we will use Math.random() and Math.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found