Skip to content

Instantly share code, notes, and snippets.

View Tom4259's full-sized avatar

Tom Tom4259

View GitHub Profile
const jwt = require('jsonwebtoken');
const http2 = require('http2');
const fs = require('fs');
/*
Read p8 file. Assumes p8 file to be in same directory
*/
const key = fs.readFileSync(__dirname + "/{your p8 filename}.p8", 'utf8')
//"iat" should not be older than 1 hr from current time or will get rejected
@marcoarment
marcoarment / apns_jwt_token.php
Last active June 18, 2025 14:36
Generate ES256 JWT tokens for Apple Push Notification Service (APNS) in PHP
<?php
function base64url_encode($binary_data) { return strtr(rtrim(base64_encode($binary_data), '='), '+/', '-_'); }
function apns_jwt_token($team_id, $key_id, $private_key_pem_str)
{
if (! function_exists('openssl_get_md_methods') || ! in_array('sha256', openssl_get_md_methods())) throw new Exception('Requires openssl with sha256 support');
$private_key = openssl_pkey_get_private($private_key_pem_str);
if (! $private_key) throw new Exception('Cannot decode private key');