2018-11-11 12:41:48 +00:00
|
|
|
"""passbook oauth_client Authorization backend"""
|
2020-07-08 18:36:48 +00:00
|
|
|
from typing import Optional
|
|
|
|
|
2018-11-11 12:41:48 +00:00
|
|
|
from django.contrib.auth.backends import ModelBackend
|
2020-07-07 19:47:07 +00:00
|
|
|
from django.http import HttpRequest
|
2018-11-11 12:41:48 +00:00
|
|
|
|
2020-07-07 19:47:07 +00:00
|
|
|
from passbook.core.models import User
|
2019-12-31 11:51:16 +00: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."
|
|
|
|
|
2020-07-07 19:47:07 +00:00
|
|
|
def authenticate(
|
|
|
|
self, request: HttpRequest, source: OAuthSource, identifier: str
|
2020-07-08 18:36:48 +00:00
|
|
|
) -> Optional[User]:
|
2018-11-11 12:41:48 +00:00
|
|
|
"Fetch user for a given source by id."
|
2020-07-07 19:47:07 +00:00
|
|
|
access = UserOAuthSourceConnection.objects.filter(
|
2020-07-08 18:36:48 +00:00
|
|
|
source=source, identifier=identifier
|
|
|
|
).select_related("user")
|
|
|
|
if not access.exists():
|
|
|
|
return None
|
|
|
|
return access.first().user
|