Skip to content

Commit 2da80d3

Browse files
committed
jose optional
1 parent c87be97 commit 2da80d3

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
## Installation
88
`composer require minishlink/web-push`
99

10+
If you have a PHP version smaller than 5.5.9, you will not be able to send any payload.
11+
12+
If you have a PHP version smaller than 7.1, you will have to `composer require spomky-labs/jose:2.0.x-dev`,
13+
and if you want to speed things up, install the [PHP Crypto](https://github.com/bukka/php-crypto) extension.
14+
1015
## Usage
1116
WebPush can be used to send notifications to endpoints which server delivers web push notifications as described in
1217
the [Web Push API specification](http://www.w3.org/TR/push-api/).
@@ -140,7 +145,8 @@ Not until the [Push API spec](http://www.w3.org/TR/push-api/) is finished.
140145
### What about security?
141146
Internally, WebPush uses the [phpecc](https://github.com/phpecc/phpecc) Elliptic Curve Cryptography library to create
142147
local public and private keys and compute the shared secret.
143-
Then, WebPush uses `openssl` in order to encrypt the payload with the encryption key.
148+
Then, if you have a PHP >= 7.1, WebPush uses `openssl` in order to encrypt the payload with the encryption key.
149+
It uses [jose](https://github.com/Spomky-Labs/jose) if you have PHP < 7.1, which is slower.
144150

145151
### How to solve "SSL certificate problem: unable to get local issuer certificate" ?
146152
Your installation lacks some certificates.

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
"php": ">=5.4",
1717
"kriswallsmith/buzz": ">=0.6",
1818
"mdanter/ecc": "^0.3.0",
19-
"lib-openssl": "*",
20-
"spomky-labs/jose": "2.0.x-dev"
19+
"lib-openssl": "*"
2120
},
2221
"require-dev": {
23-
"phpunit/phpunit": "4.8.*"
22+
"phpunit/phpunit": "4.8.*",
23+
"spomky-labs/jose": "2.0.x-dev"
24+
},
25+
"suggest": {
26+
"spomky-labs/jose:2.0.x-dev": "Payload support if you have 5.5.9 <= PHP version < 7.1"
2427
},
2528
"autoload": {
2629
"psr-4" : {

src/WebPush.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ class WebPush
4040
/** @var int Time To Live of notifications */
4141
private $TTL;
4242

43+
/** @var boolean */
44+
private $payloadEncryptionSupport;
45+
46+
/** @var boolean */
47+
private $nativePayloadEncryptionSupport;
48+
4349
/**
4450
* WebPush constructor.
4551
*
@@ -56,6 +62,9 @@ public function __construct(array $apiKeys = array(), $TTL = 2419200, $timeout =
5662
$client = isset($client) ? $client : new MultiCurl();
5763
$client->setTimeout($timeout);
5864
$this->browser = new Browser($client);
65+
66+
$this->payloadEncryptionSupport = version_compare(phpversion(), '5.5.9', '>=');
67+
$this->nativePayloadEncryptionSupport = version_compare(phpversion(), '7.1', '>=');
5968
}
6069

6170
/**
@@ -186,11 +195,11 @@ private function encrypt($userPublicKey, $payload)
186195
$salt = openssl_random_pseudo_bytes(16);
187196

188197
// get encryption key
189-
$encryptionKey = hash_hmac('sha256', $salt, $sharedSecret);
198+
$encryptionKey = hash_hmac('sha256', $salt, $sharedSecret, true);
190199

191200
// encrypt
192201
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-128-gcm'));
193-
if (phpversion() < 7.1) {
202+
if (!$this->nativePayloadEncryptionSupport) {
194203
list($encryptedText, $tag) = GCM::encrypt($encryptionKey, $iv, $payload, "");
195204
$cipherText = $encryptedText.$tag;
196205
} else {
@@ -200,7 +209,7 @@ private function encrypt($userPublicKey, $payload)
200209
return array(
201210
'localPublicKey' => $localPublicKey,
202211
'salt' => base64_encode($salt),
203-
'cipherText' => $cipherText,
212+
'cipherText' => base64_encode($cipherText),
204213
);
205214
}
206215

@@ -212,15 +221,15 @@ private function sendToStandardEndpoints(array $notifications)
212221
$payload = $notification->getPayload();
213222
$userPublicKey = $notification->getUserPublicKey();
214223

215-
if (isset($payload) && isset($userPublicKey)) {
224+
if (isset($payload) && isset($userPublicKey) && $this->payloadEncryptionSupport) {
216225
$encrypted = $this->encrypt($userPublicKey, $payload);
217226

218227
$headers = array(
219228
'Content-Length' => strlen($encrypted['cipherText']),
220229
'Content-Type' => 'application/octet-stream',
221-
'Encryption-Key' => 'keyid=p256dh;dh='.$encrypted['localPublicKey'],
222-
'Encryption' => 'keyid=p256dh;salt='.$encrypted['salt'],
223-
'Content-Encoding' => 'aesgcm128',
230+
'Content-Encoding' => 'aesgcm-128',
231+
'Encryption' => 'keyid="p256dh";salt="'.$encrypted['salt'].'"',
232+
'Encryption-Key' => 'keyid="p256dh";dh="'.$encrypted['localPublicKey'].'"',
224233
'TTL' => $this->TTL,
225234
);
226235

0 commit comments

Comments
 (0)