Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Security fix] AzureADOAuth2 backend #910

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 24 additions & 25 deletions social_core/backends/azuread.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import time

import jwt

from ..exceptions import AuthTokenError
from .oauth import BaseOAuth2

"""
Expand Down Expand Up @@ -83,32 +80,34 @@ def get_user_id(self, details, response):
return response.get("upn")

def get_user_details(self, response):
"""Return user details from Azure AD account"""
fullname, first_name, last_name = (
response.get("name", ""),
response.get("given_name", ""),
response.get("family_name", ""),
)
"""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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

username was based on name before.


return {
"username": fullname,
"email": response.get("email", response.get("upn")),
"fullname": fullname,
"first_name": first_name,
"last_name": last_name,
"username": username,
"email": email,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

email used to fall back to userPrincipalName if not present, this is now gone.

"fullname": response.get("displayName", ""),
"first_name": response.get("givenName", ""),
"last_name": response.get("surname", ""),
}

def user_data(self, access_token, *args, **kwargs):
response = kwargs.get("response")
if response and response.get("id_token"):
id_token = response.get("id_token")
else:
id_token = access_token

try:
decoded_id_token = jwt.decode(id_token, options={"verify_signature": False})
except (jwt.DecodeError, jwt.ExpiredSignatureError) as de:
raise AuthTokenError(self, de)
return decoded_id_token
"""Return user data by querying Microsoft service"""
return self.get_json(
"https://graph.microsoft.com/v1.0/me",
headers={
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
"Authorization": "Bearer " + access_token,
},
method="GET",
)

def auth_extra_arguments(self):
"""Return extra arguments needed on auth process. The defaults can be
Expand Down
Loading