summaryrefslogtreecommitdiff
path: root/ishtar_common/rest.py
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common/rest.py')
-rw-r--r--ishtar_common/rest.py42
1 files changed, 41 insertions, 1 deletions
diff --git a/ishtar_common/rest.py b/ishtar_common/rest.py
index 699440f15..3d0c90f04 100644
--- a/ishtar_common/rest.py
+++ b/ishtar_common/rest.py
@@ -1,14 +1,36 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+# Copyright (C) 2021-2025 Étienne Loks <etienne.loks at iggdrasil dot net>
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# See the file COPYING for details.
+
+import datetime
+
from django.conf import settings
from django.db.models import Q
from django.http import HttpResponse
from django.utils.translation import activate, deactivate
-from rest_framework import authentication, permissions, generics
+from rest_framework import authentication, exceptions, permissions, generics
from rest_framework.response import Response
from rest_framework.views import APIView
from ishtar_common import models_rest
from ishtar_common.models_common import GeneralType
+from ishtar_common.utils import gettext_lazy as _
from ishtar_common.views_item import get_item
@@ -253,3 +275,21 @@ class GetAPIView(generics.RetrieveAPIView):
obj = q.all()[0]
result = obj.full_serialize(search_model, request=request)
return Response(result, content_type="json")
+
+
+class GISAuthentication(authentication.TokenAuthentication):
+ model = models_rest.UserToken
+
+ def authenticate_credentials(self, key):
+ """
+ Manage date limit
+ """
+ __, token = super().authenticate_credentials(key)
+ if token.limit_date and token.limit_date < datetime.date.today():
+ raise exceptions.AuthenticationFailed(_("Access expired."))
+ return (token.user, token)
+
+
+class GISAPIView(APIView):
+ authentication_classes = (GISAuthentication,)
+ permission_classes = (permissions.IsAuthenticated,)