python-social-auth / social-core

Python Social Auth - Core
BSD 3-Clause "New" or "Revised" License
849 stars 545 forks source link

Microsoft backend "argument of type 'NoneType' is not iterable" #840

Open richardARPANET opened 1 year ago

richardARPANET commented 1 year ago

Expected behaviour

Login should complete.

Actual behaviour

Login fails to "complete", TypeError exception raises, server returns 500.

social-auth-app-django==4.0.0
social-auth-core==4.4.2

What are the steps to reproduce this issue?

I have no idea, this only happens sometimes for some users.

Any logs, error output, etc?

TypeError

argument of type 'NoneType' is not iterable
social_core/backends/microsoft.py in get_user_details at line 46
    def get_user_details(self, response):
        """Return user details from Microsoft online account"""
        email = response.get("mail")
        username = response.get("userPrincipalName")
        if "@" in username:
            if not email:
                email = username
            username = username.split("@", 1)[0]

The exception occurs on this line if "@" in username:, only sometimes.

Here is the data from response var pulled from Sentry:

{
@odata.context: 'https://graph.microsoft.com/v1.0/$metadata#users/$entity', 
access_token: REDACTED, 
displayName: None, 
expires_in: 3600, ext_expires_in: 3600, id: 'd785c8deb228b9a8', 
refresh_token: REDACTED, 
scope: 'User.Read Files.ReadWrite', 
token_type: 'Bearer', userPrincipalName: None
}

image

richardARPANET commented 1 year ago

https://learn.microsoft.com/en-us/answers/questions/1109943/will-graph-list-user-api-return-users-with-empty-u

Suggests this should never be null/none? :thinking: