Skip to content

Commit 3902d86

Browse files
authored
Merge pull request #96 from OpenConext/feature/allow-set-force-authn
Allow setting ForceAuthn on AuthnRequest
2 parents 5c5566f + 820ebc0 commit 3902d86

5 files changed

Lines changed: 42 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 4.1.10
2+
**New feature**
3+
* Allow setting the ForceAuthn property on AuthNRequest objects #96
4+
15
# 4.1.9
26
**New feature**
37
* Provide minimal Symfony 4 support #89

src/DependencyInjection/SurfnetSamlExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ private function parseRemoteServiceProviderConfiguration(
226226
*/
227227
private function parseCertificateData($path, array $provider)
228228
{
229+
$configuration = [];
229230
if (isset($provider['certificate_file']) && !isset($provider['certificate'])) {
230231
$configuration['certificateFile'] = $provider['certificate_file'];
231232
} elseif (isset($provider['certificate'])) {

src/Metadata/MetadataFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ private function buildKeyPairFrom(MetadataConfiguration $metadataConfiguration)
148148
private function getCertificateData($publicKeyFile)
149149
{
150150
$certificate = File::getFileContents($publicKeyFile);
151+
152+
$matches = [];
151153
preg_match(Certificate::CERTIFICATE_PATTERN, $certificate, $matches);
152154

153155
$certificateData = str_replace(array(' ', "\n"), '', $matches[1]);

src/SAML2/AuthnRequestFactory.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,20 @@ private static function createAuthnRequestFromHttpRequest(
127127
/**
128128
* @param ServiceProvider $serviceProvider
129129
* @param IdentityProvider $identityProvider
130+
* @param bool $forceAuthn
130131
* @return AuthnRequest
131132
*/
132-
public static function createNewRequest(ServiceProvider $serviceProvider, IdentityProvider $identityProvider)
133-
{
133+
public static function createNewRequest(
134+
ServiceProvider $serviceProvider,
135+
IdentityProvider $identityProvider,
136+
$forceAuthn = false
137+
) {
134138
$request = new SAML2AuthnRequest();
135139
$request->setAssertionConsumerServiceURL($serviceProvider->getAssertionConsumerUrl());
136140
$request->setDestination($identityProvider->getSsoUrl());
137141
$request->setIssuer($serviceProvider->getEntityId());
138142
$request->setProtocolBinding(Constants::BINDING_HTTP_POST);
143+
$request->setForceAuthn($forceAuthn);
139144
$request->setSignatureKey(self::loadPrivateKey(
140145
$serviceProvider->getPrivateKey(PrivateKey::NAME_DEFAULT)
141146
));

src/Tests/Unit/SAML2/AuthnRequestFactoryTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
namespace Surfnet\SamlBundle\Tests\Unit\SAML2;
44

5+
use Mockery as m;
56
use PHPUnit_Framework_TestCase as UnitTest;
7+
use RobRichards\XMLSecLibs\XMLSecurityKey;
8+
use SAML2\Configuration\PrivateKey;
9+
use Surfnet\SamlBundle\Entity\IdentityProvider;
10+
use Surfnet\SamlBundle\Entity\ServiceProvider;
611
use Surfnet\SamlBundle\SAML2\AuthnRequest;
712
use Surfnet\SamlBundle\SAML2\AuthnRequestFactory;
813
use Symfony\Component\HttpFoundation\Request;
@@ -46,4 +51,27 @@ public function an_exception_is_thrown_when_a_request_cannot_be_inflated()
4651

4752
AuthnRequestFactory::createFromHttpRequest($request);
4853
}
54+
55+
/**
56+
* @test
57+
* @group saml2
58+
*/
59+
public function verify_force_authn_works_as_intended()
60+
{
61+
$sp = m::mock(ServiceProvider::class);
62+
$sp->shouldReceive('getAssertionConsumerUrl')->andReturn('https://example-sp.com/acs');
63+
$sp->shouldReceive('getEntityId')->andReturn('https://example-sp.com/');
64+
65+
$pk = new PrivateKey(__DIR__.'/../../../Resources/keys/development_privatekey.pem', 'key-for-test', '');
66+
67+
$sp->shouldReceive('getPrivateKey')->andReturn($pk);
68+
69+
$idp = m::mock(IdentityProvider::class);
70+
$idp->shouldReceive('getSsoUrl')->andReturn('https://example-idp.com/sso');
71+
72+
$authnRequest = AuthnRequestFactory::createNewRequest($sp, $idp, true);
73+
$this->assertTrue($authnRequest->isForceAuthn());
74+
$authnRequest = AuthnRequestFactory::createNewRequest($sp, $idp, false);
75+
$this->assertFalse($authnRequest->isForceAuthn());
76+
}
4977
}

0 commit comments

Comments
 (0)