Skip to content

Decorator

Auth decorators exposing most of the auth logic.

access_token_required(func)

Access token decorator.

Decorator that require a jwt access token before executing the wrapped function

If the user successfully has been successfully logged in, the user model instance is added to the context dictionary with the key user

Source code in turbulette/apps/auth/decorators.py
def access_token_required(func: Callable[..., Any]):
    """Access token decorator.

    Decorator that require a jwt access token
    before executing the wrapped function

    If the user successfully has been successfully
    logged in, the user model instance is added to
    the context dictionary with the key ``user``
    """

    @_jwt_required(TokenType.ACCESS)
    async def wrapper(obj, info, **kwargs):
        return await func(obj, info, **kwargs)

    return wrapper

fresh_token_required(func)

Fresh token decorator.

Decorator that require a fresh jwt access token before executing the wrapped function

If the user successfully has been successfully logged in, the user model instance is added to the context dictionary with the key user

The "freshness" is determined by the JWT_FRESH_DELTA timedelta setting

Source code in turbulette/apps/auth/decorators.py
def fresh_token_required(func: Callable[..., Any]):
    """Fresh token decorator.

    Decorator that require a fresh jwt access token
    before executing the wrapped function

    If the user successfully has been successfully
    logged in, the user model instance is added to
    the context dictionary with the key ``user``

    The "freshness" is determined by the `JWT_FRESH_DELTA` timedelta setting
    """

    @_jwt_required(TokenType.ACCESS)
    async def wrapper(obj, info, **kwargs):
        if (
            datetime.utcnow() - datetime.utcfromtimestamp(info.context["claims"]["iat"])
        ) > settings.JWT_FRESH_DELTA:
            raise JWTNotFresh()
        return await func(obj, info, **kwargs)

    return wrapper

refresh_token_required(func)

Refresh token decorator.

Decorator that require a jwt refresh token before executing the wrapped function

If the user successfully has been successfully logged in, the user model instance is added to the context dictionary with the key user

Source code in turbulette/apps/auth/decorators.py
def refresh_token_required(func: Callable[..., Any]):
    """Refresh token decorator.

    Decorator that require a jwt refresh token
    before executing the wrapped function

    If the user successfully has been successfully
    logged in, the user model instance is added to
    the context dictionary with the key ``user``
    """

    @_jwt_required(TokenType.REFRESH)
    async def wrapper(obj, info, **kwargs):
        return await func(obj, info, **kwargs)

    return wrapper

scope_required(func)

Scope decorator.

Log a user and check if it has the required permissions before executing the wrapped function

If the user successfully has been successfully logged in, the user model instance is added to the context dictionary with the key user

Source code in turbulette/apps/auth/decorators.py
def scope_required(func: Callable[..., Any]):
    """Scope decorator.

    Log a user and check if it has the required permissions
    before executing the wrapped function

    If the user successfully has been successfully logged in,
    the user model instance is added to the context dictionary
    with the key ``user``
    """

    @access_token_required
    async def wrapper(obj, info, **kwargs):
        if await authorized(info.context["claims"], info):
            return await func(obj, info, **kwargs)
        if is_query(info):
            add_error(ErrorCode.QUERY_NOT_ALLOWED)
            return None
        add_error(ErrorCode.FIELD_NOT_ALLOWED, info.field_name)
        return None

    return wrapper

Last update: 2021-02-18