Skip to content

Commit 1134729

Browse files
committed
Deprecated method for JWE decryption
Added new method using PrivateKey parameter Pull request #175
1 parent 417cf8e commit 1134729

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

lib/Authentication/Jwt/JsonWebTokenGenerator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(\CyberSource\Logging\LogConfiguration $logConfig)
2828
//calling Signature
2929
public function generateToken($resourcePath, $payloadData, $method, $merchantConfig)
3030
{
31-
$date = date("D, d M Y G:i:s ").GlobalParameter::GMT;
31+
$date = gmdate("D, d M Y G:i:s ").GlobalParameter::GMT;
3232
if($method==GlobalParameter::GET || $method==GlobalParameter::DELETE)
3333
{
3434
$jwtBody = array("iat"=>$date);

lib/Authentication/Util/JWE/JWEUtility.php

+39
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
class JWEUtility {
2121
private static $cache = null;
2222

23+
/**
24+
* @deprecated This method has been marked as Deprecated and will be removed in coming releases.
25+
*/
2326
private static function loadKeyFromPEMFile($path) {
27+
trigger_error("This method has been marked as Deprecated and will be removed in coming releases.", E_USER_DEPRECATED);
2428
return JWKFactory::createFromKeyFile(
2529
$path,
2630
'', // Secret if the key is encrypted
@@ -30,7 +34,11 @@ private static function loadKeyFromPEMFile($path) {
3034
);
3135
}
3236

37+
/**
38+
* @deprecated This method has been marked as Deprecated and will be removed in coming releases. Use decryptJWEUsingPrivateKey(\$privateKey, \$encodedResponse) instead.
39+
*/
3340
public static function decryptJWEUsingPEM(MerchantConfiguration $merchantConfig, string $jweBase64Data) {
41+
trigger_error("This method has been marked as Deprecated and will be removed in coming releases. Use decryptJWEUsingPrivateKey(\$privateKey, \$encodedResponse) instead.", E_USER_DEPRECATED);
3442
if (!isset(self::$cache)) {
3543
self::$cache = new Cache();
3644
}
@@ -77,6 +85,37 @@ public static function decryptJWEUsingPEM(MerchantConfiguration $merchantConfig,
7785
return null;
7886
}
7987
}
88+
89+
public static function decryptJWEUsingPrivateKey(string $privateKey, string $encodedResponse) {
90+
$jwk = JWKFactory::createFromKey($privateKey);
91+
// The key encryption algorithm manager with the A256KW algorithm.
92+
$keyEncryptionAlgorithmManager = new AlgorithmManager([
93+
new RSAOAEP256()
94+
]);
95+
96+
// The content encryption algorithm manager with the A256CBC-HS256 algorithm.
97+
$contentEncryptionAlgorithmManager = new AlgorithmManager([
98+
new A256GCM(),
99+
]);
100+
101+
// The serializer manager. We only use the JWE Compact Serialization Mode.
102+
$serializerManager = new JWESerializerManager([
103+
new CompactSerializer(),
104+
]);
105+
106+
$jweDecrypter = new JWEDecrypter(
107+
$keyEncryptionAlgorithmManager,
108+
$contentEncryptionAlgorithmManager,
109+
new CompressionMethodManager([new Deflate()])
110+
);
111+
112+
$jwe = $serializerManager->unserialize($encodedResponse);
113+
if($jweDecrypter -> decryptUsingKey($jwe, $jwk, 0)) {
114+
return $jwe ->getPayload();
115+
} else {
116+
return null;
117+
}
118+
}
80119
}
81120

82121
?>

lib/Utilities/JWEResponse/JWEUtility.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@
88
class JWEUtility {
99

1010
/**
11-
* @throws Exception
11+
* @deprecated This method has been marked as Deprecated and will be removed in coming releases. Use decryptJWEResponseUsingPrivateKey(\$privateKey, \$encodedResponse) instead.
1212
*/
1313
public static function decryptJWEResponse($encodedResponse, MerchantConfiguration $merchantConfig) {
14+
trigger_error("This method has been marked as Deprecated and will be removed in coming releases. Use Use decryptJWEResponseUsingPrivateKey(\$privateKey, \$encodedResponse) instead.", E_USER_DEPRECATED);
1415
return AuthJWEUtility::decryptJWEUsingPEM($merchantConfig, $encodedResponse);
1516
}
17+
18+
public static function decryptJWEResponseUsingPrivateKey($privateKey, $encodedResponse) {
19+
return AuthJWEUtility::decryptJWEUsingPrivateKey($privateKey, $encodedResponse);
20+
}
1621
}

0 commit comments

Comments
 (0)