node.js - 如何创建签名文件以从nodejs服务器在safari中发送网络推送通知

标签 node.js safari apple-push-notifications pkcs#7

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

The Signature

The signature is a PKCS #7 detached signature of the manifest file. Sign the manifest file with the private key associated with your web push certificate that you obtained while registering with Apple. In PHP, you can do this with the openssl_pkcs7_sign function. The create_signature function in the attached createPushPackage.php companion file (the link is near the top of the page) shows how you can do this.

If the contents of your push package ever change, you’ll need to recompute your 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中实现相同的功能吗?

最佳答案

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

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

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

关于node.js - 如何创建签名文件以从nodejs服务器在safari中发送网络推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34188310/

相关文章:

javascript - 来自 Node.js 的 Javascript 中的 getToken 的 invalid_request

ios - 如果来自 iOS 上的同一对话,如何在通知中心显示 "Group/Collapse"多个通知

javascript - 在javascript中将整数转换为数字的小数部分最有效的方法是什么?

python - Selenium safari 驱动程序无法在 python 中工作

javascript - Webkit/Safari/火狐/API : Can I programmatically read/extract multiple tabs' URLs?

html - 在 Safari 6 中工作的垂直文本

ios - 没有 AppDelegate 的 SwiftUI 远程推送通知(Firebase 云消息传递)

ios - `isRegisteredForRemoteNotifications` 第一次查询时返回 false

node.js - 为运行二进制文件的 NPM 脚本设置 CWD

node.js - 设置 npm config proxy ubuntu 后,npm 不起作用