I've a question about signing with apple more precisely how to create the jw token in php after logging, here is what I do:
$pem_content =
"-----BEGIN PRIVATE KEY-----".
"XXXX".
"-----END PRIVATE KEY-----";
$header = json_encode(array(
'kid' => $keyId,
'alg' => 'ES256'
));
$decodedTokenData = json_decode(base64_decode(str_replace('_', '/', str_replace('-','+',explode('.', $_POST['id_token'])[1]))),true);
$payload = json_encode(array(
'iss' => $teamid,
'iat' => time(),
'exp' => time() + 86400*180,
'aud' => $decodedTokenData['aud'],
'sub' => $decodedTokenData['sub'],
));
$base64UrlHeader = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($header));
$base64UrlPayload = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($payload));
$key = //I don't know how to use $pem_content here
$signature = hash_hmac('sha256', $base64UrlHeader . "." . $base64UrlPayload, $key, true);
$base64UrlSignature = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($signature));
$jwt = $base64UrlHeader . "." . $base64UrlPayload . "." . $base64UrlSignature;
And to test transaction I do
curl -X POST https://appleid.apple.com/auth/token -d '{"client_id":$CLIENTID,"client_secret":$jwt,"code": $_POST['code'],"grant_type":"authorization_code","redirect_uri":$URL}'
But I get
{"error":"invalid_client"}
Two constraints here: I must use only php and avoid external libs
Thanks in advance
EDIT I generate jwt like this:
$signature = '';
openssl_sign($base64UrlHeader . "." . $base64UrlPayload, $signature,openssl_get_privatekey($pem_content),'SHA256');
$signature = $this->signatureFromDER($signature, 256);
the functions used are from here https://github.com/firebase/php-jwt/blob/d2113d9b2e0e349796e72d2a63cf9319100382d2/src/JWT.php#L172
and now curl command is
curl -X POST "https://appleid.apple.com/auth/token" -H 'content-type: application/x-www-form-urlencoded' -d"client_id=CLIENT_ID&client_secret=CLIENT_SECRET&code=CODE&grant_type=authorization_code&redireect_uri=https%3A%2F%2Fsite.com%2Findex.php"
But I still get invalid_client
EDIT2: I've nom implemented this solution: Apple Sign In "invalid_client", signing JWT for authentication using PHP and openSSL
But I keep having invalid_client error
EDIT3 even with https://github.com/firebase/php-jwt I get the error