From 93386565df1b889075711bcae4759f6c252c2cf5 Mon Sep 17 00:00:00 2001 From: Shwetabh Kumar Date: Tue, 17 Jan 2023 17:33:22 +0530 Subject: [PATCH] Adding option to Login with refresh token (#29) * Adding option to Login with refresh token * Minor Changes * Fixing pylint errors --- fyle_rest_auth/helpers.py | 58 +++++++++++++++++++++++++++++++++++++-- fyle_rest_auth/urls.py | 5 ++-- fyle_rest_auth/views.py | 25 ++++++++++++++++- setup.py | 2 +- 4 files changed, 83 insertions(+), 7 deletions(-) diff --git a/fyle_rest_auth/helpers.py b/fyle_rest_auth/helpers.py index 164d435..3206f99 100644 --- a/fyle_rest_auth/helpers.py +++ b/fyle_rest_auth/helpers.py @@ -44,11 +44,63 @@ def validate_code_and_login(request): tokens['user']['org_name'] = employee_info['data']['org']['name'] # Update Fyle Credentials with latest healthy token - async_task( - 'apps.workspaces.tasks.async_update_fyle_credentials', - employee_info['data']['org']['id'], tokens['refresh_token'] + if 'async_update_user' in settings.FYLE_REST_AUTH_SETTINGS \ + and settings.FYLE_REST_AUTH_SETTINGS['async_update_user']: + async_task( + 'apps.workspaces.tasks.async_update_fyle_credentials', + employee_info['data']['org']['id'], tokens['refresh_token'] + ) + + return tokens + + except Exception as error: + raise ValidationError(error) + + +def validate_refresh_token_and_login(request): + """ + Takes refresh_token from payload + GET Fyle Admin info + Get Or Create User + Saves AuthToken + Return Tokens + """ + refresh_token = request.data.get('refresh_token') + try: + if not refresh_token: + raise ValidationError('refresh token not found') + + tokens = auth.refresh_access_token(refresh_token) + + employee_info = get_fyle_admin(tokens['access_token'], auth.get_origin_address(request)) + users = get_user_model() + + user, _ = users.objects.get_or_create( + user_id=employee_info['data']['user']['id'], + email=employee_info['data']['user']['email'] + ) + + AuthToken.objects.update_or_create( + user=user, + defaults={ + 'refresh_token': refresh_token + } ) + serializer = import_string(settings.FYLE_REST_AUTH_SERIALIZERS['USER_DETAILS_SERIALIZER']) + tokens['user'] = serializer(user).data + tokens['user']['full_name'] = employee_info['data']['user']['full_name'] + tokens['user']['org_id'] = employee_info['data']['org']['id'] + tokens['user']['org_name'] = employee_info['data']['org']['name'] + + # Update Fyle Credentials with latest healthy token + if 'async_update_user' in settings.FYLE_REST_AUTH_SETTINGS \ + and settings.FYLE_REST_AUTH_SETTINGS['async_update_user']: + async_task( + 'apps.workspaces.tasks.async_update_fyle_credentials', + employee_info['data']['org']['id'], tokens['refresh_token'] + ) + return tokens except Exception as error: diff --git a/fyle_rest_auth/urls.py b/fyle_rest_auth/urls.py index 4207b6b..684d67d 100644 --- a/fyle_rest_auth/urls.py +++ b/fyle_rest_auth/urls.py @@ -15,9 +15,10 @@ """ from django.urls import path -from .views import LoginView, RefreshView +from .views import LoginView, RefreshView, LoginWithRefreshTokenView urlpatterns = [ path('login/', LoginView.as_view()), - path('refresh/', RefreshView.as_view()) + path('refresh/', RefreshView.as_view()), + path('login_with_refresh_token/', LoginWithRefreshTokenView.as_view()), ] diff --git a/fyle_rest_auth/views.py b/fyle_rest_auth/views.py index 7648e95..851770c 100644 --- a/fyle_rest_auth/views.py +++ b/fyle_rest_auth/views.py @@ -4,7 +4,11 @@ from rest_framework.views import APIView, status from rest_framework.response import Response -from .helpers import validate_code_and_login, validate_and_refresh_token +from .helpers import ( + validate_code_and_login, + validate_and_refresh_token, + validate_refresh_token_and_login +) class LoginView(APIView): @@ -26,6 +30,25 @@ def post(self, request): ) +class LoginWithRefreshTokenView(APIView): + """ + Login Using Fyle Account + """ + authentication_classes = [] + permission_classes = [] + + def post(self, request): + """ + Login using refresh token + """ + tokens = validate_refresh_token_and_login(request) + + return Response( + data=tokens, + status=status.HTTP_200_OK, + ) + + class RefreshView(APIView): """ Refresh Access Token diff --git a/setup.py b/setup.py index e7c83d5..b14dc7d 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setuptools.setup( name='fyle-rest-auth', - version='1.3.1', + version='1.4.0', author='Shwetabh Kumar', author_email='shwetabh.kumar@fyle.in', description='Django application to implement OAuth 2.0 using Fyle in Django rest framework',