JWT Signing with Private key - Certificate [PHP]

I wanted to generate a simple JWT signature / JWT Encode. I append the signed string in the header of HTTP request, the server can verify it.

Private key is also used while generating above JWT.

HTTP Request with JWT in client_assertion

Lets start use web-token library documentation published here

First lets install the required

composer require web-token/jwt-framework

composer require web-token/jwt-key-mgmt

composer require web-token/jwt-easy

composer require web-token/jwt-signature-algorithm-rsa

composer require web-token/jwt-signature-algorithm-ecdsa


    $key = JWKFactory::createFromKeyFile(
                'private.key'
            );
    $time = time();

    $jws = Build::jws() // We build a JWS
        ->exp($time + 1800) // The "exp" claim
        ->iat($time) // The "iat" claim
        ->jti('THEJTISTRING') // The "jti" claim.
        ->alg('PS256') // The signature algorithm.
        ->iss('ISSUEDFORYOU') // The "iss" claim
        ->claim('aud', 'NEWVALUEHERE')
        ->sub('NEWSUBCLAIM') // The "sub" claim
        ->header('kid','THEKIDSTRING')
        ->header('typ',"JWT")
        ->sign($key); // Compute the token with the given JWK

You can verify if the generated JWT is valid from here https://jwt.io

Post a Comment

Previous Post Next Post