如何将 PEM 证书/密钥作为变量传递给 node.js 项目

发布于 2024-12-16 14:12:22 字数 466 浏览 6 评论 0原文

我正在尝试使用 node-apn 设置 Apple 推送通知服务。我已经编写了项目的其他部分(RESTful API、后端、消息队列),但我遇到了 node.js 部分的阻塞问题。

我必须向 node-apn 传递一个密钥和一个证书,这通常是通过传递一对文件名作为选项来完成的。然而,这种设置并不理想,我需要将它们作为变量传递。我已经更改了node-aps的源代码,如果不是我在研究解决方案时没有考虑到的问题,它会很乐意寻找这些变量。

Javascript不接受多行变量,所以我无法传递这种格式的证书或密钥:

var cert = '-----BEGIN CERTIFICATE-----
[cut]
-----END CERTIFICATE-----';

我尝试用 \ 转义换行符,但最终得到单行证书/密钥,所以我得到“PEM_read_bio:no start”行”错误。

有什么好的解决方法吗?

I am trying to set up an Apple Push Notification service with node-apn. I have written the other parts of the project (RESTful API, backend, message queueing) but I run into a blocking problem with the node.js portion.

I have to pass node-apn a key and a certificate and this is normally done passing a pair of filenames as option to it. However this setup is not ideal and I need to pass them as variables. I have already changed node-aps' source and it would happily look for these variables if it wasn't for an issue I didn't considerate while studying a solution.

Javascript doesn't accept multiline variables so I can't pass a certificate or a key in this format:

var cert = '-----BEGIN CERTIFICATE-----
[cut]
-----END CERTIFICATE-----';

I tried escaping newlines with \ but then I end up with a one line certificate/key and so I get a "PEM_read_bio:no start line" error.

Is there any decent workaround?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哥,最终变帅啦 2024-12-23 14:12:22

您可以在字符串中包含换行符,如下所示:

var cert = '-----BEGIN CERTIFICATE-----\n\
[cut]\n\
-----END CERTIFICATE-----';

但我认为单独放置证书文件是一个更好的主意。我认为上面的代码看起来并不理想。

You can have newlines in your string like this:

var cert = '-----BEGIN CERTIFICATE-----\n\
[cut]\n\
-----END CERTIFICATE-----';

But I think that it's a better idea to put your certificate files separately. Above code doesn't look ideal in my opinion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文