Ejemplo n.º 1
0
 /**
  * @dataProvider payloadProvider
  *
  * @param string $payload
  * @param integer $maxLengthToPad
  * @param integer $expectedResLength
  */
 public function testPadPayload($payload, $maxLengthToPad, $expectedResLength)
 {
     $res = Encryption::padPayload($payload, $maxLengthToPad);
     $this->assertContains('test', $res);
     $this->assertEquals($expectedResLength, Utils::safeStrlen($res));
 }
Ejemplo n.º 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;
 }