I will start with the back story. I am trying to implement/integrate https://jaas.8x8.vc into an application. Two years ago when I followed their tutorial. It worked. We have launched a new server and when I tried to move the code from the old working server to the new one. The service no longer worked. I read all of the documentation on the 8x8 project site and there are changes from the previous implementation. I get the code snippet working on the new server. The next step involves JWT. That is the part that changed from two years ago.
Now, I will skip the next three weeks of pounding my head on the desk to where I am now. The quest became to just be able to produce a signed JWT.
I created a clean install of this package independent of my application. I used composer to bring in the dependencies. I copied and pasted the code from this section of the documentation. https://web-token.spomky-labs.com/the-components/signed-tokens-jws/jws-creation
Here is my code. I am trying to create a signed JWT.
use Jose\Component\Core\AlgorithmManager;
use Jose\Component\Core\JWK;
use Jose\Component\Signature\Algorithm\RS256;
use Jose\Component\Signature\JWSBuilder;
$algorithmManager = new AlgorithmManager([
new RS256(),
]);
$keyFile = dirname(__FILE__, 6) . "/sites/RsaPrivateKey.pk";
$contents = file_get_contents($keyFile);
use Jose\Component\KeyManagement\JWKFactory;
$key = JWKFactory::createFromKeyFile(
$keyFile, // The filename
null, // Secret if the key is encrypted, otherwise null
[
'use' => 'sig', // Additional parameters
]
);
echo "done";
$jwsBuilder = new JWSBuilder($algorithmManager);
// The payload we want to sign. The payload MUST be a string hence we use our JSON Converter.
$payload = json_encode([
'exp' => time() + 3600,
'iss' => 'My service',
'aud' => 'jitsi',
]);
$jws = $jwsBuilder
->create() // We want to create a new JWS
->withPayload($payload) // We set the payload
->addSignature($key, ['alg' => 'RS256']) // We add a signature with a simple protected header
->build(); // We build it
use Jose\Component\Signature\Serializer\CompactSerializer;
$serializer = new CompactSerializer(); // The serializer
$token = $serializer->serialize($jws, 0); // We serialize the signature at index 0 (we only have one signature).