AppleTokenService.php 846 B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. namespace App\Services;
  3. use Carbon\CarbonImmutable;
  4. use Lcobucci\JWT\Configuration;
  5. use Lcobucci\JWT\Signer\Ecdsa\Sha256;
  6. use Lcobucci\JWT\Signer\Key\InMemory;
  7. class AppleTokenService
  8. {
  9. public function generate($privateKey, $clientId, $teamId, $keyId): string
  10. {
  11. $jwtConfig = Configuration::forSymmetricSigner(
  12. new Sha256(),
  13. InMemory::plainText($privateKey)
  14. );
  15. $now = CarbonImmutable::now();
  16. $token = $jwtConfig->builder()
  17. ->issuedBy($teamId)
  18. ->issuedAt($now)
  19. ->expiresAt($now->addHour())
  20. ->permittedFor('https://appleid.apple.com')
  21. ->relatedTo($clientId)
  22. ->withHeader('kid', $keyId)
  23. ->getToken($jwtConfig->signer(), $jwtConfig->signingKey());
  24. return $token->toString();
  25. }
  26. }