Skip to content

jmwalletd.deps

jmwalletd.deps

FastAPI dependency injection helpers.

Provides get_daemon_state, get_bearer_token, require_auth, and require_wallet_match which are used by route handlers to access the daemon state and enforce authentication and resource ownership.

Classes

Functions:

get_daemon_state() -> DaemonState

FastAPI dependency that returns the daemon state.

Source code in jmwalletd/src/jmwalletd/deps.py
32
33
34
35
36
37
def get_daemon_state() -> DaemonState:
    """FastAPI dependency that returns the daemon state."""
    if _daemon_state is None:
        msg = "Daemon state not initialized"
        raise RuntimeError(msg)
    return _daemon_state

get_optional_token(request: Request) -> str | None

Extract bearer token if present, or return None.

Source code in jmwalletd/src/jmwalletd/deps.py
53
54
55
def get_optional_token(request: Request) -> str | None:
    """Extract bearer token if present, or return None."""
    return _extract_bearer_token(request)

require_auth(request: Request, state: DaemonState = Depends(get_daemon_state)) -> dict[str, Any]

FastAPI dependency that enforces bearer token authentication.

Returns the decoded JWT payload on success.

Raises: InvalidToken: If the token is missing, invalid, or expired. NoWalletFound: If no wallet is currently loaded.

Source code in jmwalletd/src/jmwalletd/deps.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def require_auth(
    request: Request,
    state: DaemonState = Depends(get_daemon_state),
) -> dict[str, Any]:
    """FastAPI dependency that enforces bearer token authentication.

    Returns the decoded JWT payload on success.

    Raises:
        InvalidToken: If the token is missing, invalid, or expired.
        NoWalletFound: If no wallet is currently loaded.
    """
    if not state.wallet_loaded:
        raise NoWalletFound()

    token = _extract_bearer_token(request)
    if not token:
        raise InvalidToken("No authorization token provided.")

    try:
        payload = state.token_authority.verify_access(token)
    except jwt.InvalidTokenError as exc:
        logger.debug("Token verification failed: {}", exc)
        raise InvalidToken(str(exc)) from exc

    return payload

require_auth_allow_expired(request: Request, state: DaemonState = Depends(get_daemon_state)) -> dict[str, Any]

Like require_auth but accepts expired access tokens.

Used for the token-refresh endpoint where the access token may be expired but still needs to be structurally valid.

Source code in jmwalletd/src/jmwalletd/deps.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def require_auth_allow_expired(
    request: Request,
    state: DaemonState = Depends(get_daemon_state),
) -> dict[str, Any]:
    """Like ``require_auth`` but accepts expired access tokens.

    Used for the token-refresh endpoint where the access token may be expired
    but still needs to be structurally valid.
    """
    if not state.wallet_loaded:
        raise NoWalletFound()

    token = _extract_bearer_token(request)
    if not token:
        raise InvalidToken("No authorization token provided.")

    try:
        payload = state.token_authority.verify_access(token, verify_exp=False)
    except jwt.InvalidTokenError as exc:
        logger.debug("Token verification failed: {}", exc)
        raise InvalidToken(str(exc)) from exc

    return payload

require_current_session(state: DaemonState, token: str | None) -> None

Re-verify a request's token right before it starts wallet-bound work.

require_auth runs before the handler body, with awaits in between, so a wallet lock may have started since. Locking resets the token keys before its first await but only stops work that already exists. Call this with no await between it and starting the work: a token that still verifies here means a later lock will see, and stop, that work.

Raises: InvalidToken: If the session that authorized the request has ended.

Source code in jmwalletd/src/jmwalletd/deps.py
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
def require_current_session(state: DaemonState, token: str | None) -> None:
    """Re-verify a request's token right before it starts wallet-bound work.

    ``require_auth`` runs before the handler body, with awaits in between, so a
    wallet lock may have started since. Locking resets the token keys before its
    first await but only stops work that already exists. Call this with no await
    between it and starting the work: a token that still verifies here means a
    later lock will see, and stop, that work.

    Raises:
        InvalidToken: If the session that authorized the request has ended.
    """
    try:
        state.token_authority.verify_access(token or "")
    except jwt.InvalidTokenError as exc:
        raise InvalidToken(str(exc)) from exc

require_wallet_match(walletname: str, state: DaemonState = Depends(get_daemon_state)) -> None

FastAPI path-dependency that validates the walletname URL parameter.

Every route whose URL contains {walletname} must include this dependency to prevent IDOR: an authenticated client with a valid token must only be able to act on the wallet that is actually loaded, not any arbitrary name they pass in the path.

Raises: WalletNotFound: If no wallet is loaded, or if the requested walletname does not match the currently loaded wallet.

Source code in jmwalletd/src/jmwalletd/deps.py
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
def require_wallet_match(
    walletname: str,
    state: DaemonState = Depends(get_daemon_state),
) -> None:
    """FastAPI path-dependency that validates the ``walletname`` URL parameter.

    Every route whose URL contains ``{walletname}`` must include this
    dependency to prevent IDOR: an authenticated client with a valid token
    must only be able to act on the wallet that is actually loaded, not any
    arbitrary name they pass in the path.

    Raises:
        WalletNotFound: If no wallet is loaded, or if the requested walletname
            does not match the currently loaded wallet.
    """
    if not state.wallet_loaded or state.wallet_name != walletname:
        raise WalletNotFound()

require_wallet_session(state: DaemonState, token: str | None, walletname: str) -> tuple[WalletService, int]

Revalidate a wallet-bound request and capture its service and generation.

Call synchronously inside the handler, without an await before the guarded read or mutation. Repeat after awaits, since dependency checks can be stale.

Source code in jmwalletd/src/jmwalletd/deps.py
104
105
106
107
108
109
110
111
112
113
114
115
116
def require_wallet_session(
    state: DaemonState, token: str | None, walletname: str
) -> tuple[WalletService, int]:
    """Revalidate a wallet-bound request and capture its service and generation.

    Call synchronously inside the handler, without an await before the guarded
    read or mutation. Repeat after awaits, since dependency checks can be stale.
    """
    require_current_session(state, token)
    service = state.wallet_service
    if service is None or state.wallet_name != walletname:
        raise WalletNotFound()
    return service, state.wallet_generation

set_daemon_state(state: DaemonState) -> None

Set the global daemon state singleton (called once at startup).

Source code in jmwalletd/src/jmwalletd/deps.py
26
27
28
29
def set_daemon_state(state: DaemonState) -> None:
    """Set the global daemon state singleton (called once at startup)."""
    global _daemon_state
    _daemon_state = state