authentik.admin.files.backends.static

 1from collections.abc import Generator
 2from pathlib import Path
 3
 4from django.http.request import HttpRequest
 5
 6from authentik.admin.files.backends.base import Backend
 7from authentik.admin.files.usage import FileUsage
 8from authentik.lib.config import CONFIG
 9
10STATIC_ASSETS_BASE_DIR = Path("web/dist")
11STATIC_ASSETS_DIRS = [Path(p) for p in ("assets/icons", "assets/images")]
12STATIC_ASSETS_SOURCES_DIR = Path("web/authentik/sources")
13STATIC_FILE_EXTENSIONS = [".svg", ".png", ".jpg", ".jpeg"]
14STATIC_PATH_PREFIX = "/static"
15
16
17class StaticBackend(Backend):
18    """Read-only backend for static files from web/dist/assets.
19
20    - Used for serving built-in static assets like icons and images.
21    - Files cannot be uploaded or deleted through this backend.
22    - Only accessible through resolve_file_url when a static path is detected.
23    """
24
25    allowed_usages = [FileUsage.MEDIA]
26
27    def supports_file(self, name: str) -> bool:
28        """Check if file path is a static path."""
29        return name.startswith(STATIC_PATH_PREFIX)
30
31    def list_files(self) -> Generator[str]:
32        """List all static files."""
33        # List built-in source icons
34        if STATIC_ASSETS_SOURCES_DIR.exists():
35            for file_path in STATIC_ASSETS_SOURCES_DIR.iterdir():
36                if file_path.is_file() and (file_path.suffix in STATIC_FILE_EXTENSIONS):
37                    yield f"{STATIC_PATH_PREFIX}/authentik/sources/{file_path.name}"
38
39        # List other static assets
40        for dir in STATIC_ASSETS_DIRS:
41            dist_dir = STATIC_ASSETS_BASE_DIR / dir
42            if dist_dir.exists():
43                for file_path in dist_dir.rglob("*"):
44                    if file_path.is_file() and (file_path.suffix in STATIC_FILE_EXTENSIONS):
45                        yield f"{STATIC_PATH_PREFIX}/dist/{dir}/{file_path.name}"
46
47    def file_url(
48        self,
49        name: str,
50        request: HttpRequest | None = None,
51        use_cache: bool = True,
52    ) -> str:
53        """Get URL for static file."""
54        prefix = CONFIG.get("web.path", "/")[:-1]
55        url = f"{prefix}{name}"
56        if request is None:
57            return url
58        return request.build_absolute_uri(url)
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'
class StaticBackend(authentik.admin.files.backends.base.Backend):
18class StaticBackend(Backend):
19    """Read-only backend for static files from web/dist/assets.
20
21    - Used for serving built-in static assets like icons and images.
22    - Files cannot be uploaded or deleted through this backend.
23    - Only accessible through resolve_file_url when a static path is detected.
24    """
25
26    allowed_usages = [FileUsage.MEDIA]
27
28    def supports_file(self, name: str) -> bool:
29        """Check if file path is a static path."""
30        return name.startswith(STATIC_PATH_PREFIX)
31
32    def list_files(self) -> Generator[str]:
33        """List all static files."""
34        # List built-in source icons
35        if STATIC_ASSETS_SOURCES_DIR.exists():
36            for file_path in STATIC_ASSETS_SOURCES_DIR.iterdir():
37                if file_path.is_file() and (file_path.suffix in STATIC_FILE_EXTENSIONS):
38                    yield f"{STATIC_PATH_PREFIX}/authentik/sources/{file_path.name}"
39
40        # List other static assets
41        for dir in STATIC_ASSETS_DIRS:
42            dist_dir = STATIC_ASSETS_BASE_DIR / dir
43            if dist_dir.exists():
44                for file_path in dist_dir.rglob("*"):
45                    if file_path.is_file() and (file_path.suffix in STATIC_FILE_EXTENSIONS):
46                        yield f"{STATIC_PATH_PREFIX}/dist/{dir}/{file_path.name}"
47
48    def file_url(
49        self,
50        name: str,
51        request: HttpRequest | None = None,
52        use_cache: bool = True,
53    ) -> str:
54        """Get URL for static file."""
55        prefix = CONFIG.get("web.path", "/")[:-1]
56        url = f"{prefix}{name}"
57        if request is None:
58            return url
59        return request.build_absolute_uri(url)

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.
allowed_usages = [<FileUsage.MEDIA: 'media'>]
def supports_file(self, name: str) -> bool:
28    def supports_file(self, name: str) -> bool:
29        """Check if file path is a static path."""
30        return name.startswith(STATIC_PATH_PREFIX)

Check if file path is a static path.

def list_files(self) -> Generator[str]:
32    def list_files(self) -> Generator[str]:
33        """List all static files."""
34        # List built-in source icons
35        if STATIC_ASSETS_SOURCES_DIR.exists():
36            for file_path in STATIC_ASSETS_SOURCES_DIR.iterdir():
37                if file_path.is_file() and (file_path.suffix in STATIC_FILE_EXTENSIONS):
38                    yield f"{STATIC_PATH_PREFIX}/authentik/sources/{file_path.name}"
39
40        # List other static assets
41        for dir in STATIC_ASSETS_DIRS:
42            dist_dir = STATIC_ASSETS_BASE_DIR / dir
43            if dist_dir.exists():
44                for file_path in dist_dir.rglob("*"):
45                    if file_path.is_file() and (file_path.suffix in STATIC_FILE_EXTENSIONS):
46                        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:
48    def file_url(
49        self,
50        name: str,
51        request: HttpRequest | None = None,
52        use_cache: bool = True,
53    ) -> str:
54        """Get URL for static file."""
55        prefix = CONFIG.get("web.path", "/")[:-1]
56        url = f"{prefix}{name}"
57        if request is None:
58            return url
59        return request.build_absolute_uri(url)

Get URL for static file.