Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions django_firebase_auth/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import importlib

from django.conf import settings

from django_firebase_auth.user_getter import AbstractUserGetter

AUTH_BACKEND = settings.DJANGO_FIREBASE_AUTH_AUTH_BACKEND
SERVICE_ACCOUNT_FILE = settings.DJANGO_FIREBASE_AUTH_SERVICE_ACCOUNT_FILE
WEB_API_KEY = settings.DJANGO_FIREBASE_AUTH_WEB_API_KEY
Expand All @@ -11,3 +15,8 @@
GET_OR_CREATE_USER_CLASS = getattr(settings, "DJANGO_FIREBASE_AUTH_GET_OR_CREATE_USER_CLASS",
'django_firebase_auth.user_getter:EmailOnlyUserGetter')
ADMIN_LOGIN_REDIRECT_URL = "admin:index"


GET_OR_CREATE_USER_MODULE, GET_OR_CREATE_USER_CLASS_NAME = GET_OR_CREATE_USER_CLASS.split(':')
GET_OR_CREATE_USER_CLASS = getattr(importlib.import_module(GET_OR_CREATE_USER_MODULE), GET_OR_CREATE_USER_CLASS_NAME)
user_getter: AbstractUserGetter = GET_OR_CREATE_USER_CLASS(CREATE_USER_IF_NOT_EXISTS)
37 changes: 37 additions & 0 deletions django_firebase_auth/drf_authentication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from django.utils.functional import SimpleLazyObject
from rest_framework.authentication import BaseAuthentication
from rest_framework.exceptions import AuthenticationFailed

from django_firebase_auth.conf import user_getter
from django_firebase_auth.views import verify_firebase_account, AuthError, NoAuthHeader


class LazyUser(SimpleLazyObject):
is_authenticated = True
is_anonymous = False

def __init__(self, func, firebase_uid):
self.__dict__['firebase_uid'] = firebase_uid
super().__init__(func)

def __bool__(self):
return True


class JWTAuthentication(BaseAuthentication):
"""
Use Django's session framework for authentication.
"""

def authenticate(self, request):
"""
Returns a `User` if the request session currently has a logged in user.
Otherwise returns `None`.
"""
try:
jwt_payload = verify_firebase_account(request.headers)
except NoAuthHeader:
return None
except AuthError as ex:
raise AuthenticationFailed(code=ex.error_type, detail=ex.error_type)
return LazyUser(lambda: user_getter.get_or_create_user(jwt_payload), jwt_payload['uid']), None
1 change: 1 addition & 0 deletions django_firebase_auth/v0/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from ..user_getter import AbstractUserGetter
from ..drf_authentication import JWTAuthentication
13 changes: 4 additions & 9 deletions django_firebase_auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@
from django.views import View

from django_firebase_auth.conf import AUTH_BACKEND, SERVICE_ACCOUNT_FILE, WEB_API_KEY, AUTH_DOMAIN, JWT_HEADER_NAME, \
ALLOW_NOT_CONFIRMED_EMAILS, ENABLE_GOOGLE_LOGIN, ADMIN_LOGIN_REDIRECT_URL, GET_OR_CREATE_USER_CLASS, \
CREATE_USER_IF_NOT_EXISTS
from django_firebase_auth.user_getter import AbstractUserGetter, UserNotFound

GET_OR_CREATE_USER_MODULE, GET_OR_CREATE_USER_CLASS_NAME = GET_OR_CREATE_USER_CLASS.split(':')
GET_OR_CREATE_USER_CLASS = getattr(importlib.import_module(GET_OR_CREATE_USER_MODULE), GET_OR_CREATE_USER_CLASS_NAME)
user_getter: AbstractUserGetter = GET_OR_CREATE_USER_CLASS(CREATE_USER_IF_NOT_EXISTS)
ALLOW_NOT_CONFIRMED_EMAILS, ENABLE_GOOGLE_LOGIN, ADMIN_LOGIN_REDIRECT_URL, user_getter
from django_firebase_auth.user_getter import UserNotFound

if SERVICE_ACCOUNT_FILE:
firebase_credentials = credentials.Certificate(SERVICE_ACCOUNT_FILE)
Expand Down Expand Up @@ -66,7 +61,7 @@ class EmailNotVerified(AuthError):

def authenticate(request: HttpRequest):
try:
jwt_payload = _verify_firebase_account(request.headers)
jwt_payload = verify_firebase_account(request.headers)
except AuthError as ex:
return JsonResponse(ex.make_response_body(), status=401)

Expand All @@ -84,7 +79,7 @@ def logout(request: HttpRequest):
return JsonResponse({"status": "ok"})


def _verify_firebase_account(headers: HttpHeaders) -> dict:
def verify_firebase_account(headers: HttpHeaders) -> dict:
jwt = headers.get(JWT_HEADER_NAME)
if jwt is None:
raise NoAuthHeader()
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
package_data={'': ['templates/firebase_authentication/*.html']},
include_package_data=True,
python_requires='>=3.6',
install_requires=["firebase_admin~=5.2.0"],
install_requires=["firebase_admin~=6.5.0"],
extras_require={'djangorestframework': ['djangorestframework>=3']},
)