Skip to content

Commit

Permalink
Decorate of session factory to create custom bags
Browse files Browse the repository at this point in the history
  • Loading branch information
parijke committed Dec 20, 2023
1 parent 5061e06 commit 372b8db
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 69 deletions.
4 changes: 4 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ services:
- '@Surfnet\StepupSelfService\SelfServiceBundle\Security\Authentication\AuthenticatedSessionStateHandler'
- []
- '@logger'

Surfnet\StepupSelfService\SamlStepupProviderBundle\Session\SessionFactoryWithAttributeBag:
decorates: session.factory
arguments: [ '@.inner' ]

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ private function loadProviderConfiguration(
$stateHandlerDefinition = new Definition(
StateHandler::class,
[
new Reference('gssp.sessionbag'),
new Reference('request_stack'),
$provider
]
);

$container->setDefinition('gssp.provider.' . $provider . '.statehandler', $stateHandlerDefinition);

$providerDefinition = new Definition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,12 @@ public function get(string $providerName): Provider

return $this->providers[$providerName];
}

/**
* @return Provider[]
*/
public function getAll(): array
{
return $this->providers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,3 @@ services:
Surfnet\StepupSelfService\SamlStepupProviderBundle\Provider\ProviderRepository:
alias: gssp.provider_repository

gssp.sessionbag:
class: Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag
# factory: ['@request_stack', 'getSession']
arguments: ['gssp']

gssp.session.attribute_bag:
public: false
class: Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag
arguments:
- '__gssp__'
- '/'
calls:
- [setName, ['gssp']]
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@

namespace Surfnet\StepupSelfService\SamlStepupProviderBundle\Saml;

use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;

final readonly class StateHandler
{
const REQUEST_ID = 'request_id';

public function __construct(
private AttributeBagInterface $attributeBag,
private string $provider,
private RequestStack $requestStack,
private string $provider,
) {
}

Expand All @@ -46,16 +47,22 @@ public function getRequestId(): ?string

public function clear(): void
{
$this->attributeBag->remove($this->provider);
$session = $this->requestStack->getSession();

$session->getBag('gssp.provider.' . $this->provider)->clear();

$this->requestStack->getSession()->remove($this->provider);
}

protected function set(string $key, $value): void
{
$this->attributeBag->set($this->provider . '/' . $key, $value);
$session = $this->requestStack->getSession();
$session->getBag('gssp.provider.' . $this->provider)->set($key, $value);
}

protected function get(string $key)
{
return $this->attributeBag->get($this->provider . '/' . $key);
$session = $this->requestStack->getSession();
return $session->getBag('gssp.provider.' . $this->provider)->get($key);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Surfnet\StepupSelfService\SamlStepupProviderBundle\Session;

use Surfnet\StepupSelfService\SamlStepupProviderBundle\Provider\ProviderRepository;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
use Symfony\Component\HttpFoundation\Session\SessionFactoryInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

/**
* This class is responsible for creating a session with attribute bags for each provider.
* It implements the SessionFactoryInterface.
*/
final readonly class SessionFactoryWithAttributeBag implements SessionFactoryInterface
{
public function __construct(
private SessionFactoryInterface $delegate,
private ProviderRepository $providerRepository,
) {
}

public function createSession(): SessionInterface
{
$session = $this->delegate->createSession();

foreach ($this->providerRepository->getAll() as $provider) {
$bag = new AttributeBag('gssp.provider.' . $provider->getName());
$bag->setName('gssp.provider.' . $provider->getName());
$session->registerBag($bag);
}

return $session;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class SurfnetStepupSelfServiceSamlStepupProviderBundle extends Bundle
{
public function build(ContainerBuilder $container): void
{
$container->addCompilerPass(new StateHandlerSessionPass());
$container->addCompilerPass(new ViewConfigCollectionPass());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Surfnet\StepupSelfService\SamlStepupProviderBundle\Provider\Provider;
use Surfnet\StepupSelfService\SamlStepupProviderBundle\Saml\StateHandler;
use Surfnet\StepupSelfService\SelfServiceBundle\Service\GsspUserAttributeService;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;

class GsspUserAttributeServiceTest extends m\Adapter\Phpunit\MockeryTestCase
Expand All @@ -54,7 +55,7 @@ public function test_if_it_adds_extensions(): void
'azuremfa',
m::spy(ServiceProvider::class),
m::spy(IdentityProvider::class),
new StateHandler(m::mock(AttributeBagInterface::class), 'test')
new StateHandler(m::mock(RequestStack::class), 'test')
);

$extensions = m::mock(Extensions::class);
Expand Down Expand Up @@ -84,7 +85,7 @@ public function test_if_it_skips_extensions(): void
'tiqr',
m::spy(ServiceProvider::class),
m::spy(IdentityProvider::class),
new StateHandler(m::mock(AttributeBagInterface::class), 'test')
new StateHandler(m::mock(RequestStack::class), 'test')
);

$extensions = m::mock(Extensions::class);
Expand Down

0 comments on commit 372b8db

Please sign in to comment.