苹果v2 通知相比v1 减少了一次再次请求服务器验证,但是JWS 格式本人接触较少,翻阅了资料大概搞懂苹果整个验签流程。
如下
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Firebase\JWT\SignatureInvalidException;
//需要用到 friebase/jwt 这个组件 推荐composer 安装;git地址:https://github.com/firebase/php-jwt:
public function validateJwt($jws)
{
$jws = json_decode($jws, true)['signedPayload'];
//Jws 根据 . 分割的三段字符串
$components = explode('.', $jws);
if (count($components) !== 3) {
throw new \Exception('JWS string must contain 3 dot separated component.');
}
$header = base64_decode($components[0]);
$headerJson = json_decode($header, true);
$this->validateAppleRootCA($headerJson);
$data = $this->decodeCertificate($jws, $headerJson, 0);
return $data;
}
private function validateAppleRootCA($headerJson)
{
$lastIndex = count($headerJson['x5c']) -