php - Apple Sign In used retails

one text

I've implemented the Apple Sign In on the website, but I can't retrieve user's full name. It's added to scope but is not being posted (it's being sent only the first time). Is there a way to get it somehow?

Frontend:

<div class="login-btn" id="appleid-signin" data-color="black" data-border="true" data-type="sign in"></div>
<script type="text/javascript"
        src="https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js"></script>
<script type="text/javascript">
  AppleID.auth.init({
    clientId: 'net.exmample.oauth',
    scope: 'email name',
    response_type: 'code',
    response_mode: 'form_post',
    redirectURI: 'https://example.net/appletest',
    usePopup : false 
  });
  const buttonElement = document.getElementById('appleid-signin');
  buttonElement.addEventListener('click', () => {
    AppleID.auth.signIn();
  });
</script>

Backend:

        $identityToken = Input::post('id_token');

        $appleSignInPayload = ASDecoder::getAppleSignInPayload($identityToken);
        $email = $appleSignInPayload->getEmail();

        var_dump('email: ' . $email);
        echo '<br>';

        $user = $appleSignInPayload->getUser();
        var_dump('userid: ' . $user);
        echo '<br>';

        $isValid = $appleSignInPayload->verifyUser($user);

        echo 'is valid : ';
        var_dump($isValid);
        echo '<br>';

        $clientId = 'net.example.oauth';

        $teamId = 'TT123';
        $keyId = 'KK123';

        $code = Input::post('code');

        echo 'Code: ' . $code . '<br>';

        $claims = [
            'iss' => $teamId,
            'aud' => 'https://appleid.apple.com',
            'sub' => $clientId,
            'iat' => time(),
            'exp' => time() + 3600,
        ];

        $headers = ['kid' => $keyId, 'alg' => 'ES256'];

        $privateKey = <<<EOD-----BEGIN PRIVATE KEY-----key goes here-----END PRIVATE KEY-----EOD;

        $publicKey = <<<EOD-----BEGIN PUBLIC KEY-----key goes here-----END PUBLIC KEY-----EOD;

        $client_secret = JWT::encode($claims, $privateKey, 'ES256', $keyId, $headers);
        //   var_dump($client_secret);

        $decoded = JWT::decode($client_secret, $publicKey, ['ES256']);
        //   var_dump($decoded);

        $ch = curl_init();

        $data = [
            'client_id' => $clientId, // app id?
            'code' => $code, //from request
            'client_secret' => $client_secret,
            'grant_type' => 'authorization_code',
            'redirect_uri' => 'https://example.net/appletest'
        ];

        curl_setopt($ch, CURLOPT_URL, "https://appleid.apple.com/auth/token");
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $server_output = curl_exec($ch);

        curl_close($ch);

        $response = json_decode($server_output, true);

        if ($response['access_token']) {
            var_dump($response);

            $appleSignInPayload = ASDecoder::getAppleSignInPayload($response['id_token']);
        }

Stack overflow requires more details to the code. So I can add that it uses AppleSignIn\ASDecoder and \Firebase\JWT\JWT libraries. Also I've discovered that in the backend you have to supply same redirect_uri as on the frontend, otherwise you get {"invalid_grant"} error. Hope it saves someone a few hours.

If someone in trouble reads this, here is how to extract public key out of .p8 file: openssl ec -in AuthKey_KEY_ID.p8 -pubout -out AuthKey_KEY_ID_Public.p8

UPD: Seems it's true that name can be obtained only the first time. I've tested it with a friend and got a response.

array(3) { 
["code"]=> string(64) "c94b9775110randoma918bc357.0.nsqty.covC4GSS1e2O4..." 
["id_token"]=> string(766) "eyJraWQiOiJlWGF1bm1MIiwiYWrandomyNTYifQ.eyJpc3MiOiJodHRwczovL2FwcGxlaW..." 
["user"]=> string(82) "{"email":"e.g@example.net","name":{"firstName":"Eeee","lastName":"Ggg"}}" } 

UPD 2: It's possible to delete application using the mobile phone in Settings->Passwords & Security->Apps Using Your Apple ID. After the deletion user data will be posted again. But it's only good for testing updating user data on production still requires profile update over time.

UPD 3: It's possible to unbind the App from your apple account not only by phone, but also on the https://appleid.apple.com/account/manage website in the Security->APPS & WEBSITES USING APPLE ID->Manage section

Source