py2http.middleware¶
aiohttp middleware that guards every route of an app: mk_jwt_middleware
(a verified Authorization: Bearer JWT) and mk_superadmin_middleware (one
shared key). The checks themselves live in py2http.auth, shared with the Bottle
plugins of py2http.bottle_plugins.
Functions
|
Make an aiohttp middleware that requires a valid |
|
Make an aiohttp middleware that only lets through requests whose |
- py2http.middleware.mk_jwt_middleware(secret, verify=True, *, algorithms=('HS256',), public_routes=('ping', 'openapi'), allow_mixed_algorithm_families=False, decode_kwargs=None)[source]¶
Make an aiohttp middleware that requires a valid
Authorization: BearerJWT on every request, and puts the decoded claims onrequest.token.- Parameters:
secret – The key tokens are verified with (an HMAC secret for
HS*algorithms, a public key forRS*/ES*/…).verify – If False, signatures and claims (
exp, …) are NOT checked: whatever a token says ends up onrequest.token, and an undecodable token only triggers a warning. For development only. (Before, PyJWT 2 ignored the legacy option this passed, so signatures were still checked even withverify=False.)algorithms (
Iterable[str]) – The algorithms a token may be signed with (PyJWT names, case-sensitive;"none"is refused). Defaults to("HS256",), aspy2http.bottle_plugins.JWTPlugindoes.public_routes (
Iterable[str]) – Names of aiohttp routes (name=inadd_route) that need no token. Matched on the route name, not the handler’s__name__, so a handler that happens to be calledpingis not public by accident. Route names are matched in sub-applications too, so a sub-app route namedpingis public as well.allow_mixed_algorithm_families (
bool) – Accepting an HMAC algorithm and a public-key algorithm with onesecretis the classic algorithm-confusion setup (a token “signed” with HS256 using the public key as the HMAC secret), so it raisesValueErrorunless this is True. The middleware used to accept["HS256", "RS256"]; pass that list with this flag to restore it.decode_kwargs (
dict) – Extrajwt.decodearguments, such asaudience,issuerorleeway(without them a token carrying anaudclaim is always refused).