Skip to content

Commit

Permalink
PRE-2605: Get the JWT from Hydra
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoaraupp committed Oct 10, 2024
1 parent 5a8b65c commit b1f5112
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
23 changes: 23 additions & 0 deletions lib/Payplug/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,27 @@ public static function getPublishableKeys(Payplug $payplug = null)
return false;
}
}

/**
* Generate a token JWT.
*
* @param Payplug $payplug the client configuration
*
* @return array the token JWT
*
* @throws Exception
*/
public static function generateJWT(Payplug $payplug = null)
{
if ($payplug === null) {
$payplug = Payplug::getDefaultConfiguration();
}

$httpClient = new Core\HttpClient($payplug);
try {
return $httpClient->post(Core\APIRoutes::getRoute(Core\APIRoutes::$HYDRA_RESOURCE));
} catch (Exception $e) {
return array();
}
}
}
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 Hydra microService
*/
public static $HYDRA_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 $HYDRA_RESOURCE from plugin
* @param $microServiceBaseUrl
*/
public static function setHydraResource($microServiceBaseUrl)
{
self::$HYDRA_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::$HYDRA_RESOURCE = 'Microservice Url';

63 changes: 62 additions & 1 deletion tests/unit_tests/AuthenticationTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace Payplug;
use Payplug;
use Payplug\Core\HttpClient;

/**
* @group unit
Expand All @@ -10,6 +9,7 @@
*/
class AuthenticationTest extends \PHPUnit\Framework\TestCase
{
private $_configuration;
private $_requestMock;

/**
Expand Down Expand Up @@ -177,4 +177,65 @@ public function testPublishableKeys()
$this->assertEquals(200, $publishable_keys['httpStatus']);
$this->assertEquals('pk_test_everythingIsUnderControl', $publishable_keys['httpResponse']['publishable_key']);
}

public function testWhenErrorResponse()
{
$response = array(
'access_token' => 'eyJhbGciOiJSUzI1NiIsImtpZCI6ImE2NmQxZWU3LTQzMWMtNDNiYS04NzA4LWQ1MzNkNTVmZjhlZCIsInR5cCI6IkpXVCJ9.eyJhdWQiOltdLCJjbGllbnRfaWQiOiIyNzdlNDk4MS0yYTBjLTQ4NGEtYTE3Ni1hZWNhOWRjNDhkNTQiLCJleHAiOjE3Mjc5NjU3OTQsImV4dCI6eyJjb21wYW55X2lkIjoiZDE0NzQ1ZmQtMTc5Yy00N2IxLTlkZDgtMTk3OTVmMzQ1MjJiIiwicmVhbG1faWQiOiIxNTM0N2NjNy0xZThmLTQzYzMtYjJjZi1iZDMxY2M5ZWU4YTEiLCJyZXNvdXJjZV9hY2Nlc3MiOnsiYXBpIjp7InJvbGVzIjpbIk1FUkNIQU5UX0FQUFMiXX19fSwiaWF0IjoxNzI3OTYyMTk0LCJpc3MiOiJodHRwczovLzEyNy4wLjAuMTo0NDQ0IiwianRpIjoiMGNhMzUyOWItOGQ5Zi00NDQxLWEyNDAtMGZhY2YyNDNmN2JiIiwibmJmIjoxNzI3OTYyMTk0LCJzY3AiOltdLCJzdWIiOiIyNzdlNDk4MS0yYTBjLTQ4NGEtYTE3Ni1hZWNhOWRjNDhkNTQifQ.K1pavlVMz4D4cAJtL8IklLXIos7ZjiC9YSofb343uLkjXdhbgel23k0GyEE_JkQ2xSSB46XLYQp0j-M1AaJIoNjCfVR-O1yWNpLYnLM07ECETO3kQc63vcvzOm5trn5oBq_T3FE78EmAIA5B3oaSu_m5_qUBci_C7oM0ItMMIpFnKYqk2ta8y2eUFFPu7detxJRLlBK4I7hW0xAt07GNhfyRl8eN7twC3aYFFkUejZmuEB_FmZkj7OqZDsNblDR21Ci_cahuZmt9WOmIqTW58l7wxXOB9vq5APtBi2LpQtE52ARofUNCWWOK1KHz_vSQlGiGDM6_56K85Whfkkj-LYvLRRZUuIXE2m528JTtnCarZr7Md5P9zHQOZyIbcWrjiUdM_daI1vELPT4UaCBIfpy-vY0wywiPtoosokNsFQNrwxo8f9affUkiuEwZedK9sreDfVL_tmrz5Bh6XzxMZB5ZiVQckUUPF7LKrBB8qDwotYvwdLIN-Wy3l2IeeTzF_NOFmO6mrNift2RhSQZP0s7Xfn2dVK1eiyO4gERNrsPvasb9nB15PIQ57wwWKzN8ue6z9utAX6YThTgc2fadrOWYMeo2W4c7KmuKr9hhLK2ThIWRM7H5rQq3H7Ke5AzlKyCQdgFlQzSl0O1gjzA0T4AnfuNW1zNedSfUsqMJCfM',
'expires_in' => 3599,
'scope' => '',
'token_type' => 'bearer'
);

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

$jwt = Authentication::generateJWT($this->_configuration);

$this->assertEquals(array(), $jwt);
}

public function testWhenSuccessResponse()
{
$response = array(
'access_token' => 'eyJhbGciOiJSUzI1NiIsImtpZCI6ImE2NmQxZWU3LTQzMWMtNDNiYS04NzA4LWQ1MzNkNTVmZjhlZCIsInR5cCI6IkpXVCJ9.eyJhdWQiOltdLCJjbGllbnRfaWQiOiIyNzdlNDk4MS0yYTBjLTQ4NGEtYTE3Ni1hZWNhOWRjNDhkNTQiLCJleHAiOjE3Mjc5NjU3OTQsImV4dCI6eyJjb21wYW55X2lkIjoiZDE0Nz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NW1zNedSfUsqMJCfM',
'expires_in' => 3599,
'scope' => '',
'token_type' => 'bearer'
);

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

$jwt = Authentication::generateJWT($this->_configuration);

$this->assertEquals(200, $jwt['httpStatus']);
$this->assertEquals($response, $jwt['httpResponse']);
}
}

0 comments on commit b1f5112

Please sign in to comment.