Skip to content

Commit

Permalink
Adding option to Login with refresh token (#29)
Browse files Browse the repository at this point in the history
* Adding option to Login with refresh token

* Minor Changes

* Fixing pylint errors
  • Loading branch information
Shwetabhk authored Jan 17, 2023
1 parent d267c3d commit 9338656
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
58 changes: 55 additions & 3 deletions fyle_rest_auth/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions fyle_rest_auth/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
]
25 changes: 24 additions & 1 deletion fyle_rest_auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setuptools.setup(
name='fyle-rest-auth',
version='1.3.1',
version='1.4.0',
author='Shwetabh Kumar',
author_email='[email protected]',
description='Django application to implement OAuth 2.0 using Fyle in Django rest framework',
Expand Down

0 comments on commit 9338656

Please sign in to comment.