Last active
September 24, 2024 14:37
-
-
Save Spomky/246eca6aaeeb7a40f11d3a2d98960282 to your computer and use it in GitHub Desktop.
Generate JWT for Apple Push Notification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'vendor/autoload.php'; | |
use Jose\Factory\JWKFactory; | |
use Jose\Factory\JWSFactory; | |
$key_file = '/PATH/TO/KEY/FILE'; | |
$secret = null; // If the key is encrypted, the secret must be set in this variable | |
$private_key = JWKFactory::createFromKeyFile($key_file, $secret, [ | |
'kid' => 'ABC123DEFG', // The Key ID obtained from your developer account | |
'alg' => 'ES256', // Not mandatory but recommended | |
'use' => 'sig', // Not mandatory but recommended | |
]); | |
var_dump('We show our private EC key'); | |
var_dump(json_encode($private_key)); | |
var_dump('We prepare the payload (claims to sign).'); | |
$payload = [ | |
'iss' => 'DEF123GHIJ', | |
'iat' => time(), | |
]; | |
var_dump($payload); | |
var_dump('We prepare the protected header.'); | |
$header = [ | |
'alg' => 'ES256', | |
'kid' => $private_key->get('kid'), | |
]; | |
var_dump($header); | |
var_dump('We create our JWS using the private key, the payload and the header'); | |
$jws = JWSFactory::createJWSToCompactJSON( | |
$payload, | |
$private_key, | |
$header | |
); | |
var_dump('We show our JWS.'); | |
var_dump($jws); | |
/***************************************/ | |
/* Same code as above without comments */ | |
/***************************************/ | |
$key_file = '/PATH/TO/KEY/FILE'; | |
$secret = null; | |
$private_key = JWKFactory::createFromKeyFile($key_file, $secret, ['kid' => 'ABC123DEFG', alg' => 'ES256', 'use' => 'sig']); | |
$payload = ['iss' => 'DEF123GHIJ', 'iat' => time()]; | |
$header = ['alg' => 'ES256', 'kid' => $private_key->get('kid')]; | |
$jws = JWSFactory::createJWSToCompactJSON($payload, $private_key, $header); |
I have a valid certificate cert.p12
exported from Mac Keychain. How can I know private and public key for alg ES256
?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am having the error "Unable to load the key" using a .p8 file