22
33declare (strict_types=1 );
44
5- namespace SimpleSAML \Test \Module \core \Auth \Source \ Selector ;
5+ namespace SimpleSAML \Test \Module \core \Auth \Source ;
66
77use PHPUnit \Framework \TestCase ;
88use SAML2 \Exception \Protocol \NoAuthnContextException ;
99use SimpleSAML \Assert \AssertionFailedException ;
1010use SimpleSAML \Auth ;
1111use SimpleSAML \Configuration ;
1212use SimpleSAML \Error \Exception ;
13- use SimpleSAML \Module \core \Auth \Source \Selector \ RequestedAuthnContextSelector ;
13+ use SimpleSAML \Module \core \Auth \Source \RequestedAuthnContextSelector ;
1414
1515/**
1616 * @covers \SimpleSAML\Module\core\Auth\Source\AbstractSourceSelector
17- * @covers \SimpleSAML\Module\core\Auth\Source\Selector\ RequestedAuthnContextSelector
17+ * @covers \SimpleSAML\Module\core\Auth\Source\RequestedAuthnContextSelector
1818 */
1919class RequestedAuthnContextSelectorTest extends TestCase
2020{
@@ -78,8 +78,9 @@ public function setUp(): void
7878
7979
8080 /**
81+ * No RequestedAuthnContext
8182 */
82- public function testAuthentication (): void
83+ public function testAuthenticationVariant1 (): void
8384 {
8485 $ info = ['AuthId ' => 'selector ' ];
8586 $ config = $ this ->sourceConfig ->getArray ('selector ' );
@@ -94,24 +95,75 @@ public static function doAuthentication(Auth\Source $as, array $state): void
9495 {
9596 // Dummy
9697 }
98+ };
99+
100+ $ state = ['saml:RequestedAuthnContext ' => ['AuthnContextClassRef ' => null ]];
101+ $ selector ->authenticate ($ state );
102+ $ this ->assertArrayNotHasKey ('saml:AuthnContextClassRef ' , $ state );
103+ }
97104
105+
106+ /**
107+ * Specific RequestedAuthnContext
108+ */
109+ public function testAuthenticationVariant2 (): void
110+ {
111+ $ info = ['AuthId ' => 'selector ' ];
112+ $ config = $ this ->sourceConfig ->getArray ('selector ' );
113+
114+ $ selector = new class ($ info , $ config ) extends RequestedAuthnContextSelector {
98115 /**
99- * @param array &$state
116+ * @param \SimpleSAML\Auth\Source $as
117+ * @param array $state
100118 * @return void
101119 */
102- public function authenticate ( array & $ state ): void
120+ public static function doAuthentication ( Auth \ Source $ as , array $ state ): void
103121 {
104- $ state [ ' finished ' ] = true ;
122+ // Dummy
105123 }
106124 };
107125
108- $ state = ['saml:RequestedAuthnContext ' => ['AuthnContextClassRef ' => null ]];
126+ $ state = ['saml:RequestedAuthnContext ' => ['AuthnContextClassRef ' => [ ' urn:x-simplesamlphp:loa1 ' ] ]];
109127 $ selector ->authenticate ($ state );
110- $ this ->assertTrue ($ state ['finished ' ]);
128+ $ this ->assertArrayHasKey ('saml:AuthnContextClassRef ' , $ state );
129+ $ this ->assertEquals ('urn:x-simplesamlphp:loa1 ' , $ state ['saml:AuthnContextClassRef ' ]);
111130 }
112131
113132
114133 /**
134+ * Specific RequestedAuthnContext with comparison=exact
135+ */
136+ public function testAuthenticationVariant3 (): void
137+ {
138+ $ info = ['AuthId ' => 'selector ' ];
139+ $ config = $ this ->sourceConfig ->getArray ('selector ' );
140+
141+ $ selector = new class ($ info , $ config ) extends RequestedAuthnContextSelector {
142+ /**
143+ * @param \SimpleSAML\Auth\Source $as
144+ * @param array $state
145+ * @return void
146+ */
147+ public static function doAuthentication (Auth \Source $ as , array $ state ): void
148+ {
149+ // Dummy
150+ }
151+ };
152+
153+ $ state = [
154+ 'saml:RequestedAuthnContext ' => [
155+ 'AuthnContextClassRef ' => ['urn:x-simplesamlphp:loa1 ' ],
156+ 'Comparison ' => 'exact ' ,
157+ ],
158+ ];
159+ $ selector ->authenticate ($ state );
160+ $ this ->assertArrayHasKey ('saml:AuthnContextClassRef ' , $ state );
161+ $ this ->assertEquals ('urn:x-simplesamlphp:loa1 ' , $ state ['saml:AuthnContextClassRef ' ]);
162+ }
163+
164+
165+ /**
166+ * Missing source
115167 */
116168 public function testIncompleteConfigurationThrowsExceptionVariant1 (): void
117169 {
@@ -123,6 +175,7 @@ public function testIncompleteConfigurationThrowsExceptionVariant1(): void
123175 10 => [
124176 'identifier ' => 'urn:x-simplesamlphp:loa1 ' ,
125177 ],
178+ 'default ' => 'phpunit ' ,
126179 ],
127180 ],
128181 ]);
@@ -140,6 +193,7 @@ public function testIncompleteConfigurationThrowsExceptionVariant1(): void
140193
141194
142195 /**
196+ * Missing identifier
143197 */
144198 public function testIncompleteConfigurationThrowsExceptionVariant2 (): void
145199 {
@@ -151,6 +205,7 @@ public function testIncompleteConfigurationThrowsExceptionVariant2(): void
151205 10 => [
152206 'source ' => 'loa1 ' ,
153207 ],
208+ 'default ' => 'phpunit ' ,
154209 ],
155210 ],
156211 ]);
@@ -167,6 +222,35 @@ public function testIncompleteConfigurationThrowsExceptionVariant2(): void
167222 }
168223
169224
225+ /**
226+ * Missing default
227+ */
228+ public function testIncompleteConfigurationThrowsExceptionVariant3 (): void
229+ {
230+ $ sourceConfig = Configuration::loadFromArray ([
231+ 'selector ' => [
232+ 'core:RequestedAuthnContextSelector ' ,
233+
234+ 'contexts ' => [
235+ 10 => [
236+ 'source ' => 'loa1 ' ,
237+ ],
238+ ],
239+ ],
240+ ]);
241+
242+ Configuration::setPreLoadedConfig ($ this ->sourceConfig , 'authsources.php ' );
243+
244+ $ info = ['AuthId ' => 'selector ' ];
245+ $ config = $ sourceConfig ->getArray ('selector ' );
246+
247+ $ this ->expectException (AssertionFailedException::class);
248+ $ this ->expectExceptionMessage ('Expected the key "default" to exist. ' );
249+
250+ new RequestedAuthnContextSelector ($ info , $ config );
251+ }
252+
253+
170254 /**
171255 * @dataProvider provideRequestedAuthnContext
172256 * @param array $requestedAuthnContext The RequestedAuthnContext
0 commit comments