authx - Extend support for legacy-auth to APIv4
authorTim Otten <totten@civicrm.org>
Sat, 5 Feb 2022 00:23:15 +0000 (16:23 -0800)
committerTim Otten <totten@civicrm.org>
Sat, 5 Feb 2022 00:29:16 +0000 (16:29 -0800)
ext/authx/authx.php

index 81b25c11df64f323d1b5460bc4540b0568f8b177..220be9577aee573afdad7969e1ea826ff11269d1 100644 (file)
@@ -37,8 +37,12 @@ Civi::dispatcher()->addListener('civi.invoke.auth', function($e) {
     }
   }
 
-  if (count($e->args) > 2 && $e->args[1] === 'ajax' && $e->args[2] === 'rest' && (!empty($_REQUEST['api_key']) || !empty($_REQUEST['key']))) {
-    return (new \Civi\Authx\LegacyRestAuthenticator())->auth($e, ['flow' => 'legacyrest', 'cred' => 'Bearer ' . $_REQUEST['api_key'] ?? '', 'siteKey' => $_REQUEST['key'] ?? NULL]);
+  // Accept legacy auth (?key=...&api_key=...) for 'civicrm/ajax/rest' and 'civicrm/ajax/api4/*'.
+  // The use of `?key=` could clash on some endpoints. Only accept on a small list of endpoints that are compatible with it.
+  if (count($e->args) > 2 && $e->args[1] === 'ajax' && in_array($e->args[2], ['rest', 'api4'])) {
+    if ((!empty($_REQUEST['api_key']) || !empty($_REQUEST['key']))) {
+      return (new \Civi\Authx\LegacyRestAuthenticator())->auth($e, ['flow' => 'legacyrest', 'cred' => 'Bearer ' . $_REQUEST['api_key'] ?? '', 'siteKey' => $_REQUEST['key'] ?? NULL]);
+    }
   }
 });