/** * Tests if example messages match actual messages after encryption/decryption and vice versa: * decrypt(encrypted_example) == unencrypted_example * encrypt(unencrypted_example) == encrypted_example * * @dataProvider filenameProvider */ public function testMessageEncryptionAgainstFixture($filename) { $fixture = json_decode(file_get_contents($filename)); foreach ($fixture->items as $example) { $cipherParams = Crypto::getDefaultParams(['key' => $fixture->key, 'algorithm' => $fixture->algorithm, 'keyLength' => $fixture->keylength, 'mode' => $fixture->mode, 'iv' => $fixture->iv, 'base64Key' => true, 'base64Iv' => true]); $decodedExample = new Message(); $decodedExample->fromJSON($example->encoded); $decryptedExample = new Message(); $decryptedExample->setCipherParams($cipherParams); $decryptedExample->fromJSON($example->encrypted); $this->assertEquals($decodedExample->data, $decryptedExample->data, 'Expected unencrypted and decrypted message\'s contents to match'); $decodedExample->setCipherParams($cipherParams); $encryptedJSON = json_decode($decodedExample->toJSON()); $this->assertEquals($example->encrypted->data, $encryptedJSON->data, 'Expected encrypted and example encrypted message\'s contents to match'); } }
/** * Posts a message to this channel * @param mixed ... Either a Message, array of Message-s, or (string eventName, string data, [string clientId]) * @throws \Ably\Exceptions\AblyException */ public function publish() { $args = func_get_args(); $json = ''; if (count($args) == 1 && is_a($args[0], 'Ably\\Models\\Message')) { // single Message $msg = $args[0]; if ($this->options->cipher) { $msg->setCipherParams($this->options->cipher); } $json = $msg->toJSON(); } else { if (count($args) == 1 && is_array($args[0])) { // array of Messages $jsonArray = []; foreach ($args[0] as $msg) { if ($this->options->cipher) { $msg->setCipherParams($this->options->cipher); } $jsonArray[] = $msg->toJSON(); } $json = '[' . implode(',', $jsonArray) . ']'; } else { if (count($args) >= 2 && count($args) <= 3) { // eventName, data[, clientId] $msg = new Message(); $msg->name = $args[0]; $msg->data = $args[1]; if (count($args) == 3) { $msg->clientId = $args[2]; } if ($this->options->cipher) { $msg->setCipherParams($this->options->cipher); } $json = $msg->toJSON(); } else { throw new AblyException('Wrong parameters provided, use either Message, array of Messages, or name and data', 40003, 400); } } } $authClientId = $this->ably->auth->clientId; // if the message has a clientId set and we're using token based auth, the clientIds must match unless we're a wildcard client if (!empty($msg->clientId) && !$this->ably->auth->isUsingBasicAuth() && $authClientId != '*' && $msg->clientId != $authClientId) { throw new AblyException('Message\'s clientId does not match the clientId of the authorisation token.', 40102, 401); } $this->ably->post($this->channelPath . '/messages', $headers = [], $json); return true; }