[英]Firebase PHP JWT “OpenSSL unable to verify data: error:0906D06C:PEM routines:PEM_read_bio:no start line”
I'm using Google Cloud PHP API via App Engine. 我正在通过App Engine使用Google Cloud PHP API。 From the client device, a request is being sent through POST with a Firebase Auth token.
从客户端设备,通过POST使用Firebase身份验证令牌发送请求。 I am, as per Firebase PHP JWT's documentation, trying to decode the token with this code:
根据Firebase PHP JWT的文档,我尝试使用以下代码解码令牌:
$decoded = JWT::decode($token, $key, array('RS256'));
$token is a line like (this being invalid for security reasons): $ token是一行(由于安全原因,这是无效的):
eyJhbGciOiJSUzI1asdaNiIsImtpZCI6Ijk2N2Q3NzQ4YmM5NTMTIzNzRhZWQasdasd3MzEyYzcwNjEyZTRlNTM4NmUifQ.tuaAsjdlkjvsdngeoijAnlnbfgLkoosdfKLnm,werkldsfNkndfkdsnfkfnlNKL2i34nkNJioj4Kkoj234j%jij1kjojsdffds98giojerNNjasndasiNjasdnJAjnasdkjnFoFjoFJOIAASD8990adsaaDknnkngs.v_Ko6HZjrahbihLbw2Bm7EuslEC2SSHXNK79rDbD9qIIVYxPjCsubsdfkyAWDIoJHwjkM9TtssYS-1Cjd_xkXghfILuDZpzLsHV6rF20J4n3eUTrsnmLDHK6UB5N3yK2LYoF1UoFrsiyWenfqELfE4Gx5wlfmsylTS1foS2CWRrT1ccqmJBinWiY6JNUS-0gg-2Aecf_VJ63RD9308sBKy1DUsBeje9yG8w2YpYsAqKIlMTC-FqLLpHlKe4LZxcveiqSF4J6PgvcLSPTMmg7-Li_8m41O-wfU1zwSpS1SJ73RJNg-kvRZ1y1ll8ExqXjZkazRDVkYVo6yu5AXi1Onl6FqBLA
Checking the token through JWT.io will hand me a correct payload. 通过JWT.io检查令牌将给我一个正确的有效载荷。
Now for the $key part. 现在为$ key部分。 I have downloaded the Default Service Account JSON file from Google API Console.
我已从Google API控制台下载了默认服务帐户JSON文件。 If I use the "private_key" which looks like:
如果我使用“private_key”,它看起来像:
-----BEGIN PRIVATE KEY-----\n[VERY_LONG_PRIVATE_KEY]\n-----END PRIVATE KEY-----\n
I get this error: 我收到此错误:
openssl_verify(): supplied key param cannot be coerced into a public key
I found an answer here at StackOverflow, that this could be used to convert the private key to a public key: 我在StackOverflow上找到了一个答案,这可以用来将私钥转换为公钥:
$private_key = openssl_pkey_get_private($c->serviceAccount->private_key);
$details = openssl_pkey_get_details($private_key);
$public_key = $details['key']
So if I use the $public_key instead, I get another error stating the following: 因此,如果我使用$ public_key,我会收到另一个错误,说明以下内容:
Uncaught exception 'DomainException' with message 'OpenSSL unable to verify data: error:0906D06C:PEM routines:PEM_read_bio:no start line
The public key looks like: 公钥如下:
-----BEGIN PUBLIC KEY-----\n[VERY_LONG_KEY]\n-----END PUBLIC KEY-----\n
So it seems like it should work. 所以它似乎应该工作。 But it doesn't.
但事实并非如此。 The algorithm RS256 is the correct one as well.
算法RS256也是正确的算法。
Any help appreciated! 任何帮助赞赏!
I ran into the same error while reading my private key using Node.js Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
我在使用Node.js读取私钥时遇到了同样的错误
Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
It turned out the issues was that Google Cloud converted \\n
into \\\\n
. 原来问题是Google Cloud将
\\n
转换为\\\\n
。 When I converted it back to \\n
, it worked. 当我将它转换回
\\n
,它有效。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const bigquery = require('@google-cloud/bigquery');
const config = functions.config();
admin.initializeApp(config.firebase);
const firestore = admin.firestore();
const sanitizePrivateKey = (key) =>
key.replace(/\\n/g, '\n');
/* firebase converts \n to \\n, we have to convert it back */
if (config.credentials) {
config.credentials.private_key = sanitizePrivateKey(config.credentials.private_key);
}
const bigqueryClient = bigquery({
projectId: 'screencastify-staging',
// eslint-disable-next-line
credentials: config.credentials,
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.