Skip to content
Snippets Groups Projects
Commit 1630c55e authored by Tim van Dijen's avatar Tim van Dijen
Browse files

Pull configuration out of factory

parent 24d856e6
Branches
Tags
No related merge requests found
...@@ -135,7 +135,10 @@ abstract class SessionHandler ...@@ -135,7 +135,10 @@ abstract class SessionHandler
*/ */
private static function createSessionHandler(): void private static function createSessionHandler(): void
{ {
$store = StoreFactory::getInstance(); $config = Configuration::getInstance();
$storeType = $config->getString('store.type', 'phpsession');
$store = StoreFactory::getInstance($storeType);
if ($store === false) { if ($store === false) {
self::$sessionHandler = new SessionHandlerPHP(); self::$sessionHandler = new SessionHandlerPHP();
} else { } else {
......
...@@ -30,19 +30,17 @@ abstract class StoreFactory implements Utils\ClearableState ...@@ -30,19 +30,17 @@ abstract class StoreFactory implements Utils\ClearableState
/** /**
* Retrieve our singleton instance. * Retrieve our singleton instance.
* *
* @param string $storeType The type of store we need to instantiate
* @return \SimpleSAML\Store\StoreInterface|false The data store, or false if it isn't enabled. * @return \SimpleSAML\Store\StoreInterface|false The data store, or false if it isn't enabled.
* *
* @throws \SimpleSAML\Error\CriticalConfigurationError * @throws \SimpleSAML\Error\CriticalConfigurationError
*/ */
public static function getInstance() public static function getInstance(string $storeType)
{ {
if (self::$instance !== null) { if (self::$instance !== null) {
return self::$instance; return self::$instance;
} }
$config = Configuration::getInstance();
$storeType = $config->getString('store.type', 'phpsession');
switch ($storeType) { switch ($storeType) {
case 'phpsession': case 'phpsession':
// we cannot support advanced features with the PHP session store // we cannot support advanced features with the PHP session store
...@@ -62,6 +60,7 @@ abstract class StoreFactory implements Utils\ClearableState ...@@ -62,6 +60,7 @@ abstract class StoreFactory implements Utils\ClearableState
try { try {
$className = Module::resolveClass($storeType, 'StoreInterface'); $className = Module::resolveClass($storeType, 'StoreInterface');
} catch (Exception $e) { } catch (Exception $e) {
$config = Configuration::getInstance();
$c = $config->toArray(); $c = $config->toArray();
$c['store.type'] = 'phpsession'; $c['store.type'] = 'phpsession';
throw new Error\CriticalConfigurationError( throw new Error\CriticalConfigurationError(
......
...@@ -386,7 +386,10 @@ class SP extends \SimpleSAML\Auth\Source ...@@ -386,7 +386,10 @@ class SP extends \SimpleSAML\Auth\Source
*/ */
private function getSLOEndpoints(): array private function getSLOEndpoints(): array
{ {
$store = StoreFactory::getInstance(); $config = Configuration::getInstance();
$storeType = $config->getString('store.type', 'phpsession');
$store = StoreFactory::getInstance($storeType);
$bindings = $this->metadata->getArray( $bindings = $this->metadata->getArray(
'SingleLogoutServiceBinding', 'SingleLogoutServiceBinding',
[ [
......
...@@ -158,12 +158,16 @@ class SQLNameID ...@@ -158,12 +158,16 @@ class SQLNameID
*/ */
private static function getStore(): Store\SQLStore private static function getStore(): Store\SQLStore
{ {
$store = StoreFactory::getInstance(); $config = Configuration::getInstance();
if (!($store instanceof Store\SQLStore)) { $storeType = $config->getString('store.type', 'phpsession');
throw new Error\Exception(
'SQL NameID store requires SimpleSAMLphp to be configured with a SQL datastore.' $store = StoreFactory::getInstance($storeType);
); Assert::isInstanceOf(
} $store,
Store\SQLStore::class,
'SQL NameID store requires SimpleSAMLphp to be configured with a SQL datastore.',
Error\Exception::class
);
return $store; return $store;
} }
......
...@@ -8,6 +8,7 @@ use Exception; ...@@ -8,6 +8,7 @@ use Exception;
use PDO; use PDO;
use SAML2\XML\saml\NameID; use SAML2\XML\saml\NameID;
use SimpleSAML\Assert\Assert; use SimpleSAML\Assert\Assert;
use SimpleSAML\Configuration;
use SimpleSAML\Logger; use SimpleSAML\Logger;
use SimpleSAML\Session; use SimpleSAML\Session;
use SimpleSAML\Store; use SimpleSAML\Store;
...@@ -209,7 +210,10 @@ class LogoutStore ...@@ -209,7 +210,10 @@ class LogoutStore
$sessionIndex = $randomUtils->generateID(); $sessionIndex = $randomUtils->generateID();
} }
$store = StoreFactory::getInstance(); $config = Configuration::getInstance();
$storeType = $config->getString('store.type', 'phpsession');
$store = StoreFactory::getInstance($storeType);
if ($store === false) { if ($store === false) {
// We don't have a datastore. // We don't have a datastore.
return; return;
...@@ -245,7 +249,10 @@ class LogoutStore ...@@ -245,7 +249,10 @@ class LogoutStore
*/ */
public static function logoutSessions(string $authId, NameID $nameId, array $sessionIndexes) public static function logoutSessions(string $authId, NameID $nameId, array $sessionIndexes)
{ {
$store = StoreFactory::getInstance(); $config = Configuration::getInstance();
$storeType = $config->getString('store.type', 'phpsession');
$store = StoreFactory::getInstance($storeType);
if ($store === false) { if ($store === false) {
// We don't have a datastore // We don't have a datastore
return false; return false;
......
...@@ -37,7 +37,9 @@ if (!($source instanceof Module\saml\Auth\Source\SP)) { ...@@ -37,7 +37,9 @@ if (!($source instanceof Module\saml\Auth\Source\SP)) {
$entityId = $source->getEntityId(); $entityId = $source->getEntityId();
$spconfig = $source->getMetadata(); $spconfig = $source->getMetadata();
$metaArray20 = $source->getHostedMetadata(); $metaArray20 = $source->getHostedMetadata();
$store = StoreFactory::getInstance();
$storeType = $config->getString('store.type', 'phpsession');
$store = StoreFactory::getInstance($storeType);
$metaBuilder = new Metadata\SAMLBuilder($entityId); $metaBuilder = new Metadata\SAMLBuilder($entityId);
$metaBuilder->addMetadataSP20($metaArray20, $source->getSupportedProtocols()); $metaBuilder->addMetadataSP20($metaArray20, $source->getSupportedProtocols());
......
...@@ -10,6 +10,7 @@ use SAML2\HTTPArtifact; ...@@ -10,6 +10,7 @@ use SAML2\HTTPArtifact;
use SAML2\Response; use SAML2\Response;
use SimpleSAML\Assert\Assert; use SimpleSAML\Assert\Assert;
use SimpleSAML\Auth; use SimpleSAML\Auth;
use SimpleSAML\Configuration;
use SimpleSAML\Error; use SimpleSAML\Error;
use SimpleSAML\Module; use SimpleSAML\Module;
use SimpleSAML\Logger; use SimpleSAML\Logger;
...@@ -159,7 +160,10 @@ $foundAuthnStatement = false; ...@@ -159,7 +160,10 @@ $foundAuthnStatement = false;
foreach ($assertions as $assertion) { foreach ($assertions as $assertion) {
// check for duplicate assertion (replay attack) // check for duplicate assertion (replay attack)
$store = StoreFactory::getInstance(); $config = Configuration::getInstance();
$storeType = $config->getString('store.type', 'phpsession');
$store = StoreFactory::getInstance($storeType);
if ($store !== false) { if ($store !== false) {
$aID = $assertion->getId(); $aID = $assertion->getId();
if ($store->get('saml.AssertionReceived', $aID) !== null) { if ($store->get('saml.AssertionReceived', $aID) !== null) {
......
...@@ -31,8 +31,11 @@ class StoreFactoryTest extends TestCase ...@@ -31,8 +31,11 @@ class StoreFactoryTest extends TestCase
{ {
Configuration::loadFromArray([], '[ARRAY]', 'simplesaml'); Configuration::loadFromArray([], '[ARRAY]', 'simplesaml');
$config = Configuration::getInstance();
$storeType = $config->getString('store.type', 'phpsession');
/** @var false $store */ /** @var false $store */
$store = StoreFactory::getInstance(); $store = StoreFactory::getInstance($storeType);
$this->assertFalse($store); $this->assertFalse($store);
} }
...@@ -43,10 +46,15 @@ class StoreFactoryTest extends TestCase ...@@ -43,10 +46,15 @@ class StoreFactoryTest extends TestCase
*/ */
public function phpSessionStore(): void public function phpSessionStore(): void
{ {
Configuration::loadFromArray([], '[ARRAY]', 'simplesaml'); Configuration::loadFromArray([
'store.type' => 'phpsession',
], '[ARRAY]', 'simplesaml');
$config = Configuration::getInstance();
$storeType = $config->getString('store.type');
/** @var false $store */ /** @var false $store */
$store = StoreFactory::getInstance(); $store = StoreFactory::getInstance($storeType);
$this->assertFalse($store); $this->assertFalse($store);
} }
...@@ -61,7 +69,10 @@ class StoreFactoryTest extends TestCase ...@@ -61,7 +69,10 @@ class StoreFactoryTest extends TestCase
'store.type' => 'memcache', 'store.type' => 'memcache',
], '[ARRAY]', 'simplesaml'); ], '[ARRAY]', 'simplesaml');
$store = StoreFactory::getInstance(); $config = Configuration::getInstance();
$storeType = $config->getString('store.type');
$store = StoreFactory::getInstance($storeType);
$this->assertInstanceOf(Store\MemcacheStore::class, $store); $this->assertInstanceOf(Store\MemcacheStore::class, $store);
} }
...@@ -77,8 +88,11 @@ class StoreFactoryTest extends TestCase ...@@ -77,8 +88,11 @@ class StoreFactoryTest extends TestCase
'store.redis.prefix' => 'phpunit_', 'store.redis.prefix' => 'phpunit_',
], '[ARRAY]', 'simplesaml'); ], '[ARRAY]', 'simplesaml');
$config = Configuration::getInstance();
$storeType = $config->getString('store.type');
/** @psalm-var \SimpleSAML\Store\RedisStore $store */ /** @psalm-var \SimpleSAML\Store\RedisStore $store */
$store = StoreFactory::getInstance(); $store = StoreFactory::getInstance($storeType);
$store->redis = $this->getMockBuilder(Client::class) $store->redis = $this->getMockBuilder(Client::class)
->setMethods(['get', 'set', 'setex', 'del', 'disconnect', '__destruct']) ->setMethods(['get', 'set', 'setex', 'del', 'disconnect', '__destruct'])
->disableOriginalConstructor() ->disableOriginalConstructor()
...@@ -99,7 +113,10 @@ class StoreFactoryTest extends TestCase ...@@ -99,7 +113,10 @@ class StoreFactoryTest extends TestCase
'store.sql.prefix' => 'phpunit_', 'store.sql.prefix' => 'phpunit_',
], '[ARRAY]', 'simplesaml'); ], '[ARRAY]', 'simplesaml');
$store = StoreFactory::getInstance(); $config = Configuration::getInstance();
$storeType = $config->getString('store.type');
$store = StoreFactory::getInstance($storeType);
$this->assertInstanceOf(Store\SQLStore::class, $store); $this->assertInstanceOf(Store\SQLStore::class, $store);
} }
...@@ -116,7 +133,10 @@ class StoreFactoryTest extends TestCase ...@@ -116,7 +133,10 @@ class StoreFactoryTest extends TestCase
'store.sql.prefix' => 'phpunit_', 'store.sql.prefix' => 'phpunit_',
], '[ARRAY]', 'simplesaml'); ], '[ARRAY]', 'simplesaml');
$store = StoreFactory::getInstance(); $config = Configuration::getInstance();
$storeType = $config->getString('store.type');
$store = StoreFactory::getInstance($storeType);
$this->assertInstanceOf(Store\SQLStore::class, $store); $this->assertInstanceOf(Store\SQLStore::class, $store);
} }
...@@ -134,7 +154,10 @@ class StoreFactoryTest extends TestCase ...@@ -134,7 +154,10 @@ class StoreFactoryTest extends TestCase
'store.sql.prefix' => 'phpunit_', 'store.sql.prefix' => 'phpunit_',
], '[ARRAY]', 'simplesaml'); ], '[ARRAY]', 'simplesaml');
StoreFactory::getInstance(); $config = Configuration::getInstance();
$storeType = $config->getString('store.type');
StoreFactory::getInstance($storeType);
} }
...@@ -143,8 +166,10 @@ class StoreFactoryTest extends TestCase ...@@ -143,8 +166,10 @@ class StoreFactoryTest extends TestCase
protected function tearDown(): void protected function tearDown(): void
{ {
$config = Configuration::getInstance(); $config = Configuration::getInstance();
$storeType = $config->getString('store.type', 'phpsession');
/** @var \SimpleSAML\Store\StoreInterface $store */ /** @var \SimpleSAML\Store\StoreInterface $store */
$store = StoreFactory::getInstance(); $store = StoreFactory::getInstance($storeType);
$this->clearInstance($config, Configuration::class); $this->clearInstance($config, Configuration::class);
$this->clearInstance($store, StoreFactory::class); $this->clearInstance($store, StoreFactory::class);
......
...@@ -46,8 +46,9 @@ class SQLNameIDTest extends TestCase ...@@ -46,8 +46,9 @@ class SQLNameIDTest extends TestCase
], '[ARRAY]', 'simplesaml'); ], '[ARRAY]', 'simplesaml');
$this->addGetDelete(); $this->addGetDelete();
$config = Configuration::getInstance(); $config = Configuration::getInstance();
$storeType = $config->getString('store.type');
/** @var \SimpleSAML\Store\StoreInterface $store */ /** @var \SimpleSAML\Store\StoreInterface $store */
$store = StoreFactory::getInstance(); $store = StoreFactory::getInstance($storeType);
$this->clearInstance($config, Configuration::class); $this->clearInstance($config, Configuration::class);
$this->clearInstance($store, StoreFactory::class); $this->clearInstance($store, StoreFactory::class);
} }
...@@ -62,13 +63,16 @@ class SQLNameIDTest extends TestCase ...@@ -62,13 +63,16 @@ class SQLNameIDTest extends TestCase
Configuration::loadFromArray([ Configuration::loadFromArray([
'store.type' => 'memcache', 'store.type' => 'memcache',
], '[ARRAY]', 'simplesaml'); ], '[ARRAY]', 'simplesaml');
$store = StoreFactory::getInstance(); $config = Configuration::getInstance();
$storeType = $config->getString('store.type');
$store = StoreFactory::getInstance($storeType);
$this->assertInstanceOf(Store\MemcacheStore::class, $store); $this->assertInstanceOf(Store\MemcacheStore::class, $store);
$this->expectException(Error\Exception::class); $this->expectException(Error\Exception::class);
$this->addGetDelete(); $this->addGetDelete();
$config = Configuration::getInstance(); $config = Configuration::getInstance();
$storeType = $config->getString('store.type');
/** @var \SimpleSAML\Store\StoreInterface $store */ /** @var \SimpleSAML\Store\StoreInterface $store */
$store = StoreFactory::getInstance(); $store = StoreFactory::getInstance($storeType);
$this->clearInstance($config, Configuration::class); $this->clearInstance($config, Configuration::class);
$this->clearInstance($store, StoreFactory::class); $this->clearInstance($store, StoreFactory::class);
} }
......
...@@ -35,7 +35,8 @@ if (!$idpMetadata->getBoolean('saml20.sendartifact', false)) { ...@@ -35,7 +35,8 @@ if (!$idpMetadata->getBoolean('saml20.sendartifact', false)) {
throw new Error\Error('NOACCESS'); throw new Error\Error('NOACCESS');
} }
$store = StoreFactory::getInstance(); $storeType = $config->getString('store.type', 'phpsession');
$store = StoreFactory::getInstance($storeType);
if ($store === false) { if ($store === false) {
throw new Exception('Unable to send artifact without a datastore configured.'); throw new Exception('Unable to send artifact without a datastore configured.');
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment