使用PHP-JWT实现Epic on FHIR
P粉593649715
P粉593649715 2023-08-30 23:58:19
[PHP讨论组]
<p>我正在尝试获取 jwt 令牌,但每次我尝试的所有操作都会出现错误。以下是我尝试过的事情。我确实在没有包的情况下获得了 jwt-token,但是当我使用 jwt.io 检查签名验证时,每次都会失败。使用该软件包时,我遇到了不同的错误,例如<em>私钥无法强制执行</em>,有时<em>算法无效</em>。请纠正我哪里搞砸了。</p> <ul> <li><strong>没有 php-Jwt</strong></li> </ul> <pre class="brush:php;toolbar:false;">$header = [ 'alg' =&gt; &quot;RS384&quot;, 'typ' =&gt; &quot;JWT&quot; ]; $payload = [ 'iss' =&gt; 'my-client-id', 'sub' =&gt; 'my-client-id', 'aud' =&gt; &quot;https://fhir.epic.com/interconnect-fhir-oauth/oauth2/token&quot;, 'jti' =&gt; (string)strtotime(gmdate(&quot;Y-m-d H:i:s&quot;)), 'exp' =&gt; strtotime(gmdate(&quot;Y-m-d H:i:s&quot;)) + 270, ]; $privateKey = &quot;my private Key&quot; $headers_encoded = $this-&gt;base64url_encode(json_encode($header)); $payload_encoded = $this-&gt;base64url_encode(json_encode($payload)); $signature = hash_hmac('sha384', $headers_encoded.'.'.$payload_encoded, $privateKey, true); // Encode the signature as a base64url string $signature_encoded = $this-&gt;base64url_encode($signature); $jwt = $headers_encoded.'.'.$payload_encoded.'.'.$signature_encoded;</pre> <p><strong>- 使用 php-jwt</strong></p> <ol> <li>安装了该软件包 <em>作曲家需要 firebase/php-jwt --ignore-platform-req=ext-mongodb</em></li> <li>在我的控制器中使用了所需的文件</li> </ol> <pre class="brush:php;toolbar:false;">use Firebase\JWT\JWT; use Firebase\JWT\Key;</pre> <ol start="3"> <li>尝试编码:</li> </ol> <pre class="brush:php;toolbar:false;">$check = JWT::encode( $header, $payload, $privateKey, 'RS384' );</pre> <p>我收到不同的错误,例如<em>无法强制执行私钥</em>,有时<em>算法无效</em>。请纠正我哪里搞砸了。</p> <p>简单来说,这就是我正在做或正在尝试做的事情。我的代码的通用形式:</p> <pre class="brush:php;toolbar:false;">&lt;?php // Load the private key from a file $privateKey = file_get_contents('private.key'); // Set the header and payload for the JWT $header = [ 'alg' =&gt; 'RS384', 'typ' =&gt; 'JWT' ]; $payload = [ 'sub' =&gt; '1234567890', 'name' =&gt; 'John Doe', 'iat' =&gt; 1516239022 ]; // Encode the header and payload as JSON strings $headerEncoded = base64_encode(json_encode($header)); $payloadEncoded = base64_encode(json_encode($payload)); // Concatenate the header, payload, and secret to create the signature $signature = hash_hmac('sha384', &quot;$headerEncoded.$payloadEncoded&quot;, $privateKey, true); // Encode the signature as a base64 string $signatureEncoded = base64_encode($signature); // Concatenate the header, payload, and signature to create the JWT $jwt = &quot;$headerEncoded.$payloadEncoded.$signatureEncoded&quot;; echo $jwt;</pre> <p>我确实得到了 jwt 签名,但在 https://jwt.io/ 上它显示未经验证。</p>
P粉593649715
P粉593649715

全部回复(1)
P粉037215587

已修复

  • 使用它代替hash_mac()
openssl_sign($data, $signature, $privateKey, OPENSSL_ALGO_SHA384);
  • 确保密钥是否存储在 PHP 变量中,按如​​下格式设置密钥:
$privateKey =  "------BEGIN PRIVATE KEY------\n".
"MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC7VJTUt9Us8cKj\n".
"MzEfYyjiWA4R4/M2bS1GB4t7NXp98C3SC6dVMvDuictGeurT8jNbvJZHtCSuYEvu\n".
.
.
.
"TQrKhArgLXX4v3CddjfTRJkFWDbE/CkvKZNOrcf1nhaGCPspRJj2KUkj1Fhl9Cnc\n".
"dn/RsYEONbwQSjIfMPkvxF+8HQ==\n".
"------END PRIVATE KEY------";

将每一行用双引号""括起来,并在每行末尾添加\n

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号