-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathJwtSharedSecretConfiguration.php
More file actions
191 lines (169 loc) · 8.13 KB
/
Copy pathJwtSharedSecretConfiguration.php
File metadata and controls
191 lines (169 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/*
* Purpose : Configuration for JWT authentication with Shared Secret (symmetric / HS256).
*
* Why JWT with Shared Secret?
* - HTTP Signature is being deprecated. JWT with Shared Secret provides a
* seamless migration path — it uses the SAME apiKeyID and secretKey
* credentials you already have for HTTP Signature.
* - Enables MLE (Message Level Encryption). MLE requires JWT authentication.
* By switching to JWT with Shared Secret, you can enable MLE without managing
* a P12 certificate file.
* - Zero credential changes. Your existing Key ID and Shared Secret from the
* CyberSource Business Center work as-is.
*
* Credentials:
* The apiKeyID and secretKey are the same credentials used for HTTP Signature
* authentication. You can obtain them from the CyberSource Business Center:
* Test: https://businesscentertest.cybersource.com/ebc2
* Prod: https://businesscenter.cybersource.com/ebc2
*/
namespace CyberSource;
require_once __DIR__ . DIRECTORY_SEPARATOR . '../vendor/autoload.php';
class JwtSharedSecretConfiguration
{
private $merchantConfig;
// Logging
private $enableLogging;
private $debugLogFile;
private $errorLogFile;
private $logDateFormat;
private $logFormat;
private $logMaxFiles;
private $logLevel;
private $enableMasking;
//initialize variable on constructor
function __construct()
{
// Logging
$this->enableLogging = true;
$this->debugLogFile = __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "Log" . DIRECTORY_SEPARATOR . "debugTest.log";
$this->errorLogFile = __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "Log" . DIRECTORY_SEPARATOR . "errorTest.log";
$this->logDateFormat = "Y-m-d\TH:i:s";
$this->logFormat = "[%datetime%] [%level_name%] [%channel%] : %message%\n";
$this->logMaxFiles = 3;
$this->logLevel = "debug";
$this->enableMasking = true;
$this->merchantConfigObject();
}
// ---------------------------------------------------------------
// Helper: build and return a LogConfiguration
// ---------------------------------------------------------------
private function buildLogConfiguration()
{
$logConfiguration = new \CyberSource\Logging\LogConfiguration();
$logConfiguration->enableLogging($this->enableLogging);
$logConfiguration->setDebugLogFile($this->debugLogFile);
$logConfiguration->setErrorLogFile($this->errorLogFile);
$logConfiguration->setLogDateFormat($this->logDateFormat);
$logConfiguration->setLogFormat($this->logFormat);
$logConfiguration->setLogMaxFiles($this->logMaxFiles);
$logConfiguration->setLogLevel($this->logLevel);
$logConfiguration->enableMasking($this->enableMasking);
return $logConfiguration;
}
// ===============================================================
// merchantConfigObject — JWT with Shared Secret (no MLE)
//
// This is a drop-in replacement for HTTP Signature authentication.
// The only changes from a typical HTTP Signature configuration are:
// 1. authenticationType = JWT (instead of http_signature)
// 2. jwtKeyType = SHARED_SECRET (new property)
//
// The apiKeyID and secretKey remain the same.
// ===============================================================
function merchantConfigObject()
{
if (!isset($this->merchantConfig)) {
$config = new \CyberSource\Authentication\Core\MerchantConfiguration();
// Authentication: JWT with Shared Secret (HS256)
$config->setAuthenticationType("JWT");
$config->setJwtKeyType("SHARED_SECRET");
$config->setMerchantID("testrest");
$config->setRunEnvironment("apitest.cybersource.com");
// Shared Secret credentials — same as HTTP Signature credentials
$config->setApiKeyID("08c94330-f618-42a3-b09d-e1e43be5efda");
$config->setSecretKey("yBJxy6LjM2TmcPGu+GaJrHtkke25fPpUX+UY6/L/1tE=");
// MetaKey Parameters
$config->setUseMetaKey(false);
$config->setPortfolioID("");
$config->setLogConfiguration($this->buildLogConfiguration());
$config->validateMerchantData();
$this->merchantConfig = $config;
}
return $this->merchantConfig;
}
// ===============================================================
// merchantConfigObjectWithMLE — JWT with Shared Secret + MLE enabled
//
// This configuration enables Message Level Encryption (MLE) for
// request payloads. Response MLE is also supported — set
// enableResponseMleGlobally to true and provide the response MLE
// private key settings.
//
// When using jwtKeyType=SHARED_SECRET, Request MLE requires the
// public certificate to be provided via mleForRequestPublicCertPath
// because there is no P12 file to auto-extract it from.
//
// Download the MLE public certificate from the CyberSource
// Business Center:
// Test: https://businesscentertest.cybersource.com/ebc2
// Prod: https://businesscenter.cybersource.com/ebc2
// ===============================================================
function merchantConfigObjectWithMLE()
{
$config = new \CyberSource\Authentication\Core\MerchantConfiguration();
// Authentication: JWT with Shared Secret (HS256)
$config->setAuthenticationType("JWT");
$config->setJwtKeyType("SHARED_SECRET");
$config->setMerchantID("testrest");
$config->setRunEnvironment("apitest.cybersource.com");
// Shared Secret credentials — same as HTTP Signature credentials
$config->setApiKeyID("08c94330-f618-42a3-b09d-e1e43be5efda");
$config->setSecretKey("yBJxy6LjM2TmcPGu+GaJrHtkke25fPpUX+UY6/L/1tE=");
// --- Request MLE Configuration ---
// When using SHARED_SECRET, the MLE certificate must be provided separately.
// Download from CyberSource Business Center:
// Test: https://businesscentertest.cybersource.com/ebc2
// Prod: https://businesscenter.cybersource.com/ebc2
$config->setEnableRequestMLEForOptionalApisGlobally(true);
$config->setMleForRequestPublicCertPath(__DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "Resources" . DIRECTORY_SEPARATOR . "MLE_PublicCert.pem");
// $config->setRequestMleKeyAlias("CyberSource_SJC_US"); // Optional — defaults to CyberSource_SJC_US
// --- Response MLE Configuration ---
// Set to true to enable response MLE (encrypted responses from CyberSource).
// Requires a private key for decryption.
$config->setEnableResponseMleGlobally(false);
// Provide a private key file path for response decryption.
// Supported formats: .p12, .pfx, .pem, .key, .p8
// $config->setResponseMlePrivateKeyFilePath(""); // e.g., "Resources/your_mle_private_key.p12"
// $config->setResponseMlePrivateKeyFilePassword(""); // Required for .p12/.pfx or encrypted keys
// responseMleKID: Optional for CyberSource-generated P12 files (auto-extracted).
// Required for PEM/KEY files.
// $config->setResponseMleKID("");
// MetaKey Parameters
$config->setUseMetaKey(false);
$config->setPortfolioID("");
$config->setLogConfiguration($this->buildLogConfiguration());
$config->validateMerchantData();
return $config;
}
// ---------------------------------------------------------------
// ConnectionHost helpers
// ---------------------------------------------------------------
function ConnectionHost()
{
$merchantConf = $this->merchantConfigObject();
$config = new Configuration();
$config->setHost($merchantConf->getHost());
$config->setLogConfiguration($merchantConf->getLogConfiguration());
return $config;
}
function ConnectionHostFrom($merchantConf)
{
$config = new Configuration();
$config->setHost($merchantConf->getHost());
$config->setLogConfiguration($merchantConf->getLogConfiguration());
return $config;
}
}
?>