Skip to content

Commit 07bd808

Browse files
committed
auth can be customized per notification, close #58
1 parent d914e9b commit 07bd808

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ Your installation lacks some certificates.
249249

250250
You can also force using a client without peer verification.
251251

252+
### I lost my VAPID keys!
253+
See [issue #58](https://github.com/web-push-libs/web-push-php/issues/58).
254+
252255
### I need to send notifications to native apps. (eg. APNS for iOS)
253256
WebPush is for web apps.
254257
You need something like [RMSPushNotificationsBundle](https://github.com/richsage/RMSPushNotificationsBundle) (Symfony).

src/Notification.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,20 @@ class Notification
2525
/** @var string */
2626
private $userAuthToken;
2727

28-
/** @var array Options : TTL, urgency, topic * */
28+
/** @var array Options : TTL, urgency, topic */
2929
private $options;
3030

31-
public function __construct($endpoint, $payload, $userPublicKey, $userAuthToken, $options)
31+
/** @var array Auth details : GCM, VAPID */
32+
private $auth;
33+
34+
public function __construct($endpoint, $payload, $userPublicKey, $userAuthToken, $options, $auth)
3235
{
3336
$this->endpoint = $endpoint;
3437
$this->payload = $payload;
3538
$this->userPublicKey = $userPublicKey;
3639
$this->userAuthToken = $userAuthToken;
3740
$this->options = $options;
41+
$this->auth = $auth;
3842
}
3943

4044
/**
@@ -83,4 +87,14 @@ public function getOptions(array $defaultOptions = array())
8387

8488
return $options;
8589
}
90+
91+
/**
92+
* @param array $defaultAuth
93+
*
94+
* @return array
95+
*/
96+
public function getAuth(array $defaultAuth)
97+
{
98+
return count($this->auth) > 0 ? $this->auth : $defaultAuth;
99+
}
86100
}

src/VAPID.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ public static function getVapidHeaders($audience, $subject, $publicKey, $private
134134
);
135135
}
136136

137-
138137
/**
139138
* This method creates VAPID keys in case you would not be able to have a Linux bash.
140139
* DO NOT create keys at each initialization! Save those keys and reuse them.
@@ -152,7 +151,7 @@ private static function getUncompressedKeys(PrivateKeyInterface $privateKeyObjec
152151
{
153152
$pointSerializer = new UncompressedPointSerializer(EccFactory::getAdapter());
154153
$vapid['publicKey'] = base64_encode(hex2bin($pointSerializer->serialize($privateKeyObject->getPublicKey()->getPoint())));
155-
$vapid['privateKey'] = base64_encode(hex2bin(str_pad(gmp_strval($privateKeyObject->getSecret(), 16), 2*self::PRIVATE_KEY_LENGTH, '0', STR_PAD_LEFT)));
154+
$vapid['privateKey'] = base64_encode(hex2bin(str_pad(gmp_strval($privateKeyObject->getSecret(), 16), 2 * self::PRIVATE_KEY_LENGTH, '0', STR_PAD_LEFT)));
156155

157156
return $vapid;
158157
}

src/WebPush.php

+12-6
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,14 @@ public function __construct(array $auth = array(), $defaultOptions = array(), $t
7373
* @param string|null $userAuthToken
7474
* @param bool $flush If you want to flush directly (usually when you send only one notification)
7575
* @param array $options Array with several options tied to this notification. If not set, will use the default options that you can set in the WebPush object
76+
* @param array $auth Use this auth details instead of what you provided when creating WebPush
7677
*
7778
* @return array|bool Return an array of information if $flush is set to true and the queued requests has failed.
7879
* Else return true
7980
*
8081
* @throws \ErrorException
8182
*/
82-
public function sendNotification($endpoint, $payload = null, $userPublicKey = null, $userAuthToken = null, $flush = false, $options = array())
83+
public function sendNotification($endpoint, $payload = null, $userPublicKey = null, $userAuthToken = null, $flush = false, $options = array(), $auth = array())
8384
{
8485
if (isset($payload)) {
8586
if (Utils::safeStrlen($payload) > Encryption::MAX_PAYLOAD_LENGTH) {
@@ -89,7 +90,11 @@ public function sendNotification($endpoint, $payload = null, $userPublicKey = nu
8990
$payload = Encryption::padPayload($payload, $this->automaticPadding);
9091
}
9192

92-
$this->notifications[] = new Notification($endpoint, $payload, $userPublicKey, $userAuthToken, $options);
93+
if (array_key_exists('VAPID', $auth)) {
94+
$auth['VAPID'] = VAPID::validate($auth['VAPID']);
95+
}
96+
97+
$this->notifications[] = new Notification($endpoint, $payload, $userPublicKey, $userAuthToken, $options, $auth);
9398

9499
if ($flush) {
95100
$res = $this->flush();
@@ -180,6 +185,7 @@ private function prepareAndSend(array $notifications)
180185
$userPublicKey = $notification->getUserPublicKey();
181186
$userAuthToken = $notification->getUserAuthToken();
182187
$options = $notification->getOptions($this->getDefaultOptions());
188+
$auth = $notification->getAuth($this->auth);
183189

184190
if (isset($payload) && isset($userPublicKey) && isset($userAuthToken)) {
185191
$encrypted = Encryption::encrypt($payload, $userPublicKey, $userAuthToken, $this->nativePayloadEncryptionSupport);
@@ -213,15 +219,15 @@ private function prepareAndSend(array $notifications)
213219

214220
// if GCM
215221
if (substr($endpoint, 0, strlen(self::GCM_URL)) === self::GCM_URL) {
216-
if (array_key_exists('GCM', $this->auth)) {
217-
$headers['Authorization'] = 'key='.$this->auth['GCM'];
222+
if (array_key_exists('GCM', $auth)) {
223+
$headers['Authorization'] = 'key='.$auth['GCM'];
218224
} else {
219225
throw new \ErrorException('No GCM API Key specified.');
220226
}
221227
}
222228
// if VAPID (GCM doesn't support it but FCM does)
223-
elseif (array_key_exists('VAPID', $this->auth)) {
224-
$vapid = $this->auth['VAPID'];
229+
elseif (array_key_exists('VAPID', $auth)) {
230+
$vapid = $auth['VAPID'];
225231

226232
$audience = parse_url($endpoint, PHP_URL_SCHEME).'://'.parse_url($endpoint, PHP_URL_HOST);
227233

0 commit comments

Comments
 (0)