Skip to content

Commit f1f4cc4

Browse files
committed
AC-1271: Add rate limiting for payment information endpoint and mutation
1 parent d578ae2 commit f1f4cc4

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

app/code/Magento/Authorization/Model/IdentityProvider.php

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,22 @@
1010

1111
use Magento\Framework\App\Backpressure\ContextInterface;
1212
use Magento\Framework\App\Backpressure\IdentityProviderInterface;
13+
use Magento\Framework\Exception\RuntimeException;
1314
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
1415

1516
/**
16-
* Utilizes UserContext for backpressure identity.
17+
* Utilizes UserContext for backpressure identity
1718
*/
1819
class IdentityProvider implements IdentityProviderInterface
1920
{
21+
/**
22+
* User context identity type map
23+
*/
24+
private const USER_CONTEXT_IDENTITY_TYPE_MAP = [
25+
UserContextInterface::USER_TYPE_CUSTOMER => ContextInterface::IDENTITY_TYPE_CUSTOMER,
26+
UserContextInterface::USER_TYPE_ADMIN => ContextInterface::IDENTITY_TYPE_ADMIN
27+
];
28+
2029
/**
2130
* @var UserContextInterface
2231
*/
@@ -38,37 +47,41 @@ public function __construct(UserContextInterface $userContext, RemoteAddress $re
3847
}
3948

4049
/**
41-
* @inheritDoc
50+
* {@inheritDoc}
51+
*
52+
* @throws RuntimeException
4253
*/
4354
public function fetchIdentityType(): int
4455
{
45-
if ($this->userContext->getUserType() === UserContextInterface::USER_TYPE_CUSTOMER
46-
&& $this->userContext->getUserId()
47-
) {
48-
return ContextInterface::IDENTITY_TYPE_CUSTOMER;
49-
} elseif ($this->userContext->getUserType() === UserContextInterface::USER_TYPE_ADMIN
50-
&& $this->userContext->getUserId()
51-
) {
52-
return ContextInterface::IDENTITY_TYPE_ADMIN;
53-
} else {
56+
if (!$this->userContext->getUserId()) {
5457
return ContextInterface::IDENTITY_TYPE_IP;
5558
}
59+
60+
$userType = $this->userContext->getUserType();
61+
if (isset(self::USER_CONTEXT_IDENTITY_TYPE_MAP[$userType])) {
62+
return self::USER_CONTEXT_IDENTITY_TYPE_MAP[$userType];
63+
}
64+
65+
throw new RuntimeException(__('User type not defined'));
5666
}
5767

5868
/**
59-
* @inheritDoc
69+
* {@inheritDoc}
70+
*
71+
* @throws RuntimeException
6072
*/
6173
public function fetchIdentity(): string
6274
{
63-
if ($this->userContext->getUserId()) {
64-
return (string) $this->userContext->getUserId();
75+
$userId = $this->userContext->getUserId();
76+
if ($userId) {
77+
return (string)$userId;
6578
}
6679

67-
$addr = $this->remoteAddress->getRemoteAddress();
68-
if (!$addr) {
69-
throw new \RuntimeException('Failed to extract remote address');
80+
$address = $this->remoteAddress->getRemoteAddress();
81+
if (!$address) {
82+
throw new RuntimeException(__('Failed to extract remote address'));
7083
}
7184

72-
return $addr;
85+
return $address;
7386
}
7487
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
"We can't find the role for the user you wanted.","We can't find the role for the user you wanted."
22
"Something went wrong while compiling a list of allowed resources. You can find out more in the exceptions log.","Something went wrong while compiling a list of allowed resources. You can find out more in the exceptions log."
3+
"User type not defined","User type not defined"
4+
"Failed to extract remote address","Failed to extract remote address"

0 commit comments

Comments
 (0)