push
object
%push_subscription
/phlo/resources/push/push.subscription.phlo
static
push_subscription :: table:string
line 10
'push_subscriptions'static
push_subscription :: key (string $endpoint):string
line 12
substr(hash('sha256', $endpoint), 0, 40)static
push_subscription :: subscribe (array|object $subscription, string $owner = void):?static
line 14
$s = json_decode(json_encode($subscription), true)
$endpoint = (string)($s['endpoint'] ?? void)
$p256dh = (string)($s['keys']['p256dh'] ?? void)
$auth = (string)($s['keys']['auth'] ?? void)
if (!str_starts_with($endpoint, 'https://') || strlen(WebPush::decode($p256dh)) !== 65 || strlen(WebPush::decode($auth)) !== 16) return null
static::delete('id=?', $id = static::key($endpoint))
return static::create(id: $id, owner: $owner, endpoint: $endpoint, p256dh: $p256dh, auth: $auth, created: time())object
%WebPush
/phlo/resources/push/WebPush.phlo
static
WebPush :: publicOnly:bool
line 9
truestatic
WebPush :: encode (string $raw):string
line 11
rtrim(strtr(base64_encode($raw), '+/', '-_'), '=')static
WebPush :: decode (string $text):string
line 12
(string)base64_decode(strtr($text, '-_', '+/'))static
WebPush :: publicKey:string
line 14
(string)(%creds->webpush->public_key ?? void)static
WebPush :: keys:array
line 17
A new pair for [webpush] of the creds: public_key and private_key, base64url.
$pair = static::pair()
return ['public_key' => static::encode($pair['public']), 'private_key' => static::encode($pair['private'])]static
WebPush :: pair:array
line 23
A P-256 pair as raw bytes: the uncompressed public point and the private scalar.
$ec = openssl_pkey_get_details(openssl_pkey_new(['curve_name' => 'prime256v1', 'private_key_type' => OPENSSL_KEYTYPE_EC]))['ec']
$pad = fn($part) => str_pad($part, 32, chr(0), STR_PAD_LEFT)
return ['public' => chr(4).$pad($ec['x']).$pad($ec['y']), 'private' => $pad($ec['d'])]static
WebPush :: pem (string $label, string $der):string
line 29
"-----BEGIN $label-----\n".chunk_split(base64_encode($der), 64, "\n")."-----END $label-----\n"static
WebPush :: publicPem (string $point):string
line 30
static::pem('PUBLIC KEY', hex2bin('3059301306072a8648ce3d020106082a8648ce3d030107034200').$point)static
WebPush :: privatePem (string $private, string $point):string
line 31
static::pem('EC PRIVATE KEY', hex2bin('30770201010420').$private.hex2bin('a00a06082a8648ce3d030107a144034200').$point)static
WebPush :: encrypt (string $payload, string $p256dh, string $auth, ?string $salt = null, ?array $sender = null):string
line 35
The body of RFC 8291 for the keys of a browser: one aes128gcm record of 4096 with a key of its own for this message.
strlen($payload) <= 3993 || error('WebPush: a message is at most 3993 bytes', 413)
$ua = static::decode($p256dh)
$sender ??= static::pair()
$salt ??= random_bytes(16)
$secret = openssl_pkey_derive(static::publicPem($ua), static::privatePem($sender['private'], $sender['public']))
$ikm = hash_hkdf('sha256', $secret, 32, 'WebPush: info'.chr(0).$ua.$sender['public'], static::decode($auth))
$cek = hash_hkdf('sha256', $ikm, 16, 'Content-Encoding: aes128gcm'.chr(0), $salt)
$nonce = hash_hkdf('sha256', $ikm, 12, 'Content-Encoding: nonce'.chr(0), $salt)
$cipher = openssl_encrypt($payload.chr(2), 'aes-128-gcm', $cek, OPENSSL_RAW_DATA, $nonce, $tag)
return $salt.pack('N', 4096).chr(65).$sender['public'].$cipher.$tagstatic
WebPush :: vapid (string $endpoint):string
line 49
The Authorization of RFC 8292 for a push service: a JWT for its origin, for twelve hours, signed with ES256.
$url = parse_url($endpoint)
$claims = ['aud' => $url['scheme'].'://'.$url['host'].(isset($url['port']) ? colon.$url['port'] : void), 'exp' => time() + 43200, 'sub' => (string)(%creds->webpush->subject ?? void)]
$jwt = static::encode(json_encode(['typ' => 'JWT', 'alg' => 'ES256'])).dot.static::encode(json_encode($claims, JSON_UNESCAPED_SLASHES))
$pem = static::privatePem(static::decode((string)(%creds->webpush->private_key ?? void)), static::decode(static::publicKey()))
openssl_sign($jwt, $der, $pem, OPENSSL_ALGO_SHA256) || error('WebPush: the keys in [webpush] do not sign')
return 'vapid t='.$jwt.dot.static::encode(static::signature($der)).', k='.static::publicKey()static
WebPush :: signature (string $der):string
line 59
An ECDSA signature as openssl gives it, DER, as the 64 bytes of r and s a JWT carries.
$r = substr($der, 4, $length = ord($der[3]))
$s = substr($der, 6 + $length, ord($der[5 + $length]))
return str_pad(ltrim($r, chr(0)), 32, chr(0), STR_PAD_LEFT).str_pad(ltrim($s, chr(0)), 32, chr(0), STR_PAD_LEFT)static
WebPush :: send (push_subscription $to, string|array $payload, int $ttl = 86400, ?string $urgency = null, ?string $topic = null):bool
line 66
One message to one browser: whether the push service took it.
$body = static::encrypt(is_string($payload) ? $payload : json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), $to->p256dh, $to->auth)
$headers = array_values(array_filter(['Authorization: '.static::vapid($to->endpoint), 'Content-Encoding: aes128gcm', 'Content-Type: application/octet-stream', 'TTL: '.$ttl, $urgency ? 'Urgency: '.$urgency : null, $topic ? 'Topic: '.$topic : null]))
try {
HTTP($to->endpoint, $headers, POST: $body, timeout: 15, response: $response, public: static::$publicOnly)
}
catch (\Throwable $e){
return false
}
in_array($response->status, [404, 410], true) && push_subscription::delete('id=?', $to->id)
return $response->okstatic
WebPush :: toOwner (string $owner, string|array $payload, int $ttl = 86400):int
line 79
count(array_filter(push_subscription::records(owner: $owner), fn($to) => static::send($to, $payload, $ttl)))Last updated on 23 November 2026