【问题标题】:Supplied key param cannot be coerced into a private key提供的密钥参数不能强制转换为私钥
【发布时间】:2019-05-29 14:54:58
【问题描述】:

我正在尝试使用 JWT Bearer 执行 OAuth2 的步骤 here,我已包含 installation instructions 中所述的相应文件。

但是,当我运行以下 PHP 代码时:

$private_key = file_get_contents("/www/var/includes/keyfile.pem");
$client_id   = 'example_client_id';
$user_id     = 'example_user_id';
$grant_type  = 'urn:ietf:params:oauth:grant-type:jwt-bearer';
$jwt = generateJWT($private_key, $client_id, $user_id, 'https://api.example.com');

...我收到以下错误:

openssl_sign(): supplied key param cannot be coerced into a private key

Uncaught exception 'Exception' with message 'Unable to sign data.'

我已经读到使用“file_get_contents”可能不是最好的方法,已经尝试了其他方法但没有运气。第一个链接中的文档特别说要使用它,这似乎很奇怪?

【问题讨论】:

  • 听起来你的keyfile.pem文件有问题
  • 从您链接的示例中,他们使用 generateJWT 和 RSA 私钥(来自公钥/私钥对)。 .pem 文件通常是证书
  • 好的,有道理!您知道将 .pem 文件与 JWT 一起使用的方法吗? .pem 是此 API 的要求。

标签: php oauth-2.0 jwt


【解决方案1】:
  1. 准备“私钥ID”:
   $fp = fopen("/path/to/file/privatekey.pem", "r");
   
   $privKey = fread($fp, 8192);
   
   fclose($fp);

   $pKeyId = openssl_get_privatekey($privKey, 'optional_passphrase');

  1. 在 openssl_sign 中使用:

openssl_sign($dataToSign, $signatureVar, $pKeyId);

信用:https://rupom.wordpress.com/2015/09/19/openssl_sign-supplied-key-param-cannot-be-coerced-into-a-private-key/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-04
    • 2023-03-16
    • 2021-03-01
    • 2014-12-11
    • 1970-01-01
    • 2016-04-02
    • 2013-07-17
    • 1970-01-01
    相关资源
    最近更新 更多