pushok icon indicating copy to clipboard operation
pushok copied to clipboard

Support for creating WeatherKit JWT tokens

Open NPAssoc opened this issue 3 years ago • 1 comments

The WeatherKit REST interface requires JWT tokens, similar to to those required for push notifications. I was wondering if a class for generating those could be added to this package without too much difficulty.

NPAssoc avatar Jul 22 '22 12:07 NPAssoc

While this should be easily possible, it has nothing really in common with this library.

You could write an implementation like this:

...
$privateKey = file_get_contents(realpath(dirname(__FILE__)).'/your_cert.p8');
$keyId = 'YOUR_KEY_ID';
$time = time();
$expires = $time + 3600;

// Create a token
$payload = [
'iss' => $teamId,            // Team ID
'iat' => $time,         // Issued-at time
'exp' => $expires,   // Expiration time
'sub' => $serviceId,       // Service ID
];

// Create the header
$header = [
'alg' => 'ES256',   // Algorithm (ES256)
'kid' => $keyId,
'id' => $teamId.'.'.$serviceId, // 10-character key identifier
];

$token = \Firebase\JWT\JWT::encode($payload, $privateKey, 'ES256', null, $header);

nikischin avatar Sep 24 '24 17:09 nikischin