diff --git a/src/Surfnet/StepupSelfService/SelfServiceBundle/Service/U2fService.php b/src/Surfnet/StepupSelfService/SelfServiceBundle/Service/U2fService.php index 616955189..fbc7ba9ba 100644 --- a/src/Surfnet/StepupSelfService/SelfServiceBundle/Service/U2fService.php +++ b/src/Surfnet/StepupSelfService/SelfServiceBundle/Service/U2fService.php @@ -18,8 +18,9 @@ namespace Surfnet\StepupSelfService\SelfServiceBundle\Service; -use GuzzleHttp\ClientInterface as GuzzleClient; +use GuzzleHttp\Client; use Psr\Log\LoggerInterface; +use Surfnet\StepupBundle\Http\JsonHelper; use Surfnet\StepupSelfService\SelfServiceBundle\Command\CreateU2fRegisterRequestCommand; use Surfnet\StepupSelfService\SelfServiceBundle\Command\RevokeU2fRegistrationCommand; use Surfnet\StepupSelfService\SelfServiceBundle\Command\VerifyU2fRegistrationCommand; @@ -34,11 +35,12 @@ /** * @SuppressWarnings(PHPMD.CyclomaticComplexity) -- We're verifying a JSON format. Not much to do towards reducing the * complexity. + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) -- JSON verification, helper classes and DTOs introduce coupling */ final class U2fService { /** - * @var \GuzzleHttp\ClientInterface + * @var \GuzzleHttp\Client */ private $guzzleClient; @@ -53,11 +55,11 @@ final class U2fService private $logger; /** - * @param GuzzleClient $guzzleClient + * @param Client $guzzleClient * @param ValidatorInterface $validator * @param LoggerInterface $logger */ - public function __construct(GuzzleClient $guzzleClient, ValidatorInterface $validator, LoggerInterface $logger) + public function __construct(Client $guzzleClient, ValidatorInterface $validator, LoggerInterface $logger) { $this->guzzleClient = $guzzleClient; $this->validator = $validator; @@ -76,12 +78,12 @@ public function createRegisterRequest(CreateU2fRegisterRequestCommand $command) $response = $this->guzzleClient->post( 'api/u2f/create-register-request', - ['json' => $body, 'exceptions' => false] + ['json' => $body, 'http_errors' => false] ); $statusCode = $response->getStatusCode(); try { - $result = $response->json(); + $result = JsonHelper::decode((string) $response->getBody()); } catch (\RuntimeException $e) { $this->logger->critical( 'U2F register request creation failed; server responded with malformed JSON', @@ -165,11 +167,11 @@ public function verifyRegistration(VerifyU2fRegistrationCommand $command) ], ]; - $response = $this->guzzleClient->post('api/u2f/register', ['json' => $body, 'exceptions' => false]); + $response = $this->guzzleClient->post('api/u2f/register', ['json' => $body, 'http_errors' => false]); $statusCode = $response->getStatusCode(); try { - $result = $response->json(); + $result = JsonHelper::decode((string) $response->getBody()); } catch (\RuntimeException $e) { $this->logger->critical('U2F registration verification failed; JSON decoding failed.'); @@ -217,11 +219,11 @@ public function revokeRegistration(RevokeU2fRegistrationCommand $command) 'key_handle' => ['value' => $command->keyHandle], ]; - $response = $this->guzzleClient->post('api/u2f/revoke-registration', ['json' => $body, 'exceptions' => false]); + $response = $this->guzzleClient->post('api/u2f/revoke-registration', ['json' => $body, 'http_errors' => false]); $statusCode = $response->getStatusCode(); try { - $result = $response->json(); + $result = JsonHelper::decode((string) $response->getBody()); } catch (\RuntimeException $e) { $this->logger->critical('U2F registration revocation failed; JSON decoding failed.'); diff --git a/src/Surfnet/StepupSelfService/SelfServiceBundle/Service/YubikeyService.php b/src/Surfnet/StepupSelfService/SelfServiceBundle/Service/YubikeyService.php index ed2347a13..0ed13eb0c 100644 --- a/src/Surfnet/StepupSelfService/SelfServiceBundle/Service/YubikeyService.php +++ b/src/Surfnet/StepupSelfService/SelfServiceBundle/Service/YubikeyService.php @@ -18,14 +18,15 @@ namespace Surfnet\StepupSelfService\SelfServiceBundle\Service; -use GuzzleHttp\ClientInterface; +use GuzzleHttp\Client; use Psr\Log\LoggerInterface; +use Surfnet\StepupBundle\Http\JsonHelper; use Surfnet\StepupSelfService\SelfServiceBundle\Command\VerifyYubikeyOtpCommand; class YubikeyService { /** - * @var ClientInterface + * @var Client */ private $guzzleClient; @@ -35,10 +36,10 @@ class YubikeyService private $logger; /** - * @param ClientInterface $guzzleClient A Guzzle client configured with the Yubikey API base URL and authentication. + * @param Client $guzzleClient A Guzzle client configured with the Yubikey API base URL and authentication. * @param LoggerInterface $logger */ - public function __construct(ClientInterface $guzzleClient, LoggerInterface $logger) + public function __construct(Client $guzzleClient, LoggerInterface $logger) { $this->guzzleClient = $guzzleClient; $this->logger = $logger; @@ -56,7 +57,7 @@ public function verify(VerifyYubikeyOtpCommand $command) 'requester' => ['institution' => $command->institution, 'identity' => $command->identity], 'otp' => ['value' => $command->otp], ]; - $response = $this->guzzleClient->post('api/verify-yubikey', ['json' => $body, 'exceptions' => false]); + $response = $this->guzzleClient->post('api/verify-yubikey', ['json' => $body, 'http_errors' => false]); $statusCode = $response->getStatusCode(); if ($statusCode != 200) { @@ -67,7 +68,7 @@ public function verify(VerifyYubikeyOtpCommand $command) } try { - $result = $response->json(); + $result = JsonHelper::decode((string) $response->getBody()); } catch (\RuntimeException $e) { $this->logger->error('Yubikey OTP verification failed; server responded with malformed JSON.');