AWS Cognito JWT verify in PHP

Viewed 19

I am a website hosted in EC2 behind loadbalancer. Loadbalancer linked to AWS Cognito and allows traffic to EC2 only if user has been verified. Once user has logged, will then be directed to EC2 instance running the website. I am trying to verify the JWT token (oidcIdentity) that I get in the header of the request in the EC2 instance.

Its a PHP system and I am referring to https://docs.aws.amazon.com/elasticloadbalancing/latest/application/listener-authenticate-users.html#user-claims-encoding, which provides python code.

I am trying do same in PHP code and tried using firebase/php-jwt, but didn't work, failing with error:

PHP Fatal error:  Uncaught UnexpectedValueException: Incorrect key for this algorithm in firebase/vendor/firebase/php-jwt/src/JWT.php:133 Stack trace:
#0 firebase/test.php(17): Firebase\JWT\JWT::decode('eyJ0eXAiOiJKV1Q...', Object(Firebase\JWT\Key))
#1 {main}   thrown in firebase/vendor/firebase/php-jwt/src/JWT.php on line 133

The PHP code is:

<?php

require_once('vendor/autoload.php');

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

$jwt = '<TOKEN>';

$publicKey = <<<EOD
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAExnt5oG9ysxSDOLpgbBrRicWkjMs8
dIpFkF29iSSTpUyFQuZDf4MIQBRW0MT3Zb3z7uJmhrUPauWfDTHKhjQcng==
-----END PUBLIC KEY-----
EOD;

$decoded = JWT::decode($jwt, new Key($publicKey, 'RS256'));
print_r($decoded);

What am i doing wrong, can someone please help

0 Answers
Related