requarks / wiki

Wiki.js | A modern and powerful wiki app built on Node.js
https://js.wiki
GNU Affero General Public License v3.0
24.43k stars 2.7k forks source link

Add support for azure ad b2c auth #7052

Open lfernandes00 opened 6 months ago

lfernandes00 commented 6 months ago

I was having problems trying to implement azure ad b2c user flow in Azure AD auth strategy. I kept getting the error "Invalid email / username or password on sign in".

After running some tests I found out that the issue came from the JWT sent by AZURE AD B2C auth which do not contain email or preferred_username claims expected by Azure AD authentication. The JWT from b2c returns the user email on an array claim emails.

I fix this by changing the following lines in /server/modules/authentication/azure/authentication.js file.

I changed:

const usrEmail = _.get(profile, '_json.email', null) || _.get(profile, '_json.preferred_username')

To:

const emails = _.get(profile, '_json.emails', null)
const usrEmail = _.get(profile, '_json.email', null) || _.get(profile, '_json.preferred_username') || emails[0]

Also, the JWT from B2C doesn't contain the oid claim. So I also change this lines to fix it:

From:

profile: {
              id: profile.oid,
              displayName: profile.displayName,
              email: usrEmail,
              picture: ''
            }

To:

const id = _.get(profile, '_json.sub', null)
profile: {
              id: profile.oid || id,
              displayName: profile.displayName,
              email: usrEmail,
              picture: ''
            }

I tested everything and it worked as expected.

@NGPixel I'd like to get your feedback as soon as possible. Thank you!

lfernandes00 commented 3 months ago

@NGPixel Can I get any feedback please?

lfernandes00 commented 1 month ago

@NGPixel If you can provide any feedback on this I would really appreciate it. I believe adding support for Azure Ad B2C auth would really be a quick win and a very nit feature addition to Wiki.js. Thanks in advance for your attention, let me know if anything is needed.