diff --git a/django_firebase_auth/__init__.py b/django_firebase_auth/__init__.py index 7d7765a..c70020d 100644 --- a/django_firebase_auth/__init__.py +++ b/django_firebase_auth/__init__.py @@ -1 +1 @@ -from .settings import * +from .settings import * \ No newline at end of file diff --git a/django_firebase_auth/firebase_auth.py b/django_firebase_auth/firebase_auth.py index f7cc385..fa98252 100644 --- a/django_firebase_auth/firebase_auth.py +++ b/django_firebase_auth/firebase_auth.py @@ -7,14 +7,19 @@ import firebase_admin.auth from abstract_auth.abstract_auth import AbstractAuthentication -from django_firebase_auth.models import AuthFirebaseProfile +from django_firebase_auth.models import UserFirebaseProfile class FirebaseAuthentication(AbstractAuthentication): - def _get_or_create_profile(self, user, uid): - return AuthFirebaseProfile.objects.get_or_create( + token_post_index_name = 'firebase_auth_token' + def _get_or_create_profile(self, user, uid,avatar:str): + return UserFirebaseProfile.objects.update_or_create( user=user, + defaults={ + "uid": uid, + "photo_url": avatar, + } )[0] def _verify_token(self, id_token): - return firebase_admin.auth.verify_id_token(id_token) + return firebase_admin.auth.verify_id_token(id_token, check_revoked=False) diff --git a/django_firebase_auth/models.py b/django_firebase_auth/models.py index 4bfd3ae..ff3e531 100644 --- a/django_firebase_auth/models.py +++ b/django_firebase_auth/models.py @@ -1,6 +1,7 @@ from abstract_auth.abstract_user_profile import AbstractAuthProfile -class AuthFirebaseProfile(AbstractAuthProfile): +class UserFirebaseProfile(AbstractAuthProfile): class Meta: app_label = "django_firebase_auth" + abstract = False diff --git a/django_firebase_auth/serializers/__init__.py b/django_firebase_auth/serializers/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/django_firebase_auth/serializers/firebase_authentication_serializer.py b/django_firebase_auth/serializers/firebase_authentication_serializer.py new file mode 100644 index 0000000..cec69c3 --- /dev/null +++ b/django_firebase_auth/serializers/firebase_authentication_serializer.py @@ -0,0 +1,15 @@ +from django.utils.translation import gettext_lazy as _ +from rest_framework import serializers + + + +class FirebaseAuthTokenSerializer(serializers.Serializer): + firebase_auth_token = serializers.CharField() + + def validate(self, attrs): + firebase_auth_token = attrs.get("firebase_auth_token") + if firebase_auth_token is None: + msg = _('Must include "firebase_auth_token".') + raise serializers.ValidationError(msg, code="authorization") + attrs["user"] = self.context["user"] + return attrs \ No newline at end of file diff --git a/django_firebase_auth/urls.py b/django_firebase_auth/urls.py new file mode 100644 index 0000000..0f0a757 --- /dev/null +++ b/django_firebase_auth/urls.py @@ -0,0 +1,8 @@ +from django.urls import path +from django_firebase_auth.viewsets.firebase_auth_viewset import FirebaseAuthViewSet + +app_name = 'firebase_auth' +urlpatterns = [ + path('login/', FirebaseAuthViewSet.as_view(), name='login'), + # path('logout/', FirebaseAuthLogoutViewSet.as_view(), name='logout'), +] diff --git a/django_firebase_auth/viewsets/__init__.py b/django_firebase_auth/viewsets/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/django_firebase_auth/viewsets/firebase_auth_logout_viewset.py b/django_firebase_auth/viewsets/firebase_auth_logout_viewset.py new file mode 100644 index 0000000..0996b97 --- /dev/null +++ b/django_firebase_auth/viewsets/firebase_auth_logout_viewset.py @@ -0,0 +1,13 @@ +from rest_framework import viewsets + + +class FirebaseAuthLogoutViewSet(viewsets.ViewSet): + """ + This viewset is used to logout the user from the firebase. + """ + + def create(self, request): + """ + This method is used to logout the user from the firebase. + """ + pass \ No newline at end of file diff --git a/django_firebase_auth/viewsets/firebase_auth_viewset.py b/django_firebase_auth/viewsets/firebase_auth_viewset.py new file mode 100644 index 0000000..6d28b7f --- /dev/null +++ b/django_firebase_auth/viewsets/firebase_auth_viewset.py @@ -0,0 +1,17 @@ +from rest_framework import permissions +from rest_framework.authtoken.views import ObtainAuthToken + +from django_firebase_auth.firebase_auth import FirebaseAuthentication +from django_firebase_auth.serializers.firebase_authentication_serializer import ( + FirebaseAuthTokenSerializer, +) + + +class FirebaseAuthViewSet(ObtainAuthToken): + permission_classes = (permissions.AllowAny,) + authentication_classes = [FirebaseAuthentication] + serializer_class = FirebaseAuthTokenSerializer + + def get_serializer_context(self): + user = self.request.user + return {"user": user}