如何创建签名文件以在来自nodejs服务器的safari中发送Web推送通知

问题描述 投票:0回答:1

现在我有一个服务器可以发送推送通知到chrome,我也希望扩展为safari,在apple doc(https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/NotificationProgrammingGuideForWebsites/PushNotifications/PushNotifications.html)但我不知道如何在nodeJS中制作签名文件

签名

签名是清单文件的PKCS#7分离签名。使用与Apple注册时获得的Web推送证书关联的私钥对清单文件进行签名。在PHP中,您可以使用openssl_pkcs7_sign函数执行此操作。附加的createPushPackage.php配套文件中的create_signature函数(该链接位于页面顶部附近)显示了如何执行此操作。

如果推送包的内容发生变化,您需要重新计算签名。

在同一页面中,苹果在php中举了一个例子:

// Creates a signature of the manifest using the push notification certificate.
function create_signature($package_dir, $cert_path, $cert_password) {
    // Load the push notification certificate
    $pkcs12 = file_get_contents($cert_path);
    $certs = array();
    if(!openssl_pkcs12_read($pkcs12, $certs, $cert_password)) {
        return;
    }

    $signature_path = "$package_dir/signature";

    // Sign the manifest.json file with the private key from the certificate
    $cert_data = openssl_x509_read($certs['cert']);
    $private_key = openssl_pkey_get_private($certs['pkey'], $cert_password);
    openssl_pkcs7_sign("$package_dir/manifest.json", $signature_path, $cert_data, $private_key, array(), PKCS7_BINARY | PKCS7_DETACHED);

    // Convert the signature from PEM to DER
    $signature_pem = file_get_contents($signature_path);
    $matches = array();
    if (!preg_match('~Content-Disposition:[^\n]+\s*?([A-Za-z0-9+=/\r\n]+)\s*?-----~', $signature_pem, $matches)) {
        return;
    }
    $signature_der = base64_decode($matches[1]);
    file_put_contents($signature_path, $signature_der);
}

有人知道如何在nodeJS中使用相同的功能吗?

node.js safari apple-push-notifications pkcs#7
1个回答
2
投票

好吧我终于找到了如何做到这一点,你必须先将证书和密钥转换为PEM格式

$ openssl x509 -in cert.cer -inform DER -outform PEM -out cert.pem
$ openssl pkcs12 -in key.p12 -out key.pem -nodes

之后,你可以使用smime模块签署你的清单(我使用https://github.com/hipush/smime),我们准备好了,我们有一个签名:) :) :)

© www.soinside.com 2019 - 2024. All rights reserved.