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); |
Do you allow me to use this key in a new test case for the library?
Yes, I do =)
Has a solution to the issue @edamov listed above been found? I am getting a similar "Unable to load the key" error. I'm not sure if my issue starts like @edamov's did with the openssl needing to be upgraded or not. Any ideas?
- in Assertion.php line 275
- at Assertion::createException(true, 'Unable to load the key', '38', null) in Assertion.php line 1644
- at Assertion::false(true, 'Unable to load the key') in KeyConverter.php line 157
- at KeyConverter::loadKeyFromPEM('-----BEGIN PRIVATE KEY----- private key -----END PRIVATE KEY-----', null) in KeyConverter.php line 120
- at KeyConverter::loadFromKey('-----BEGIN PRIVATE KEY----- private key -----END PRIVATE KEY-----', null) in KeyConverter.php line 104
- at KeyConverter::loadFromKeyFile('APNsAuthKey.p8', null) in JWKFactory.php line 161
- at JWKFactory::createFromKeyFile('APNsAuthKey.p8', null, array('kid' => 'key_id', 'alg' => 'ES256', 'use' => 'sig')) in ApiSupportController.php line 330
I am having the error "Unable to load the key" using a .p8 file
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
Many thanks.
I think I have found the error. It seems it comes from my library which is not fully compliant with the RFC5915
In the Elliptic Curve Private Key Format, the parameters are optional, however my library reject private keys without parameters.
I will try to push a fix today.
Stay tuned.