Skip to content

Commit f3468da

Browse files
authored
Merge pull request #5 from BulkGate/jwt-default-parameters
Jwt default parameters
2 parents c195734 + 716d8a9 commit f3468da

2 files changed

Lines changed: 129 additions & 3 deletions

File tree

src/User/Sign.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class Sign
2626

2727
private Logger $logger;
2828

29+
/**
30+
* @var array<string, mixed>
31+
*/
32+
private array $default_parameters = [];
33+
2934

3035
public function __construct(Settings $settings, Connection $connection, Url $url, Configuration $configuration, Language $language, Logger $logger)
3136
{
@@ -37,6 +42,14 @@ public function __construct(Settings $settings, Connection $connection, Url $url
3742
$this->logger = $logger;
3843
}
3944

45+
/**
46+
* @param array<array-key, mixed> $parameters
47+
*/
48+
public function setDefaultParameters(array $parameters): void
49+
{
50+
$this->default_parameters = $parameters;
51+
}
52+
4053

4154
/**
4255
* @param array<string, mixed> $parameters
@@ -51,9 +64,13 @@ public function authenticate(bool $reload = false, array $parameters = []): ?str
5164
'application_product' => $this->configuration->product(),
5265
'application_language' => $this->language->get(),
5366
'application_version' => $this->configuration->version(),
54-
'application_parameters' => array_merge([
55-
'guest' => $token === null,
56-
], $parameters),
67+
'application_parameters' => array_merge(
68+
$this->default_parameters,
69+
$parameters,
70+
[
71+
'guest' => $token === null,
72+
],
73+
),
5774
], $token ?? '');
5875
}
5976

tests/User/SignTestJwt.phpt

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace BulkGate\Plugin\User\Test;
4+
5+
/**
6+
* @author Lukáš Piják 2023 TOPefekt s.r.o.
7+
* @link https://www.bulkgate.com/
8+
*/
9+
10+
use Mockery;
11+
use Tester\{Assert, TestCase};
12+
use BulkGate\Plugin\{Debug\Logger,
13+
Eshop\ConfigurationDefault,
14+
IO\Connection,
15+
IO\Url,
16+
Localization\Language,
17+
Settings\Settings,
18+
User\Sign,
19+
Utils\Jwt};
20+
21+
require_once __DIR__ . '/../bootstrap.php';
22+
23+
/**
24+
* @testCase
25+
*/
26+
class SignTest extends TestCase
27+
{
28+
public function testDefaultParameters(): void
29+
{
30+
$sign = new Sign($settings = Mockery::mock(Settings::class), Mockery::mock(Connection::class), new Url(), new ConfigurationDefault('url', 'eshop', '1.0', 'Test Eshop'), $language = Mockery::mock(Language::class), Mockery::mock(Logger::class));
31+
$sign->setDefaultParameters(['test1' => 'default_test1', 'test3' => 'default_test3']);
32+
33+
$settings->shouldReceive('load')->with('static:application_token', true)->andReturn('test_application_token');
34+
$settings->shouldReceive('load')->with('static:application_id')->andReturn(12345);
35+
$language->shouldReceive('get')->withNoArgs()->once()->andReturn('cs');
36+
37+
$jwt = Mockery::mock('overload:' . Jwt::class);
38+
$jwt->shouldReceive('encode')->with([
39+
'application_id' => 12345,
40+
'application_installation' => 'url',
41+
'application_product' => 'eshop',
42+
'application_language' => 'cs',
43+
'application_version' => '1.0',
44+
'application_parameters' => [
45+
'guest' => false,
46+
'test1' => 'test1',
47+
'test2' => 'test2',
48+
'test3' => 'default_test3'
49+
]
50+
], 'test_application_token')->once()->andReturn('0.0.0');
51+
52+
53+
Assert::match('~^[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+$~', $sign->authenticate(true, ['test1' => 'test1', 'test2' => 'test2']));
54+
}
55+
56+
public function testGuestParameterOverride(): void
57+
{
58+
$sign = new Sign($settings = Mockery::mock(Settings::class), Mockery::mock(Connection::class), new Url(), new ConfigurationDefault('url', 'eshop', '1.0', 'Test Eshop'), $language = Mockery::mock(Language::class), Mockery::mock(Logger::class));
59+
60+
$settings->shouldReceive('load')->with('static:application_token', true)->andReturn('test_application_token');
61+
$settings->shouldReceive('load')->with('static:application_id')->andReturn(12345);
62+
$language->shouldReceive('get')->withNoArgs()->once()->andReturn('cs');
63+
64+
$jwt = Mockery::mock('overload:' . Jwt::class);
65+
$jwt->shouldReceive('encode')->with([
66+
'application_id' => 12345,
67+
'application_installation' => 'url',
68+
'application_product' => 'eshop',
69+
'application_language' => 'cs',
70+
'application_version' => '1.0',
71+
'application_parameters' => [
72+
'guest' => false,
73+
]
74+
], 'test_application_token')->once()->andReturn('0.0.0');
75+
76+
Assert::match('~^[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+$~', $sign->authenticate(true, ['guest' => 'someValue']));
77+
}
78+
79+
public function testGuestParameterNullToken(): void
80+
{
81+
$sign = new Sign($settings = Mockery::mock(Settings::class), Mockery::mock(Connection::class), new Url(), new ConfigurationDefault('url', 'eshop', '1.0', 'Test Eshop'), $language = Mockery::mock(Language::class), Mockery::mock(Logger::class));
82+
83+
$settings->shouldReceive('load')->with('static:application_token', false)->andReturn(null);
84+
$settings->shouldReceive('load')->with('static:application_id')->andReturn(12345);
85+
$language->shouldReceive('get')->withNoArgs()->once()->andReturn('cs');
86+
87+
$jwt = Mockery::mock('overload:' . Jwt::class);
88+
$jwt->shouldReceive('encode')->with([
89+
'application_id' => 12345,
90+
'application_installation' => 'url',
91+
'application_product' => 'eshop',
92+
'application_language' => 'cs',
93+
'application_version' => '1.0',
94+
'application_parameters' => [
95+
'guest' => true,
96+
]
97+
], '')->once()->andReturn('0.0.0');
98+
99+
Assert::match('~^[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+$~', $sign->authenticate());
100+
}
101+
102+
103+
public function tearDown(): void
104+
{
105+
Mockery::close();
106+
}
107+
}
108+
109+
(new SignTest())->run();

0 commit comments

Comments
 (0)