Skip to content

Commit

Permalink
Merge pull request #122 from OpenConext/bugfix/upgrade-guzzle-interface
Browse files Browse the repository at this point in the history
Correctly implement Guzzle 6 interfaces
  • Loading branch information
Alex Rothuis authored Mar 27, 2017
2 parents 5a37f03 + f8de62e commit 2e05f8c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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',
Expand Down Expand Up @@ -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.');

Expand Down Expand Up @@ -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.');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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.');

Expand Down

0 comments on commit 2e05f8c

Please sign in to comment.