Skip to content

Commit

Permalink
fix other issues
Browse files Browse the repository at this point in the history
Signed-off-by: Marc 'risson' Schmitt <[email protected]>
  • Loading branch information
rissson committed Oct 15, 2024
1 parent 863958b commit 929e42d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
21 changes: 13 additions & 8 deletions authentik/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.models import UserManager as DjangoUserManager
from django.db import models
from django.db.models import QuerySet, options
from django.db.models import Q, QuerySet, options
from django.db.models.constants import LOOKUP_SEP
from django.http import HttpRequest
from django.utils.functional import SimpleLazyObject, cached_property
Expand Down Expand Up @@ -802,19 +802,24 @@ def expire_action(self, *args, **kwargs):
return self.delete(*args, **kwargs)

@classmethod
def filter_not_expired(cls, **kwargs) -> QuerySet["Token"]:
def _not_expired_filter(cls):
return Q(expires__gt=now(), expiring=True) | Q(expiring=False)

@classmethod
def filter_not_expired(cls, delete_expired=False, **kwargs) -> QuerySet["ExpiringModel"]:
"""Filer for tokens which are not expired yet or are not expiring,
and match filters in `kwargs`"""
return cls.objects.filter(expires__gt=now(), expiring=True).filter(**kwargs)
if delete_expired:
cls.delete_expired(**kwargs)
return cls.objects.filter(cls._not_expired_filter()).filter(**kwargs)

@classmethod
def delete_expired(cls) -> int:
objects = (
cls.objects.all().exclude(expiring=False).exclude(expiring=True, expires__gt=now())
)
amount = objects.count()
def delete_expired(cls, **kwargs) -> int:
objects = cls.objects.all().exclude(cls._not_expired_filter()).filter(**kwargs)
amount = 0
for obj in objects:
obj.expire_action()
amount += 1

Check warning on line 822 in authentik/core/models.py

View check run for this annotation

Codecov / codecov/patch

authentik/core/models.py#L821-L822

Added lines #L821 - L822 were not covered by tests
return amount

@property
Expand Down
30 changes: 13 additions & 17 deletions authentik/outposts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from dacite.core import from_dict
from django.contrib.auth.models import Permission
from django.core.cache import cache
from django.db import IntegrityError, models, transaction
from django.db import models, transaction
from django.db.models.base import Model
from django.utils.translation import gettext_lazy as _
from guardian.models import UserObjectPermission
Expand Down Expand Up @@ -380,26 +380,22 @@ def token(self) -> Token:
"""Get/create token for auto-generated user"""
managed = f"goauthentik.io/outpost/{self.token_identifier}"
tokens = Token.filter_not_expired(
delete_expired=True,
identifier=self.token_identifier,
intent=TokenIntents.INTENT_API,
managed=managed,
)
if tokens.exists():
return tokens.first()
try:
return Token.objects.create(
user=self.user,
identifier=self.token_identifier,
intent=TokenIntents.INTENT_API,
description=f"Autogenerated by authentik for Outpost {self.name}",
expiring=False,
managed=managed,
)
except IntegrityError:
# Integrity error happens mostly when managed is reused
Token.objects.filter(managed=managed).delete()
Token.objects.filter(identifier=self.token_identifier).delete()
return self.token
token: Token | None = tokens.first()
if token:
return token
return Token.objects.create(
user=self.user,
identifier=self.token_identifier,
intent=TokenIntents.INTENT_API,
description=f"Autogenerated by authentik for Outpost {self.name}",
expiring=False,
managed=managed,
)

def get_required_objects(self) -> Iterable[models.Model | str]:
"""Get an iterator of all objects the user needs read access to"""
Expand Down
3 changes: 1 addition & 2 deletions authentik/stages/consent/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
user = self.executor.plan.context[PLAN_CONTEXT_PENDING_USER]

# Remove expired consents to prevent database unique constraints errors
UserConsent.delete_expired()
consent: UserConsent | None = UserConsent.filter_not_expired(
user=user, application=application
delete_expired=True, user=user, application=application
).first()
self.executor.plan.context[PLAN_CONTEXT_CONSENT] = consent

Expand Down

0 comments on commit 929e42d

Please sign in to comment.