/** * @param RequestInterface $request * @param ResponseInterface $response * @param \Exception|null $previous * * @return AcmeCoreServerException */ public function createAcmeExceptionForResponse(RequestInterface $request, ResponseInterface $response, \Exception $previous = null) { $body = \GuzzleHttp\Psr7\copy_to_string($response->getBody()); $data = @json_decode($body, true); if (!$data || !isset($data['type'], $data['detail'])) { // Not JSON: not an ACME error response return $this->createDefaultExceptionForResponse($request, $response, $previous); } $type = preg_replace('/^urn:acme:error:/i', '', $data['type']); if (!isset(self::$exceptions[$type])) { // Unknown type: not an ACME error response return $this->createDefaultExceptionForResponse($request, $response, $previous); } $exceptionClass = self::$exceptions[$type]; return new $exceptionClass($request, sprintf('%s (on request "%s %s")', $data['detail'], $request->getMethod(), $request->getUri()), $previous); }
private function listen() { $this->docker->listenEvents(function (Event $event) { if (null === $event->getId()) { return; } try { $response = $this->docker->getContainerManager()->find($event->getId(), [], ContainerManager::FETCH_RESPONSE); $container = json_decode(\GuzzleHttp\Psr7\copy_to_string($response->getBody()), true); } catch (\Exception $e) { return; } if (null === $container) { return; } if ($this->isExposed($container)) { $this->activeContainers[$container['Id']] = $container; } else { unset($this->activeContainers[$container['Id']]); } $this->write(); }); }
public function testSignedRequestPayload() { $container = []; $stack = HandlerStack::create(new MockHandler([new Response(200, [], json_encode(['test' => 'ok']))])); $stack->push(Middleware::history($container)); $keyPairGenerator = new KeyPairGenerator(); $dataSigner = $this->getMockBuilder(DataSigner::class)->getMock(); $dataSigner->expects($this->once())->method('signData')->willReturn('foobar'); $client = new SecureHttpClient($keyPairGenerator->generateKeyPair(), new Client(['handler' => $stack]), new Base64SafeEncoder(), new KeyParser(), $dataSigner, $this->getMockBuilder(ServerErrorHandler::class)->getMock()); $client->signedRequest('POST', '/acme/new-reg', ['contact' => '*****@*****.**'], true); // Check request object $this->assertCount(1, $container); /** @var RequestInterface $request */ $request = $container[0]['request']; $this->assertInstanceOf(RequestInterface::class, $request); $this->assertEquals('POST', $request->getMethod()); $this->assertEquals('/acme/new-reg', $request->getUri() instanceof Uri ? $request->getUri()->getPath() : $request->getUri()); $body = \GuzzleHttp\Psr7\copy_to_string($request->getBody()); $payload = @json_decode($body, true); $this->assertInternalType('array', $payload); $this->assertArrayHasKey('header', $payload); $this->assertArrayHasKey('alg', $payload['header']); $this->assertArrayHasKey('jwk', $payload['header']); $this->assertArrayHasKey('protected', $payload); $this->assertArrayHasKey('payload', $payload); $this->assertArrayHasKey('signature', $payload); $this->assertEquals('Zm9vYmFy', $payload['signature']); }
/** * Send a request encoded in the format defined by the ACME protocol. * * @param string $method * @param string $endpoint * @param array $data * @param bool $returnJson * * @throws AcmeCoreServerException When the ACME server returns an error HTTP status code. * @throws AcmeCoreClientException When an error occured during response parsing. * * @return array|string Array of parsed JSON if $returnJson = true, string otherwise */ public function unsignedRequest($method, $endpoint, array $data = null, $returnJson = true) { $request = $this->createRequest($method, $endpoint, $data); try { $this->lastResponse = $this->httpClient->send($request); } catch (\Exception $exception) { $this->handleClientException($request, $exception); } $body = \GuzzleHttp\Psr7\copy_to_string($this->lastResponse->getBody()); if ($returnJson) { $data = @json_decode($body, true); if (!$data) { throw new ExpectedJsonException(sprintf('ACME client excepted valid JSON as a response to request "%s %s" (given: "%s")', $request->getMethod(), $request->getUri(), ServerErrorHandler::getResponseBodySummary($this->lastResponse))); } return $data; } return $body; }