Skip to content

Commit 6c10124

Browse files
nrostrow-metaNoah Ostrowski
andauthored
Rsa signature validation error logging (#718)
* Adding error logging for RSA signature validation and fixed error with having empty string in extra_data * empty_string -> empty_str * Fixing static test failure * Attempting to resolve static test failure --------- Co-authored-by: Noah Ostrowski <[email protected]>
1 parent e0e4733 commit 6c10124

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

app/code/Meta/BusinessExtension/Model/Api/CustomApiKey/Authenticator.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Magento\Framework\App\Config\ScopeConfigInterface;
2424
use Magento\Framework\App\Request\Http;
2525
use Magento\Framework\Exception\LocalizedException;
26+
use Meta\BusinessExtension\Helper\FBEHelper;
2627
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
2728

2829
class Authenticator
@@ -42,33 +43,42 @@ class Authenticator
4243
*/
4344
private SystemConfig $systemConfig;
4445

46+
/**
47+
* @var FBEHelper
48+
*/
49+
private FBEHelper $fbeHelper;
50+
4551
/**
4652
* Authenticator constructor
4753
*
4854
* @param ScopeConfigInterface $scopeConfig
4955
* @param Http $httpRequest
5056
* @param SystemConfig $systemConfig
57+
* @param FBEHelper $fbeHelper
5158
*/
5259
public function __construct(
5360
ScopeConfigInterface $scopeConfig,
5461
Http $httpRequest,
55-
SystemConfig $systemConfig
62+
SystemConfig $systemConfig,
63+
FBEHelper $fbeHelper
5664
) {
5765
$this->scopeConfig = $scopeConfig;
5866
$this->httpRequest = $httpRequest;
5967
$this->systemConfig = $systemConfig;
68+
$this->fbeHelper = $fbeHelper;
6069
}
6170

6271
/**
6372
* Authenticate an API request (validate the token and RSA signature)
6473
*
74+
* @param string|null $storeId
6575
* @return void
6676
* @throws LocalizedException
6777
*/
68-
public function authenticateRequest(): void
78+
public function authenticateRequest(?string $storeId = null): void
6979
{
7080
$this->authenticateToken();
71-
$this->authenticateSignature();
81+
$this->authenticateSignature($storeId);
7282
}
7383

7484
/**
@@ -104,10 +114,11 @@ private function authenticateToken(): void
104114
/**
105115
* Authenticate RSA Signature for API Request
106116
*
117+
* @param string|null $storeId
107118
* @return void
108119
* @throws LocalizedException
109120
*/
110-
private function authenticateSignature(): void
121+
private function authenticateSignature(?string $storeId = null): void
111122
{
112123
// phpcs:ignore Magento2.Functions.DiscouragedFunction
113124
$publicKey = file_get_contents(__DIR__ . '/PublicKey.pem');
@@ -130,7 +141,23 @@ private function authenticateSignature(): void
130141
$verification = openssl_verify($originalMessage, $decodedSignature, $publicKeyResource, OPENSSL_ALGO_SHA256);
131142

132143
if (!$verification) {
133-
throw new LocalizedException(__('RSA Signature Validation Failed'));
144+
$ex = new LocalizedException(__('RSA Signature Validation Failed'));
145+
if ($storeId !== null) {
146+
$this->fbeHelper->logExceptionImmediatelyToMeta(
147+
$ex,
148+
[
149+
'store_id' => $storeId,
150+
'event' => 'authentication_error',
151+
'event_type' => 'rsa_signature_validation_error',
152+
'extra_data' => [
153+
'request_uri' => $requestUri,
154+
'request_body' => $requestBody,
155+
'request_signature' => $signature
156+
]
157+
]
158+
);
159+
}
160+
throw $ex;
134161
}
135162
}
136163
}

app/code/Meta/BusinessExtension/Model/PersistMetaLogImmediatelyHandler.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,12 @@ public function persistMetaLogImmediately(string $message): void
6565
// Meta expects extra_data field to be dict<string, string>
6666
$context['extra_data'] = array_map(
6767
function ($value) {
68+
if ($value === '') {
69+
return 'empty_str';
70+
}
6871
return is_string($value) ? $value : json_encode($value);
69-
}, $context['extra_data']
72+
},
73+
$context['extra_data']
7074
);
7175
$this->graphApiAdapter->persistLogToMeta($context, $accessToken);
7276
}

app/code/Meta/Sales/Model/Api/HealthCheckApi.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public function __construct(
5757
*/
5858
public function healthCheck(string $externalBusinessId): bool
5959
{
60-
$this->authenticator->authenticateRequest();
61-
$this->orderHelper->getStoreIdByExternalBusinessId($externalBusinessId);
60+
$storeId = $this->orderHelper->getStoreIdByExternalBusinessId($externalBusinessId);
61+
$this->authenticator->authenticateRequest($storeId);
6262

6363
return true;
6464
}

0 commit comments

Comments
 (0)