/**
  * @param string $path
  * @param string $method
  * @param array $data
  * @param string $contentType
  *
  * @return array
  * @throws CommunicationError
  * @throws Exception\InvalidResponseError
  * @throws Exception\LaunchKeyEngineError
  * @throws Exception\NoPairedDevicesError
  * @throws Exception\NoSuchUserError
  * @throws Exception\RateLimitExceededError
  * @throws ExpiredAuthRequestError
  * @throws InvalidCredentialsError
  * @throws InvalidRequestError
  */
 private function sendRequest($path, $method, array $data = array(), array $parameters = array(), $contentType = 'application/x-www-form-urlencoded')
 {
     $headers = array('Accept' => 'application/json', 'Connection' => 'close');
     if (!empty($data)) {
         $headers['Content-Type'] = $contentType;
         if ($contentType === 'application/x-www-form-urlencoded') {
             $body = http_build_query($data);
         } elseif ($contentType === 'application/json') {
             $body = json_encode($data);
             $parameters['signature'] = $this->cryptService->sign($body);
         }
     } else {
         $body = null;
     }
     if (!empty($parameters)) {
         $path .= '?' . http_build_query($parameters);
     }
     $this->debugLog("Sending request", array('path' => $path, 'method' => $method, 'headers' => $headers, 'body' => $body));
     $response = $this->http->request($this->getUrl($path), array('method' => $method, 'timeout' => $this->requestTimeout, 'redirection' => 0, 'httpversion' => '1.1', 'sslverify' => $this->sslVerify, 'body' => $body, 'headers' => $headers));
     if ($response instanceof \WP_Error) {
         $msg = implode(' => ', $response->get_error_messages());
         throw new CommunicationError($msg);
     } else {
         $this->debugLog("Response received", array($response));
         $data = $this->jsonDecodeData($response['body']);
         if (!in_array($response['response']['code'], array(200, 201))) {
             $this->throwExceptionForErrorResponse($data);
         }
     }
     return $data;
 }
 /**
  * Create a white label user with the following identifier
  *
  * @param string $identifier Unique and permanent identifier for the user in the white label application.  This identifier
  * will be used in all future communications regarding this user.  As such, it cannot ever change.
  *
  * @return WhiteLabelUser
  * @throws CommunicationError If there was an error communicating with the endpoint
  * @throws InvalidCredentialsError If the credentials supplied to the endpoint were invalid
  * @throws InvalidRequestError If the endpoint proclaims the request invalid
  * @throws InvalidResponseError If the encrypted data is not valid JSON
  */
 public function createWhiteLabelUser($identifier)
 {
     $body = json_encode(array("app_key" => $this->appKey, "secret_key" => base64_encode($this->getEncryptedSecretKey()), "identifier" => $identifier));
     $request = $this->guzzleClient->post("/v1/users")->setBody($body, "application/json");
     $request->getQuery()->add("signature", $this->cryptService->sign($body));
     $data = $this->sendRequest($request);
     $cipher = $this->cryptService->decryptRSA($data["cipher"]);
     $key = substr($cipher, 0, strlen($cipher) - 16);
     $iv = substr($cipher, -16);
     $userJsonData = $this->cryptService->decryptAES($data["data"], $key, $iv);
     try {
         $userData = $this->jsonDecodeData($userJsonData);
     } catch (InvalidResponseError $e) {
         throw new InvalidResponseError("Response data is not valid JSON when decrypted", $e->getCode(), $e);
     }
     return new WhiteLabelUser($userData["qrcode"], $userData["code"]);
 }
 /**
  * @return string
  */
 protected function getEncryptedSecretKey()
 {
     $encryptedSecretKey = $this->cryptService->encryptRSA(json_encode(array("secret" => $this->secretKey, "stamped" => $this->getLaunchKeyDateString())), $this->getPublicKey(), false);
     return $encryptedSecretKey;
 }
 public function testVerifySignatureReturnsFalseWhenPlainTextSignatureIsNotValid()
 {
     $actual = $this->service->verifySignature(base64_decode(static::BASE64_RSA_ENCRYPTED), base64_decode(static::BASE64_RSA_ENCRYPTED), static::PUBLIC_KEY, false);
     $this->assertFalse($actual);
 }