Exemplo n.º 1
0
 public function decode($msg)
 {
     return TPSecurityUtils::decrypt($this->privateKey, $msg);
 }
 /**
  * Process found webhook data
  *
  * @param string $data encrypted data
  *
  * @return TinypassWebhookResult
  * @throws Exception
  */
 public function processWebhookData($data)
 {
     // Decrypt data
     $data = TPSecurityUtils::decrypt($this->privateKey(), $data);
     if (false === $data) {
         throw new Exception(__('Failed to decrypt data', 'tinypass'));
     }
     // Data expected to be in json
     $data = json_decode($data, true);
     if (null === $data) {
         throw new Exception(__('Failed to parse data', 'tinypass'));
     }
     // Data should always have event_type and version attributes
     if (!isset($data['type']) || !isset($data['version'])) {
         throw new Exception(__('Invalid webhook data', 'tinypass'));
     }
     // Check if configured application id differs from provided by tinypass
     if (self::appId() != (isset($data['aid']) ? $data['aid'] : '')) {
         throw new Exception(__('Invalid application id', 'tinypass'));
     }
     switch ($data['type']) {
         // Event to key / unkey content
         case 'content_algorithm':
             if ($data['version'] == 2) {
                 return $this->webhookAlgorithmicKey(isset($data['content_id']) ? $data['content_id'] : '', isset($data['event']) ? $data['event'] : '');
             }
     }
     // If processing didn't end at any point - that means no valid webhook processing was found
     throw new Exception(__('No valid webhook processor found', 'tinypass'), self::ERROR_WEBHOOK_NO_PROCESSOR_FOUND);
 }