Verificar Mensajes
Verificar Mensajes
Verificar Firma
Ejemplos
from shinkansen.responses import ResponseMessage
from shinkansen.common import SHINKANSEN, FinancialInstitution
detached_jws = ... # read Shinkansen-Jws-Signature from HTTP header
payload = ... # read from HTTP body
shinkansen_certificates = [...] # previously loaded in memory
response_message = ResponseMessage.from_json(payload)
response_message.verify(
detached_jws, shinkansen_certificates,
sender=SHINKANSEN,
receiver=FinancialInstitution("MY-IDENTIFIER-AS-RECEIVER")
) # raises an exception if invalid
# We are good, we can continue reading message.header, message.responses...from jwcrypto.jwk import JWK
from jwcrypto.jws import JWS
from base64 import b64encode, b64decode
from cryptography.hazmat.primitives import serialization
from cryptography import x509
import json
detached_jws = ... # read Shinkansen-Jws-Signature from HTTP header
payload = ... # read from HTTP body
certificate_whitelist = [...] # previously loaded in memory
# Parse JWS:
jws = JWS()
jws.deserialize(detached_jws)
# Find certificate:
if ("x5c" not in jws.jose_header) or (not jws.jose_header["x5c"]):
raise ... # Reject, missing x5c certificate
# Take the first certificate and parse it:
b64_der_certificate = jws.jose_header["x5c"][0]
der_certificate = b64decode(b64_der_certificate)
certificate = x509.load_der_x509_certificate(der_certificate)
# Create a JWK public key from certificate:
jwk = JWK()
jwk.import_from_pyca(certificate.public_key())
# Verify:
jws.verify(jwk, alg="PS256",
detached_payload=payload) # raises exception if invalid
# Make sure the certificate was whitelisted
if all(
# Compare every certificate c in whitelist against the x5c certificate
c.public_bytes(encoding=serialization.Encoding.DER) != der_certificate
for c in certificate_whitelist
):
raise ... # Reject if certificate not on the whitelist
# We are good, continueÚltima actualización