prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Proxy Provider not working 
  Hello
  
  Please help me, I updated the authentik server to 23.1.2, it worked perfectly until now, now the Proxy Provider is not working because of the following error
  
  this is in the server log
  
  {"error":"Post \"https://auth.xxx.com/application/o/token/\": dial tcp 192.168.10.240:443: connect: connection refused","event":"failed to redeem code","level":"warning","logger":"authentik.outpost.proxyv2.application","name":"Kuma","timestamp":"2023-01-24T13:01:34Z"}
  
  The IP in the log is the IP of the nginx reverse proxy manager. The proxy works properly, I don't see any errors. Anyone have any ideas?
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `authentik/core/tasks.py`
  Content:
  ```
  1 """authentik core tasks"""
  2 from datetime import datetime, timedelta
  3 
  4 from django.contrib.sessions.backends.cache import KEY_PREFIX
  5 from django.core.cache import cache
  6 from django.utils.timezone import now
  7 from structlog.stdlib import get_logger
  8 
  9 from authentik.core.models import (
  10     USER_ATTRIBUTE_EXPIRES,
  11     USER_ATTRIBUTE_GENERATED,
  12     AuthenticatedSession,
  13     ExpiringModel,
  14     User,
  15 )
  16 from authentik.events.monitored_tasks import (
  17     MonitoredTask,
  18     TaskResult,
  19     TaskResultStatus,
  20     prefill_task,
  21 )
  22 from authentik.root.celery import CELERY_APP
  23 
  24 LOGGER = get_logger()
  25 
  26 
  27 @CELERY_APP.task(bind=True, base=MonitoredTask)
  28 @prefill_task
  29 def clean_expired_models(self: MonitoredTask):
  30     """Remove expired objects"""
  31     messages = []
  32     for cls in ExpiringModel.__subclasses__():
  33         cls: ExpiringModel
  34         objects = (
  35             cls.objects.all().exclude(expiring=False).exclude(expiring=True, expires__gt=now())
  36         )
  37         amount = objects.count()
  38         for obj in objects:
  39             obj.expire_action()
  40         LOGGER.debug("Expired models", model=cls, amount=amount)
  41         messages.append(f"Expired {amount} {cls._meta.verbose_name_plural}")
  42     # Special case
  43     amount = 0
  44     for session in AuthenticatedSession.objects.all():
  45         cache_key = f"{KEY_PREFIX}{session.session_key}"
  46         try:
  47             value = cache.get(cache_key)
  48         # pylint: disable=broad-except
  49         except Exception as exc:
  50             LOGGER.debug("Failed to get session from cache", exc=exc)
  51         if not value:
  52             session.delete()
  53             amount += 1
  54     LOGGER.debug("Expired sessions", model=AuthenticatedSession, amount=amount)
  55     messages.append(f"Expired {amount} {AuthenticatedSession._meta.verbose_name_plural}")
  56     self.set_status(TaskResult(TaskResultStatus.SUCCESSFUL, messages))
  57 
  58 
  59 @CELERY_APP.task(bind=True, base=MonitoredTask)
  60 @prefill_task
  61 def clean_temporary_users(self: MonitoredTask):
  62     """Remove temporary users created by SAML Sources"""
  63     _now = datetime.now()
  64     messages = []
  65     deleted_users = 0
  66     for user in User.objects.filter(**{f"attributes__{USER_ATTRIBUTE_GENERATED}": True}):
  67         if not user.attributes.get(USER_ATTRIBUTE_EXPIRES):
  68             continue
  69         delta: timedelta = _now - datetime.fromtimestamp(
  70             user.attributes.get(USER_ATTRIBUTE_EXPIRES)
  71         )
  72         if delta.total_seconds() > 0:
  73             LOGGER.debug("User is expired and will be deleted.", user=user, delta=delta)
  74             user.delete()
  75             deleted_users += 1
  76     messages.append(f"Successfully deleted {deleted_users} users.")
  77     self.set_status(TaskResult(TaskResultStatus.SUCCESSFUL, messages))
  78
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/authentik/core/tasks.py b/authentik/core/tasks.py
  --- a/authentik/core/tasks.py
  +++ b/authentik/core/tasks.py
  @@ -43,6 +43,7 @@
       amount = 0
       for session in AuthenticatedSession.objects.all():
           cache_key = f"{KEY_PREFIX}{session.session_key}"
  +        value = None
           try:
               value = cache.get(cache_key)
           # pylint: disable=broad-except
  
verification_info: {"golden_diff": "diff --git a/authentik/core/tasks.py b/authentik/core/tasks.py\n--- a/authentik/core/tasks.py\n+++ b/authentik/core/tasks.py\n@@ -43,6 +43,7 @@\n     amount = 0\n     for session in AuthenticatedSession.objects.all():\n         cache_key = f\"{KEY_PREFIX}{session.session_key}\"\n+        value = None\n         try:\n             value = cache.get(cache_key)\n         # pylint: disable=broad-except\n", "issue": "Proxy Provider not working \nHello\r\n\r\nPlease help me, I updated the authentik server to 23.1.2, it worked perfectly until now, now the Proxy Provider is not working because of the following error\r\n\r\nthis is in the server log\r\n\r\n{\"error\":\"Post \\\"https://auth.xxx.com/application/o/token/\\\": dial tcp 192.168.10.240:443: connect: connection refused\",\"event\":\"failed to redeem code\",\"level\":\"warning\",\"logger\":\"authentik.outpost.proxyv2.application\",\"name\":\"Kuma\",\"timestamp\":\"2023-01-24T13:01:34Z\"}\r\n\r\nThe IP in the log is the IP of the nginx reverse proxy manager. The proxy works properly, I don't see any errors. Anyone have any ideas?\r\n\n", "before_files": [{"content": "\"\"\"authentik core tasks\"\"\"\nfrom datetime import datetime, timedelta\n\nfrom django.contrib.sessions.backends.cache import KEY_PREFIX\nfrom django.core.cache import cache\nfrom django.utils.timezone import now\nfrom structlog.stdlib import get_logger\n\nfrom authentik.core.models import (\n    USER_ATTRIBUTE_EXPIRES,\n    USER_ATTRIBUTE_GENERATED,\n    AuthenticatedSession,\n    ExpiringModel,\n    User,\n)\nfrom authentik.events.monitored_tasks import (\n    MonitoredTask,\n    TaskResult,\n    TaskResultStatus,\n    prefill_task,\n)\nfrom authentik.root.celery import CELERY_APP\n\nLOGGER = get_logger()\n\n\n@CELERY_APP.task(bind=True, base=MonitoredTask)\n@prefill_task\ndef clean_expired_models(self: MonitoredTask):\n    \"\"\"Remove expired objects\"\"\"\n    messages = []\n    for cls in ExpiringModel.__subclasses__():\n        cls: ExpiringModel\n        objects = (\n            cls.objects.all().exclude(expiring=False).exclude(expiring=True, expires__gt=now())\n        )\n        amount = objects.count()\n        for obj in objects:\n            obj.expire_action()\n        LOGGER.debug(\"Expired models\", model=cls, amount=amount)\n        messages.append(f\"Expired {amount} {cls._meta.verbose_name_plural}\")\n    # Special case\n    amount = 0\n    for session in AuthenticatedSession.objects.all():\n        cache_key = f\"{KEY_PREFIX}{session.session_key}\"\n        try:\n            value = cache.get(cache_key)\n        # pylint: disable=broad-except\n        except Exception as exc:\n            LOGGER.debug(\"Failed to get session from cache\", exc=exc)\n        if not value:\n            session.delete()\n            amount += 1\n    LOGGER.debug(\"Expired sessions\", model=AuthenticatedSession, amount=amount)\n    messages.append(f\"Expired {amount} {AuthenticatedSession._meta.verbose_name_plural}\")\n    self.set_status(TaskResult(TaskResultStatus.SUCCESSFUL, messages))\n\n\n@CELERY_APP.task(bind=True, base=MonitoredTask)\n@prefill_task\ndef clean_temporary_users(self: MonitoredTask):\n    \"\"\"Remove temporary users created by SAML Sources\"\"\"\n    _now = datetime.now()\n    messages = []\n    deleted_users = 0\n    for user in User.objects.filter(**{f\"attributes__{USER_ATTRIBUTE_GENERATED}\": True}):\n        if not user.attributes.get(USER_ATTRIBUTE_EXPIRES):\n            continue\n        delta: timedelta = _now - datetime.fromtimestamp(\n            user.attributes.get(USER_ATTRIBUTE_EXPIRES)\n        )\n        if delta.total_seconds() > 0:\n            LOGGER.debug(\"User is expired and will be deleted.\", user=user, delta=delta)\n            user.delete()\n            deleted_users += 1\n    messages.append(f\"Successfully deleted {deleted_users} users.\")\n    self.set_status(TaskResult(TaskResultStatus.SUCCESSFUL, messages))\n", "path": "authentik/core/tasks.py"}], "after_files": [{"content": "\"\"\"authentik core tasks\"\"\"\nfrom datetime import datetime, timedelta\n\nfrom django.contrib.sessions.backends.cache import KEY_PREFIX\nfrom django.core.cache import cache\nfrom django.utils.timezone import now\nfrom structlog.stdlib import get_logger\n\nfrom authentik.core.models import (\n    USER_ATTRIBUTE_EXPIRES,\n    USER_ATTRIBUTE_GENERATED,\n    AuthenticatedSession,\n    ExpiringModel,\n    User,\n)\nfrom authentik.events.monitored_tasks import (\n    MonitoredTask,\n    TaskResult,\n    TaskResultStatus,\n    prefill_task,\n)\nfrom authentik.root.celery import CELERY_APP\n\nLOGGER = get_logger()\n\n\n@CELERY_APP.task(bind=True, base=MonitoredTask)\n@prefill_task\ndef clean_expired_models(self: MonitoredTask):\n    \"\"\"Remove expired objects\"\"\"\n    messages = []\n    for cls in ExpiringModel.__subclasses__():\n        cls: ExpiringModel\n        objects = (\n            cls.objects.all().exclude(expiring=False).exclude(expiring=True, expires__gt=now())\n        )\n        amount = objects.count()\n        for obj in objects:\n            obj.expire_action()\n        LOGGER.debug(\"Expired models\", model=cls, amount=amount)\n        messages.append(f\"Expired {amount} {cls._meta.verbose_name_plural}\")\n    # Special case\n    amount = 0\n    for session in AuthenticatedSession.objects.all():\n        cache_key = f\"{KEY_PREFIX}{session.session_key}\"\n        value = None\n        try:\n            value = cache.get(cache_key)\n        # pylint: disable=broad-except\n        except Exception as exc:\n            LOGGER.debug(\"Failed to get session from cache\", exc=exc)\n        if not value:\n            session.delete()\n            amount += 1\n    LOGGER.debug(\"Expired sessions\", model=AuthenticatedSession, amount=amount)\n    messages.append(f\"Expired {amount} {AuthenticatedSession._meta.verbose_name_plural}\")\n    self.set_status(TaskResult(TaskResultStatus.SUCCESSFUL, messages))\n\n\n@CELERY_APP.task(bind=True, base=MonitoredTask)\n@prefill_task\ndef clean_temporary_users(self: MonitoredTask):\n    \"\"\"Remove temporary users created by SAML Sources\"\"\"\n    _now = datetime.now()\n    messages = []\n    deleted_users = 0\n    for user in User.objects.filter(**{f\"attributes__{USER_ATTRIBUTE_GENERATED}\": True}):\n        if not user.attributes.get(USER_ATTRIBUTE_EXPIRES):\n            continue\n        delta: timedelta = _now - datetime.fromtimestamp(\n            user.attributes.get(USER_ATTRIBUTE_EXPIRES)\n        )\n        if delta.total_seconds() > 0:\n            LOGGER.debug(\"User is expired and will be deleted.\", user=user, delta=delta)\n            user.delete()\n            deleted_users += 1\n    messages.append(f\"Successfully deleted {deleted_users} users.\")\n    self.set_status(TaskResult(TaskResultStatus.SUCCESSFUL, messages))\n", "path": "authentik/core/tasks.py"}]}