Skip to content

base_auth_manager

BaseAuthManager

Bases: ABC, Generic[T, S]

Base class for the authentication manager.

Source code in spark_on_k8s/api/auth/base_auth_manager.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class BaseAuthManager(ABC, Generic[T, S]):
    """
    Base class for the authentication manager.
    """
    authenticator: Callable[[Request], Coroutine[Any, Any, Optional[T]]] = None

    @final
    async def authenticate(self, request: Request) -> Coroutine[Any, Any, Optional[T]]:
        if not self.authenticator:
            raise NotImplementedError("Authenticator not defined.")
        return self.authenticator(request)

    @final
    async def needs_authentication(self, auth_info: Annotated[T, Depends(authenticate)]) -> bool:
        user = await self.get_user(auth_info)
        # TODO: get the resource and actions from the request
        return await self.is_authorized(user, "needs_authentication", [PermissionActions.GET])

    @abstractmethod
    async def get_user(self, auth_info: T) -> S:
        """Get the user."""
        raise NotImplementedError()


    async def is_authorized(self, user: S, resource: str, actions: list[PermissionActions]) -> bool:
        """Check if the user is authorized to perform the actions on the resource."""
        raise NotImplementedError()

get_user(auth_info) abstractmethod async

Get the user.

Source code in spark_on_k8s/api/auth/base_auth_manager.py
46
47
48
49
@abstractmethod
async def get_user(self, auth_info: T) -> S:
    """Get the user."""
    raise NotImplementedError()

is_authorized(user, resource, actions) async

Check if the user is authorized to perform the actions on the resource.

Source code in spark_on_k8s/api/auth/base_auth_manager.py
52
53
54
async def is_authorized(self, user: S, resource: str, actions: list[PermissionActions]) -> bool:
    """Check if the user is authorized to perform the actions on the resource."""
    raise NotImplementedError()

PermissionActions

Bases: StrEnum

Permission actions.

Source code in spark_on_k8s/api/auth/base_auth_manager.py
16
17
18
19
20
21
22
23
class PermissionActions(StrEnum):
    """Permission actions."""
    GET = "GET"
    PUT = "PUT"
    POST = "POST"
    DELETE = "DELETE"
    PATCH = "PATCH"
    ALL = "ALL"