Skip to content

Commit

Permalink
PRE-2604: get the client id and scret mask from user-manager
Browse files Browse the repository at this point in the history
  • Loading branch information
ilajili committed Oct 18, 2024
1 parent 8c16f35 commit ee6f292
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 5 deletions.
95 changes: 93 additions & 2 deletions lib/Payplug/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Authentication
* This function is for user-friendly interface purpose only.
* You should probably not use this more than once, login/password MUST NOT be stored and API Keys are enough to interact with API.
*
* @param string $email the user email
* @param string $password the user password
* @param string $email the user email
* @param string $password the user password
*
* @return null|array the API keys
*
Expand Down Expand Up @@ -144,4 +144,95 @@ private static function validateToken(Payplug $payplug)
throw new ConfigurationException('The Payplug configuration requires a valid token.');
}
}

/**
* Retrieve client id and client_secret_mask from the user manager resource.
*
* @param Payplug $payplug the client configuration
*
* @return array the client id and client_secret_mask
*
* @throws Exception
*/
public static function getClientData($session = null, Payplug $payplug = null)
{
if ($payplug === null) {
$payplug = Payplug::getDefaultConfiguration();
}
$kratosSession = self::setKratosSession($session);

$httpClient = new Core\HttpClient($payplug);
$response = $httpClient->get(Core\APIRoutes::$USER_MANAGER_RESOURCE, null, $kratosSession);
$result = array();

foreach ($response['httpResponse'] as $client) {
$result[] = array(
'client_id' => $client['client_id'],
'client_secret_mask' => $client['client_secret_mask'],
'client_name' => $client['client_name'],
'client_type' => $client['client_type'],
'mode' => $client['mode'],

);
}

return $result;
}

/**
* Create a client ID and secret.
* @param string $clientName The name of the client.
* @param string $clientType The type of the client.
* @param string $companyId The ID of the company.
* @param string $mode The mode (e.g., test or live).
* @param Payplug|null $payplug The Payplug configuration. If null, the default configuration is used.
* @param string|null $session The session value to be set in the cookie.
*
* @return array The client ID and client secret.
*
* @throws Exception\ConfigurationNotSetException
* @throws Exception\ConnectionException
* @throws Exception\HttpException
* @throws Exception\UnexpectedAPIResponseException
*/
public static function createClientIdAndSecret($clientName, $clientType, $companyId, $mode, $session = null, Payplug $payplug = null)
{
if ($payplug === null) {
$payplug = Payplug::getDefaultConfiguration();
}
$kratosSession = self::setKratosSession($session);

$httpClient = new Core\HttpClient($payplug);
$response = $httpClient->post(Core\APIRoutes::$USER_MANAGER_RESOURCE, array(
'company_id' => $companyId,
'client_name' => $clientName,
'client_type' => $clientType,
'mode' => $mode,
), $kratosSession);

$result = array();

foreach ($response['httpResponse'] as $client) {
$result[] = array(
'client_id' => $client['client_id'],
'client_secret' => $client['client_secret'],
);
}

return $result;
}


/**
* Set the Kratos session cookie.
*
* @param string $session The session value to be set in the cookie.
*
* @return string The formatted Kratos session cookie string.
*/
public static function setKratosSession($session)
{
return 'ory_kratos_session=' . $session;
}

}
15 changes: 15 additions & 0 deletions lib/Payplug/Core/APIRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class APIRoutes
*/
public static $MERCHANT_PLUGINS_DATA_COLLECTOR_RESOURCE;

/**
* @var string the root URL of the User Manager microService
*/
public static $USER_MANAGER_RESOURCE;

const API_VERSION = 1;

// Resources routes
Expand Down Expand Up @@ -77,6 +82,15 @@ public static function setMerchantPluginsDataCollectorService($microServiceBaseU
self::$MERCHANT_PLUGINS_DATA_COLLECTOR_RESOURCE = $microServiceBaseUrl;
}

/**
* @description set $USER_MANAGER_RESOURCE from plugin
* @param $microServiceBaseUrl
*/
public static function setUserManagerResource($microServiceBaseUrl)
{
self::$USER_MANAGER_RESOURCE = $microServiceBaseUrl;
}

/**
* Gets a route that allows to check whether the remote API is up.
*
Expand All @@ -90,4 +104,5 @@ public static function getTestRoute()

APIRoutes::$API_BASE_URL = 'https://api.payplug.com';
APIRoutes::$MERCHANT_PLUGINS_DATA_COLLECTOR_RESOURCE = 'Microservice Url';
APIRoutes::$USER_MANAGER_RESOURCE ='User manager resource';

11 changes: 8 additions & 3 deletions lib/Payplug/Core/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ public function delete($resource, $data = null)
* @throws Payplug\Exception\HttpException When status code is not 2xx.
* @throws Payplug\Exception\ConnectionException When an error was encountered while connecting to the resource.
*/
public function get($resource, $data = null)
public function get($resource, $data = null, $cookie=null)
{
return $this->request('GET', $resource, $data);
return $this->request('GET', $resource, $data, true, $cookie);
}

/**
Expand Down Expand Up @@ -226,7 +226,7 @@ public static function getUserAgent()
* @throws Payplug\Exception\HttpException When status code is not 2xx.
* @throws Payplug\Exception\ConnectionException When an error was encountered while connecting to the resource.
*/
private function request($httpVerb, $resource, array $data = null, $authenticated = true)
private function request($httpVerb, $resource, array $data = null, $authenticated = true, $cookie = null)
{
if (self::$REQUEST_HANDLER === null) {
$request = new CurlRequest();
Expand All @@ -246,6 +246,10 @@ private function request($httpVerb, $resource, array $data = null, $authenticate
$headers[] = 'PayPlug-Version: ' . $this->_configuration->getApiVersion();
}

if (!empty($cookie)) {
$headers[] = 'Cookie:' . $cookie;
}

$request->setopt(CURLOPT_FAILONERROR, false);
$request->setopt(CURLOPT_RETURNTRANSFER, true);
$request->setopt(CURLOPT_CUSTOMREQUEST, $httpVerb);
Expand All @@ -254,6 +258,7 @@ private function request($httpVerb, $resource, array $data = null, $authenticate
$request->setopt(CURLOPT_SSL_VERIFYPEER, true);
$request->setopt(CURLOPT_SSL_VERIFYHOST, 2);
$request->setopt(CURLOPT_CAINFO, self::$CACERT_PATH);
$request->setopt(CURLOPT_FOLLOWLOCATION, true);
if (!empty($data)) {
$request->setopt(CURLOPT_POSTFIELDS, json_encode($data));
}
Expand Down
85 changes: 85 additions & 0 deletions tests/unit_tests/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,89 @@ public function testPublishableKeys()
$this->assertEquals(200, $publishable_keys['httpStatus']);
$this->assertEquals('pk_test_everythingIsUnderControl', $publishable_keys['httpResponse']['publishable_key']);
}

/**
* Test the getClientIdAndSecretMask method.
*
* This test verifies that the getClientData method correctly retrieves
* the client_id, client_secret_mask , client_name client_type and mode from the user manager resource.
*
* @throws \Exception
*/
public function testGetClientData()
{
$response = array(
array(
'client_id' => 'test_client_id',
'client_secret_mask' => 'test_secret_mask',
'client_name' => 'test_client_name',
'client_type' => 'test_client_type',
'mode' => 'test_mode',
),
);

$this->_requestMock
->expects($this->once())
->method('exec')
->will($this->returnValue(json_encode($response)));

$this->_requestMock
->expects($this->any())
->method('getinfo')
->will($this->returnCallback(function($option) {
switch($option) {
case CURLINFO_HTTP_CODE:
return 200;
}
return null;
}));

$session = 'test_session_value';
$result = Authentication::getClientData($session, $this->_configuration);
$this->assertCount(1, $result);
$this->assertEquals('test_client_id', $result[0]['client_id']);
$this->assertEquals('test_secret_mask', $result[0]['client_secret_mask']);
$this->assertEquals('test_client_name', $result[0]['client_name']);
$this->assertEquals('test_client_type', $result[0]['client_type']);
$this->assertEquals('test_mode', $result[0]['mode']);

}

/**
* Test the createClientIdAndSecret correctly creates
* a client ID and client secret.
*
* @throws \Exception
*/
public function testCreateClientIdAndSecret()
{
$response = array(
array(
'client_id' => 'test_client_id',
'client_secret' => 'test_client_secret',
),
);

$this->_requestMock
->expects($this->once())
->method('exec')
->will($this->returnValue(json_encode($response)));

$this->_requestMock
->expects($this->any())
->method('getinfo')
->will($this->returnCallback(function($option) {
switch($option) {
case CURLINFO_HTTP_CODE:
return 200;
}
return null;
}));
$session = 'test_session_value';
$result = Authentication::createClientIdAndSecret('test_client_name', 'test_client_type', 'test_company_id', 'test_mode',$session, $this->_configuration);
$this->assertCount(1, $result);
$this->assertEquals('test_client_id', $result[0]['client_id']);
$this->assertEquals('test_client_secret', $result[0]['client_secret']);
}

}

0 comments on commit ee6f292

Please sign in to comment.