Skip to content

Instantly share code, notes, and snippets.

@uyar
Last active November 30, 2022 07:31
Show Gist options
  • Save uyar/6264e8079ee222843328c05f323e01b5 to your computer and use it in GitHub Desktop.
Save uyar/6264e8079ee222843328c05f323e01b5 to your computer and use it in GitHub Desktop.
Authomatic provider for Microsoft Online (Azure Active Directory)
class MicrosoftOnline(OAuth2):
"""
Microsoft Online |oauth2| provider.
Supported :class:`.User` properties:
* email
* first_name
* id
* last_name
* name
* picture
Unsupported :class:`.User` properties:
* birth_date
* city
* country
* gender
* link
* locale
* location
* nickname
* phone
* postal_code
* timezone
* username
"""
# TODO: The below settings are for multi-tenant apps.
# For single-tenant apps the "common" part needs to be replaced
# with the tenant id.
# The tenant id should be a configuration parameter.
user_authorization_url = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize'
access_token_url = 'https://login.microsoftonline.com/common/oauth2/v2.0/token'
user_info_url = 'https://graph.microsoft.com/oidc/userinfo'
user_info_scope = ['openid profile'] # this might also need "offline_access"
supported_user_attributes = core.SupportedUserAttributes(
id=True,
email=True,
first_name=True,
last_name=True,
name=True,
picture=True,
)
@classmethod
def _x_credentials_parser(cls, credentials, data):
if data.get('token_type') == 'bearer':
credentials.token_type = cls.BEARER
return credentials
@staticmethod
def _x_user_parser(user, data):
user.id = data.get('sub')
user.name = data.get('name')
user.first_name = data.get('given_name', '')
user.last_name = data.get('family_name', '')
user.email = data.get('email', '')
user.picture = data.get('picture', '')
return user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment