Skip to content

Rule

Missil main class. Works as a FastAPI dependency, so it can be injected in API routes.

missil.Rule

Rule(area, level, bearer, use_cache=True)

Bases: Depends

FastAPI dependency to set and endpoint-level access rule.

Grant or deny user access to an endpoint.

Access is granted through the verification of the business area and verification of the access level expressed in the jwt token captured by the declared TokenBearer object.

PARAMETER DESCRIPTION
area

Business area name, like 'financial' or 'human resources'.

TYPE: str

level

Access level: READ = 0 / WRITE = 1.

TYPE: int

bearer

JWT token source source. See Bearers module.

TYPE: TokenBearer

use_cache

FastAPI Depends class parameter, by default True.

TYPE: bool DEFAULT: True

Source code in missil/rules.py
def __init__(
    self,
    area: str,
    level: int,
    bearer: TokenBearer,
    use_cache: bool = True,
):
    """
    Grant or deny user access to an endpoint.

    Access is granted through the verification of the business area and
    verification of the access level expressed in the jwt token captured by
    the declared TokenBearer object.

    Parameters
    ----------
    area : str
        Business area name, like 'financial' or 'human resources'.
    level : int
        Access level: READ = 0 / WRITE = 1.
    bearer : TokenBearer
        JWT token source source. See Bearers module.
    use_cache : bool, optional
        FastAPI Depends class parameter, by default True.
    """
    self.area = area
    self.level = level
    self.bearer = bearer
    self.use_cache = use_cache

area instance-attribute

area = area

level instance-attribute

level = level

bearer instance-attribute

bearer = bearer

use_cache instance-attribute

use_cache = use_cache

dependency property writable

dependency

Allows Missil to pass a FastAPI dependency that gets correctly evaluated.

Make Rule

:: missil.make_rule

Make Rules

missil.make_rules

make_rules(bearer, *areas)

Create a Missil ruleset, conveniently.

A ruleset is a simple mapping bearing endpoint-appliable rule bearers

rules: dict[str, Area] = make_rules(..., ...).

Given a token source (see Bearers module) and some business area names, like "it", "finances", "hr", this function will return something lile the following:

{
    'it': Area,
    'finances': Area,
    'hr': Area
    ...
}

So, one can pass like a FastAPI dependency, as shown in the following example:

@app.get("/items/{item_id}", dependencies=[rules["finances"].READ])
def read_item(item_id: int, q: Union[str, None] = None): ...

See the sample API (sample/main.py) to a folly working usage example.

PARAMETER DESCRIPTION
bearer

JWT token source source. See Bearers module.

TYPE: TokenBearer

RETURNS DESCRIPTION
dict[str, Area]

Dict containing endpoint-appliable rules.

Source code in missil/rules.py
def make_rules(bearer: TokenBearer, *areas: str) -> dict[str, Area]:
    """
    Create a Missil ruleset, conveniently.

    A ruleset is a simple mapping bearing endpoint-appliable rule
    bearers

    ```python
    rules: dict[str, Area] = make_rules(..., ...).
    ```

    Given a token source (see Bearers module) and some business
    area names, like "it", "finances", "hr", this function will return something
    lile the following:

    ```python
    {
        'it': Area,
        'finances': Area,
        'hr': Area
        ...
    }
    ```

    So, one can pass like a FastAPI dependency, as shown in the following example:

    ```python
    @app.get("/items/{item_id}", dependencies=[rules["finances"].READ])
    def read_item(item_id: int, q: Union[str, None] = None): ...
    ```

    See the sample API (sample/main.py) to a folly working usage example.

    Parameters
    ----------
    bearer : TokenBearer
        JWT token source source. See Bearers module.

    Returns
    -------
    dict[str, Area]
        Dict containing endpoint-appliable rules.
    """
    return {area: Area(area, bearer) for area in areas}