简体   繁体   English

Laravel-无法从.env文件获取.pem公钥数据

[英]Laravel - Can't get .pem public key data from .env file

I have an webapp built with Laravel and i need to use a public key from a .pem file to verify some data. 我有一个使用Laravel构建的webapp,我需要使用.pem文件中的公钥来验证一些数据。 The key is kept in the .env file and retrieved via config/app.php with the env() helper. 密钥保存在.env文件中,并使用env()帮助器通过config/app.php进行检索。 Since the .pem key needs to be on separate lines with specific length i use \\n characters to keep the key on one line. 由于.pem密钥需要位于具有特定长度的单独行中,因此我使用\\ n字符将密钥保持在一行中。 Problem is that when i use the \\n characters the variable is not cached and i cannot access it. 问题是,当我使用\\ n字符时,该变量未缓存并且无法访问它。 When i remove the \\n characters i can retrieve the variable but the openssl_get_privatekey($key); 当我删除\\ n字符时,我可以检索变量,但是openssl_get_privatekey($key); returns false. 返回false。 What am i doing wrong? 我究竟做错了什么? Is this a Laravel or some general PHP issue? 这是Laravel还是一些一般的PHP问题?

PHP's dotenv package does not seem to support multi-line environment variables. PHP的dotenv软件包似乎不支持多行环境变量。

You should ideally keep your .pem file as a file and reference it by path like eg: 理想情况下,您应该将.pem文件保留为文件,并通过路径引用它,例如:

PEM_FILE=/path/to/file.pem

and in the config: 并在配置中:

return [
    //...
    "key" => file_get_contents(env('PEM_FILE'))

This makes sense since generally certificates should generally go in the dedicated certificates path on the server. 这是有道理的,因为通常证书通常应放在服务器上的专用证书路径中。 If you cache the config then the actual contents of the .pem file are only read once on deployment. 如果缓存配置,则.pem文件的实际内容在部署时仅读取一次。

However if you must put it in dotenv then you can do: 但是,如果必须将其放入dotenv中,则可以执行以下操作:

In .env 在.env中

PEM_KEY="-----BEGIN RSA PRIVATE KEY-----\n…\n-----END DSA PRIVATE KEY-----"

In configs: 在配置中:

return [
    //...
    "key" => str_replace("\\n", "\n", env('PEM_KEY')), 

Since the key is generally base64 I don't think there's a chance \\n would naturally occur inside a .pem file. 因为密钥通常是base64,所以我认为\\n .pem文件中不会自然出现\\n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM