1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Analytics\Model\Connector;
use Magento\Analytics\Model\AnalyticsToken;
use Magento\Analytics\Model\Connector\Http\ResponseResolver;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\HTTP\ZendClient;
use Magento\Store\Model\Store;
use Psr\Log\LoggerInterface;
/**
* Representation of an 'OTP' request.
*
* The request is responsible for obtaining of an OTP from the MBI service.
*
* OTP (One-Time Password) is a password that is valid for short period of time
* and may be used only for one login session.
*/
class OTPRequest
{
/**
* Resource for handling MBI token value.
*
* @var AnalyticsToken
*/
private $analyticsToken;
/**
* @var Http\ClientInterface
*/
private $httpClient;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var ScopeConfigInterface
*/
private $config;
/**
* @var ResponseResolver
*/
private $responseResolver;
/**
* Path to the configuration value which contains
* an URL that provides an OTP.
*
* @var string
*/
private $otpUrlConfigPath = 'analytics/url/otp';
/**
* @param AnalyticsToken $analyticsToken
* @param Http\ClientInterface $httpClient
* @param ScopeConfigInterface $config
* @param ResponseResolver $responseResolver
* @param LoggerInterface $logger
*/
public function __construct(
AnalyticsToken $analyticsToken,
Http\ClientInterface $httpClient,
ScopeConfigInterface $config,
ResponseResolver $responseResolver,
LoggerInterface $logger
) {
$this->analyticsToken = $analyticsToken;
$this->httpClient = $httpClient;
$this->config = $config;
$this->responseResolver = $responseResolver;
$this->logger = $logger;
}
/**
* Performs obtaining of an OTP from the MBI service.
*
* Returns received OTP or FALSE in case of failure.
*
* @return string|false
*/
public function call()
{
$result = false;
if ($this->analyticsToken->isTokenExist()) {
$response = $this->httpClient->request(
ZendClient::POST,
$this->config->getValue($this->otpUrlConfigPath),
[
"access-token" => $this->analyticsToken->getToken(),
"url" => $this->config->getValue(Store::XML_PATH_SECURE_BASE_URL),
]
);
$result = $this->responseResolver->getResult($response);
if (!$result) {
$this->logger->warning(
sprintf(
'Obtaining of an OTP from the MBI service has been failed: %s. Content-Type: %s',
!empty($response->getBody()) ? $response->getBody() : 'Response body is empty',
$response->getHeader('Content-Type')
)
);
}
}
return $result;
}
}