Change to our own token authentication.
parent
cc23d516a0
commit
c00c208199
@ -0,0 +1,5 @@
|
|||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class TokenAuthConfig(AppConfig):
|
||||||
|
name = 'django_etesync.token_auth'
|
@ -0,0 +1,46 @@
|
|||||||
|
from django.utils import timezone
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
from rest_framework import exceptions
|
||||||
|
from rest_framework.authentication import TokenAuthentication as DRFTokenAuthentication
|
||||||
|
|
||||||
|
from .models import AuthToken, get_default_expiry
|
||||||
|
|
||||||
|
|
||||||
|
AUTO_REFRESH = True
|
||||||
|
MIN_REFRESH_INTERVAL = 60
|
||||||
|
|
||||||
|
|
||||||
|
class TokenAuthentication(DRFTokenAuthentication):
|
||||||
|
keyword = 'Token'
|
||||||
|
model = AuthToken
|
||||||
|
|
||||||
|
def authenticate_credentials(self, key):
|
||||||
|
msg = _('Invalid token.')
|
||||||
|
model = self.get_model()
|
||||||
|
try:
|
||||||
|
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.'))
|
||||||
|
|
||||||
|
if token.expiry is not None:
|
||||||
|
if token.expiry < timezone.now():
|
||||||
|
token.delete()
|
||||||
|
raise exceptions.AuthenticationFailed(msg)
|
||||||
|
|
||||||
|
if AUTO_REFRESH:
|
||||||
|
self.renew_token(token)
|
||||||
|
|
||||||
|
return (token.user, token)
|
||||||
|
|
||||||
|
def renew_token(self, auth_token):
|
||||||
|
current_expiry = auth_token.expiry
|
||||||
|
new_expiry = get_default_expiry()
|
||||||
|
# Throttle refreshing of token to avoid db writes
|
||||||
|
delta = (new_expiry - current_expiry).total_seconds()
|
||||||
|
if delta > MIN_REFRESH_INTERVAL:
|
||||||
|
auth_token.expiry = new_expiry
|
||||||
|
auth_token.save(update_fields=('expiry',))
|
@ -0,0 +1,28 @@
|
|||||||
|
# Generated by Django 3.0.3 on 2020-06-03 12:49
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
from django_etesync.token_auth import models as token_auth_models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
initial = True
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
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)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
@ -0,0 +1,26 @@
|
|||||||
|
from django.contrib.auth import get_user_model
|
||||||
|
from django.db import models
|
||||||
|
from django.utils import timezone
|
||||||
|
from django.utils.crypto import get_random_string
|
||||||
|
|
||||||
|
User = get_user_model()
|
||||||
|
|
||||||
|
|
||||||
|
def generate_key():
|
||||||
|
return get_random_string(40)
|
||||||
|
|
||||||
|
|
||||||
|
def get_default_expiry():
|
||||||
|
return timezone.now() + timezone.timedelta(days=14)
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
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)
|
Loading…
Reference in New Issue