Self hosted FLOSS fitness/workout, nutrition and weight tracker
added jwt usage docs and added deprecation message for /login/ endpoint
Rohan Karan25f0e6094ff3
diff --git a/wger/core/api/views.py b/wger/core/api/views.pyindex 43507ce57..6a38637c9 100644--- a/wger/core/api/views.py+++ b/wger/core/api/views.py@@ -17,6 +17,8 @@# Standard Libraryimport logging+import warnings+from warnings import warn# Djangofrom django.contrib.auth.models import User@@ -62,7 +64,6 @@ from wger.utils.permissions import (WgerPermission,)-logger = logging.getLogger(__name__)@@ -101,7 +102,7 @@ class ApplicationVersionView(viewsets.ViewSet):"""Returns the application's version"""- permission_classes = (AllowAny, )+ permission_classes = (AllowAny,)@staticmethoddef get(request):@@ -112,7 +113,7 @@ class RequiredApplicationVersionView(viewsets.ViewSet):"""Returns the minimum required version of flutter app to access this server"""- permission_classes = (AllowAny, )+ permission_classes = (AllowAny,)@staticmethoddef get(request):@@ -122,6 +123,7 @@ class RequiredApplicationVersionView(viewsets.ViewSet):class UserAPILoginView(viewsets.ViewSet):"""API endpoint for api user objects+ .. warning:: This endpoint is deprecated"""permission_classes = (AllowAny, )queryset = User.objects.all()@@ -129,7 +131,15 @@ class UserAPILoginView(viewsets.ViewSet):throttle_scope = 'login'def get(self, request):- return Response({'message': "You must send a 'username' and 'password' via POST"})+ return Response(+ data={+ 'message': "You must send a 'username' and 'password' via POST",+ 'warning': "This endpoint is deprecated."+ },+ headers={+ "Deprecation": "Sat, 01 Oct 2022 23:59:59 GMT",+ },+ )def post(self, request):data = request.data@@ -147,7 +157,13 @@ class UserAPILoginView(viewsets.ViewSet):)token = create_token(form.get_user())- return Response({'token': token.key}, status=status.HTTP_200_OK)+ return Response(+ data={'token': token.key, 'message': "This endpoint is deprecated."},+ status=status.HTTP_200_OK,+ headers={+ "Deprecation": "Sat, 01 Oct 2022 23:59:59 GMT",+ }+ )class UserAPIRegistrationViewSet(viewsets.ViewSet):@@ -198,7 +214,7 @@ class DaysOfWeekViewSet(viewsets.ReadOnlyModelViewSet):queryset = DaysOfWeek.objects.all()serializer_class = DaysOfWeekSerializerordering_fields = '__all__'- filterset_fields = ('day_of_week', )+ filterset_fields = ('day_of_week',)class LicenseViewSet(viewsets.ReadOnlyModelViewSet):diff --git a/wger/software/templates/api.html b/wger/software/templates/api.htmlindex 46a0b40c1..7ab00bcd0 100644--- a/wger/software/templates/api.html+++ b/wger/software/templates/api.html@@ -16,11 +16,47 @@ ingredients can be accessed without authentication. For <strong>user ownedobjects such as workouts, you need to generate an API KEY</strong> and passit in the header, see the link on the sidebar for details.</p>-<p>You can also generate a token via the <code>login</code> endpoint. Send a-username and password and you will get the user's token or a new one will be-generated. At the moment it is not possible to register via the API.</p>+<h6>JWT Authentication</h6>++<p>+You can generate access token via <code>/token/</code> endpoint. Send a username and password, and you will get the+<code>access</code> token which you can use to access the private endpoints.+<pre>+curl \+ -X POST \+ -H "Content-Type: application/json" \+ -d '{"username": "example_username", "password": "example_password "}' \+ https://wger.de/api/v2/token/++...+{+ "access":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX3BrIjoxLCJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiY29sZF9zdHVmZiI6IuKYgyIsImV4cCI6MTIzNDU2LCJqdGkiOiJmZDJmOWQ1ZTFhN2M0MmU4OTQ5MzVlMzYyYmNhOGJjYSJ9.NHlztMGER7UADHZJlxNG0WSi22a2KaYSfd1S-AuT7lU",+ "refresh":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX3BrIjoxLCJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImNvbGRfc3R1ZmYiOiLimIMiLCJleHAiOjIzNDU2NywianRpIjoiZGUxMmY0ZTY3MDY4NDI3ODg5ZjE1YWMyNzcwZGEwNTEifQ.aEoAYkSJjoWH1boshQAaTkf8G3yn0kapko6HFRt7Rh4"+}+</pre>++<p>Additionally, you can send an access token to <code>/token/verify/</code> endpoint to verify that token.</p>++<p>When this short-lived access token expires, you can use the longer-lived <code>refresh</code>+token to obtain another access token.+<pre>+curl \+ -X POST \+ -H "Content-Type: application/json" \+ -d '{"refresh":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX3BrIjoxLCJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImNvbGRfc3R1ZmYiOiLimIMiLCJleHAiOjIzNDU2NywianRpIjoiZGUxMmY0ZTY3MDY4NDI3ODg5ZjE1YWMyNzcwZGEwNTEifQ.aEoAYkSJjoWH1boshQAaTkf8G3yn0kapko6HFRt7Rh4"}' \+ https://wger.de/api/v2/token/refresh/++...+{"access":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX3BrIjoxLCJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiY29sZF9zdHVmZiI6IuKYgyIsImV4cCI6MTIzNTY3LCJqdGkiOiJjNzE4ZTVkNjgzZWQ0NTQyYTU0NWJkM2VmMGI0ZGQ0ZSJ9.ekxRxgb9OKmHkfy-zs1Ro_xs1eMLXiR17dIDBVxeT-w"}+</pre>+</p>+</p><p>You should always use HTTPS if possible when communicating with the server.</p>+<p>At the moment it is not possible to register via the API.</p>+<p><strong>Deprecated: </strong>You can also generate a token via the <code>login</code> endpoint. Send a+username and password, and you will get the user's token or a new one will be+generated.</p><div class="container">