Skip to content

Commit d709a60

Browse files
committed
use new standard and temp server for Chrome
1 parent 51f0f44 commit d709a60

File tree

2 files changed

+16
-50
lines changed

2 files changed

+16
-50
lines changed

src/Encryption.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ public static function encrypt($payload, $userPublicKey, $userAuthToken, $native
7272
$context = self::createContext($userPublicKey, $localPublicKey);
7373

7474
// derive the Content Encryption Key
75-
// TODO Chrome GCM wants 'aesgcm'?
76-
$contentEncryptionKeyInfo = self::createInfo('aesgcm128', $context);
75+
$contentEncryptionKeyInfo = self::createInfo('aesgcm', $context);
7776
$contentEncryptionKey = self::hkdf($salt, $ikm, $contentEncryptionKeyInfo, 16);
7877

7978
// section 3.3, derive the nonce
@@ -165,7 +164,6 @@ private static function createInfo($type, $context) {
165164
throw new \ErrorException('Context argument has invalid size');
166165
}
167166

168-
// TODO Why 'P-256'?
169167
return 'Content-Encoding: '.$type.chr(0).'P-256'.$context;
170168
}
171169
}

src/WebPush.php

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
class WebPush
2121
{
22+
const GCM_URL = 'https://android.googleapis.com/gcm/send';
23+
const TEMP_GCM_URL = 'https://gcm-http.googleapis.com/gcm';
24+
2225
/** @var Browser */
2326
protected $browser;
2427

@@ -30,7 +33,7 @@ class WebPush
3033

3134
/** @var array Array of not standard endpoint sources */
3235
private $urlByServerType = array(
33-
'GCM' => 'https://android.googleapis.com/gcm/send',
36+
'GCM' => self::GCM_URL,
3437
);
3538

3639
/** @var int Time To Live of notifications */
@@ -104,7 +107,7 @@ public function sendNotification($endpoint, $payload = null, $userPublicKey = nu
104107

105108
// if there has been a problem with at least one notification
106109
if (is_array($res)) {
107-
// if there was only one notification, return the informations directly
110+
// if there was only one notification, return the information directly
108111
if (count($res) === 1) {
109112
return $res[0];
110113
}
@@ -143,14 +146,7 @@ public function flush()
143146
// for each endpoint server type
144147
$responses = array();
145148
foreach ($this->notificationsByServerType as $serverType => $notifications) {
146-
switch ($serverType) {
147-
case 'GCM':
148-
$responses += $this->sendToGCMEndpoints($notifications);
149-
break;
150-
case 'standard':
151-
$responses += $this->sendToStandardEndpoints($notifications);
152-
break;
153-
}
149+
$responses += $this->prepareAndSend($notifications, $serverType);
154150
}
155151

156152
// if multi curl, flush
@@ -191,11 +187,12 @@ public function flush()
191187
return $completeSuccess ? true : $return;
192188
}
193189

194-
private function sendToStandardEndpoints(array $notifications)
190+
private function prepareAndSend(array $notifications, $serverType)
195191
{
196192
$responses = array();
197193
/** @var Notification $notification */
198194
foreach ($notifications as $notification) {
195+
$endpoint = $notification->getEndpoint();
199196
$payload = $notification->getPayload();
200197
$userPublicKey = $notification->getUserPublicKey();
201198

@@ -205,7 +202,7 @@ private function sendToStandardEndpoints(array $notifications)
205202
$headers = array(
206203
'Content-Length' => strlen($encrypted['cipherText']),
207204
'Content-Type' => 'application/octet-stream',
208-
'Content-Encoding' => 'aesgcm128',
205+
'Content-Encoding' => 'aesgcm',
209206
'Encryption' => 'keyid="p256dh";salt="'.$encrypted['salt'].'"',
210207
'Crypto-Key' => 'keyid="p256dh";dh="'.$encrypted['localPublicKey'].'"',
211208
'TTL' => $this->TTL,
@@ -221,43 +218,14 @@ private function sendToStandardEndpoints(array $notifications)
221218
$content = '';
222219
}
223220

224-
$responses[] = $this->sendRequest($notification->getEndpoint(), $headers, $content);
225-
}
226-
227-
return $responses;
228-
}
229-
230-
private function sendToGCMEndpoints(array $notifications)
231-
{
232-
$maxBatchSubscriptionIds = 1000;
233-
$url = $this->urlByServerType['GCM'];
234-
235-
$headers = array(
236-
'Authorization' => 'key='.$this->apiKeys['GCM'],
237-
'Content-Type' => 'application/json',
238-
'TTL' => $this->TTL,
239-
);
221+
if ($serverType === 'GCM') {
222+
// FUTURE remove when Chrome servers are all up-to-date
223+
$endpoint = str_replace(self::GCM_URL, self::TEMP_GCM_URL, $endpoint);
240224

241-
$subscriptionIds = array();
242-
/** @var Notification $notification */
243-
foreach ($notifications as $notification) {
244-
// get all subscriptions ids
245-
$endpointsSections = explode('/', $notification->getEndpoint());
246-
$subscriptionIds[] = $endpointsSections[count($endpointsSections) - 1];
247-
}
248-
249-
// chunk by max number
250-
$batch = array_chunk($subscriptionIds, $maxBatchSubscriptionIds);
251-
252-
$responses = array();
253-
foreach ($batch as $subscriptionIds) {
254-
$content = json_encode(array(
255-
'registration_ids' => $subscriptionIds,
256-
));
257-
258-
$headers['Content-Length'] = strlen($content);
225+
$headers['Authorization'] = 'key='.$this->apiKeys['GCM'];
226+
}
259227

260-
$responses[] = $this->sendRequest($url, $headers, $content);
228+
$responses[] = $this->sendRequest($endpoint, $headers, $content);
261229
}
262230

263231
return $responses;

0 commit comments

Comments
 (0)