简体   繁体   English

Firebase PHP JWT“OpenSSL无法验证数据:错误:0906D06C:PEM例程:PEM_read_bio:无启动行”

[英]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.

相关问题 openssl_verify和“错误:0906D06C:PEM例程:PEM_read_bio:无起始行” - openssl_verify and “error:0906D06C:PEM routines:PEM_read_bio:no start line” 将 PEM 格式的公钥传递给 openssl_pkey_get_public 会给出错误:0906D06C:PEM 例程:PEM_read_bio:no start line - Passing public key in PEM format to openssl_pkey_get_public gives error:0906D06C:PEM routines:PEM_read_bio:no start line lombucci/jwt 无法解析您的密钥,原因:error:0909006C:PEMroutines:get_name:no start line - lombucci/jwt It was not possible to parse your key, reason: error:0909006C:PEM routines:get_name:no start line PHP OpenSSL 无法读取 PEM 格式的公钥 - PHP OpenSSL cannot read public key in PEM format 在php函数openssl_pkcs7_sign中打开pem文件时出错 - Error opening pem file in php function openssl_pkcs7_sign 错误0x2006D002:BIO例程:BIO_new_file:Windows上具有PHP的系统库 - Error 0x2006D002:BIO routines:BIO_new_file:system lib with PHP on Windows ECDSA:将 60 字节的二进制公钥转换为 PEM 格式,以便与 PHP openssl_verify() 一起使用 - ECDSA: Convert a binary public key of 60 bytes into PEM format for using it with PHP openssl_verify() 使用php-jwt库解码Firebase自定义令牌时出现openssl_verify()错误 - openssl_verify() error while decoding firebase custom token using php-jwt library PHP openssl_pkcs12_read “error:0308010C:digital envelope routines::unsupported” - PHP openssl_pkcs12_read "error:0308010C:digital envelope routines::unsupported" PHP openssl_pkcs12_read “error:0308010C:digital envelope routines::unsupported” - PHP openssl_pkcs12_read "error:0308010C:digital envelope routines::unsupported"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM