1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
<?php
declare(strict_types=1);
namespace SimpleSAML\Test\Module\core\Controller;
use ReflectionClass;
use SimpleSAML\Auth\Simple;
use SimpleSAML\Auth\AuthenticationFactory;
use SimpleSAML\Configuration;
use SimpleSAML\Error\Exception;
use SimpleSAML\HTTP\RunnableResponse;
use SimpleSAML\Locale\Localization;
use SimpleSAML\Module\core\Controller;
use SimpleSAML\Session;
use SimpleSAML\Test\Utils\ClearStateTestCase;
use SimpleSAML\XHTML\Template;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
/**
* Set of tests for the controllers in the "core" module.
*
* For now, this test extends ClearStateTestCase so that it doesn't interfere with other tests. Once every class has
* been made PSR-7-aware, that won't be necessary any longer.
*
* @package SimpleSAML\Test
*/
class LoginTest extends ClearStateTestCase
{
/** @var array */
protected $authSources;
/** @var \SimpleSAML\Configuration */
protected $config;
/** @var \SimpleSAML\Configuration[] */
protected $loadedConfigs;
/**
* Set up for each test.
* @return void
*/
protected function setUp()
{
parent::setUp();
$this->authSources = [
'admin' => [
'core:adminPassword'
],
'example-userpass' => [
'exampleauth:UserPass',
'username:password' => [
'uid' => ['test']
]
]
];
$this->config = Configuration::loadFromArray(
[
'baseurlpath' => 'https://example.org/simplesaml',
'module.enable' => ['exampleauth' => true],
'usenewui' => true,
],
'[ARRAY]',
'simplesaml'
);
Configuration::setPreLoadedConfig($this->config, 'config.php');
}
/**
* Test that authentication is started immediately if we hit the login endpoint and there's only one non-admin
* source configured.
* @return void
*/
public function testAutomaticLoginWhenOnlyOneSource(): void
{
$asConfig = Configuration::loadFromArray($this->authSources);
Configuration::setPreLoadedConfig($asConfig, 'authsources.php');
$request = new Request();
$session = Session::getSessionFromRequest();
$factory = new AuthenticationFactory($this->config, $session);
$c = new Controller\Login($this->config, $session, $factory);
/** @var \SimpleSAML\HTTP\RunnableResponse $response */
$response = $c->login($request);
$this->assertInstanceOf(RunnableResponse::class, $response);
$this->assertIsCallable($response->getCallable());
$arguments = $response->getArguments();
$this->assertArrayHasKey('ErrorURL', $arguments[0]);
$this->assertArrayHasKey('ReturnTo', $arguments[0]);
}
/**
* Test that the user can choose what auth source to use when there are multiple defined (admin excluded).
* @return void
*/
public function testMultipleAuthSources(): void
{
$_SERVER['REQUEST_URI'] = '/';
$asConfig = Configuration::loadFromArray(
array_merge(
$this->authSources,
[
'example-static' => [
'exampleauth:StaticSource',
'uid' => ['test']
]
]
)
);
Configuration::setPreLoadedConfig($asConfig, 'authsources.php');
$request = new Request();
$session = Session::getSessionFromRequest();
$factory = new AuthenticationFactory($this->config, $session);
$c = new Controller\Login($this->config, $session, $factory);
/** @var \SimpleSAML\XHTML\Template $response */
$response = $c->login($request);
$this->assertInstanceOf(Template::class, $response);
$this->assertEquals('core:login.twig', $response->getTemplateName());
$this->assertArrayHasKey('sources', $response->data);
$this->assertArrayHasKey('example-userpass', $response->data['sources']);
$this->assertArrayHasKey('example-static', $response->data['sources']);
}
/**
* Test that specifying an invalid auth source while trying to login raises an exception.
* @return void
*/
public function testLoginWithInvalidAuthSource(): void
{
$asConfig = Configuration::loadFromArray($this->authSources);
Configuration::setPreLoadedConfig($asConfig, 'authsources.php');
$request = new Request();
$session = Session::getSessionFromRequest();
$factory = new AuthenticationFactory($this->config, $session);
$c = new Controller\Login($this->config, $session, $factory);
$this->expectException(Exception::class);
$c->login($request, 'invalid-auth-source');
}
/**
* Test that we get redirected to /account/authsource when accessing the login endpoint while being already
* authenticated.
* @return void
*/
public function testLoginWhenAlreadyAuthenticated(): void
{
$asConfig = Configuration::loadFromArray($this->authSources);
Configuration::setPreLoadedConfig($asConfig, 'authsources.php');
$session = Session::getSessionFromRequest();
$session->setConfiguration($this->config);
$class = new ReflectionClass($session);
$authData = $class->getProperty('authData');
$authData->setAccessible(true);
$authData->setValue($session, [
'example-userpass' => [
'exampleauth:UserPass',
'Attributes' => ['uid' => ['test']],
'Authority' => 'example-userpass',
'AuthnInstant' => time(),
'Expire' => time() + 8 * 60 * 60
]
]);
$factory = new AuthenticationFactory($this->config, $session);
$request = new Request();
$c = new Controller\Login($this->config, $session, $factory);
/** @var \Symfony\Component\HttpFoundation\RedirectResponse $response */
$response = $c->login($request);
$this->assertInstanceOf(RedirectResponse::class, $response);
$this->assertEquals(
'https://example.org/simplesaml/module.php/core/account/example-userpass',
$response->getTargetUrl()
);
}
/**
* Test that triggering the logout controller actually proceeds to log out from the specified source.
* @return void
*/
public function testLogout(): void
{
$asConfig = Configuration::loadFromArray($this->authSources);
Configuration::setPreLoadedConfig($asConfig, 'authsources.php');
$session = Session::getSessionFromRequest();
$factory = new AuthenticationFactory($this->config, $session);
$c = new Controller\Login($this->config, $session, $factory);
$response = $c->logout('example-userpass');
$this->assertInstanceOf(RunnableResponse::class, $response);
$this->assertIsCallable($response->getCallable());
$this->assertEquals('/simplesaml/', $response->getArguments()[0]);
}
/**
* Test that accessing the "account" endpoint without being authenticated gets you redirected to the "login"
* endpoint.
* @return void
*/
public function testNotAuthenticated(): void
{
$asConfig = Configuration::loadFromArray($this->authSources);
Configuration::setPreLoadedConfig($asConfig, 'authsources.php');
$session = Session::getSessionFromRequest();
$factory = new AuthenticationFactory($this->config, $session);
$c = new Controller\Login($this->config, $session, $factory);
/** @var RedirectResponse $response */
$response = $c->account('example-userpass');
$this->assertInstanceOf(RedirectResponse::class, $response);
$this->assertEquals(
'https://example.org/simplesaml/module.php/core/login/example-userpass',
$response->getTargetUrl()
);
}
/**
* Test that we are presented with a regular page if we are authenticated and try to access the "account" endpoint.
* @return void
*/
public function testAuthenticated(): void
{
$asConfig = Configuration::loadFromArray($this->authSources);
Configuration::setPreLoadedConfig($asConfig, 'authsources.php');
$session = Session::getSessionFromRequest();
$class = new ReflectionClass($session);
$authData = $class->getProperty('authData');
$authData->setAccessible(true);
$authData->setValue($session, [
'example-userpass' => [
'exampleauth:UserPass',
'Attributes' => ['uid' => ['test']],
'Authority' => 'example-userpass',
'AuthnInstant' => time(),
'Expire' => time() + 8 * 60 * 60
]
]);
$factory = new AuthenticationFactory($this->config, $session);
$c = new Controller\Login($this->config, $session, $factory);
/** @var \SimpleSAML\XHTML\Template $response */
$response = $c->account('example-userpass');
$this->assertInstanceOf(Template::class, $response);
$this->assertEquals('auth_status.twig', $response->getTemplateName());
}
}
|