aeat.adapters.outbound.aeat.auth._certificate_backends._playwright_context module¶
Playwright per-context client-certificate backend.
Playwright (Python, >=1.46) exposes client certs as a per-context
kwarg on playwright.async_api.Browser.new_context():
await browser.new_context(client_certificates=[{
"origin": "https://sede.agenciatributaria.gob.es",
"pfxPath": str(bundle.path),
"passphrase": password_value,
}])
There is no post-hoc injection hook: a context that was constructed
without client_certificates cannot be retrofitted with one. This
backend therefore validates the contract at call time rather than
mutating the context.
CertificateContextProvisioner calls
build_client_certificates_kwarg() while creating a browser context and
then stamps CERTIFICATE_CONTEXT_MARKER; PlaywrightContextBackend
later checks that marker for the selected LoadedCertificate.
- build_client_certificates_kwarg(cert, origin)[source]¶
Build the Playwright
client_certificateskwarg forcert.Materialises the passphrase from
pydantic.SecretStrat the exact call site and nowhere else. The returned list is wired directly intobrowser.new_context(client_certificates=...)byadapters.outbound.aeat.browser.
- class PlaywrightContextBackend[source]¶
Bases:
_CertBackendPrimary backend — per-context client cert via Playwright.
Implements the
_CertBackendcontract for browser-driven certificate sessions. Thepreloadleg validates theCERTIFICATE_CONTEXT_MARKERstamp produced byCertificateContextProvisioner. Theverifyleg delegates toHttpxFallbackBackend, which fails closed unless a future backend can perform mTLS verification without materialising plaintext key files.- preload(cert, context)[source]¶
Verify the context was constructed with this cert.
The browser session layer is expected to tag the constructed
playwright.async_api.BrowserContextwith an attribute namedCERTIFICATE_CONTEXT_MARKERmatchingcert.sha256_thumbprint. If the marker is absent, raisesadapters.outbound.aeat.auth.certificate.CertificateErrorpointing the operator atbuild_client_certificates_kwarg().- Parameters:
cert (
LoadedCertificate) – The loaded PKCS#12 certificate.context (
object) – A PlaywrightBrowserContextobject (typed asobjectto avoid leaking the Playwright dependency upward).
- Raises:
CertificateError – When the context lacks the expected thumbprint marker.
- Return type:
- verify(cert, url)[source]¶
Delegate to the fail-closed httpx fallback for handshake verification.
The Playwright backend has no standalone handshake primitive — spinning up a full browser just to probe TLS would be wasteful. The fallback refuses verification rather than writing decrypted PEM/key material to temporary files.
- Parameters:
cert (
LoadedCertificate) – The loaded PKCS#12 certificate to present.url (
str) – HTTPS endpoint to probe.
- Return type:
- Returns:
A
HandshakeResultdescribing the outcome.