Skip to content

Commit 7fa75d1

Browse files
committed
add createVapidKeys helper
1 parent dd9d225 commit 7fa75d1

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ $webPush->sendNotification(
6868
There are several good examples and tutorials on the web:
6969
* Mozilla's [ServiceWorker Cookbooks](https://serviceworke.rs/push-payload_index_doc.html) (don't mind the `server.js` file: it should be replaced by your PHP server code with this library)
7070
* Google's [introduction to push notifications](https://developers.google.com/web/fundamentals/getting-started/push-notifications/) (as of 03-20-2016, it doesn't mention notifications with payload)
71-
* you may want to take a look at my own implementation: [sw.js](https://github.com/Minishlink/physbook/blob/2ed8b9a8a217446c9747e9191a50d6312651125d/web/service-worker.js) and [app.js](https://github.com/Minishlink/physbook/blob/d6855ca8f485556ab2ee5c047688fbf745367045/app/Resources/public/js/app.js)
71+
* you may want to take a look at my own implementation: [sw.js](https://github.com/Minishlink/physbook/blob/02a0d5d7ca0d5d2cc6d308a3a9b81244c63b3f14/web/service-worker.js) and [app.js](https://github.com/Minishlink/physbook/blob/02a0d5d7ca0d5d2cc6d308a3a9b81244c63b3f14/app/Resources/public/js/app.js)
7272

73-
### Authentication
73+
### Authentication (VAPID)
7474
Browsers need to verify your identity. A standard called VAPID can authenticate you for all browsers. You'll need to create and provide a public and private key for your server.
7575

7676
You can specify your authentication details when instantiating WebPush. The keys can be passed directly, or you can load a PEM file or its content:
@@ -103,6 +103,19 @@ $ openssl ec -in private_key.pem -pubout -outform DER|tail -c 65|base64|tr -d '=
103103
$ openssl ec -in private_key.pem -outform DER|tail -c +8|head -c 32|base64|tr -d '=' |tr '/+' '_-' >> private_key.txt
104104
```
105105

106+
If you can't access a Linux bash, you can print the output of the `createVapidKeys` function:
107+
```php
108+
var_dump(VAPID::createVapidKeys()); // store the keys afterwards
109+
```
110+
111+
On the client-side, don't forget to subscribe with the VAPID public key as the `applicationServerKey`: (`urlBase64ToUint8Array` source [here](https://github.com/Minishlink/physbook/blob/02a0d5d7ca0d5d2cc6d308a3a9b81244c63b3f14/app/Resources/public/js/app.js#L177))
112+
```js
113+
serviceWorkerRegistration.pushManager.subscribe({
114+
userVisibleOnly: true,
115+
applicationServerKey: urlBase64ToUint8Array(vapidPublicKey)
116+
})
117+
```
118+
106119
### Notification options
107120
Each notification can have a specific Time To Live, urgency, and topic.
108121
You can change the default options with `setDefaultOptions()` or in the constructor:

src/VAPID.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,21 @@ public static function getVapidHeaders($audience, $subject, $publicKey, $private
131131
'Crypto-Key' => 'p256ecdsa='.Base64Url::encode($publicKey),
132132
);
133133
}
134+
135+
136+
/**
137+
* This method creates VAPID keys in case you would not be able to have a Linux bash.
138+
* DO NOT create keys at each initialization! Save those keys and reuse them.
139+
*
140+
* @return array
141+
*/
142+
public static function createVapidKeys()
143+
{
144+
$privateKeyObject = EccFactory::getNistCurves()->generator256()->createPrivateKey();
145+
$pointSerializer = new UncompressedPointSerializer(EccFactory::getAdapter());
146+
$vapid['publicKey'] = base64_encode(hex2bin($pointSerializer->serialize($privateKeyObject->getPublicKey()->getPoint())));
147+
$vapid['privateKey'] = base64_encode(hex2bin(gmp_strval($privateKeyObject->getSecret(), 16)));
148+
149+
return $vapid;
150+
}
134151
}

tests/VAPIDTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,13 @@ private function explodeAuthorization($auth)
5858
array_pop($auth); // delete the signature which changes each time
5959
return $auth;
6060
}
61+
62+
public function testCreateVapidKeys()
63+
{
64+
$keys = VAPID::createVapidKeys();
65+
$this->assertArrayHasKey('publicKey', $keys);
66+
$this->assertArrayHasKey('privateKey', $keys);
67+
$this->assertEquals(strlen($keys['publicKey']), 88);
68+
$this->assertEquals(strlen($keys['privateKey']), 44);
69+
}
6170
}

0 commit comments

Comments
 (0)