From 9dfaf0f60b0052533f18d88c8243cbb307d39e37 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 27 Sep 2024 14:23:25 +0200 Subject: [PATCH] token api activate --- api/views.py | 14 +++++++------- user/management/commands/add_user.py | 6 ++++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/api/views.py b/api/views.py index fbccb79..f852c05 100644 --- a/api/views.py +++ b/api/views.py @@ -29,14 +29,14 @@ def NewSnapshot(request): return JsonResponse({'error': 'Invalid request method'}, status=400) # Authentication - # auth_header = request.headers.get('Authorization') - # if not auth_header or not auth_header.startswith('Bearer '): - # return JsonResponse({'error': 'Invalid or missing token'}, status=401) + auth_header = request.headers.get('Authorization') + if not auth_header or not auth_header.startswith('Bearer '): + return JsonResponse({'error': 'Invalid or missing token'}, status=401) - # token = auth_header.split(' ')[1] - # tk = Token.objects.filter(token=token).first() - # if not tk: - # return JsonResponse({'error': 'Invalid or missing token'}, status=401) + token = auth_header.split(' ')[1] + tk = Token.objects.filter(token=token).first() + if not tk: + return JsonResponse({'error': 'Invalid or missing token'}, status=401) # Validation snapshot try: diff --git a/user/management/commands/add_user.py b/user/management/commands/add_user.py index 499adfa..cc40ac1 100644 --- a/user/management/commands/add_user.py +++ b/user/management/commands/add_user.py @@ -1,6 +1,9 @@ +from uuid import uuid4 + from django.core.management.base import BaseCommand from django.contrib.auth import get_user_model from user.models import Institution +from api.models import Token User = get_user_model() @@ -28,3 +31,6 @@ class Command(BaseCommand): ) self.u.set_password(self.password) self.u.save() + token = uuid4() + Token.objects.create(token=token, owner=self.u) + print(f"TOKEN: {token}")