Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PRE-2605: Get the JWT from Hydra #86

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions lib/Payplug/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,36 @@ public static function getPublishableKeys(Payplug $payplug = null)
}
}

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

if ($payplug === null) {
$payplug = Payplug::getDefaultConfiguration();
}

$httpClient = new Core\HttpClient($payplug);
try {
return $httpClient->post(
Core\APIRoutes::getRoute(Core\APIRoutes::$HYDRA_RESOURCE),
array('client_id' => $client_id, 'grant_type' => 'client_credentials')
);
} catch (Exception $e) {
return array();
}
}

/**
* Validates the Payplug token
*
Expand Down
16 changes: 15 additions & 1 deletion 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;

/**
* @var string the root URL of the User Manager microService
*/
Expand Down Expand Up @@ -82,6 +87,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;
}

/**
* @description set $USER_MANAGER_RESOURCE from plugin
* @param $microServiceBaseUrl
Expand All @@ -105,4 +119,4 @@ 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';

APIRoutes::$HYDRA_RESOURCE = 'Microservice Url';
68 changes: 68 additions & 0 deletions tests/unit_tests/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
class AuthenticationTest extends \PHPUnit\Framework\TestCase
{
private $_configuration;
private $_requestMock;

/**
Expand Down Expand Up @@ -308,4 +309,71 @@ public function testSetKratosSessionNull()
$this->expectExceptionMessage('The session value must be set.');
Authentication::setKratosSession(null);
}

public function testGenerateJWTWithEmptyClientId()
{
$jwt = Authentication::generateJWT($this->_configuration);
$this->assertEquals(array(), $jwt);
}

public function testGenerateJWTWhenErrorResponse()
{
$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('some_client_id', $this->_configuration);

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

public function testGenerateJWTWhenSuccessResponse()
{
$response = array(
'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osokNsFQNrwxo8f9affUkiuEwZedK9sreDfVL_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 200;
}
return null;
}));

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

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