2018-11-11 12:41:48 +00:00
|
|
|
"""passbook oauth_client Authorization backend"""
|
|
|
|
|
|
|
|
from django.contrib.auth.backends import ModelBackend
|
|
|
|
from django.db.models import Q
|
|
|
|
|
2019-10-07 15:33:48 +01:00
|
|
|
from passbook.sources.oauth.models import (OAuthSource,
|
|
|
|
UserOAuthSourceConnection)
|
2018-11-11 12:41:48 +00:00
|
|
|
|
2018-11-16 08:10:35 +00:00
|
|
|
|
2018-11-11 12:41:48 +00:00
|
|
|
class AuthorizedServiceBackend(ModelBackend):
|
|
|
|
"Authentication backend for users registered with remote OAuth provider."
|
|
|
|
|
|
|
|
def authenticate(self, request, source=None, identifier=None):
|
|
|
|
"Fetch user for a given source by id."
|
|
|
|
source_q = Q(source__name=source)
|
|
|
|
if isinstance(source, OAuthSource):
|
|
|
|
source_q = Q(source=source)
|
|
|
|
try:
|
|
|
|
access = UserOAuthSourceConnection.objects.filter(
|
|
|
|
source_q, identifier=identifier
|
|
|
|
).select_related('user')[0]
|
|
|
|
except IndexError:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return access.user
|