diff --git a/fyle_rest_auth/helpers.py b/fyle_rest_auth/helpers.py index b30aca2..be2c966 100644 --- a/fyle_rest_auth/helpers.py +++ b/fyle_rest_auth/helpers.py @@ -19,6 +19,31 @@ logger.level = logging.INFO +def get_cluster_domain_by_code(request): + authorization_code = request.data.get('code') + try: + if not authorization_code: + raise ValidationError('authorization code not found') + + tokens = auth.generate_fyle_refresh_token(authorization_code=authorization_code) + + cluster_domain = settings.API_URL.split('/api')[0] if settings.DEBUG \ + else get_cluster_domain(tokens['access_token'], auth.get_origin_address(request)) + + return { + 'cluster_domain': cluster_domain, + 'tokens': tokens + } + + except ValidationError as error: + logger.info(error) + raise + + except Exception as error: + logger.error(traceback.format_exc()) + raise ValidationError(error) + + def validate_code_and_login(request): authorization_code = request.data.get('code') try: diff --git a/fyle_rest_auth/urls.py b/fyle_rest_auth/urls.py index 684d67d..f84c455 100644 --- a/fyle_rest_auth/urls.py +++ b/fyle_rest_auth/urls.py @@ -15,10 +15,11 @@ """ from django.urls import path -from .views import LoginView, RefreshView, LoginWithRefreshTokenView +from .views import LoginView, RefreshView, LoginWithRefreshTokenView, ClusterDomainView urlpatterns = [ path('login/', LoginView.as_view()), path('refresh/', RefreshView.as_view()), path('login_with_refresh_token/', LoginWithRefreshTokenView.as_view()), + path('cluster_domain/', ClusterDomainView.as_view()), ] diff --git a/fyle_rest_auth/views.py b/fyle_rest_auth/views.py index 851770c..719da8c 100644 --- a/fyle_rest_auth/views.py +++ b/fyle_rest_auth/views.py @@ -7,7 +7,8 @@ from .helpers import ( validate_code_and_login, validate_and_refresh_token, - validate_refresh_token_and_login + validate_refresh_token_and_login, + get_cluster_domain_by_code ) @@ -63,3 +64,19 @@ def post(self, request): data=tokens, status=status.HTTP_200_OK ) + + +class ClusterDomainView(APIView): + """ + Get Cluster Domain + """ + authentication_classes = [] + permission_classes = [] + + def post(self, request): + response = get_cluster_domain_by_code(request) + + return Response( + data=response, + status=status.HTTP_200_OK + ) diff --git a/setup.py b/setup.py index d2719c0..242962b 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setuptools.setup( name='fyle-rest-auth', - version='1.5.0', + version='1.6.0', author='Shwetabh Kumar', author_email='shwetabh.kumar@fyle.in', description='Django application to implement OAuth 2.0 using Fyle in Django rest framework',