From 9a9ba2560b2d607442905aabbcc5284bb68b5489 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sat, 9 Jul 2022 13:40:36 +0200 Subject: [PATCH] core: delete expired models when filtering instead of excluding them closes #3233 Signed-off-by: Jens Langhammer --- authentik/core/models.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/authentik/core/models.py b/authentik/core/models.py index b278a7845..74116d5ad 100644 --- a/authentik/core/models.py +++ b/authentik/core/models.py @@ -482,8 +482,9 @@ class ExpiringModel(models.Model): def filter_not_expired(cls, **kwargs) -> QuerySet: """Filer for tokens which are not expired yet or are not expiring, and match filters in `kwargs`""" - expired = Q(expires__lt=now(), expiring=True) - return cls.objects.exclude(expired).filter(**kwargs) + for obj in cls.objects.filter(**kwargs).filter(Q(expires__lt=now(), expiring=True)): + obj.delete() + return cls.objects.filter(**kwargs) @property def is_expired(self) -> bool: