Skip to content

Commit 0fe600e

Browse files
committed
"error handling refactored"
1 parent 0070f33 commit 0fe600e

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

lib/Authentication/Jwt/JsonWebTokenHeader.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,19 @@ public function getJsonWebToken($jwtBody, $merchantConfig)
4848
self::$logger->warning($warning_msg);
4949
}
5050

51-
$cacheData = self::$cache->grabFileFromP12($merchantConfig);
51+
try {
52+
$cacheData = self::$cache->grabFileFromP12($merchantConfig);
53+
} catch (AuthException $e) {
54+
self::$logger->error("Failed to grab file from P12: " . $e->getMessage());
55+
throw $e;
56+
}
5257

5358
if (!empty($cacheData['private_key']) && !empty($cacheData['publicKey'])) {
5459
$privateKey = $cacheData['private_key'];
5560
$publicKey = $cacheData['publicKey'];
5661
} else {
57-
//which exception would be best to be thrown here?
58-
$exception = new AuthException(GlobalParameter::INCORRECT_KEY_PASSWORD, 0);
59-
self::$logger->error("AuthException : " . GlobalParameter::INCORRECT_KEY_PASSWORD);
60-
self::$logger->close();
61-
throw $exception;
62+
self::$logger->error("AuthException: " . GlobalParameter::EMPTY_PRIVATE_OR_PUBLIC_KEY_ERROR);
63+
throw new AuthException("AuthException: " . GlobalParameter::EMPTY_PRIVATE_OR_PUBLIC_KEY_ERROR);
6264
}
6365

6466
$x5cArray = array($publicKey);

lib/Authentication/Util/Cache.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44
use Jose\Component\KeyManagement\JWKFactory;
55
use CyberSource\Authentication\Util\GlobalParameter as GlobalParameter;
66
use CyberSource\Authentication\Core\AuthException as AuthException;
7+
use CyberSource\Logging\LogFactory as LogFactory;
78

89
class Cache
910
{
1011
private static $file_cache = array();
12+
private static $logger = null;
13+
1114

1215
public function __construct()
1316
{
14-
17+
if (self::$logger === null) {
18+
self::$logger = (new LogFactory())->getLogger(\CyberSource\Utilities\Helpers\ClassHelper::getClassName(get_class($this)), new \CyberSource\Logging\LogConfiguration());
19+
}
1520
}
1621

1722
public function updateCache($filePath, $merchantConfig)
@@ -20,8 +25,15 @@ public function updateCache($filePath, $merchantConfig)
2025
$fileModTime = filemtime($filePath);
2126
$keyPass = $merchantConfig->getKeyPassword();
2227
$cacheKey = $fileName . '_' . "jwt";
28+
$certStore = null;
29+
if (file_exists($filePath)) {
30+
$certStore = file_get_contents($filePath);
31+
} else {
32+
$exception = new AuthException(GlobalParameter::KEY_FILE_INCORRECT, 0);
33+
self::$logger->error("AuthException : " . GlobalParameter::KEY_FILE_INCORRECT);
34+
throw $exception;
35+
}
2336

24-
$certStore = file_get_contents($filePath);
2537
$privateKey = null;
2638
$publicKey = null;
2739
$mleCert = null;
@@ -34,6 +46,10 @@ public function updateCache($filePath, $merchantConfig)
3446
}
3547
$publicKey = Utility::findCertByAlias($certs, $keyAlias);
3648
$publicKey = $this->PemToDer($publicKey);
49+
} else {
50+
$exception = new AuthException(GlobalParameter::INCORRECT_KEY_PASSWORD, 0);
51+
self::$logger->error("AuthException : " . GlobalParameter::INCORRECT_KEY_PASSWORD);
52+
throw $exception;
3753
}
3854

3955
$mleCert = Utility::findCertByAlias($certs, $merchantConfig->getMleKeyAlias());
@@ -110,4 +126,4 @@ private function PemToDer($Pem)
110126
unset($lines[0]);
111127
return implode("\n", $lines);
112128
}
113-
}
129+
}

lib/Authentication/Util/GlobalParameter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,6 @@ class GlobalParameter
106106
const DEFAULT_MLE_ALIAS_FOR_CERT = "CyberSource_SJC_US";
107107
const CERTIFICATE_EXPIRY_DATE_WARNING_DAYS = 90;
108108
const MLE_AUTH_ERROR = "MLE is only supported in JWT auth type";
109+
const EMPTY_PRIVATE_OR_PUBLIC_KEY_ERROR = "Private key or public key is empty";
109110
}
110111
?>

0 commit comments

Comments
 (0)