Example #1
0
 public function testCreateVapidKeys()
 {
     $keys = VAPID::createVapidKeys();
     $this->assertArrayHasKey('publicKey', $keys);
     $this->assertArrayHasKey('privateKey', $keys);
     $this->assertEquals(strlen($keys['publicKey']), 88);
     $this->assertEquals(strlen($keys['privateKey']), 44);
 }
Example #2
0
 private function prepareAndSend(array $notifications)
 {
     $responses = array();
     /** @var Notification $notification */
     foreach ($notifications as $notification) {
         $endpoint = $notification->getEndpoint();
         $payload = $notification->getPayload();
         $userPublicKey = $notification->getUserPublicKey();
         $userAuthToken = $notification->getUserAuthToken();
         $options = $notification->getOptions($this->getDefaultOptions());
         $auth = $notification->getAuth($this->auth);
         if (isset($payload) && isset($userPublicKey) && isset($userAuthToken)) {
             $encrypted = Encryption::encrypt($payload, $userPublicKey, $userAuthToken, $this->nativePayloadEncryptionSupport);
             $headers = array('Content-Length' => Utils::safeStrlen($encrypted['cipherText']), 'Content-Type' => 'application/octet-stream', 'Content-Encoding' => 'aesgcm', 'Encryption' => 'salt=' . $encrypted['salt'], 'Crypto-Key' => 'dh=' . $encrypted['localPublicKey']);
             $content = $encrypted['cipherText'];
         } else {
             $headers = array('Content-Length' => 0);
             $content = '';
         }
         $headers['TTL'] = $options['TTL'];
         if (isset($options['urgency'])) {
             $headers['Urgency'] = $options['urgency'];
         }
         if (isset($options['topic'])) {
             $headers['Topic'] = $options['topic'];
         }
         // if GCM
         if (substr($endpoint, 0, strlen(self::GCM_URL)) === self::GCM_URL) {
             if (array_key_exists('GCM', $auth)) {
                 $headers['Authorization'] = 'key=' . $auth['GCM'];
             } else {
                 throw new \ErrorException('No GCM API Key specified.');
             }
         } elseif (array_key_exists('VAPID', $auth)) {
             $vapid = $auth['VAPID'];
             $audience = parse_url($endpoint, PHP_URL_SCHEME) . '://' . parse_url($endpoint, PHP_URL_HOST);
             if (!parse_url($audience)) {
                 throw new \ErrorException('Audience "' . $audience . '"" could not be generated.');
             }
             $vapidHeaders = VAPID::getVapidHeaders($audience, $vapid['subject'], $vapid['publicKey'], $vapid['privateKey']);
             $headers['Authorization'] = $vapidHeaders['Authorization'];
             if (array_key_exists('Crypto-Key', $headers)) {
                 // FUTURE replace ';' with ','
                 $headers['Crypto-Key'] .= ';' . $vapidHeaders['Crypto-Key'];
             } else {
                 $headers['Crypto-Key'] = $vapidHeaders['Crypto-Key'];
             }
         }
         $responses[] = $this->sendRequest($endpoint, $headers, $content);
     }
     return $responses;
 }