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'])) {
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']);
42 * @return \Civi\Authx\AuthxInterface
44 function _authx_uf() {
45 $class = 'Civi\\Authx\\' . CIVICRM_UF
;
46 return class_exists($class) ?
new $class() : new \Civi\Authx\
None();
50 * For parameter-based authentication, this option will hide parameters.
51 * This is mostly a precaution, hedging against the possibility that some routes
52 * make broad use of $_GET or $_PARAMS.
56 function _authx_redact(array $keys) {
57 foreach ($keys as $key) {
58 unset($_POST[$key], $_GET[$key], $_REQUEST[$key]);
63 * Reload the current page-view.
65 * @param string $route
66 * @param string $queryString
68 function _authx_reload($route, $queryString) {
69 parse_str($queryString, $query);
70 foreach (array_keys($query) as $key) {
71 if (CRM_Utils_String
::startsWith($key, '_authx')) {
75 $url = CRM_Utils_System
::url($route, $query, TRUE, NULL, FALSE, CRM_Core_Config
::singleton()->userSystem
->isFrontEndPage());
76 CRM_Utils_System
::redirect($url);
80 * Implements hook_civicrm_config().
82 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/
84 function authx_civicrm_config(&$config) {
85 _authx_civix_civicrm_config($config);
89 * Implements hook_civicrm_install().
91 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
93 function authx_civicrm_install() {
94 _authx_civix_civicrm_install();
98 * Implements hook_civicrm_postInstall().
100 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall
102 function authx_civicrm_postInstall() {
103 _authx_civix_civicrm_postInstall();
107 * Implements hook_civicrm_uninstall().
109 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_uninstall
111 function authx_civicrm_uninstall() {
112 _authx_civix_civicrm_uninstall();
116 * Implements hook_civicrm_enable().
118 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
120 function authx_civicrm_enable() {
121 _authx_civix_civicrm_enable();
125 * Implements hook_civicrm_disable().
127 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_disable
129 function authx_civicrm_disable() {
130 _authx_civix_civicrm_disable();
134 * Implements hook_civicrm_upgrade().
136 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_upgrade
138 function authx_civicrm_upgrade($op, CRM_Queue_Queue
$queue = NULL) {
139 return _authx_civix_civicrm_upgrade($op, $queue);
143 * Implements hook_civicrm_entityTypes().
145 * Declare entity types provided by this module.
147 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_entityTypes
149 function authx_civicrm_entityTypes(&$entityTypes) {
150 _authx_civix_civicrm_entityTypes($entityTypes);
154 * Implements hook_civicrm_permission().
156 * @see CRM_Utils_Hook::permission()
158 function authx_civicrm_permission(&$permissions) {
159 $permissions['authenticate with password'] = E
::ts('AuthX: Authenticate to services with password');
160 $permissions['authenticate with api key'] = E
::ts('AuthX: Authenticate to services with API key');
163 // --- Functions below this ship commented out. Uncomment as required. ---
166 * Implements hook_civicrm_preProcess().
168 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_preProcess
170 //function authx_civicrm_preProcess($formName, &$form) {
175 * Implements hook_civicrm_navigationMenu().
177 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_navigationMenu
179 //function authx_civicrm_navigationMenu(&$menu) {
180 // _authx_civix_insert_navigation_menu($menu, 'Mailings', array(
181 // 'label' => E::ts('New subliminal message'),
182 // 'name' => 'mailing_subliminal_message',
183 // 'url' => 'civicrm/mailing/subliminal',
184 // 'permission' => 'access CiviMail',
185 // 'operator' => 'OR',
188 // _authx_civix_navigationMenu($menu);