|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Krtv\SingleSignOn\Manager\Http\Provider\Guzzle; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
|
6
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
7
|
|
|
use Krtv\SingleSignOn\Model\OneTimePassword; |
|
8
|
|
|
use Krtv\SingleSignOn\Model\OneTimePasswordInterface; |
|
9
|
|
|
use Krtv\SingleSignOn\Manager\Http\Provider\ProviderInterface; |
|
10
|
|
|
use Psr\Log\LoggerInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class OneTimePasswordProvider |
|
14
|
|
|
* @package FM\SingleSignOnBundle\Manager\Http\Provider\Guzzle |
|
15
|
|
|
*/ |
|
16
|
|
|
class OneTimePasswordProvider implements ProviderInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var ClientInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $client; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $resource; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var LoggerInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $logger; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param ClientInterface $client |
|
35
|
|
|
* @param string $resource |
|
36
|
|
|
* @param LoggerInterface $logger |
|
37
|
|
|
*/ |
|
38
|
2 |
|
public function __construct(ClientInterface $client, $resource, LoggerInterface $logger = null) |
|
39
|
|
|
{ |
|
40
|
2 |
|
$this->client = $client; |
|
41
|
2 |
|
$this->resource = $resource; |
|
42
|
2 |
|
$this->logger = $logger; |
|
43
|
2 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $otp |
|
47
|
|
|
* @return OneTimePasswordInterface|null |
|
48
|
|
|
* @throws RequestException |
|
49
|
|
|
* @throws \Exception |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function fetch($otp) |
|
52
|
|
|
{ |
|
53
|
|
|
try { |
|
54
|
2 |
|
$response = $this->client->request('GET', sprintf('%s?_otp=%s', $this->resource, $otp)); |
|
55
|
|
|
|
|
56
|
|
|
// JMSSerializer here ?? |
|
57
|
2 |
|
$data = json_decode($response->getBody()->getContents(), true); |
|
58
|
2 |
|
if (json_last_error() !== JSON_ERROR_NONE) { |
|
59
|
1 |
|
if (null !== $this->logger) { |
|
60
|
1 |
|
$this->logger->error(sprintf('json_decode error. Gateway response: %s', $response->getBody()->getContents())); |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
return null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
$otp = new OneTimePassword(); |
|
67
|
1 |
|
$otp->setCreated(new \DateTime($data['data']['created_at'])); |
|
68
|
1 |
|
$otp->setHash($data['data']['hash']); |
|
69
|
1 |
|
$otp->setPassword($data['data']['password']); |
|
70
|
1 |
|
$otp->setUsed($data['data']['is_used']); |
|
71
|
|
|
|
|
72
|
1 |
|
return $otp; |
|
73
|
|
|
} catch (RequestException $e) { |
|
74
|
|
|
if (null !== $this->logger) { |
|
75
|
|
|
$this->logger->error($e->getMessage()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return null; |
|
79
|
|
|
} catch (\Exception $e) { |
|
80
|
|
|
if (null !== $this->logger) { |
|
81
|
|
|
$this->logger->critical($e->getMessage()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
throw $e; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|