Auth & Context¶
Authentication and request-scoped context for RPC methods.
Quick Overview¶
Server methods can accept an optional ctx: CallContext parameter. The framework injects it automatically — it does not appear in the Protocol definition:
from vgi_rpc import CallContext
class MyServiceImpl:
def public_method(self) -> str:
"""No ctx — accessible to all callers."""
return "ok"
def protected_method(self, ctx: CallContext) -> str:
"""Require authentication, then return caller identity."""
ctx.auth.require_authenticated()
return f"Hello, {ctx.auth.principal}"
HTTP authentication¶
Pass an authenticate callback to make_wsgi_app:
import falcon
from vgi_rpc import AuthContext, RpcServer, make_wsgi_app
def authenticate(req: falcon.Request) -> AuthContext:
token = req.get_header("Authorization") or ""
if not token.startswith("Bearer "):
raise ValueError("Missing Bearer token")
# ... validate token ...
return AuthContext(domain="jwt", authenticated=True, principal="alice")
server = RpcServer(MyService, MyServiceImpl())
app = make_wsgi_app(server, authenticate=authenticate)
Over pipe/subprocess transport, ctx.auth is always AuthContext.anonymous().
Transport metadata¶
ctx.transport_metadata provides transport-level information like remote_addr and user_agent (HTTP only). It's a read-only mapping populated by the transport layer.
API Reference¶
AuthContext¶
AuthContext
dataclass
¶
AuthContext(
domain: str | None,
authenticated: bool,
principal: str | None = None,
claims: Mapping[str, Any] = dict(),
)
Authentication context for the current request.
Populated by transport-specific authenticate callbacks (e.g. JWT
validation on HTTP) and injected into method implementations via
:class:CallContext.
| ATTRIBUTE | DESCRIPTION |
|---|---|
domain |
Authentication scheme that produced this context, or
TYPE:
|
authenticated |
Whether the caller was successfully authenticated.
TYPE:
|
principal |
Identity of the caller (e.g. username, service account).
TYPE:
|
claims |
Arbitrary claims from the authentication token.
TYPE:
|
anonymous
classmethod
¶
anonymous() -> AuthContext
require_authenticated
¶
Raise if not authenticated.
| RAISES | DESCRIPTION |
|---|---|
PermissionError
|
If |
CallContext¶
CallContext
¶
CallContext(
auth: AuthContext,
emit_client_log: ClientLog,
transport_metadata: Mapping[str, Any] | None = None,
*,
server_id: str = "",
method_name: str = "",
protocol_name: str = ""
)
Request-scoped context injected into methods that declare a ctx parameter.
Provides authentication, logging, and transport metadata in a single injection point.
Initialize with auth context, client-log callback, and optional server context fields.
Source code in vgi_rpc/rpc/_common.py
logger
property
¶
Server-side logger with request context pre-bound.
| RETURNS | DESCRIPTION |
|---|---|
LoggerAdapter[Logger]
|
A |
LoggerAdapter[Logger]
|
|
LoggerAdapter[Logger]
|
|
LoggerAdapter[Logger]
|
|
LoggerAdapter[Logger]
|
|
CallStatistics¶
CallStatistics
dataclass
¶
CallStatistics(
input_batches: int = 0,
output_batches: int = 0,
input_rows: int = 0,
output_rows: int = 0,
input_bytes: int = 0,
output_bytes: int = 0,
)
Mutable accumulator of per-call I/O counters for usage accounting.
Created at dispatch start and populated as batches flow through the server. Surfaced through the access log and OTel dispatch hook.
Byte measurement: uses pa.RecordBatch.get_total_buffer_size()
which reports logical Arrow buffer sizes (O(columns), negligible cost).
This is an approximation — it does not include IPC framing
overhead (padding, schema messages, EOS markers).
| ATTRIBUTE | DESCRIPTION |
|---|---|
input_batches |
Number of input batches read by the server.
TYPE:
|
output_batches |
Number of output batches written by the server.
TYPE:
|
input_rows |
Total rows across all input batches.
TYPE:
|
output_rows |
Total rows across all output batches.
TYPE:
|
input_bytes |
Approximate logical bytes across all input batches.
TYPE:
|
output_bytes |
Approximate logical bytes across all output batches.
TYPE:
|