From 201481bde3ba48956dc56bbac02dd215a30ac8ef Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Fri, 15 Mar 2024 02:35:10 +0100 Subject: [PATCH] generate api client Signed-off-by: Jens Langhammer --- authentik/lib/expression/evaluator.py | 64 +++++++++++++++++++++++++++ poetry.lock | 19 +++++++- pyproject.toml | 1 + 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/authentik/lib/expression/evaluator.py b/authentik/lib/expression/evaluator.py index 8b36d49905..02b89faf9f 100644 --- a/authentik/lib/expression/evaluator.py +++ b/authentik/lib/expression/evaluator.py @@ -4,10 +4,35 @@ import re import socket from collections.abc import Iterable from ipaddress import ip_address, ip_network +from pathlib import Path +from tempfile import gettempdir from textwrap import indent from typing import Any +from authentik_client.api.admin_api import AdminApi +from authentik_client.api.authenticators_api import AuthenticatorsApi +from authentik_client.api.core_api import CoreApi +from authentik_client.api.crypto_api import CryptoApi +from authentik_client.api.enterprise_api import EnterpriseApi +from authentik_client.api.events_api import EventsApi +from authentik_client.api.flows_api import FlowsApi +from authentik_client.api.managed_api import ManagedApi +from authentik_client.api.oauth2_api import Oauth2Api +from authentik_client.api.outposts_api import OutpostsApi +from authentik_client.api.policies_api import PoliciesApi +from authentik_client.api.propertymappings_api import PropertymappingsApi +from authentik_client.api.providers_api import ProvidersApi +from authentik_client.api.rac_api import RacApi +from authentik_client.api.rbac_api import RbacApi +from authentik_client.api.root_api import RootApi +from authentik_client.api.schema_api import SchemaApi +from authentik_client.api.sources_api import SourcesApi +from authentik_client.api.stages_api import StagesApi +from authentik_client.api.tenants_api import TenantsApi +from authentik_client.api_client import ApiClient +from authentik_client.configuration import Configuration from cachetools import TLRUCache, cached +from django.conf import settings from django.core.exceptions import FieldError from guardian.shortcuts import get_anonymous_user from rest_framework.serializers import ValidationError @@ -32,6 +57,30 @@ from authentik.policies.types import PolicyRequest, PolicyResult from authentik.stages.authenticator import devices_for_user LOGGER = get_logger() +_tmp = Path(gettempdir()) + +API_CLIENTS = { + "AdminApi": AdminApi, + "AuthenticatorsApi": AuthenticatorsApi, + "CoreApi": CoreApi, + "CryptoApi": CryptoApi, + "EnterpriseApi": EnterpriseApi, + "EventsApi": EventsApi, + "FlowsApi": FlowsApi, + "ManagedApi": ManagedApi, + "Oauth2Api": Oauth2Api, + "OutpostsApi": OutpostsApi, + "PoliciesApi": PoliciesApi, + "PropertymappingsApi": PropertymappingsApi, + "ProvidersApi": ProvidersApi, + "RacApi": RacApi, + "RbacApi": RbacApi, + "RootApi": RootApi, + "SchemaApi": SchemaApi, + "SourcesApi": SourcesApi, + "StagesApi": StagesApi, + "TenantsApi": TenantsApi, +} class BaseEvaluator: @@ -69,13 +118,28 @@ class BaseEvaluator: "USER_ATTRIBUTE_CHANGE_EMAIL": USER_ATTRIBUTE_CHANGE_EMAIL, "USER_ATTRIBUTE_CHANGE_NAME": USER_ATTRIBUTE_CHANGE_NAME, "USER_ATTRIBUTE_CHANGE_USERNAME": USER_ATTRIBUTE_CHANGE_USERNAME, + "api": self.get_api_client(), } for app in get_apps(): # Load models from each app for model in app.get_models(): self._globals[model.__name__] = model + self._globals.update(API_CLIENTS) self._context = {} + def get_api_client(self): + token = "" + config = Configuration( + f"unix://{str(_tmp.joinpath('authentik-core.sock'))}", + api_key={ + "authentik": token, + }, + api_key_prefix={"authentik": "Bearer "}, + ) + if settings.DEBUG: + config.host = "http://localhost:8000" + return ApiClient(config) + @cached(cache=TLRUCache(maxsize=32, ttu=lambda key, value, now: now + 180)) @staticmethod def expr_resolve_dns(host: str, ip_version: int | None = None) -> list[str]: diff --git a/poetry.lock b/poetry.lock index 7d38633697..e1393448a5 100644 --- a/poetry.lock +++ b/poetry.lock @@ -269,6 +269,23 @@ tests = ["attrs[tests-no-zope]", "zope-interface"] tests-mypy = ["mypy (>=1.6)", "pytest-mypy-plugins"] tests-no-zope = ["attrs[tests-mypy]", "cloudpickle", "hypothesis", "pympler", "pytest (>=4.3.0)", "pytest-xdist[psutil]"] +[[package]] +name = "authentik-client" +version = "2024.4.1.post1714149882" +description = "authentik" +optional = false +python-versions = "<4.0,>=3.7" +files = [ + {file = "authentik_client-2024.4.1.post1714149882-py3-none-any.whl", hash = "sha256:867b66b6a0fd59fac5a96d2098889fca1fa17409d042c00923d06ba21db27622"}, + {file = "authentik_client-2024.4.1.post1714149882.tar.gz", hash = "sha256:2917c6f531dcbe44392d6f758fe10065399972de0c324d5431ea183e4b11ed7b"}, +] + +[package.dependencies] +pydantic = ">=2" +python-dateutil = ">=2.8.2" +typing-extensions = ">=4.7.1" +urllib3 = ">=1.25.3" + [[package]] name = "autobahn" version = "23.6.2" @@ -4698,4 +4715,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "~3.12" -content-hash = "5e010ef54035f3491a5273a76d8711f70db6f366625d9a48f9b0dd6dbd34990d" +content-hash = "647a426ecb06caf2b8f3165c4e85ed0439568e692b6ade8c7933683985c90b18" diff --git a/pyproject.toml b/pyproject.toml index 2c640eb8f8..34fcd280e4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -83,6 +83,7 @@ filterwarnings = [ ] [tool.poetry.dependencies] +authentik_client = "2024.4.1.post1714149882" argon2-cffi = "*" celery = "*" channels = { version = "*", extras = ["daphne"] }