2020-06-03 12:49:38 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils import timezone
|
|
|
|
from django.utils.crypto import get_random_string
|
2022-05-08 13:10:48 +00:00
|
|
|
from etebase_server.myauth.models import get_typed_user_model
|
2020-06-03 12:49:38 +00:00
|
|
|
|
2020-12-29 11:22:36 +00:00
|
|
|
User = get_typed_user_model()
|
2020-06-03 12:49:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def generate_key():
|
|
|
|
return get_random_string(40)
|
|
|
|
|
|
|
|
|
|
|
|
def get_default_expiry():
|
2020-07-12 08:27:47 +00:00
|
|
|
return timezone.now() + timezone.timedelta(days=30)
|
2020-06-03 12:49:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AuthToken(models.Model):
|
|
|
|
|
|
|
|
key = models.CharField(max_length=40, unique=True, db_index=True, default=generate_key)
|
2020-11-14 15:04:41 +00:00
|
|
|
user = models.ForeignKey(User, null=False, blank=False, related_name="auth_token_set", on_delete=models.CASCADE)
|
2020-06-03 12:49:38 +00:00
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
expiry = models.DateTimeField(null=True, blank=True, default=get_default_expiry)
|
|
|
|
|
|
|
|
def __str__(self):
|
2020-11-14 15:04:41 +00:00
|
|
|
return "{}: {}".format(self.key, self.user)
|