function testDecodeRequest() { $this->cryptoManager->expects($this->once())->method('decrypt')->with($this->equalTo('xxer4rt34x'), $this->equalTo('FakePrivateKey'))->will($this->returnValue('{"secret": "top-secret"}')); $this->assertTrue($this->securityManager->decodeRequest($this->commandRequest)); $data = $this->commandRequest->getData(); $this->assertEquals($data['foo'], 'bar', 'Plain JSON data was decoded'); $this->assertEquals($data['secret'], 'top-secret', 'Encrypted JSON data was decoded'); }
/** * Decrypt and merge encrypted data for the command request * * @param tx_caretakerinstance_CommandRequest $commandRequest * @return boolean TRUE if the command request could be decrypted */ public function decodeRequest(tx_caretakerinstance_CommandRequest $commandRequest) { $data = json_decode($commandRequest->getRawData(), TRUE); $commandRequest->mergeData($data); if (strlen($commandRequest->getData('encrypted'))) { $raw = $this->cryptoManager->decrypt($commandRequest->getData('encrypted'), $this->privateKey); if (!$raw) { // Decryption failed return FALSE; } $data = json_decode($raw, TRUE); // merge decrypted data into raw data $commandRequest->mergeData($data); } return TRUE; }
/** * Execute the given command request * * @param tx_caretakerinstance_CommandRequest $commandRequest * @return tx_caretakerinstance_CommandResult */ public function executeRequest($commandRequest) { $httpRequestResult = $this->executeHttpRequest($commandRequest->getServerUrl(), array('st' => $commandRequest->getSessionToken(), 'd' => $commandRequest->getData(), 's' => $commandRequest->getSignature())); if (is_array($httpRequestResult)) { if ($httpRequestResult['info']['http_code'] === 200) { $json = $this->securityManager->decodeResult($httpRequestResult['response']); // TODO: check if valid json if ($json) { return tx_caretakerinstance_CommandResult::fromJson($json); } else { if (!empty($httpRequestResult['response'])) { $json = json_decode($httpRequestResult['response'], TRUE); if ($json && $json['status'] == -1) { return $this->getCommandResult(tx_caretakerinstance_CommandResult::status_undefined, NULL, 'Error while executing remote command: ' . $json['message'] . ' (' . $json['exception']['code'] . ')'); } } return $this->getCommandResult(tx_caretakerinstance_CommandResult::status_undefined, NULL, 'Cant decode remote command result'); } } else { if ($httpRequestResult['info']['http_code'] === 0) { // seems to be a timeout return $this->getCommandResult(tx_caretakerinstance_CommandResult::status_undefined, NULL, 'No Response/Timeout (Total-Time: ' . $httpRequestResult['info']['total_time'] . ')'); } else { return $this->getCommandResult(tx_caretakerinstance_CommandResult::status_error, NULL, 'Invalid result: ' . $httpRequestResult['response'] . chr(10) . 'CURL Info: ' . var_export($httpRequestResult['info'], true)); } } } else { return $this->getCommandResult(tx_caretakerinstance_CommandResult::status_error, NULL, 'Invalid result request could not be executed' . chr(10) . 'CURL Info: ' . var_export($httpRequestResult['info'], true)); } }