gpt4 book ai didi

python - 在 python 中验证 StoreKit 2 事务 jwsRepresentation 的正确方法是什么?

转载 作者:行者123 更新时间:2023-12-03 08:04:06 54 4
gpt4 key购买 nike

从文档中尚不清楚您实际上做了什么来验证 jwsRepresentation来自服务器端 StoreKit 2 事务的字符串。

还有 Apple App Store Notifications V2 中的“signedPayload”似乎是相同的,但除了在设备上的客户端验证之外,也没有关于实际验证的文档。

什么给了?我们用这个 JWS/JWT 做什么?

最佳答案

(免责声明:我是加密货币新手,因此如果我在整个过程中使用了错误的术语等,请检查我)

jwsRepresentation 中的 JWS 和通知 V2 JSON 正文中的 signedPayload 都是 JWT — 您可以在 jwt.io 中查看它。 。这项工作是验证 JWT 签名,并在您充分确信它确实来自 Apple 后提取有效负载。然后,有效负载本身包含可用于升级用户帐户等的信息。服务器端一旦数据可信。

要验证 JWT,您需要找到 JWT header 的 "x5c" 集合中指定的用于签署 JWT 的签名,验证证书链,然后验证签名是否为确实来自苹果。

第一步:加载来自 Apple 的众所周知的根证书和中间证书。

import requests
from OpenSSL import crypto

ROOT_CER_URL = "https://www.apple.com/certificateauthority/AppleRootCA-G3.cer"
G6_CER_URL = "https://www.apple.com/certificateauthority/AppleWWDRCAG6.cer"

root_cert_bytes: bytes = requests.get(ROOT_CER_URL).content
root_cert = crypto.load_certificate(crypto.FILETYPE_ASN1, root_cert_bytes)

g6_cert_bytes: bytes = requests.get(G6_CER_URL).content
g6_cert = crypto.load_certificate(crypto.FILETYPE_ASN1, g6_cert_bytes)

第二步:从 JWT header 中获取证书链

import jwt  # PyJWT library

# Get the signing keys out of the JWT header. The header will look like:
# {"alg": "ES256", "x5c": ["...base64 cert...", "...base64 cert..."]}
header = jwt.get_unverified_header(apple_jwt_string)

provided_certificates: List[crypto.X509] = []
for cert_base64 in header['x5c']:
cert_bytes = base64url_decode(cert_base64)
cert = crypto.load_certificate(crypto.FILETYPE_ASN1, cert_bytes)
provided_certificates.append(cert)

第三步:验证链是否如您所想 - 这可确保证书链由真正的 Apple 根证书和中间证书签名。

# First make sure these are the root & intermediate certs from Apple:
assert provided_certificates[-2].digest('sha256') == g6_cert.digest('sha256')
assert provided_certificates[-1].digest('sha256') == root_cert.digest('sha256')

# Now validate that the cert chain is cryptographically legit:
store = crypto.X509Store()
store.add_cert(root_cert)
store.add_cert(g6_cert)
for cert in provided_certificates[:-2]:
try:
crypto.X509StoreContext(store, cert).verify_certificate()
except crypto.X509StoreContextError:
logging.error("Invalid certificate chain in JWT: %s", apple_jwt)
return None
store.add_cert(cert)

最终:使用 header 中现在受信任的证书加载并验证 JWT。

# Now that the cert is validated, we can use it to verify the actual signature
# of the JWT. PyJWT does not understand this certificate if we pass it in, so
# we have to get the cryptography library's version of the same key:
cryptography_version_of_key = provided_certificates[0].get_pubkey().to_cryptography_key()
try:
return jwt.decode(apple_jwt, cryptography_version_of_key, algorithms=["ES256"])
except Exception:
logging.exception("Problem validating Apple JWT")
return None

瞧,您现在可以使用来自 App Store 的经过验证的 JWT 正文。

整个解决方案的要点:https://gist.github.com/taylorhughes/3968575b40dd97f851f35892931ebf3e

关于python - 在 python 中验证 StoreKit 2 事务 jwsRepresentation 的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73000068/

54 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com