/** * @param array|string $formats * @param string $blob * @return Message * @throws InvalidMessageException */ public function decode($formats, $blob) { $formats = (array) $formats; $prefixLen = 0; foreach ($formats as $format) { $prefixLen = max($prefixLen, strlen($format)); } list($prefix) = explode(Constants::PROTOCOL_DELIM, substr($blob, 0, $prefixLen + 1)); if (!in_array($prefix, $formats)) { if (in_array(GarbledMessage::NAME, $formats)) { return GarbledMessage::decode($blob); } else { throw new InvalidMessageException("Unexpected message type."); } } switch ($prefix) { case StdMessage::NAME: return StdMessage::decode($this->cxnStore, $blob); case InsecureMessage::NAME: return InsecureMessage::decode($blob); case RegistrationMessage::NAME: return RegistrationMessage::decode($this->appStore, $blob); case AppMetasMessage::NAME: return AppMetasMessage::decode($this->certValidator, $blob); default: throw new InvalidMessageException("Unrecognized message type."); } }
/** * @param string $entity * @param string $action * @param array $params * @throws GarbledMessageException * @throws InvalidMessageException * @return mixed */ public function call($entity, $action, $params) { $this->log->debug("Send API call: {entity}.{action} over {cxnId}", array('entity' => $entity, 'action' => $action, 'cxnId' => $this->cxnId)); $cxn = $this->cxnStore->getByCxnId($this->cxnId); $req = new StdMessage($cxn['cxnId'], $cxn['secret'], array($entity, $action, $params, $this->appMeta['appCert'])); list($respHeaders, $respCiphertext, $respCode) = $this->http->send('POST', $cxn['siteUrl'], $req->encode(), array('Content-type' => Constants::MIME_TYPE)); $respMessage = $this->decode(array(StdMessage::NAME, GarbledMessage::NAME), $respCiphertext); if ($respMessage instanceof GarbledMessage) { throw new GarbledMessageException($respMessage); } elseif ($respMessage instanceof StdMessage) { if ($respMessage->getCxnId() != $cxn['cxnId']) { // Tsk, tsk, Mallory! throw new InvalidMessageException('Received response from incorrect connection.'); } return $respMessage->getData(); } else { throw new InvalidMessageException('Unrecognized message type.'); } }
public function decode($formats, $message) { $prefixLen = 0; foreach ($formats as $format) { $prefixLen = max($prefixLen, strlen($format)); } list($prefix) = explode(Constants::PROTOCOL_DELIM, substr($message, 0, $prefixLen + 1)); if (!in_array($prefix, $formats)) { throw new InvalidMessageException("Unexpected message type."); } switch ($prefix) { case StdMessage::NAME: return StdMessage::decode($this->cxnStore, $message); case InsecureMessage::NAME: return InsecureMessage::decode($message); case RegistrationMessage::NAME: return RegistrationMessage::decode($this->appId, $this->appPrivKey, $message); default: throw new InvalidMessageException("Unrecognized message type"); } }