Pluggable authentication core for simple_module apps. Owns the stable public contracts every other module imports — UserContext, the AuthProvider protocol, the PrincipalResolver chain, and the get_current_user / CurrentUser dependencies — plus the AuthMiddleware that resolves the current principal on every request.
Heads up: for most apps you don't install this directly — an auth-provider module (simple_module_users for email+password, simple_module_keycloak for OIDC) pulls it in and registers itself on app.state.auth.auth_provider.
pip install simple_module_authUserContext— the request-scoped principal (id,name,email,roles).AuthProviderprotocol — the swappable-backend contract (resolve_user,get_login_url,get_logout_url,is_bearer_request); exactly one provider module registers an implementation onapp.state.auth.auth_provider.PrincipalResolverchain — async(Request) -> UserContext | Nonecallables apps append toapp.state.auth.principal_resolvers(e.g. PAT/bearer-token or API-key auth), consulted after the session path.AuthMiddleware— delegates to the provider + resolver chain on every request and populates the request principal.get_current_user/CurrentUserdependency.auth.oidc_common— provider-neutral OIDC helpers (JWKS/JWT validation, refresh-token sealing, sign-in rotation, renewal, cache persistence, directory) shared by theoidcandkeycloakmodules.- Anonymous-access is declared via the framework's method-aware
register_public_routeshook (with theSM_AUTH_PUBLIC_PATHShost-level escape hatch).
from fastapi import APIRouter
from auth.web.deps import CurrentUser
router = APIRouter()
@router.get("/me")
async def me(user: CurrentUser):
return {"user_id": user.id, "email": user.email}Routes that need a specific permission use RequiresPermission from hosting:
from fastapi import Depends
from simple_module_hosting.auth.permissions import RequiresPermission
from .constants import PERM_PRODUCTS_CREATE
@router.post("/", dependencies=[Depends(RequiresPermission(PERM_PRODUCTS_CREATE))])
async def create_product(): ...RequiresPermission(PERM_VIEW, PERM_EDIT) admits a user holding any supplied
key. Declare separate dependencies when every permission is required. The gate
honours direct grants, token scope caps and administrators demoted during a request.
simple_module_core,simple_module_dbitsdangerous
MIT — see LICENSE.