@@ -243,24 +243,16 @@ private function addAuthenticationPluiginNode()
243243 ->then (function ($ config ) {
244244 switch ($ config ['type ' ]) {
245245 case 'basic ' :
246- if (empty ($ config ['username ' ]) || empty ($ config ['password ' ])) {
247- throw new InvalidConfigurationException ('Authentication "basic" requires both "username" and "password". ' );
248- }
246+ $ this ->validateAuthenticationType (['username ' , 'password ' ], $ config , 'basic ' );
249247 break ;
250248 case 'bearer ' :
251- if (empty ($ config ['token ' ])) {
252- throw new InvalidConfigurationException ('Authentication "bearer" requires a "token". ' );
253- }
249+ $ this ->validateAuthenticationType (['token ' ], $ config , 'bearer ' );
254250 break ;
255251 case 'service ' :
256- if (empty ($ config ['service ' ])) {
257- throw new InvalidConfigurationException ('Authentication "service" requires a "service". ' );
258- }
252+ $ this ->validateAuthenticationType (['service ' ], $ config , 'service ' );
259253 break ;
260254 case 'wsse ' :
261- if (empty ($ config ['username ' ]) || empty ($ config ['password ' ])) {
262- throw new InvalidConfigurationException ('Authentication "wsse" requires both "username" and "password". ' );
263- }
255+ $ this ->validateAuthenticationType (['username ' , 'password ' ], $ config , 'wsse ' );
264256 break ;
265257 }
266258
@@ -283,4 +275,32 @@ private function addAuthenticationPluiginNode()
283275
284276 return $ node ;
285277 }
278+
279+ /**
280+ * Validate that the configuration fragment has the specified keys and none other.
281+ *
282+ * @param array $expected Fields that must exist
283+ * @param array $actual Actual configuration hashmap
284+ * @param string $authName Name of authentication method for error messages
285+ *
286+ * @throws InvalidConfigurationException If $actual does not have exactly the keys specified in $expected (plus 'type')
287+ */
288+ private function validateAuthenticationType (array $ expected , array $ actual , $ authName )
289+ {
290+ unset($ actual ['type ' ]);
291+ $ actual = array_keys ($ actual );
292+ sort ($ actual );
293+ sort ($ expected );
294+
295+ if ($ expected === $ actual ) {
296+ return ;
297+ }
298+
299+ throw new InvalidConfigurationException (sprintf (
300+ 'Authentication "%s" requires %s but got %s ' ,
301+ $ authName ,
302+ implode (', ' , $ expected ),
303+ implode (', ' , $ actual )
304+ ));
305+ }
286306}
0 commit comments