3 require_once 'authx.civix.php';
5 use CRM_Authx_ExtensionUtil
as E
;
8 Civi
::dispatcher()->addListener('civi.invoke.auth', function($e) {
9 $params = ($_SERVER['REQUEST_METHOD'] === 'GET') ?
$_GET : $_POST;
10 $siteKey = $_SERVER['HTTP_X_CIVI_KEY'] ??
$params['_authxSiteKey'] ??
NULL;
12 if (!empty($_SERVER['HTTP_X_CIVI_AUTH'])) {
13 return (new \Civi\Authx\
Authenticator())->auth($e, ['flow' => 'xheader', 'cred' => $_SERVER['HTTP_X_CIVI_AUTH'], 'siteKey' => $siteKey]);
16 if (!empty($_SERVER['HTTP_AUTHORIZATION']) && !empty(Civi
::settings()->get('authx_header_cred'))) {
17 return (new \Civi\Authx\
Authenticator())->auth($e, ['flow' => 'header', 'cred' => $_SERVER['HTTP_AUTHORIZATION'], 'siteKey' => $siteKey]);
20 if (!empty($params['_authx'])) {
21 if ((implode('/', $e->args
) === 'civicrm/authx/login')) {
22 (new \Civi\Authx\
Authenticator())->auth($e, ['flow' => 'login', 'cred' => $params['_authx'], 'useSession' => TRUE, 'siteKey' => $siteKey]);
23 _authx_redact(['_authx']);
25 elseif (!empty($params['_authxSes'])) {
26 (new \Civi\Authx\
Authenticator())->auth($e, ['flow' => 'auto', 'cred' => $params['_authx'], 'useSession' => TRUE, 'siteKey' => $siteKey]);
27 if ($_SERVER['REQUEST_METHOD'] === 'GET') {
28 _authx_reload(implode('/', $e->args
), $_SERVER['QUERY_STRING']);
31 _authx_redact(['_authx', '_authxSes']);
35 (new \Civi\Authx\
Authenticator())->auth($e, ['flow' => 'param', 'cred' => $params['_authx'], 'siteKey' => $siteKey]);
36 _authx_redact(['_authx']);
40 // Accept legacy auth (?key=...&api_key=...) for 'civicrm/ajax/rest' and 'civicrm/ajax/api4/*'.
41 // The use of `?key=` could clash on some endpoints. Only accept on a small list of endpoints that are compatible with it.
42 if (count($e->args
) > 2 && $e->args
[1] === 'ajax' && in_array($e->args
[2], ['rest', 'api4'])) {
43 if ((!empty($_REQUEST['api_key']) ||
!empty($_REQUEST['key']))) {
44 return (new \Civi\Authx\
LegacyRestAuthenticator())->auth($e, ['flow' => 'legacyrest', 'cred' => 'Bearer ' . $_REQUEST['api_key'] ??
'', 'siteKey' => $_REQUEST['key'] ??
NULL]);
50 * Perform a system login.
52 * This is useful for backend scripts that need to switch to a specific user.
54 * As needed, this will update the Civi session and CMS data.
56 * @param array{flow: ?string, useSession: ?bool, principal: ?array, cred: ?string,} $details
57 * Describe the authentication process with these properties:
59 * - string $flow (default 'script');
60 * The type of authentication flow being used
61 * Ex: 'param', 'header', 'auto'
62 * - bool $useSession (default FALSE)
63 * If TRUE, then the authentication should be persistent (in a session variable).
64 * If FALSE, then the authentication should be ephemeral (single page-request).
66 * And then ONE of these properties to describe the user/principal:
69 * The credential, as formatted in the 'Authorization' header.
70 * Ex: 'Bearer 12345', 'Basic ASDFFDSA=='
72 * Description of a validated principal.
73 * Must include 'contactId', 'userId', xor 'user'
74 * @return array{contactId: int, userId: ?int, flow: string, credType: string, useSession: bool}
75 * An array describing the authenticated session.
76 * @throws \Civi\Authx\AuthxException
78 function authx_login(array $details): array {
79 $defaults = ['flow' => 'script', 'useSession' => FALSE];
80 $details = array_merge($defaults, $details);
81 $auth = new \Civi\Authx\
Authenticator();
82 $auth->setRejectMode('exception');
83 $auth->auth(NULL, array_merge($defaults, $details));
84 return \CRM_Core_Session
::singleton()->get("authx");
88 * @return \Civi\Authx\AuthxInterface
90 function _authx_uf() {
91 $class = 'Civi\\Authx\\' . CIVICRM_UF
;
92 return class_exists($class) ?
new $class() : new \Civi\Authx\
None();
96 * For parameter-based authentication, this option will hide parameters.
97 * This is mostly a precaution, hedging against the possibility that some routes
98 * make broad use of $_GET or $_PARAMS.
102 function _authx_redact(array $keys) {
103 foreach ($keys as $key) {
104 unset($_POST[$key], $_GET[$key], $_REQUEST[$key]);
109 * Reload the current page-view.
111 * @param string $route
112 * @param string $queryString
114 function _authx_reload($route, $queryString) {
115 parse_str($queryString, $query);
116 foreach (array_keys($query) as $key) {
117 if (CRM_Utils_String
::startsWith($key, '_authx')) {
121 $url = CRM_Utils_System
::url($route, $query, TRUE, NULL, FALSE, CRM_Core_Config
::singleton()->userSystem
->isFrontEndPage());
122 CRM_Utils_System
::redirect($url);
126 * Implements hook_civicrm_config().
128 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/
130 function authx_civicrm_config(&$config) {
131 _authx_civix_civicrm_config($config);
135 * Implements hook_civicrm_install().
137 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
139 function authx_civicrm_install() {
140 _authx_civix_civicrm_install();
144 * Implements hook_civicrm_postInstall().
146 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall
148 function authx_civicrm_postInstall() {
149 _authx_civix_civicrm_postInstall();
153 * Implements hook_civicrm_uninstall().
155 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_uninstall
157 function authx_civicrm_uninstall() {
158 _authx_civix_civicrm_uninstall();
162 * Implements hook_civicrm_enable().
164 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
166 function authx_civicrm_enable() {
167 _authx_civix_civicrm_enable();
171 * Implements hook_civicrm_disable().
173 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_disable
175 function authx_civicrm_disable() {
176 _authx_civix_civicrm_disable();
180 * Implements hook_civicrm_upgrade().
182 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_upgrade
184 function authx_civicrm_upgrade($op, CRM_Queue_Queue
$queue = NULL) {
185 return _authx_civix_civicrm_upgrade($op, $queue);
189 * Implements hook_civicrm_entityTypes().
191 * Declare entity types provided by this module.
193 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_entityTypes
195 function authx_civicrm_entityTypes(&$entityTypes) {
196 _authx_civix_civicrm_entityTypes($entityTypes);
200 * Implements hook_civicrm_permission().
202 * @see CRM_Utils_Hook::permission()
204 function authx_civicrm_permission(&$permissions) {
205 $permissions['authenticate with password'] = E
::ts('AuthX: Authenticate to services with password');
206 $permissions['authenticate with api key'] = E
::ts('AuthX: Authenticate to services with API key');
209 // --- Functions below this ship commented out. Uncomment as required. ---
212 * Implements hook_civicrm_preProcess().
214 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_preProcess
216 //function authx_civicrm_preProcess($formName, &$form) {
221 * Implements hook_civicrm_navigationMenu().
223 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_navigationMenu
225 //function authx_civicrm_navigationMenu(&$menu) {
226 // _authx_civix_insert_navigation_menu($menu, 'Mailings', array(
227 // 'label' => E::ts('New subliminal message'),
228 // 'name' => 'mailing_subliminal_message',
229 // 'url' => 'civicrm/mailing/subliminal',
230 // 'permission' => 'access CiviMail',
231 // 'operator' => 'OR',
234 // _authx_civix_navigationMenu($menu);