簡體   English   中英

使用 PHP 進行 JWE/JWT 身份驗證

[英]JWE/JWT authentication with PHP

上周我們開始使用提供商的新 API。 為了進行身份驗證,他們在 https 請求的標頭中請求 JWE。

我們沒有使用 JWE 的經驗,然后我們開始查找有關它的信息,以使用 PHP 開發它。

經過多次測試並感謝 DinoChiesa 在線 JWT 解碼器 ( https://dinochiesa.github.io/jwt/ ),我們開發了一個函數來實現它,但還有一些問題。 最后 Dino Chiesa 幫助我們解決了問題,下面的功能現在可以正常工作了。

<?php 
    require_once("auth/key.php");
    include 'vendor/autoload.php';
    use phpseclib3\Crypt\PublicKeyLoader;  
    
    function jwe (){
        global $payloadAuth; //put here payload that you have to use

        /**************************************************/
        /********************HEADER************************/
        /**************************************************/
        // base64 encodes the header json
        $arr = array('enc' => 'A256GCM', 'alg' => 'RSA-OAEP');
        $arr2 = json_encode($arr);
        $encoded_header=base64url_encode($arr2);     

        /**************************************************/
        /********************JWE KEY***********************/
        /**************************************************/
        $CEK=openssl_random_pseudo_bytes(32);    
        $encoded_EncCEK = base64url_encode(rsaEncryptionOaepSha256($publicKey, $CEK));

        /**************************************************/
        /********************VECTOR************************/
        /**************************************************/
        $iv = openssl_random_pseudo_bytes(12);
        $encoded_iv = base64url_encode($iv);
        
        /**************************************************/
        /********************CYPHERTEXT********************/
        $cipher = "aes-256-gcm";
        $option=1;    
        $aad=$encoded_header;

        $ciphertext=openssl_encrypt($payloadAuth, $cipher, $CEK, $option, $iv, $tag, $aad);
        $encoded_ciphertext=base64url_encode($ciphertext);
        
        /**************************************************/
        /********************TAG***************************/
        /**************************************************/
        $encoded_tag=base64url_encode($tag);
    
        /**************************************************/
        /********************FIN***************************/
        /**************************************************/

        return $encoded_header . '.' . $encoded_EncCEK . '.' . $encoded_iv . '.' . $encoded_ciphertext . '.' . $encoded_tag;

    }
  
    function base64url_encode($data) {
        return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
      }

    function rsaEncryptionOaepSha256($publicKey, $plaintext) {
        global $publicKey;       

        $rsa = PublicKeyLoader::load($publicKey)->withHash('sha1')->withMGFHash('sha1');
        return $rsa->encrypt($plaintext);
    }    

?>

如果“enc”=“A256CBC-HS512”和“alg”=“RSA-OAEP-256”,有人可以解釋如何更改代碼嗎?

謝謝和問候, 路易斯

我希望使用 PHP 的人能夠找到 JWE 身份驗證的完整解決方案。

我建議使用web-token/jwt-framework而不是嘗試創建自己的構建器/解析器函數,因為這些事情會變得非常棘手!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM