getVapidHeaders() public static method

This method takes the required VAPID parameters and returns the required header to be added to a Web Push Protocol Request.
public static getVapidHeaders ( string $audience, string $subject, string $publicKey, string $privateKey, integer $expiration = null ) : array
$audience string This must be the origin of the push service
$subject string This should be a URL or a 'mailto:' email address
$publicKey string The decoded VAPID public key
$privateKey string The decoded VAPID private key
$expiration integer The expiration of the VAPID JWT. (UNIX timestamp)
return array Returns an array with the 'Authorization' and 'Crypto-Key' values to be used as headers
Example #1
0
 /**
  * @dataProvider vapidProvider
  *
  * @param $audience
  * @param $vapid
  * @param $expiration
  * @param $expectedAuthorization
  * @param $expectedCryptoKey
  */
 public function testGetVapidHeaders($audience, $vapid, $expiration, $expectedAuthorization, $expectedCryptoKey)
 {
     $vapid = VAPID::validate($vapid);
     $headers = VAPID::getVapidHeaders($audience, $vapid['subject'], $vapid['publicKey'], $vapid['privateKey'], $expiration);
     $this->assertArrayHasKey('Authorization', $headers);
     $this->assertEquals(Utils::safeStrlen($expectedAuthorization), Utils::safeStrlen($headers['Authorization']));
     $this->assertEquals($this->explodeAuthorization($expectedAuthorization), $this->explodeAuthorization($headers['Authorization']));
     $this->assertArrayHasKey('Crypto-Key', $headers);
     $this->assertEquals($expectedCryptoKey, $headers['Crypto-Key']);
 }
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;
 }