Skip to content

Commit 92531e0

Browse files
authored
Merge pull request #1 from snavinch/oauth-changes
Oauth changes
2 parents 23fea63 + 3c2781e commit 92531e0

File tree

9 files changed

+1310
-5
lines changed

9 files changed

+1310
-5
lines changed

generator/cybersource-php-template/ApiClient.mustache

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,13 @@ class ApiClient
220220
$postData = json_encode(\{{invokerPackage}}\ObjectSerializer::sanitizeForSerialization($postData));
221221
}
222222
$resourcePath= utf8_encode($resourcePath);
223-
$authHeader = $this->callAuthenticationHeader($method, $postData, $resourcePath);
224-
$headers = array_merge($headers, $authHeader);
223+
224+
if($this->merchantConfig->getAuthenticationType() != GlobalParameter::MUTUAL_AUTH)
225+
{
226+
$authHeader = $this->callAuthenticationHeader($method, $postData, $resourcePath);
227+
$headers = array_merge($headers, $authHeader);
228+
}
229+
225230
foreach ($headers as $value) {
226231
$splitArr= explode(":", $value, 2);
227232
$this->config->addRequestHeader($splitArr[0], $splitArr[1]);
@@ -326,6 +331,15 @@ class ApiClient
326331
curl_setopt($curl, CURLOPT_HEADER, 1);
327332
}
328333

334+
// Adding Client Cert if Required
335+
if($this->merchantConfig->getEnableClientCert())
336+
{
337+
$clientCertPath = $this->merchantConfig->getClientCertDirectory().$this->merchantConfig->getClientCertFile();
338+
curl_setopt($curl, CURLOPT_SSLCERT, $clientCertPath);
339+
curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'P12');
340+
curl_setopt($curl, CURLOPT_SSLCERTPASSWD, $this->merchantConfig->getClientCertPassword());
341+
}
342+
329343
// Make the request
330344
$response = curl_exec($curl);
331345

@@ -530,6 +544,11 @@ class ApiClient
530544
'Authorization:'.$getToken
531545
);
532546
}
547+
else if($merchantConfig->getAuthenticationType()==GlobalParameter::OAUTH){
548+
$headers = array(
549+
'Authorization:'.$getToken
550+
);
551+
}
533552
else{
534553
echo "Invalid Authentication type!";
535554
}

lib/Api/OAuthApi.php

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
/**
3+
* OAuthApi
4+
* PHP version 5
5+
*
6+
* @category Class
7+
* @package CyberSource
8+
*/
9+
10+
/**
11+
* CyberSource Merged Spec
12+
*
13+
* All CyberSource API specs merged together. These are available at https://developer.cybersource.com/api/reference/api-reference.html
14+
*
15+
* OpenAPI spec version: 0.0.1
16+
*
17+
*/
18+
19+
20+
namespace CyberSource\Api;
21+
22+
use \CyberSource\ApiClient;
23+
use \CyberSource\ApiException;
24+
use \CyberSource\Configuration;
25+
use \CyberSource\ObjectSerializer;
26+
27+
/**
28+
* OAuthApi Class Doc Comment
29+
*
30+
* @category Class
31+
* @package CyberSource
32+
*/
33+
class OAuthApi
34+
{
35+
/**
36+
* API Client
37+
*
38+
* @var \CyberSource\ApiClient instance of the ApiClient
39+
*/
40+
protected $apiClient;
41+
42+
/**
43+
* Constructor
44+
*
45+
* @param \CyberSource\ApiClient|null $apiClient The api client to use
46+
*/
47+
public function __construct(\CyberSource\ApiClient $apiClient = null)
48+
{
49+
if ($apiClient === null) {
50+
$apiClient = new ApiClient();
51+
}
52+
53+
$this->apiClient = $apiClient;
54+
}
55+
56+
/**
57+
* Get API client
58+
*
59+
* @return \CyberSource\ApiClient get the API client
60+
*/
61+
public function getApiClient()
62+
{
63+
return $this->apiClient;
64+
}
65+
66+
/**
67+
* Set the API client
68+
*
69+
* @param \CyberSource\ApiClient $apiClient set the API client
70+
*
71+
* @return OAuthApi
72+
*/
73+
public function setApiClient(\CyberSource\ApiClient $apiClient)
74+
{
75+
$this->apiClient = $apiClient;
76+
return $this;
77+
}
78+
79+
/**
80+
* Operation postAccessTokenRequest
81+
*
82+
* Post Access Token
83+
*
84+
* @param \CyberSource\Model\CreateAccessTokenRequest $createAccessTokenRequest (required)
85+
* @throws \CyberSource\ApiException on non-2xx response
86+
* @return array of \CyberSource\Model\AccessTokenResponse, HTTP status code, HTTP response headers (array of strings)
87+
*/
88+
public function postAccessTokenRequest($createAccessTokenRequest)
89+
{
90+
list($response, $statusCode, $httpHeader) = $this->postAccessTokenRequestWithHttpInfo($createAccessTokenRequest);
91+
return [$response, $statusCode, $httpHeader];
92+
}
93+
94+
/**
95+
* Operation postAccessTokenRequestWithHttpInfo
96+
*
97+
* Post Access Token
98+
*
99+
* @param \CyberSource\Model\CreateAccessTokenRequest $createAccessTokenRequest (required)
100+
* @throws \CyberSource\ApiException on non-2xx response
101+
* @return array of \CyberSource\Model\AccessTokenResponse, HTTP status code, HTTP response headers (array of strings)
102+
*/
103+
public function postAccessTokenRequestWithHttpInfo($createAccessTokenRequest)
104+
{
105+
// verify the required parameter 'createAccessTokenRequest' is set
106+
if ($createAccessTokenRequest === null) {
107+
throw new \InvalidArgumentException('Missing the required parameter $createAccessTokenRequest when calling postAccessTokenRequest');
108+
}
109+
// parse inputs
110+
$resourcePath = "/oauth2/v3/token";
111+
$httpBody = '';
112+
$queryParams = [];
113+
$headerParams = [];
114+
$formParams = [];
115+
$_header_accept = $this->apiClient->selectHeaderAccept(['application/hal+json;charset=utf-8']);
116+
if (!is_null($_header_accept)) {
117+
$headerParams['Accept'] = $_header_accept;
118+
}
119+
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(['application/x-www-form-urlencoded']);
120+
121+
// body params
122+
$_tempBody = null;
123+
if (isset($createAccessTokenRequest)) {
124+
$_tempBody = $createAccessTokenRequest;
125+
}
126+
127+
// for model (json/xml)
128+
if (isset($_tempBody)) {
129+
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
130+
} elseif (count($formParams) > 0) {
131+
$httpBody = $formParams; // for HTTP post (form)
132+
}
133+
// make the API Call
134+
try {
135+
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
136+
$resourcePath,
137+
'POST',
138+
$queryParams,
139+
$httpBody,
140+
$headerParams,
141+
'\CyberSource\Model\AccessTokenResponse',
142+
'/oauth2/v3/token'
143+
);
144+
145+
return [$this->apiClient->getSerializer()->deserialize($response, '\CyberSource\Model\AccessTokenResponse', $httpHeader), $statusCode, $httpHeader];
146+
} catch (ApiException $e) {
147+
switch ($e->getCode()) {
148+
case 201:
149+
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '\CyberSource\Model\AccessTokenResponse', $e->getResponseHeaders());
150+
$e->setResponseObject($data);
151+
break;
152+
case 400:
153+
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '\CyberSource\Model\PtsV2PaymentsCapturesPost400Response', $e->getResponseHeaders());
154+
$e->setResponseObject($data);
155+
break;
156+
case 502:
157+
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '\CyberSource\Model\PtsV2PaymentsPost502Response', $e->getResponseHeaders());
158+
$e->setResponseObject($data);
159+
break;
160+
}
161+
162+
throw $e;
163+
}
164+
}
165+
}

lib/ApiClient.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,12 @@ public function callApi($resourcePath, $method, $queryParams, $postData, $header
230230
$postData = json_encode(\CyberSource\ObjectSerializer::sanitizeForSerialization($postData));
231231
}
232232
$resourcePath= utf8_encode($resourcePath);
233-
$authHeader = $this->callAuthenticationHeader($method, $postData, $resourcePath);
234-
$headers = array_merge($headers, $authHeader);
233+
234+
if($this->merchantConfig->getAuthenticationType() != GlobalParameter::MUTUAL_AUTH)
235+
{
236+
$authHeader = $this->callAuthenticationHeader($method, $postData, $resourcePath);
237+
$headers = array_merge($headers, $authHeader);
238+
}
235239
foreach ($headers as $value) {
236240
$splitArr= explode(":", $value, 2);
237241
$this->config->addRequestHeader($splitArr[0], $splitArr[1]);
@@ -336,6 +340,15 @@ public function callApi($resourcePath, $method, $queryParams, $postData, $header
336340
curl_setopt($curl, CURLOPT_HEADER, 1);
337341
}
338342

343+
// Adding Client Cert if Required
344+
if($this->merchantConfig->getEnableClientCert())
345+
{
346+
$clientCertPath = $this->merchantConfig->getClientCertDirectory().$this->merchantConfig->getClientCertFile();
347+
curl_setopt($curl, CURLOPT_SSLCERT, $clientCertPath);
348+
curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'P12');
349+
curl_setopt($curl, CURLOPT_SSLCERTPASSWD, $this->merchantConfig->getClientCertPassword());
350+
}
351+
339352
// Make the request
340353
$response = curl_exec($curl);
341354

@@ -540,6 +553,11 @@ public function callAuthenticationHeader($method, $postData, $resourcePath)
540553
'Authorization:'.$getToken
541554
);
542555
}
556+
else if($merchantConfig->getAuthenticationType()==GlobalParameter::OAUTH){
557+
$headers = array(
558+
'Authorization:'.$getToken
559+
);
560+
}
543561
else{
544562
echo "Invalid Authentication type!";
545563
}

lib/Authentication/Core/Authentication.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php
22
/*
3-
Purpose : This is focusly on split the services HTTP or JWT
3+
Purpose : This is focusly on split the services HTTP or JWT or OAuth
44
*/
55
namespace CyberSource\Authentication\Core;
66
use CyberSource\Authentication\Http\HttpSignatureGenerator as HttpSignatureGenerator;
77
use CyberSource\Authentication\Jwt\JsonWebTokenGenerator as JsonWebTokenGenerator;
8+
use CyberSource\Authentication\OAuth\OAuthTokenGenerator as OAuthTokenGenerator;
89
use CyberSource\Authentication\Util\GlobalParameter as GlobalParameter;
910
use CyberSource\Authentication\Log\Logger as Logger;
1011

@@ -41,6 +42,9 @@ function getTokenGenerator($merchantConfig) {
4142
return new HttpSignatureGenerator();
4243
} else if($authType == GlobalParameter::JWT){
4344
return new JsonWebTokenGenerator();
45+
}
46+
else if($authType == GlobalParameter::OAUTH){
47+
return new OAuthTokenGenerator();
4448
} else {
4549
$exception = new AuthException(GlobalParameter::AUTH_ERROR, 0);
4650
self::$logger->log($merchantConfig, $exception);

0 commit comments

Comments
 (0)