This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
authentik/passbook/sources/oauth/auth.py

24 lines
867 B
Python
Raw Normal View History

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
from django.http import HttpRequest
2018-11-11 12:41:48 +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."
def authenticate(
self, request: HttpRequest, source: OAuthSource, identifier: str
) -> User:
2018-11-11 12:41:48 +00:00
"Fetch user for a given source by id."
source_q = Q(source__name=source)
if isinstance(source, OAuthSource):
source_q = Q(source=source)
access = UserOAuthSourceConnection.objects.filter(
source_q, identifier=identifier
).select_related("user")[0]
return access.user