Core\WebPush::applicationServerKey
Core\WebPush::applicationServerKey(Core\Crypto\KeyPair $vapid): stringCore\WebPush::applicationServerKey( Core\Crypto\KeyPair $vapid, ): string
Returns the public half of your VAPID key as base64url text. The browser needs this text before it can create a push subscription.
A VAPID key is a key pair that identifies your server to the push services of the browsers. It is a Core\Crypto\KeyPair of kind P256. Create it once and store it. The page passes the text from this method to pushManager.subscribe() as applicationServerKey. Later, Core\WebPush::send signs every message with the same key pair. A message signed with another key is not delivered.
The text is 87 characters long, and it contains only the public key. The private half stays on your server. A key pair of another kind throws a LogicError, and the error message does not contain the key.
The examples show the length of the text, a route that gives the text to the page, and a key of the wrong kind.
Parameters
$vapidCore\Crypto\KeyPairYour application's VAPID key: aCore\Crypto\KeyPairof kindP256. Create it once withCore\Crypto::generateKeyPair, keep it secret, and use the same key for every message.
Return value
Returns string. The public key: 87 characters of base64url, such as BCVxsr7N_eNg….
Errors
LogicError$vapidis not a key pair of kindP256.
Examples
The text for the browser
<?nvs// Shows how to get the public key that the browser needs for push messages.
Core\Crypto\KeyPair $vapid = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::P256);string $public = Core\WebPush::applicationServerKey($vapid);
// The key is random, so the text is different on every run. Its length is always 87.echo 'length: ', Core\Str::length($public), "\n";
// The same key pair always gives the same text. This prints `yes`.echo 'the same text again: ', ($public == Core\WebPush::applicationServerKey($vapid) ? 'yes' : 'no'), "\n";length: 87the same text again: yesA route for the page
<?nvs// Shows a route that sends the public key to the page as JSON.
// The server stores its key pair once and reads it at every start.// A real server reads this text from a file, not from the program.string $pem = "-----BEGIN PRIVATE KEY-----\n" . "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg5r670zQwShttDPdB\n" . "D/RglrBxryiWxCoC4NICQrtumOChRANCAAS3WhLrnL6ASulayXljHGKKpTO5KsAc\n" . "GA5ETpt3P1JHea8QXI+Jf1N8+WFZRFPD+2/47qMfZolauZJrU9ZO4SdU\n" . "-----END PRIVATE KEY-----\n";Core\Crypto\KeyPair $vapid = Core\Crypto\KeyPair::read($pem as bytes, Core\Crypto\KeyKind::P256);
// The page fetches this route. Then it passes the key to `pushManager.subscribe()`// as `applicationServerKey`. The response contains only the public key.Core\Response::sendJson({ applicationServerKey: Core\WebPush::applicationServerKey($vapid) });{"applicationServerKey":"BLdaEuucvoBK6VrJeWMcYoqlM7kqwBwYDkROm3c_Ukd5rxBcj4l_U3z5YVlEU8P7b_juox9miVq5kmtT1k7hJ1Q"}A key of the wrong kind
<?nvs// Shows what happens when the key pair is not of kind `P256`.
// An `Ed25519` key pair can sign, but push services accept only `P256` keys.Core\Crypto\KeyPair $wrong = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::Ed25519);
// So `applicationServerKey` throws a `LogicError`. This prints its message.try { echo Core\WebPush::applicationServerKey($wrong), "\n";} catch (LogicError $error) { echo $error->message, "\n";}
// A new `P256` key pair works.Core\Crypto\KeyPair $vapid = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::P256);echo 'length: ', Core\Str::length(Core\WebPush::applicationServerKey($vapid)), "\n";Core\WebPush::applicationServerKey(): $vapid is not a key pair of kind `P256`.length: 87