Skip to content

Commit 282cbe0

Browse files
committed
"comments resolved"
1 parent b1efb70 commit 282cbe0

File tree

3 files changed

+46
-42
lines changed

3 files changed

+46
-42
lines changed

lib/Api/PaymentsApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public function createPaymentWithHttpInfo($createPaymentRequest)
306306
$httpBody = MLEUtility::encryptRequestPayload($this->apiClient->merchantConfig, $httpBody);
307307
} catch (Exception $e) {
308308
self::$logger->error("Failed to encrypt request body: $e");
309-
throw new ApiException("Failed to encrypt request body : " + $e->getMessage());
309+
throw new ApiException("Failed to encrypt request body : " . $e->getMessage());
310310
}
311311
}
312312

lib/Authentication/Util/Cache.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ public function updateCache($filePath, $merchantConfig)
1919
$fileName = basename($filePath);
2020
$fileModTime = filemtime($filePath);
2121
$keyPass = $merchantConfig->getKeyPassword();
22-
if (empty($keyPass)) {
23-
$keyPass = $merchantConfig->getMerchantID();
24-
}
22+
$cacheKey = $fileName . '_' . "jwt";
2523

2624
$certStore = file_get_contents($filePath);
2725
$privateKey = null;
@@ -30,14 +28,17 @@ public function updateCache($filePath, $merchantConfig)
3028

3129
if (openssl_pkcs12_read($certStore, $certs, $keyPass)) {
3230
$privateKey = $certs['pkey'];
33-
$publicKey = $this->PemToDer($certs['cert']);
31+
if (!empty($merchantConfig->getKeyAlias())) {
32+
$publicKey = Utility::findCertByAlias($certs, $merchantConfig->getKeyAlias());
33+
} else {
34+
$publicKey = $certs['cert'];
35+
}
36+
$publicKey = $this->PemToDer($publicKey);
3437
}
3538

36-
if (!empty($merchantConfig->getMleKeyAlias())) {
37-
$mleCert = Utility::findCertByAlias($certs, $merchantConfig->getMleKeyAlias());
38-
}
39+
$mleCert = Utility::findCertByAlias($certs, $merchantConfig->getMleKeyAlias());
3940

40-
self::$file_cache[$fileName] = [
41+
self::$file_cache[$cacheKey] = [
4142
'private_key' => $privateKey,
4243
'publicKey' => $publicKey,
4344
'file_mod_time' => $fileModTime,
@@ -51,11 +52,13 @@ public function grabFileFromP12($merchantConfig)
5152

5253
$fileName = basename($filePath);
5354
$fileModTime = filemtime($filePath);
55+
$cacheKey = $fileName . '_' . "jwt";
5456

55-
if (!isset(self::$file_cache[$fileName]) || self::$file_cache[$fileName]['file_mod_time'] !== $fileModTime) {
57+
if (!isset(self::$file_cache[$cacheKey]) || self::$file_cache[$cacheKey]['file_mod_time'] !== $fileModTime) {
5658
$this->updateCache($filePath, $merchantConfig);
5759
}
58-
return $this->file_cache[$fileName];
60+
61+
return self::$file_cache[$cacheKey];
5962
}
6063

6164
private function getFilePath($merchantConfig)
@@ -87,16 +90,17 @@ public function grabKeyFromPEM($filePath)
8790
{
8891
$fileName = basename($filePath);
8992
$fileModTime = filemtime($filePath);
93+
$cacheKey = $fileName . '_' . "jwe";
9094

91-
if (!isset(self::$file_cache[$fileName]) || self::$file_cache[$fileName]['file_mod_time'] !== $fileModTime) {
95+
if (!isset(self::$file_cache[$cacheKey]) || self::$file_cache[$cacheKey]['file_mod_time'] !== $fileModTime) {
9296
$privateKeyFromPEMFile = self::loadKeyFromPEMFile($filePath);
93-
self::$file_cache[$fileName] = [
97+
self::$file_cache[$cacheKey] = [
9498
'private_key' => $privateKeyFromPEMFile,
9599
'file_mod_time' => $fileModTime,
96100
];
97101
}
98102

99-
return self::$file_cache[$fileName]['private_key'];
103+
return self::$file_cache[$cacheKey]['private_key'];
100104
}
101105

102106
private function PemToDer($Pem)

lib/Authentication/Util/MLEUtility.php

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Cybersource\GlobalParameter;
66
use CyberSource\Authentication\Util\Cache as Cache;
7-
use CyberSource\Logging\LogFactory as LogFactory;
7+
use \CyberSource\Logging\LogFactory as LogFactory;
88
use CyberSource\Logging\LogConfiguration;
99
use Jose\Component\Core\JWK;
1010
use Jose\Component\Core\AlgorithmManager;
@@ -23,7 +23,7 @@
2323

2424
class MLEUtility
2525
{
26-
private $logger = null;
26+
private static $logger = null;
2727

2828
private static $cache = null;
2929

@@ -50,30 +50,30 @@ public static function checkIsMLEForAPI($merchantConfig, $isMLESupportedByCybsFo
5050

5151
public static function encryptRequestPayload($merchantConfig, $requestBody)
5252
{
53-
$logger = (new LogFactory())->getLogger(\CyberSource\Utilities\Helpers\ClassHelper::getClassName(get_class()), $merchantConfig->getLogConfiguration());
54-
55-
$mleCert = self::getMLECert($merchantConfig, $logger);
53+
if (self::$logger === null) {
54+
self::$logger = (new LogFactory())->getLogger(\CyberSource\Utilities\Helpers\ClassHelper::getClassName(get_class()), $merchantConfig->getLogConfiguration());
55+
}
56+
$mleCert = self::getMLECert($merchantConfig);
5657

5758
if ($merchantConfig->getLogConfiguration()->isMaskingEnabled()) {
5859
$printRequestBody = \CyberSource\Utilities\Helpers\DataMasker::maskData($requestBody);
5960
} else {
6061
$printRequestBody = $requestBody;
6162
}
6263

63-
$logger->debug("Request before MLE:\n" . print_r($printRequestBody, true));
64+
self::$logger->debug("Request before MLE:\n" . print_r($printRequestBody, true));
6465

65-
$jweToken = self::generateToken($mleCert, $requestBody, $logger);
66+
$jweToken = self::generateToken($mleCert, $requestBody);
6667
$mleRequest = json_encode(['encryptedRequest' => $jweToken]);
6768

68-
$logger->debug("Request after MLE:\n" . print_r($mleRequest, true));
69-
// self::$logger->close();
69+
self::$logger->debug("Request after MLE:\n" . print_r($mleRequest, true));
7070
return $mleRequest;
7171
}
7272

73-
private static function generateToken($cert, $requestBody, $logger)
73+
private static function generateToken($cert, $requestBody)
7474
{
7575
try {
76-
$serialNumber = self::extractSerialNumber($cert, $logger);
76+
$serialNumber = self::extractSerialNumber($cert);
7777

7878
$publicKey = openssl_pkey_get_details(openssl_pkey_get_public($cert))['key'];
7979

@@ -114,12 +114,12 @@ private static function generateToken($cert, $requestBody, $logger)
114114
$serializer = new CompactSerializer();
115115
return $serializer->serialize($jwe);
116116
} catch (\Exception $e) {
117-
$logger->error("Error encrypting request payload: " . $e->getMessage());
117+
self::$logger->error("Error encrypting request payload: " . $e->getMessage());
118118
throw new MLEException("Error encrypting request payload: " . $e->getMessage());
119119
}
120120
}
121121

122-
public static function getMLECert($merchantConfig, $logger)
122+
public static function getMLECert($merchantConfig)
123123
{
124124
try {
125125
if (!isset(self::$cache)) {
@@ -131,18 +131,19 @@ public static function getMLECert($merchantConfig, $logger)
131131
$x509Cert = $fileCache['mle_cert'];
132132

133133
if ($x509Cert) {
134-
self::validateCertificateExpiry($x509Cert, $merchantConfig->getMleKeyAlias(), $logger);
134+
self::validateCertificateExpiry($x509Cert, $merchantConfig->getMleKeyAlias());
135+
// throw new MLEException("Certificate with MLE alias $keyAlias is expired.");
135136
return $x509Cert;
136137
} else {
137-
throw new MLEException("Certificate with alias $mleKeyAlias not found.");
138+
throw new MLEException("Certificate with alias " . $merchantConfig->getMleKeyAlias() . " not found");
138139
}
139140
} catch (\Exception $e) {
140-
$logger->error("Error fetching MLE certificate: " . $e->getMessage());
141+
self::$logger->error("Error fetching MLE certificate: " . $e->getMessage());
141142
throw new MLEException("Error fetching MLE certificate: " . $e->getMessage());
142143
}
143144
}
144145

145-
public static function extractSerialNumber($cert, $logger)
146+
public static function extractSerialNumber($cert)
146147
{
147148
try {
148149
$certDetails = openssl_x509_parse($cert);
@@ -153,42 +154,41 @@ public static function extractSerialNumber($cert, $logger)
153154
}
154155

155156
if ($serialNumber === null) {
156-
$logger->warning("Serial number not found in MLE certificate for alias.");
157-
// this will be in hexdec is it fine?
157+
self::$logger->warning("Serial number not found in MLE certificate for alias.");
158158
$serialNumber = $certDetails['serialNumber'];
159159
}
160160
return $serialNumber;
161161
} catch (\Exception $e) {
162-
$logger->error("Error extracting serial number from certificate: " . $e->getMessage());
162+
self::$logger->error("Error extracting serial number from certificate: " . $e->getMessage());
163163
throw new MLEException("Error extracting serial number from certificate: " . $e->getMessage());
164164
}
165165
}
166166

167-
public static function validateCertificateExpiry($certificate, $keyAlias, $logger)
167+
public static function validateCertificateExpiry($certificate, $keyAlias)
168168
{
169169
try {
170170
$certDetails = openssl_x509_parse($certificate);
171171
$notValidAfter = isset($certDetails['validTo_time_t']) ? $certDetails['validTo_time_t'] : null;
172172

173173
if ($notValidAfter === null) {
174-
$logger->warning("Certificate with MLE alias $keyAlias does not have a valid expiry date.");
175-
throw new MLEException("Certificate with MLE alias $keyAlias does not have a valid expiry date.");
174+
self::$logger->warning("Certificate with MLE alias $keyAlias does not have a valid expiry date.");
176175
}
177176

178177
if ($notValidAfter < time()) {
179-
$logger->warning("Certificate with MLE alias $keyAlias is expired as of " . date('Y-m-d H:i:s', $notValidAfter) . ". Please update p12 file.");
180-
// throw new MLEException("Certificate with MLE alias $keyAlias is expired.");
178+
self::$logger->warning("Certificate with MLE alias $keyAlias is expired as of " . date('Y-m-d H:i:s', $notValidAfter) . ". Please update p12 file.");
179+
// throw new MLEException("Certificate with MLE alias $keyAlias is expired.");
181180
} else {
182181
$timeToExpire = $notValidAfter - time();
183182
$warningPeriod = GlobalLabelParameters::CERTIFICATE_EXPIRY_DATE_WARNING_DAYS * 24 * 60 * 60;
184183

185184
if ($timeToExpire < $warningPeriod) {
186-
$logger->warning("Certificate for MLE with alias $keyAlias is going to expire on " . date('Y-m-d H:i:s', $notValidAfter) . ". Please update p12 file before that.");
185+
self::$logger->warning("Certificate for MLE with alias $keyAlias is going to expire on " . date('Y-m-d H:i:s', $notValidAfter) . ". Please update p12 file before that.");
187186
}
188187
}
189188
} catch (\Exception $e) {
190-
$logger->error("Error while checking certificate expiry: " . $e->getMessage());
191-
}
189+
self::$logger->error("Error validating certificate expiry: " . $e->getMessage());
190+
// throw new MLEException("Error validating certificate expiry: " . $e->getMessage());
191+
}
192192
}
193193
}
194194
?>

0 commit comments

Comments
 (0)