Skip to content

Commit

Permalink
Black formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
AngellusMortis committed Jan 19, 2019
1 parent 6b8af73 commit 7b789c5
Show file tree
Hide file tree
Showing 20 changed files with 669 additions and 569 deletions.
6 changes: 3 additions & 3 deletions microsoft_auth/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

from ._version import get_versions
__version__ = get_versions()['version']

__version__ = get_versions()["version"]
del get_versions

default_app_config = 'microsoft_auth.apps.MicrosoftAuthConfig'
default_app_config = "microsoft_auth.apps.MicrosoftAuthConfig"
17 changes: 7 additions & 10 deletions microsoft_auth/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
User = get_user_model()

# override admin site template
admin.site.login_template = 'microsoft/admin_login.html'
admin.site.login_template = "microsoft/admin_login.html"

# djangoql support
extra_base = []
if apps.is_installed('djangoql'):
if apps.is_installed("djangoql"):
from djangoql.admin import DjangoQLSearchMixin

extra_base = [DjangoQLSearchMixin]
Expand All @@ -27,28 +27,25 @@

@admin.register(MicrosoftAccount)
class MicrosoftAccountAdmin(*base_admin):
readonly_fields = ('microsoft_id',)
readonly_fields = ("microsoft_id",)


class MicrosoftAccountInlineAdmin(admin.StackedInline):
model = MicrosoftAccount
readonly_fields = ('microsoft_id',)
readonly_fields = ("microsoft_id",)


@admin.register(XboxLiveAccount)
class XboxLiveAccountAdmin(*base_admin):
readonly_fields = ('xbox_id', 'gamertag')
readonly_fields = ("xbox_id", "gamertag")


class XboxLiveAccountInlineAdmin(admin.StackedInline):
model = XboxLiveAccount
readonly_fields = ('xbox_id', 'gamertag')
readonly_fields = ("xbox_id", "gamertag")


@admin.register(User)
class UserAdmin(*base_user_admin):
# adds MicrosoftAccount and XboxLiveAccount foreign keys to User model
inlines = (
MicrosoftAccountInlineAdmin,
XboxLiveAccountInlineAdmin
)
inlines = (MicrosoftAccountInlineAdmin, XboxLiveAccountInlineAdmin)
77 changes: 45 additions & 32 deletions microsoft_auth/apps.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@


class MicrosoftAuthConfig(AppConfig):
name = 'microsoft_auth'
verbose_name = 'Microsoft Auth'
name = "microsoft_auth"
verbose_name = "Microsoft Auth"


@register()
Expand All @@ -15,15 +15,18 @@ def microsoft_auth_validator(app_configs, **kwargs):

errors = []

if apps.is_installed('microsoft_auth') and \
not apps.is_installed('django.contrib.sites'):
if apps.is_installed("microsoft_auth") and not apps.is_installed(
"django.contrib.sites"
):

errors.append(
Critical(
'`django.contrib.sites` is not installed',
hint=('`microsoft_auth` requires `django.contrib.sites` '
'to be installed and configured'),
id='microsoft_auth.E001',
"`django.contrib.sites` is not installed",
hint=(
"`microsoft_auth` requires `django.contrib.sites` "
"to be installed and configured"
),
id="microsoft_auth.E001",
)
)

Expand All @@ -32,10 +35,12 @@ def microsoft_auth_validator(app_configs, **kwargs):
except KeyError:
errors.append(
Critical(
'current site not configured',
hint=('`django.contrib.sites` requires a `SITE_ID` setting '
'to be set'),
id='microsoft_auth.E002',
"current site not configured",
hint=(
"`django.contrib.sites` requires a `SITE_ID` setting "
"to be set"
),
id="microsoft_auth.E002",
)
)
else:
Expand All @@ -44,41 +49,49 @@ def microsoft_auth_validator(app_configs, **kwargs):
except OperationalError:
errors.append(
Warning(
'`django.contrib.sites` migrations not ran',
id='microsoft_auth.W001',
"`django.contrib.sites` migrations not ran",
id="microsoft_auth.W001",
)
)
else:
if str(current_site) == 'example.com':
if str(current_site) == "example.com":
errors.append(
Warning(
('current site is still `example.com`, Microsoft auth '
'might not work'),
hint=('Microsoft/Xbox auth uses OAuth, which requires '
'a real redirect URI to come back to'),
id='microsoft_auth.W002',
(
"current site is still `example.com`, Microsoft "
"auth might not work"
),
hint=(
"Microsoft/Xbox auth uses OAuth, which requires "
"a real redirect URI to come back to"
),
id="microsoft_auth.W002",
)
)

if config.MICROSOFT_AUTH_LOGIN_ENABLED:
if config.MICROSOFT_AUTH_CLIENT_ID == '':
if config.MICROSOFT_AUTH_CLIENT_ID == "":
errors.append(
Warning(
('`MICROSOFT_AUTH_CLIENT_ID` is not configured'),
hint=('`MICROSOFT_AUTH_LOGIN_ENABLED` is `True`, but '
'`MICROSOFT_AUTH_CLIENT_ID` is empty. Microsoft '
'auth will be disabled'),
id='microsoft_auth.W003',
("`MICROSOFT_AUTH_CLIENT_ID` is not configured"),
hint=(
"`MICROSOFT_AUTH_LOGIN_ENABLED` is `True`, but "
"`MICROSOFT_AUTH_CLIENT_ID` is empty. Microsoft "
"auth will be disabled"
),
id="microsoft_auth.W003",
)
)
if config.MICROSOFT_AUTH_CLIENT_SECRET == '':
if config.MICROSOFT_AUTH_CLIENT_SECRET == "":
errors.append(
Warning(
('`MICROSOFT_AUTH_CLIENT_SECRET` is not configured'),
hint=('`MICROSOFT_AUTH_LOGIN_ENABLED` is `True`, but '
'`MICROSOFT_AUTH_CLIENT_SECRET` is empty. Microsoft '
'auth will be disabled'),
id='microsoft_auth.W004',
("`MICROSOFT_AUTH_CLIENT_SECRET` is not configured"),
hint=(
"`MICROSOFT_AUTH_LOGIN_ENABLED` is `True`, but "
"`MICROSOFT_AUTH_CLIENT_SECRET` is empty. Microsoft "
"auth will be disabled"
),
id="microsoft_auth.W004",
)
)

Expand Down
53 changes: 29 additions & 24 deletions microsoft_auth/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ class MicrosoftAuthenticationBackend(ModelBackend):

config = None
microsoft = None
profile_url = 'https://graph.microsoft.com/v1.0/me'
profile_url = "https://graph.microsoft.com/v1.0/me"

def __init__(self, user=None):
from .conf import config

self.config = config

def authenticate(self, request, code=None):
Expand All @@ -39,8 +40,9 @@ def authenticate(self, request, code=None):
token = self.microsoft.fetch_token(code=code)

# validate permission scopes
if 'access_token' in token and \
self.microsoft.valid_scopes(token['scope']):
if "access_token" in token and self.microsoft.valid_scopes(
token["scope"]
):
user = self._authenticate_user()

return user
Expand All @@ -54,7 +56,7 @@ def _authenticate_user(self):
def _authenticate_xbox_user(self):
xbox_token = self.microsoft.fetch_xbox_token()

if 'Token' in xbox_token:
if "Token" in xbox_token:
response = self.microsoft.get_xbox_profile()
return self._get_user_from_xbox(response)
return None
Expand All @@ -63,7 +65,7 @@ def _authenticate_microsoft_user(self):
response = self.microsoft.get(self.profile_url)
if response.status_code == 200:
response = response.json()
if 'error' not in response:
if "error" not in response:
return self._get_user_from_microsoft(response)
return None

Expand All @@ -78,8 +80,10 @@ def _get_user_from_xbox(self, data):

user = xbox_user.user

if self.config.MICROSOFT_AUTH_XBL_SYNC_USERNAME and \
user.username != xbox_user.gamertag:
if (
self.config.MICROSOFT_AUTH_XBL_SYNC_USERNAME
and user.username != xbox_user.gamertag
):
user.username = xbox_user.gamertag
user.save()

Expand All @@ -89,17 +93,17 @@ def _get_xbox_user(self, data):
xbox_user = None

try:
xbox_user = XboxLiveAccount.objects.get(xbox_id=data['xid'])
xbox_user = XboxLiveAccount.objects.get(xbox_id=data["xid"])
# update Gamertag since they can change over time
if xbox_user.gamertag != data['gtg']:
xbox_user.gamertag = data['gtg']
if xbox_user.gamertag != data["gtg"]:
xbox_user.gamertag = data["gtg"]
xbox_user.save()
except XboxLiveAccount.DoesNotExist:
if self.config.MICROSOFT_AUTH_AUTO_CREATE:
# create new Xbox Live Account
xbox_user = XboxLiveAccount(
xbox_id=data['xid'],
gamertag=data['gtg'])
xbox_id=data["xid"], gamertag=data["gtg"]
)
xbox_user.save()

return xbox_user
Expand Down Expand Up @@ -129,13 +133,13 @@ def _get_microsoft_user(self, data):
microsoft_user = None

try:
microsoft_user = \
MicrosoftAccount.objects.get(microsoft_id=data['id'])
microsoft_user = MicrosoftAccount.objects.get(
microsoft_id=data["id"]
)
except MicrosoftAccount.DoesNotExist:
if self.config.MICROSOFT_AUTH_AUTO_CREATE:
# create new Microsoft Account
microsoft_user = MicrosoftAccount(
microsoft_id=data['id'])
microsoft_user = MicrosoftAccount(microsoft_id=data["id"])
microsoft_user.save()

return microsoft_user
Expand All @@ -144,18 +148,19 @@ def _verify_microsoft_user(self, microsoft_user, data):
if microsoft_user.user is None:
try:
# create new Django user from provided data
user = User.objects.get(email=data['userPrincipalName'])
user = User.objects.get(email=data["userPrincipalName"])

if user.first_name == '' and user.last_name == '':
user.first_name = data.get('givenName', '')
user.last_name = data.get('surname', '')
if user.first_name == "" and user.last_name == "":
user.first_name = data.get("givenName", "")
user.last_name = data.get("surname", "")
user.save()
except User.DoesNotExist:
user = User(
username=data['id'],
first_name=data.get('givenName', ''),
last_name=data.get('surname', ''),
email=data['userPrincipalName'])
username=data["id"],
first_name=data.get("givenName", ""),
last_name=data.get("surname", ""),
email=data["userPrincipalName"],
)
user.save()

microsoft_user.user = user
Expand Down
Loading

0 comments on commit 7b789c5

Please sign in to comment.