Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make lib more performant by allowing to provide local key objects and shared secrets #222

Closed
wants to merge 15 commits into from

Conversation

marcvdm
Copy link
Contributor

@marcvdm marcvdm commented Mar 21, 2019

This pull request adds the ability to generate the local keys before hand and provide them to the WebPush client. This way it does not generate a new local key object for each notification.

This also opens the possibility to generate the shared secret before hand so that this is also skipped when you send the notification.

Here is an example:

Generate the local public and private key and store them

use Jose\Component\Core\Util\Ecc\NistCurve;
use Minishlink\WebPush\Encryption;
use Minishlink\WebPush\Utils;
use Base64Url\Base64Url;

[$localPublicKeyObject, $localPrivateKeyObject] = Encryption::createLocalKeyObject();

// Store these variables
$localPublicKey = Base64Url::encode(Utils::serializePublicKey($localPublicKeyObject));
$localPrivateKey = Base64Url::encode(Utils::serializePrivateKey($localPrivateKeyObject));

When a user subscribes we generate the shared secret and store it with all the other info

use Minishlink\WebPush\Encryption;
use Minishlink\WebPush\Utils;
use Base64Url\Base64Url;

$localPrivateKey = Base64Url::decode('storedLocalPrivateKey');
$userPublicKey = "TheUsersPublicKey";

// Store the shared secret with the subscription
$sharedSecret = Encryption::createSharedSecret($userPublicKey, Utils::unserializePrivateKey($localPrivateKey));

When you want to send a notification

use Minishlink\WebPush\Encryption;
use Minishlink\WebPush\Utils;
use Base64Url\Base64Url;

$localPublicKey = 'storedLocalPublicKey';
$localPrivateKey = 'storedLocalPrivateKey';

$webPush = new WebPush();
$webPush->setLocalKeys($localPublicKey, $localPrivateKey);

$endpoint = 'endpoint'; // From your data store
$publicKey = 'publicKey'; // From your data store
$authToken = 'authToken'; // From your data store
$contentEncoding= 'contentEncoding'; // From your data store
$sharedSecret = 'sharedSecret'; // From your data store, previously created

$subscriptions = new Subscription($endpoint, $publicKey, $authToken, $contentEncoding, $sharedSecret);

$webPush->sendNotification($subscription, '{"test":"payload"}');

Copy link
Member

@Minishlink Minishlink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks cool!
Can you rebase and fix the static errors please ?

@marcvdm
Copy link
Contributor Author

marcvdm commented Mar 25, 2019

Fixed the static errors, is it possible for you to rebase and merge pull request?

@marcvdm
Copy link
Contributor Author

marcvdm commented Apr 2, 2019

Pinging @Minishlink Can we move forward with this? I'm unable to rebase the current merge request

@Minishlink
Copy link
Member

@marcvdm Thanks Marc, I'll work on it probably this week-end

@marcvdm
Copy link
Contributor Author

marcvdm commented Apr 19, 2019

Closing this merge request for the rebased version #227

@marcvdm marcvdm closed this Apr 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants