authentik.admin.files.backends.static
1from collections.abc import Generator 2from pathlib import Path, PurePosixPath 3 4from django.conf import settings 5from django.http.request import HttpRequest 6 7from authentik.admin.files.backends.base import THEME_VARIABLE, Backend, get_valid_themes 8from authentik.admin.files.usage import FileUsage 9from authentik.lib.config import CONFIG 10 11STATIC_ASSETS_BASE_DIR = Path("web/dist") 12STATIC_ASSETS_DIRS = [Path(p) for p in ("assets/icons", "assets/images")] 13STATIC_ASSETS_SOURCES_DIR = Path("web/authentik/sources") 14STATIC_FILE_EXTENSIONS = [".svg", ".png", ".jpg", ".jpeg"] 15STATIC_PATH_PREFIX = "/static" 16STATIC_SOURCES_PREFIX = f"{STATIC_PATH_PREFIX}/authentik/sources/" 17 18 19def static_url_name(url: str | None) -> str | None: 20 """Name of the static file behind a URL from Django's ``static()``, as this backend 21 names it. Models use ``static()`` for their default icons.""" 22 if not url or not url.startswith(settings.STATIC_URL): 23 return None 24 return f"{STATIC_PATH_PREFIX}/{url.removeprefix(settings.STATIC_URL)}" 25 26 27class StaticBackend(Backend): 28 """Read-only backend for static files from web/dist/assets. 29 30 - Used for serving built-in static assets like icons and images. 31 - Files cannot be uploaded or deleted through this backend. 32 - Only accessible through resolve_file_url when a static path is detected. 33 """ 34 35 allowed_usages = [FileUsage.MEDIA] 36 37 def supports_file(self, name: str) -> bool: 38 """Check if file path is a static path.""" 39 return name.startswith(STATIC_PATH_PREFIX) 40 41 def list_files(self) -> Generator[str]: 42 """List all static files.""" 43 # List built-in source icons, one folder per icon with a variant per theme 44 if STATIC_ASSETS_SOURCES_DIR.exists(): 45 for icon_dir in STATIC_ASSETS_SOURCES_DIR.iterdir(): 46 if not icon_dir.is_dir(): 47 continue 48 for light in icon_dir.glob("light.*"): 49 if light.suffix in STATIC_FILE_EXTENSIONS: 50 icon = f"{icon_dir.name}/{THEME_VARIABLE}{light.suffix}" 51 yield f"{STATIC_SOURCES_PREFIX}{icon}" 52 53 # List other static assets 54 for dir in STATIC_ASSETS_DIRS: 55 dist_dir = STATIC_ASSETS_BASE_DIR / dir 56 if dist_dir.exists(): 57 for file_path in dist_dir.rglob("*"): 58 if file_path.is_file() and (file_path.suffix in STATIC_FILE_EXTENSIONS): 59 yield f"{STATIC_PATH_PREFIX}/dist/{dir}/{file_path.name}" 60 61 def file_url( 62 self, 63 name: str, 64 request: HttpRequest | None = None, 65 use_cache: bool = True, 66 ) -> str: 67 """Get URL for static file.""" 68 prefix = CONFIG.get("web.path", "/")[:-1] 69 url = f"{prefix}{name}" 70 if request is None: 71 return url 72 return request.build_absolute_uri(url) 73 74 def themed_urls( 75 self, 76 name: str, 77 request: HttpRequest | None = None, 78 use_cache: bool = True, 79 ) -> dict[str, str] | None: 80 """Theme variants of ``%(theme)s`` files, and of a single variant of a built-in 81 source icon, such as ``github/light.svg``, which models use as default icons.""" 82 path = PurePosixPath(name) 83 if path.parent.parent == PurePosixPath(STATIC_SOURCES_PREFIX) and ( 84 path.stem in get_valid_themes() 85 ): 86 name = str(path.with_stem(THEME_VARIABLE)) 87 return super().themed_urls(name, request, use_cache=use_cache)
STATIC_ASSETS_BASE_DIR =
PosixPath('web/dist')
STATIC_ASSETS_DIRS =
[PosixPath('assets/icons'), PosixPath('assets/images')]
STATIC_ASSETS_SOURCES_DIR =
PosixPath('web/authentik/sources')
STATIC_FILE_EXTENSIONS =
['.svg', '.png', '.jpg', '.jpeg']
STATIC_PATH_PREFIX =
'/static'
STATIC_SOURCES_PREFIX =
'/static/authentik/sources/'
def
static_url_name(url: str | None) -> str | None:
20def static_url_name(url: str | None) -> str | None: 21 """Name of the static file behind a URL from Django's ``static()``, as this backend 22 names it. Models use ``static()`` for their default icons.""" 23 if not url or not url.startswith(settings.STATIC_URL): 24 return None 25 return f"{STATIC_PATH_PREFIX}/{url.removeprefix(settings.STATIC_URL)}"
Name of the static file behind a URL from Django's static(), as this backend
names it. Models use static() for their default icons.
28class StaticBackend(Backend): 29 """Read-only backend for static files from web/dist/assets. 30 31 - Used for serving built-in static assets like icons and images. 32 - Files cannot be uploaded or deleted through this backend. 33 - Only accessible through resolve_file_url when a static path is detected. 34 """ 35 36 allowed_usages = [FileUsage.MEDIA] 37 38 def supports_file(self, name: str) -> bool: 39 """Check if file path is a static path.""" 40 return name.startswith(STATIC_PATH_PREFIX) 41 42 def list_files(self) -> Generator[str]: 43 """List all static files.""" 44 # List built-in source icons, one folder per icon with a variant per theme 45 if STATIC_ASSETS_SOURCES_DIR.exists(): 46 for icon_dir in STATIC_ASSETS_SOURCES_DIR.iterdir(): 47 if not icon_dir.is_dir(): 48 continue 49 for light in icon_dir.glob("light.*"): 50 if light.suffix in STATIC_FILE_EXTENSIONS: 51 icon = f"{icon_dir.name}/{THEME_VARIABLE}{light.suffix}" 52 yield f"{STATIC_SOURCES_PREFIX}{icon}" 53 54 # List other static assets 55 for dir in STATIC_ASSETS_DIRS: 56 dist_dir = STATIC_ASSETS_BASE_DIR / dir 57 if dist_dir.exists(): 58 for file_path in dist_dir.rglob("*"): 59 if file_path.is_file() and (file_path.suffix in STATIC_FILE_EXTENSIONS): 60 yield f"{STATIC_PATH_PREFIX}/dist/{dir}/{file_path.name}" 61 62 def file_url( 63 self, 64 name: str, 65 request: HttpRequest | None = None, 66 use_cache: bool = True, 67 ) -> str: 68 """Get URL for static file.""" 69 prefix = CONFIG.get("web.path", "/")[:-1] 70 url = f"{prefix}{name}" 71 if request is None: 72 return url 73 return request.build_absolute_uri(url) 74 75 def themed_urls( 76 self, 77 name: str, 78 request: HttpRequest | None = None, 79 use_cache: bool = True, 80 ) -> dict[str, str] | None: 81 """Theme variants of ``%(theme)s`` files, and of a single variant of a built-in 82 source icon, such as ``github/light.svg``, which models use as default icons.""" 83 path = PurePosixPath(name) 84 if path.parent.parent == PurePosixPath(STATIC_SOURCES_PREFIX) and ( 85 path.stem in get_valid_themes() 86 ): 87 name = str(path.with_stem(THEME_VARIABLE)) 88 return super().themed_urls(name, request, use_cache=use_cache)
Read-only backend for static files from web/dist/assets.
- Used for serving built-in static assets like icons and images.
- Files cannot be uploaded or deleted through this backend.
- Only accessible through resolve_file_url when a static path is detected.
def
supports_file(self, name: str) -> bool:
38 def supports_file(self, name: str) -> bool: 39 """Check if file path is a static path.""" 40 return name.startswith(STATIC_PATH_PREFIX)
Check if file path is a static path.
def
list_files(self) -> Generator[str]:
42 def list_files(self) -> Generator[str]: 43 """List all static files.""" 44 # List built-in source icons, one folder per icon with a variant per theme 45 if STATIC_ASSETS_SOURCES_DIR.exists(): 46 for icon_dir in STATIC_ASSETS_SOURCES_DIR.iterdir(): 47 if not icon_dir.is_dir(): 48 continue 49 for light in icon_dir.glob("light.*"): 50 if light.suffix in STATIC_FILE_EXTENSIONS: 51 icon = f"{icon_dir.name}/{THEME_VARIABLE}{light.suffix}" 52 yield f"{STATIC_SOURCES_PREFIX}{icon}" 53 54 # List other static assets 55 for dir in STATIC_ASSETS_DIRS: 56 dist_dir = STATIC_ASSETS_BASE_DIR / dir 57 if dist_dir.exists(): 58 for file_path in dist_dir.rglob("*"): 59 if file_path.is_file() and (file_path.suffix in STATIC_FILE_EXTENSIONS): 60 yield f"{STATIC_PATH_PREFIX}/dist/{dir}/{file_path.name}"
List all static files.
def
file_url( self, name: str, request: django.http.request.HttpRequest | None = None, use_cache: bool = True) -> str:
62 def file_url( 63 self, 64 name: str, 65 request: HttpRequest | None = None, 66 use_cache: bool = True, 67 ) -> str: 68 """Get URL for static file.""" 69 prefix = CONFIG.get("web.path", "/")[:-1] 70 url = f"{prefix}{name}" 71 if request is None: 72 return url 73 return request.build_absolute_uri(url)
Get URL for static file.
def
themed_urls( self, name: str, request: django.http.request.HttpRequest | None = None, use_cache: bool = True) -> dict[str, str] | None:
75 def themed_urls( 76 self, 77 name: str, 78 request: HttpRequest | None = None, 79 use_cache: bool = True, 80 ) -> dict[str, str] | None: 81 """Theme variants of ``%(theme)s`` files, and of a single variant of a built-in 82 source icon, such as ``github/light.svg``, which models use as default icons.""" 83 path = PurePosixPath(name) 84 if path.parent.parent == PurePosixPath(STATIC_SOURCES_PREFIX) and ( 85 path.stem in get_valid_themes() 86 ): 87 name = str(path.with_stem(THEME_VARIABLE)) 88 return super().themed_urls(name, request, use_cache=use_cache)
Theme variants of %(theme)s files, and of a single variant of a built-in
source icon, such as github/light.svg, which models use as default icons.