Minishlink\WebPush\VAPID::validate PHP Метод

validate() публичный статический Метод

public static validate ( array $vapid ) : array
$vapid array
Результат array
    public static function validate(array $vapid)
    {
        if (!array_key_exists('subject', $vapid)) {
            throw new \ErrorException('[VAPID] You must provide a subject that is either a mailto: or a URL.');
        }
        if (array_key_exists('pemFile', $vapid)) {
            $vapid['pem'] = file_get_contents($vapid['pemFile']);
            if (!$vapid['pem']) {
                throw new \ErrorException('Error loading PEM file.');
            }
        }
        if (array_key_exists('pem', $vapid)) {
            $pem = $vapid['pem'];
            $posStartKey = strpos($pem, '-----BEGIN EC PRIVATE KEY-----');
            $posEndKey = strpos($pem, '-----END EC PRIVATE KEY-----');
            if ($posStartKey === false || $posEndKey === false) {
                throw new \ErrorException('Invalid PEM data.');
            }
            $posStartKey += 30;
            // length of '-----BEGIN EC PRIVATE KEY-----'
            $pemSerializer = new PemPrivateKeySerializer(new DerPrivateKeySerializer());
            $keys = self::getUncompressedKeys($pemSerializer->parse(substr($pem, $posStartKey, $posEndKey - $posStartKey)));
            $vapid['publicKey'] = $keys['publicKey'];
            $vapid['privateKey'] = $keys['privateKey'];
        }
        if (!array_key_exists('publicKey', $vapid)) {
            throw new \ErrorException('[VAPID] You must provide a public key.');
        }
        $publicKey = Base64Url::decode($vapid['publicKey']);
        if (Utils::safeStrlen($publicKey) !== self::PUBLIC_KEY_LENGTH) {
            throw new \ErrorException('[VAPID] Public key should be 65 bytes long when decoded.');
        }
        if (!array_key_exists('privateKey', $vapid)) {
            throw new \ErrorException('[VAPID] You must provide a private key.');
        }
        $privateKey = Base64Url::decode($vapid['privateKey']);
        if (Utils::safeStrlen($privateKey) !== self::PRIVATE_KEY_LENGTH) {
            throw new \ErrorException('[VAPID] Private key should be 32 bytes long when decoded.');
        }
        return array('subject' => $vapid['subject'], 'publicKey' => $publicKey, 'privateKey' => $privateKey);
    }

Usage Example

Пример #1
0
 /**
  * Send a notification.
  *
  * @param string      $endpoint
  * @param string|null $payload       If you want to send an array, json_encode it
  * @param string|null $userPublicKey
  * @param string|null $userAuthToken
  * @param bool        $flush         If you want to flush directly (usually when you send only one notification)
  * @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
  * @param array       $auth          Use this auth details instead of what you provided when creating WebPush
  *
  * @return array|bool Return an array of information if $flush is set to true and the queued requests has failed.
  *                    Else return true
  *
  * @throws \ErrorException
  */
 public function sendNotification($endpoint, $payload = null, $userPublicKey = null, $userAuthToken = null, $flush = false, $options = array(), $auth = array())
 {
     if (isset($payload)) {
         if (Utils::safeStrlen($payload) > Encryption::MAX_PAYLOAD_LENGTH) {
             throw new \ErrorException('Size of payload must not be greater than ' . Encryption::MAX_PAYLOAD_LENGTH . ' octets.');
         }
         $payload = Encryption::padPayload($payload, $this->automaticPadding);
     }
     if (array_key_exists('VAPID', $auth)) {
         $auth['VAPID'] = VAPID::validate($auth['VAPID']);
     }
     $this->notifications[] = new Notification($endpoint, $payload, $userPublicKey, $userAuthToken, $options, $auth);
     if ($flush) {
         $res = $this->flush();
         // if there has been a problem with at least one notification
         if (is_array($res)) {
             // if there was only one notification, return the information directly
             if (count($res) === 1) {
                 return $res[0];
             }
             return $res;
         }
         return true;
     }
     return true;
 }
All Usage Examples Of Minishlink\WebPush\VAPID::validate