La documentation est un peu de mauvaise qualité, mais voici un exemple appel PHP cURL pour obtenir l'ID/Accès Tokens en utilisant votre code d'autorisation pour le flux d'autorisation: -
$url = 'https://<YOURDOMAIN>.auth.us-east-1.amazoncognito.com/oauth2/token';
$client_key = '<YOUR_CLIENT_ID>';
$client_secret = '<YOUR_CLIENT_SECRET>';
$data = [ 'grant_type' => 'authorization_code',
'client_id'=>$client_key, 'code'=>$_GET["code"],
'redirect_uri'=>'<YOUR_REDIRECT_URI>'];
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_VERBOSE, true);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($handle, CURLOPT_USERPWD, $client_key . ":" . $client_secret);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$field_string = http_build_query($data);
curl_setopt($handle, CURLOPT_POSTFIELDS, $field_string);
$resp = json_decode(curl_exec($handle),true);
Une fois que vous avez obtenu le jeton d'identité dont vous avez besoin pour analyser le fichier JSON JWK de
https://cognito-idp.us-east-1.amazonaws.com/<USER_POOL_ID/.well-known/jwks.json
puis rechercher le champ d'enfant dans la tête jeton er, et l'utiliser comme le secret pour décoder le jeton. J'ai utilisé cette bibliothèque: - https://github.com/firebase/php-jwt
Ainsi, le code de jeton de validation ressemble à quelque chose comme: -
$jwks_json = file_get_contents("https://cognito-idp.us-east-1.amazonaws.com/<USER_POOL_ID>/.well-known/jwks.json");
$jwk = JWK::parseKeySet($jwks_json);
$tks = explode('.', <YOUR_TOKEN>);
list($headb64, $bodyb64, $cryptob64) = $tks;
$jwt_header = json_decode(base64_decode($headb64),true);
$jwt_body = json_decode(base64_decode($bodyb64),true);
$key=$jwk[$jwt_header["kid"]];
try
{
$decoded = JWT::decode(<YOUR_TOKEN>, $key, array($jwt_header["alg"]));
$decoded_array = (array) $decoded;
// GREAT SUCCESS!
}
catch (\Exception $e)
{
// TOKEN COULDN'T BE VALIDATED
}