23
23
use Magento \Framework \App \Config \ScopeConfigInterface ;
24
24
use Magento \Framework \App \Request \Http ;
25
25
use Magento \Framework \Exception \LocalizedException ;
26
+ use Meta \BusinessExtension \Helper \FBEHelper ;
26
27
use Meta \BusinessExtension \Model \System \Config as SystemConfig ;
27
28
28
29
class Authenticator
@@ -42,33 +43,42 @@ class Authenticator
42
43
*/
43
44
private SystemConfig $ systemConfig ;
44
45
46
+ /**
47
+ * @var FBEHelper
48
+ */
49
+ private FBEHelper $ fbeHelper ;
50
+
45
51
/**
46
52
* Authenticator constructor
47
53
*
48
54
* @param ScopeConfigInterface $scopeConfig
49
55
* @param Http $httpRequest
50
56
* @param SystemConfig $systemConfig
57
+ * @param FBEHelper $fbeHelper
51
58
*/
52
59
public function __construct (
53
60
ScopeConfigInterface $ scopeConfig ,
54
61
Http $ httpRequest ,
55
- SystemConfig $ systemConfig
62
+ SystemConfig $ systemConfig ,
63
+ FBEHelper $ fbeHelper
56
64
) {
57
65
$ this ->scopeConfig = $ scopeConfig ;
58
66
$ this ->httpRequest = $ httpRequest ;
59
67
$ this ->systemConfig = $ systemConfig ;
68
+ $ this ->fbeHelper = $ fbeHelper ;
60
69
}
61
70
62
71
/**
63
72
* Authenticate an API request (validate the token and RSA signature)
64
73
*
74
+ * @param string|null $storeId
65
75
* @return void
66
76
* @throws LocalizedException
67
77
*/
68
- public function authenticateRequest (): void
78
+ public function authenticateRequest (? string $ storeId = null ): void
69
79
{
70
80
$ this ->authenticateToken ();
71
- $ this ->authenticateSignature ();
81
+ $ this ->authenticateSignature ($ storeId );
72
82
}
73
83
74
84
/**
@@ -104,10 +114,11 @@ private function authenticateToken(): void
104
114
/**
105
115
* Authenticate RSA Signature for API Request
106
116
*
117
+ * @param string|null $storeId
107
118
* @return void
108
119
* @throws LocalizedException
109
120
*/
110
- private function authenticateSignature (): void
121
+ private function authenticateSignature (? string $ storeId = null ): void
111
122
{
112
123
// phpcs:ignore Magento2.Functions.DiscouragedFunction
113
124
$ publicKey = file_get_contents (__DIR__ . '/PublicKey.pem ' );
@@ -130,7 +141,23 @@ private function authenticateSignature(): void
130
141
$ verification = openssl_verify ($ originalMessage , $ decodedSignature , $ publicKeyResource , OPENSSL_ALGO_SHA256 );
131
142
132
143
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 ;
134
161
}
135
162
}
136
163
}
0 commit comments