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 263 264 265 266 267 268 269 270 271
|
<?php
namespace SimpleSAML\Test\Module\saml\Auth\Source;
use PHPUnit\Framework\TestCase;
use \SimpleSAML_Configuration as Configuration;
/**
* Custom Exception to throw to terminate a TestCase.
*/
class ExitTestException extends \Exception
{
private $testResult;
public function __construct($testResult)
{
parent::__construct("ExitTestException", 0, null);
$this->testResult = $testResult;
}
public function getTestResult()
{
return $this->testResult;
}
}
/**
* Wrap the SSP sspmod_saml_Auth_Source_SP class
* - Use introspection to make startSSO2Test available
* - Override sendSAML2AuthnRequest() to catch the AuthnRequest being sent
*/
class SP_Tester extends \sspmod_saml_Auth_Source_SP
{
public function __construct($info, $config)
{
parent::__construct($info, $config);
}
public function startSSO2Test(\SimpleSAML_Configuration $idpMetadata, array $state)
{
$reflector = new \ReflectionObject($this);
$method = $reflector->getMethod('startSSO2');
$method->setAccessible(true);
$method->invoke($this, $idpMetadata, $state);
}
// override the method that sends the request to avoid sending anything
public function sendSAML2AuthnRequest(array &$state, \SAML2\Binding $binding, \SAML2\AuthnRequest $ar)
{
// Exit test. Continuing would mean running into a assert(FALSE)
throw new ExitTestException(
array(
'state' => $state,
'binding' => $binding,
'ar' => $ar,
)
);
}
}
/**
* Set of test cases for sspmod_saml_Auth_Source_SP.
*/
class SP_Test extends TestCase
{
private $idpMetadata = null;
private $idpConfigArray;
private function getIdpMetadata()
{
if (!$this->idpMetadata) {
$this->idpMetadata = new \SimpleSAML_Configuration(
$this->idpConfigArray,
'Auth_Source_SP_Test::getIdpMetadata()'
);
}
return $this->idpMetadata;
}
protected function setUp()
{
$this->idpConfigArray = array(
'metadata-set' => 'saml20-idp-remote',
'entityid' => 'https://engine.surfconext.nl/authentication/idp/metadata',
'SingleSignOnService' => array(
array(
'Binding' => 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect',
'Location' => 'https://engine.surfconext.nl/authentication/idp/single-sign-on',
),
),
'keys' => array(
array(
'encryption' => false,
'signing' => true,
'type' => 'X509Certificate',
'X509Certificate' =>
'MIID3zCCAsegAwIBAgIJAMVC9xn1ZfsuMA0GCSqGSIb3DQEBCwUAMIGFMQswCQYDVQQGEwJOTDEQMA4GA1UECAwHVXRyZ'.
'WNodDEQMA4GA1UEBwwHVXRyZWNodDEVMBMGA1UECgwMU1VSRm5ldCBCLlYuMRMwEQYDVQQLDApTVVJGY29uZXh0MSYwJA'.
'YDVQQDDB1lbmdpbmUuc3VyZmNvbmV4dC5ubCAyMDE0MDUwNTAeFw0xNDA1MDUxNDIyMzVaFw0xOTA1MDUxNDIyMzVaMIG'.
'FMQswCQYDVQQGEwJOTDEQMA4GA1UECAwHVXRyZWNodDEQMA4GA1UEBwwHVXRyZWNodDEVMBMGA1UECgwMU1VSRm5ldCBC'.
'LlYuMRMwEQYDVQQLDApTVVJGY29uZXh0MSYwJAYDVQQDDB1lbmdpbmUuc3VyZmNvbmV4dC5ubCAyMDE0MDUwNTCCASIwD'.
'QYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKthMDbB0jKHefPzmRu9t2h7iLP4wAXr42bHpjzTEk6gttHFb4l/hFiz1Y'.
'BI88TjiH6hVjnozo/YHA2c51us+Y7g0XoS7653lbUN/EHzvDMuyis4Xi2Ijf1A/OUQfH1iFUWttIgtWK9+fatXoGUS6ti'.
'rQvrzVh6ZstEp1xbpo1SF6UoVl+fh7tM81qz+Crr/Kroan0UjpZOFTwxPoK6fdLgMAieKSCRmBGpbJHbQ2xxbdykBBrBb'.
'dfzIX4CDepfjE9h/40ldw5jRn3e392jrS6htk23N9BWWrpBT5QCk0kH3h/6F1Dm6TkyG9CDtt73/anuRkvXbeygI4wml9'.
'bL3rE8CAwEAAaNQME4wHQYDVR0OBBYEFD+Ac7akFxaMhBQAjVfvgGfY8hNKMB8GA1UdIwQYMBaAFD+Ac7akFxaMhBQAjV'.
'fvgGfY8hNKMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAC8L9D67CxIhGo5aGVu63WqRHBNOdo/FAGI7LUR'.
'DFeRmG5nRw/VXzJLGJksh4FSkx7aPrxNWF1uFiDZ80EuYQuIv7bDLblK31ZEbdg1R9LgiZCdYSr464I7yXQY9o6FiNtSK'.
'ZkQO8EsscJPPy/Zp4uHAnADWACkOUHiCbcKiUUFu66dX0Wr/v53Gekz487GgVRs8HEeT9MU1reBKRgdENR8PNg4rbQfLc'.
'3YQKLWK7yWnn/RenjDpuCiePj8N8/80tGgrNgK/6fzM3zI18sSywnXLswxqDb/J+jgVxnQ6MrsTf1urM8MnfcxG/82oHI'.
'wfMh/sXPCZpo+DTLkhQxctJ3M=',
),
),
);
$this->config = Configuration::loadFromArray(array(), '[ARRAY]', 'simplesaml');
}
/**
* Create a SAML AuthnRequest using sspmod_saml_Auth_Source_SP
*
* @param array $state The state array to use in the test. This is an array of the parameters described in section
* 2 of https://simplesamlphp.org/docs/development/saml:sp
*
* @return \SAML2\AuthnRequest The AuthnRequest generated.
*/
private function createAuthnRequest($state = array())
{
$info = array('AuthId' => 'default-sp');
$config = array();
$as = new SP_Tester($info, $config);
/** @var \SAML2\AuthnRequest $ar */
$ar = null;
try {
$as->startSSO2Test($this->getIdpMetadata(), $state);
$this->assertTrue(false, 'Expected ExitTestException');
} catch (ExitTestException $e) {
$r = $e->getTestResult();
$ar = $r['ar'];
}
return $ar;
}
/**
* Test generating an AuthnRequest
* @test
*/
public function testAuthnRequest()
{
/** @var \SAML2\AuthnRequest $ar */
$ar = $this->createAuthnRequest();
// Assert values in the generated AuthnRequest
/** @var $xml \DOMElement */
$xml = $ar->toSignedXML();
$q = \SAML2\Utils::xpQuery($xml, '/samlp:AuthnRequest/@Destination');
$this->assertEquals(
$this->idpConfigArray['SingleSignOnService'][0]['Location'],
$q[0]->value
);
$q = \SAML2\Utils::xpQuery($xml, '/samlp:AuthnRequest/saml:Issuer');
$this->assertEquals(
'http://localhost/simplesaml/module.php/saml/sp/metadata.php/default-sp',
$q[0]->textContent
);
}
/**
* Test setting a Subject
* @test *
*/
public function testNameID()
{
$state = array(
'saml:NameID' => array('Value' => 'user@example.org', 'Format' => \SAML2\Constants::NAMEID_UNSPECIFIED)
);
/** @var \SAML2\AuthnRequest $ar */
$ar = $this->createAuthnRequest($state);
$nameID = $ar->getNameId();
$this->assertEquals($state['saml:NameID']['Value'], $nameID->value);
$this->assertEquals($state['saml:NameID']['Format'], $nameID->Format);
/** @var $xml \DOMElement */
$xml = $ar->toSignedXML();
$q = \SAML2\Utils::xpQuery($xml, '/samlp:AuthnRequest/saml:Subject/saml:NameID/@Format');
$this->assertEquals(
$state['saml:NameID']['Format'],
$q[0]->value
);
$q = \SAML2\Utils::xpQuery($xml, '/samlp:AuthnRequest/saml:Subject/saml:NameID');
$this->assertEquals(
$state['saml:NameID']['Value'],
$q[0]->textContent
);
}
/**
* Test setting an AuthnConextClassRef
* @test *
*/
public function testAuthnContextClassRef()
{
$state = array(
'saml:AuthnContextClassRef' => 'http://example.com/myAuthnContextClassRef'
);
/** @var \SAML2\AuthnRequest $ar */
$ar = $this->createAuthnRequest($state);
$a = $ar->getRequestedAuthnContext();
$this->assertEquals(
$state['saml:AuthnContextClassRef'],
$a['AuthnContextClassRef'][0]
);
/** @var $xml \DOMElement */
$xml = $ar->toSignedXML();
$q = \SAML2\Utils::xpQuery($xml, '/samlp:AuthnRequest/samlp:RequestedAuthnContext/saml:AuthnContextClassRef');
$this->assertEquals(
$state['saml:AuthnContextClassRef'],
$q[0]->textContent
);
}
/**
* Test setting ForcedAuthn
* @test *
*/
public function testForcedAuthn()
{
$state = array(
'ForceAuthn' => true
);
/** @var \SAML2\AuthnRequest $ar */
$ar = $this->createAuthnRequest($state);
$this->assertEquals(
$state['ForceAuthn'],
$ar->getForceAuthn()
);
/** @var $xml \DOMElement */
$xml = $ar->toSignedXML();
$q = \SAML2\Utils::xpQuery($xml, '/samlp:AuthnRequest/@ForceAuthn');
$this->assertEquals(
$state['ForceAuthn'] ? 'true' : 'false',
$q[0]->value
);
}
}
|