@@ -2,4 +2,4 @@ from django.apps import AppConfig
|
||||
|
||||
|
||||
class TokenAuthConfig(AppConfig):
|
||||
name = 'django_etebase.token_auth'
|
||||
name = "django_etebase.token_auth"
|
||||
|
||||
@@ -12,19 +12,19 @@ MIN_REFRESH_INTERVAL = 60
|
||||
|
||||
|
||||
class TokenAuthentication(DRFTokenAuthentication):
|
||||
keyword = 'Token'
|
||||
keyword = "Token"
|
||||
model = AuthToken
|
||||
|
||||
def authenticate_credentials(self, key):
|
||||
msg = _('Invalid token.')
|
||||
msg = _("Invalid token.")
|
||||
model = self.get_model()
|
||||
try:
|
||||
token = model.objects.select_related('user').get(key=key)
|
||||
token = model.objects.select_related("user").get(key=key)
|
||||
except model.DoesNotExist:
|
||||
raise exceptions.AuthenticationFailed(msg)
|
||||
|
||||
if not token.user.is_active:
|
||||
raise exceptions.AuthenticationFailed(_('User inactive or deleted.'))
|
||||
raise exceptions.AuthenticationFailed(_("User inactive or deleted."))
|
||||
|
||||
if token.expiry is not None:
|
||||
if token.expiry < timezone.now():
|
||||
@@ -43,4 +43,4 @@ class TokenAuthentication(DRFTokenAuthentication):
|
||||
delta = (new_expiry - current_expiry).total_seconds()
|
||||
if delta > MIN_REFRESH_INTERVAL:
|
||||
auth_token.expiry = new_expiry
|
||||
auth_token.save(update_fields=('expiry',))
|
||||
auth_token.save(update_fields=("expiry",))
|
||||
|
||||
@@ -16,13 +16,23 @@ class Migration(migrations.Migration):
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='AuthToken',
|
||||
name="AuthToken",
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('key', models.CharField(db_index=True, default=token_auth_models.generate_key, max_length=40, unique=True)),
|
||||
('created', models.DateTimeField(auto_now_add=True)),
|
||||
('expiry', models.DateTimeField(blank=True, default=token_auth_models.get_default_expiry, null=True)),
|
||||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='auth_token_set', to=settings.AUTH_USER_MODEL)),
|
||||
("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
|
||||
(
|
||||
"key",
|
||||
models.CharField(db_index=True, default=token_auth_models.generate_key, max_length=40, unique=True),
|
||||
),
|
||||
("created", models.DateTimeField(auto_now_add=True)),
|
||||
("expiry", models.DateTimeField(blank=True, default=token_auth_models.get_default_expiry, null=True)),
|
||||
(
|
||||
"user",
|
||||
models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="auth_token_set",
|
||||
to=settings.AUTH_USER_MODEL,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
]
|
||||
|
||||
@@ -17,10 +17,9 @@ def get_default_expiry():
|
||||
class AuthToken(models.Model):
|
||||
|
||||
key = models.CharField(max_length=40, unique=True, db_index=True, default=generate_key)
|
||||
user = models.ForeignKey(User, null=False, blank=False,
|
||||
related_name='auth_token_set', on_delete=models.CASCADE)
|
||||
user = models.ForeignKey(User, null=False, blank=False, related_name="auth_token_set", on_delete=models.CASCADE)
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
expiry = models.DateTimeField(null=True, blank=True, default=get_default_expiry)
|
||||
|
||||
def __str__(self):
|
||||
return '{}: {}'.format(self.key, self.user)
|
||||
return "{}: {}".format(self.key, self.user)
|
||||
|
||||
Reference in New Issue
Block a user