Skip to content

Commit 5b77e86

Browse files
committed
Move to Str helper in ClientForm
1 parent ef72119 commit 5b77e86

File tree

4 files changed

+41
-27
lines changed

4 files changed

+41
-27
lines changed

src/Factories/FormFactory.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@
1818

1919
use Nette\Forms\Form;
2020
use SimpleSAML\Error\Exception;
21+
use SimpleSAML\Module\oidc\Bridges\SspBridge;
2122
use SimpleSAML\Module\oidc\Forms\Controls\CsrfProtection;
23+
use SimpleSAML\Module\oidc\Helpers;
2224
use SimpleSAML\Module\oidc\ModuleConfig;
2325

2426
class FormFactory
2527
{
26-
public function __construct(private readonly ModuleConfig $moduleConfig, protected CsrfProtection $csrfProtection)
27-
{
28+
public function __construct(
29+
protected readonly ModuleConfig $moduleConfig,
30+
protected readonly CsrfProtection $csrfProtection,
31+
protected readonly SspBridge $sspBridge,
32+
protected readonly Helpers $helpers,
33+
) {
2834
}
2935

3036
/**
@@ -39,6 +45,11 @@ public function build(string $classname): Form
3945
}
4046

4147
/** @psalm-suppress UnsafeInstantiation */
42-
return new $classname($this->moduleConfig, $this->csrfProtection);
48+
return new $classname(
49+
$this->moduleConfig,
50+
$this->csrfProtection,
51+
$this->sspBridge,
52+
$this->helpers,
53+
);
4354
}
4455
}

src/Forms/ClientForm.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use SimpleSAML\Locale\Translate;
2121
use SimpleSAML\Module\oidc\Bridges\SspBridge;
2222
use SimpleSAML\Module\oidc\Forms\Controls\CsrfProtection;
23+
use SimpleSAML\Module\oidc\Helpers;
2324
use SimpleSAML\Module\oidc\ModuleConfig;
2425
use SimpleSAML\OpenID\Codebooks\ClientRegistrationTypesEnum;
2526
use Traversable;
@@ -62,6 +63,7 @@ public function __construct(
6263
protected readonly ModuleConfig $moduleConfig,
6364
protected CsrfProtection $csrfProtection,
6465
protected SspBridge $sspBridge,
66+
protected Helpers $helpers,
6567
) {
6668
parent::__construct();
6769

@@ -217,14 +219,14 @@ public function getValues(string|object|bool|null $returnType = null, ?array $co
217219
$values = parent::getValues(self::TYPE_ARRAY);
218220

219221
// Sanitize redirect_uri and allowed_origin
220-
$values['redirect_uri'] = $this->convertTextToArrayWithLinesAsValues((string)$values['redirect_uri']);
222+
$values['redirect_uri'] = $this->helpers->str()->convertTextToArray((string)$values['redirect_uri']);
221223
if (! $values['is_confidential'] && isset($values['allowed_origin'])) {
222-
$values['allowed_origin'] = $this->convertTextToArrayWithLinesAsValues((string)$values['allowed_origin']);
224+
$values['allowed_origin'] = $this->helpers->str()->convertTextToArray((string)$values['allowed_origin']);
223225
} else {
224226
$values['allowed_origin'] = [];
225227
}
226228
$values['post_logout_redirect_uri'] =
227-
$this->convertTextToArrayWithLinesAsValues((string)$values['post_logout_redirect_uri']);
229+
$this->helpers->str()->convertTextToArray((string)$values['post_logout_redirect_uri']);
228230

229231
$bclUri = trim((string)$values['backchannel_logout_uri']);
230232
$values['backchannel_logout_uri'] = empty($bclUri) ? null : $bclUri;
@@ -423,18 +425,6 @@ protected function getScopes(): array
423425
);
424426
}
425427

426-
/**
427-
* TODO mivanci Move to Str helper.
428-
* @return string[]
429-
*/
430-
protected function convertTextToArrayWithLinesAsValues(string $text): array
431-
{
432-
return array_filter(
433-
preg_split("/[\t\r\n]+/", $text),
434-
fn(string $line): bool => !empty(trim($line)),
435-
);
436-
}
437-
438428
/**
439429
* @return string[]
440430
*/

src/Services/Container.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,19 @@ public function __construct()
137137
$session = Session::getSessionFromRequest();
138138
$this->services[Session::class] = $session;
139139

140+
$sspBridge = new SspBridge();
141+
$this->services[SspBridge::class] = $sspBridge;
142+
143+
$helpers = new Helpers();
144+
$this->services[Helpers::class] = $helpers;
145+
140146
$csrfProtection = new CsrfProtection('{oidc:client:csrf_error}', $session);
141-
$formFactory = new FormFactory($moduleConfig, $csrfProtection);
147+
$formFactory = new FormFactory(
148+
$moduleConfig,
149+
$csrfProtection,
150+
$sspBridge,
151+
$helpers,
152+
);
142153
$this->services[FormFactory::class] = $formFactory;
143154

144155
$jsonWebKeySetService = new JsonWebKeySetService($moduleConfig);
@@ -150,9 +161,6 @@ public function __construct()
150161
$sessionMessagesService = new SessionMessagesService($session);
151162
$this->services[SessionMessagesService::class] = $sessionMessagesService;
152163

153-
$sspBridge = new SspBridge();
154-
$this->services[SspBridge::class] = $sspBridge;
155-
156164
$oidcMenu = new Menu();
157165
$this->services[Menu::class] = $oidcMenu;
158166

@@ -193,8 +201,6 @@ public function __construct()
193201
$stateService = new StateService();
194202
$this->services[StateService::class] = $stateService;
195203

196-
$helpers = new Helpers();
197-
198204
$core = new Core();
199205
$this->services[Core::class] = $core;
200206
$classInstanceBuilder = new ClassInstanceBuilder();

tests/unit/src/Forms/ClientFormTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@
66

77
use DateTimeImmutable;
88
use Laminas\Diactoros\ServerRequest;
9+
use PHPUnit\Framework\Attributes\CoversClass;
910
use PHPUnit\Framework\Attributes\DataProvider;
1011
use PHPUnit\Framework\Attributes\TestDox;
12+
use PHPUnit\Framework\Attributes\UsesClass;
1113
use PHPUnit\Framework\MockObject\MockObject;
1214
use PHPUnit\Framework\TestCase;
1315
use SimpleSAML\Module\oidc\Bridges\SspBridge;
1416
use SimpleSAML\Module\oidc\Codebooks\RegistrationTypeEnum;
1517
use SimpleSAML\Module\oidc\Forms\ClientForm;
1618
use SimpleSAML\Module\oidc\Forms\Controls\CsrfProtection;
19+
use SimpleSAML\Module\oidc\Helpers;
1720
use SimpleSAML\Module\oidc\ModuleConfig;
1821

19-
/**
20-
* @covers \SimpleSAML\Module\oidc\Forms\ClientForm
21-
*/
22+
#[CoversClass(ClientForm::class)]
23+
#[UsesClass(Helpers::class)]
2224
class ClientFormTest extends TestCase
2325
{
2426
protected MockObject $csrfProtectionMock;
@@ -29,6 +31,7 @@ class ClientFormTest extends TestCase
2931
protected MockObject $sspBridgeMock;
3032
protected MockObject $sspBridgeAuthMock;
3133
protected MockObject $sspBridgeAuthSourceMock;
34+
protected Helpers $helpers;
3235

3336
protected array $clientDataSample;
3437

@@ -42,6 +45,7 @@ public function setUp(): void
4245
$this->moduleConfigMock = $this->createMock(ModuleConfig::class);
4346
$this->serverRequestMock = $this->createMock(ServerRequest::class);
4447
$this->sspBridgeMock = $this->createMock(SspBridge::class);
48+
$this->helpers = new Helpers();
4549

4650
$this->sspBridgeAuthMock = $this->createMock(SspBridge\Auth::class);
4751
$this->sspBridgeMock->method('auth')->willReturn($this->sspBridgeAuthMock);
@@ -85,15 +89,18 @@ protected function sut(
8589
?ModuleConfig $moduleConfig = null,
8690
?CsrfProtection $csrfProtection = null,
8791
?SspBridge $sspBridge = null,
92+
?Helpers $helpers = null,
8893
): ClientForm {
8994
$moduleConfig ??= $this->moduleConfigMock;
9095
$csrfProtection ??= $this->csrfProtectionMock;
9196
$sspBridge ??= $this->sspBridgeMock;
97+
$helpers ??= $this->helpers;
9298

9399
return new ClientForm(
94100
$moduleConfig,
95101
$csrfProtection,
96102
$sspBridge,
103+
$helpers,
97104
);
98105
}
99106

0 commit comments

Comments
 (0)