CI / validate-monitoring (push) Successful in 7s
CI / lint (push) Successful in 12s
CI / test (push) Successful in 1m6s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 6s
Pure code in federation/protocol/signatures, one module per scheme and a registry to pick them by name: - draft-cavage: sign (request-target) host date digest with rsa-sha256; verify rsa-sha256/hs2019, require the ActivityPub minimum, check Digest and a ±12h Date window - RFC 9421: sign @method @target-uri content-digest with rsa-v1_5-sha256 and created/keyid/alg; verify with a generic signature-base builder, created/expires checks and Content-Digest (sha-256/sha-512); includes the RFC 8941 subset these headers use - Failures carry a reason (missing, malformed, expired, digest_mismatch, bad_signature, unsupported, unknown_key, insufficient) for metrics Tested against the published vectors (draft-cavage-12 C.1 and C.2, RFC 9421's rsa-v1_5-sha256 example; test keys extracted from the specs into tests/data), round trips, and tampering with body, URL, method, date, key, coverage and clock. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
62 lines
1.8 KiB
Python
62 lines
1.8 KiB
Python
"""HTTP signatures for ActivityPub: one module per scheme, chosen by name.
|
|
|
|
verify(request, public_key_for) whichever scheme the request carries
|
|
sign(request, scheme, key_id, key) headers to add, using the named scheme
|
|
|
|
Adding or dropping a scheme is a module here plus an entry in SCHEMES.
|
|
"""
|
|
|
|
from datetime import UTC, datetime
|
|
|
|
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKey
|
|
|
|
from scrobbler.federation.protocol.signatures.base import (
|
|
PublicKeyFor,
|
|
SignatureError,
|
|
SignatureScheme,
|
|
SignedRequest,
|
|
VerifiedSignature,
|
|
)
|
|
from scrobbler.federation.protocol.signatures.draft_cavage import DraftCavage
|
|
from scrobbler.federation.protocol.signatures.rfc9421 import Rfc9421
|
|
|
|
SCHEMES: dict[str, SignatureScheme] = {s.name: s for s in (Rfc9421(), DraftCavage())}
|
|
|
|
__all__ = [
|
|
"SCHEMES",
|
|
"PublicKeyFor",
|
|
"SignatureError",
|
|
"SignedRequest",
|
|
"VerifiedSignature",
|
|
"detect",
|
|
"sign",
|
|
"verify",
|
|
]
|
|
|
|
|
|
def detect(request: SignedRequest) -> SignatureScheme:
|
|
for scheme in SCHEMES.values(): # RFC 9421 first: its Signature header differs
|
|
if scheme.present(request):
|
|
return scheme
|
|
raise SignatureError("missing")
|
|
|
|
|
|
def verify(
|
|
request: SignedRequest, public_key_for: PublicKeyFor, now: datetime | None = None
|
|
) -> VerifiedSignature:
|
|
return detect(request).verify(request, public_key_for, now or datetime.now(UTC))
|
|
|
|
|
|
def sign(
|
|
request: SignedRequest,
|
|
scheme: str,
|
|
key_id: str,
|
|
private_key: RSAPrivateKey,
|
|
now: datetime | None = None,
|
|
) -> dict[str, str]:
|
|
try:
|
|
chosen = SCHEMES[scheme]
|
|
except KeyError:
|
|
raise SignatureError("unsupported", f"scheme {scheme}") from None
|
|
return chosen.sign(request, key_id, private_key, now or datetime.now(UTC))
|