Skip to content

Commit f6312ae

Browse files
committed
update headers to latest IETF draft and encode in URL safe base64
1 parent 02f5061 commit f6312ae

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"php": ">=5.4",
1717
"kriswallsmith/buzz": ">=0.6",
1818
"mdanter/ecc": "^0.3.0",
19-
"lib-openssl": "*"
19+
"lib-openssl": "*",
20+
"spomky-labs/base64url": "^1.0"
2021
},
2122
"require-dev": {
2223
"phpunit/phpunit": "4.8.*",

src/WebPush.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Minishlink\WebPush;
1313

14+
use Base64Url\Base64Url;
1415
use Buzz\Browser;
1516
use Buzz\Client\AbstractClient;
1617
use Buzz\Client\MultiCurl;
@@ -177,7 +178,7 @@ public function flush()
177178
}
178179

179180
/**
180-
* @param string $userPublicKey base 64 encoded
181+
* @param string $userPublicKey MIME base 64 encoded
181182
* @param string $payload
182183
*
183184
* @return array
@@ -193,7 +194,7 @@ private function encrypt($userPublicKey, $payload)
193194
// get local key pair
194195
$localPrivateKeyObject = $curveGenerator->createPrivateKey();
195196
$localPublicKeyObject = $localPrivateKeyObject->getPublicKey();
196-
$localPublicKey = base64_encode(hex2bin($keySerializer->serialize($localPublicKeyObject->getPoint())));
197+
$localPublicKey = hex2bin($keySerializer->serialize($localPublicKeyObject->getPoint()));
197198

198199
// get user public key object
199200
$userPublicKeyObject = new PublicKey($math, $curveGenerator, $keySerializer->unserialize($curve, bin2hex(base64_decode($userPublicKey))));
@@ -216,10 +217,11 @@ private function encrypt($userPublicKey, $payload)
216217
$cipherText = openssl_encrypt($payload, 'aes-128-gcm', $encryptionKey, false, $iv); // base 64 encoded
217218
}
218219

220+
// return values in url safe base64
219221
return array(
220-
'localPublicKey' => $localPublicKey,
221-
'salt' => base64_encode($salt),
222-
'cipherText' => base64_encode($cipherText),
222+
'localPublicKey' => Base64Url::encode($localPublicKey),
223+
'salt' => Base64Url::encode($salt),
224+
'cipherText' => Base64Url::encode($cipherText),
223225
);
224226
}
225227

@@ -237,9 +239,9 @@ private function sendToStandardEndpoints(array $notifications)
237239
$headers = array(
238240
'Content-Length' => strlen($encrypted['cipherText']),
239241
'Content-Type' => 'application/octet-stream',
240-
'Content-Encoding' => 'aesgcm-128',
242+
'Content-Encoding' => 'aesgcm128',
241243
'Encryption' => 'keyid="p256dh";salt="'.$encrypted['salt'].'"',
242-
'Encryption-Key' => 'keyid="p256dh";dh="'.$encrypted['localPublicKey'].'"',
244+
'Crypto-Key' => 'keyid="p256dh";dh="'.$encrypted['localPublicKey'].'"',
243245
'TTL' => $this->TTL,
244246
);
245247

0 commit comments

Comments
 (0)