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 2, 2024
1 parent 5a8b65c commit 78cc939
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 8 deletions.
52 changes: 47 additions & 5 deletions lib/Payplug/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,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 @@ -89,9 +89,9 @@ public static function getPermissionsByLogin($email, $password)
{
$keys = self::getKeysByLogin($email, $password);
$payplug = Payplug::init(array(
'secretKey' => $keys['httpResponse']['secret_keys']['live'],
'apiVersion' => null,
));
'secretKey' => $keys['httpResponse']['secret_keys']['live'],
'apiVersion' => null,
));

$httpClient = new Core\HttpClient($payplug);
$response = $httpClient->get(Core\APIRoutes::getRoute(Core\APIRoutes::ACCOUNT_RESOURCE));
Expand Down Expand Up @@ -121,4 +121,46 @@ public static function getPublishableKeys(Payplug $payplug = null)
return false;
}
}

/**
* 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 getClientIdAndSecretMask(Payplug $payplug = null, $session = 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']
);
}

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
40 changes: 40 additions & 0 deletions tests/unit_tests/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,44 @@ 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 getClientIdAndSecretMask method correctly retrieves
* the client ID and client secret mask from the user manager resource.
*
* @throws \Exception
*/
public function testGetClientIdAndSecretMask()

{
$response = array(
'httpResponse' => array(
array('client_id' => 'test_client_id', 'client_secret_mask' => 'test_secret_mask')
)
);

$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;
}));

$result = Authentication::getClientIdAndSecretMask($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']);
}

}

0 comments on commit 78cc939

Please sign in to comment.