diff --git a/lib/Payplug/Authentication.php b/lib/Payplug/Authentication.php index a4650e3..0c02144 100644 --- a/lib/Payplug/Authentication.php +++ b/lib/Payplug/Authentication.php @@ -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 * @@ -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)); @@ -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; + } + } diff --git a/lib/Payplug/Core/APIRoutes.php b/lib/Payplug/Core/APIRoutes.php index dc39efc..5874209 100644 --- a/lib/Payplug/Core/APIRoutes.php +++ b/lib/Payplug/Core/APIRoutes.php @@ -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 @@ -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. * @@ -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'; diff --git a/lib/Payplug/Core/HttpClient.php b/lib/Payplug/Core/HttpClient.php index 0da94c5..08ed9ca 100644 --- a/lib/Payplug/Core/HttpClient.php +++ b/lib/Payplug/Core/HttpClient.php @@ -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); } /** @@ -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(); @@ -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); @@ -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)); } diff --git a/tests/unit_tests/AuthenticationTest.php b/tests/unit_tests/AuthenticationTest.php index d659728..637880a 100644 --- a/tests/unit_tests/AuthenticationTest.php +++ b/tests/unit_tests/AuthenticationTest.php @@ -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']); + } + }