2024-04-25 22:56:22 +12:00
|
|
|
__package__ = 'archivebox.api'
|
|
|
|
|
2024-04-10 11:29:24 +12:00
|
|
|
import uuid
|
2024-04-25 22:56:22 +12:00
|
|
|
import secrets
|
2024-04-10 11:29:24 +12:00
|
|
|
from datetime import timedelta
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.db import models
|
|
|
|
from django.utils import timezone
|
|
|
|
|
2024-05-01 16:43:22 +12:00
|
|
|
from django_stubs_ext.db.models import TypedModelMeta
|
2024-04-10 11:29:24 +12:00
|
|
|
|
|
|
|
|
2024-04-25 22:56:22 +12:00
|
|
|
def generate_secret_token() -> str:
|
|
|
|
# returns cryptographically secure string with len() == 32
|
|
|
|
return secrets.token_hex(16)
|
|
|
|
|
|
|
|
|
|
|
|
class APIToken(models.Model):
|
|
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
|
|
|
|
|
|
|
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
|
|
|
token = models.CharField(max_length=32, default=generate_secret_token, unique=True)
|
|
|
|
|
2024-04-10 11:29:24 +12:00
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
2024-04-25 22:56:22 +12:00
|
|
|
expires = models.DateTimeField(null=True, blank=True)
|
|
|
|
|
2024-05-01 16:43:22 +12:00
|
|
|
class Meta(TypedModelMeta):
|
2024-04-25 22:56:22 +12:00
|
|
|
verbose_name = "API Key"
|
|
|
|
verbose_name_plural = "API Keys"
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return self.token
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f'<APIToken user={self.user.username} token=************{self.token[-4:]}>'
|
|
|
|
|
|
|
|
def __json__(self) -> dict:
|
|
|
|
return {
|
|
|
|
"TYPE": "APIToken",
|
|
|
|
"id": str(self.id),
|
|
|
|
"user_id": str(self.user.id),
|
|
|
|
"user_username": self.user.username,
|
|
|
|
"token": self.token,
|
|
|
|
"created": self.created.isoformat(),
|
|
|
|
"expires": self.expires_as_iso8601,
|
|
|
|
}
|
2024-04-10 11:29:24 +12:00
|
|
|
|
|
|
|
@property
|
2024-04-25 22:56:22 +12:00
|
|
|
def expires_as_iso8601(self):
|
2024-04-10 11:29:24 +12:00
|
|
|
"""Returns the expiry date of the token in ISO 8601 format or a date 100 years in the future if none."""
|
2024-04-25 22:56:22 +12:00
|
|
|
expiry_date = self.expires or (timezone.now() + timedelta(days=365 * 100))
|
|
|
|
|
2024-04-10 11:29:24 +12:00
|
|
|
return expiry_date.isoformat()
|
|
|
|
|
2024-04-25 22:56:22 +12:00
|
|
|
def is_valid(self, for_date=None):
|
|
|
|
for_date = for_date or timezone.now()
|
|
|
|
|
|
|
|
if self.expires and self.expires < for_date:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|