# Path: app/api/models.py
# class ApiKey(TimestampMixin):
#     """Model representing clients' api keys."""
# 
#     token = HashidField(
#         min_length=32,
#         alphabet='0123456789abcdef',
#         default=timestamp_seconds,
#         unique=True,
#         editable=False
#     )
#     revoked = models.BooleanField(default=False)
#     owner = models.ForeignKey(User, on_delete=models.CASCADE)
# 
#     def clean(self):
#         if not self.owner.is_superuser:
#             raise ValidationError(_('Ensure the owner is a superuser.'))
# 
#         super().clean()
# 
#     def save(self, *args, **kwargs):
#         self.full_clean()
#         return super().save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.token.hashid


from django.http import JsonResponse
from .models import ApiKey



def authorization_required(func=None):
    def decorator(view_func):
        def wrapper(request, *args, **kwargs):
            api_key = 'HTTP_X_TAVERNATOKEN'
            if api_key not in request.META:
                data = {'message': 'Set your api_key in X-TavernaToken header.'}
                return JsonResponse(data, status=401)

            try:
                ApiKey.objects.get(token=request.META[api_key], revoked=False)

# Path: app/api/models.py
# class ApiKey(TimestampMixin):
#     """Model representing clients' api keys."""
# 
#     token = HashidField(
#         min_length=32,
#         alphabet='0123456789abcdef',
#         default=timestamp_seconds,
#         unique=True,
#         editable=False
#     )
#     revoked = models.BooleanField(default=False)
#     owner = models.ForeignKey(User, on_delete=models.CASCADE)
# 
#     def clean(self):
#         if not self.owner.is_superuser:
#             raise ValidationError(_('Ensure the owner is a superuser.'))
# 
#         super().clean()
# 
#     def save(self, *args, **kwargs):
#         self.full_clean()
#         return super().save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.token.hashid


# Path: app/api/auth.py
from django.http import JsonResponse
from .models import ApiKey




def authorization_required(func=None):
    def decorator(view_func):
        def wrapper(request, *args, **kwargs):
            api_key = 'HTTP_X_TAVERNATOKEN'
            if api_key not in request.META:
                data = {'message': 'Set your api_key in X-TavernaToken header.'}
                return JsonResponse(data, status=401)

            try:
                ApiKey.objects.get(token=request.META[api_key], revoked=False)

            except (ApiKey.DoesNotExist, TypeError):