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 * Perform a system login.
44 * This is useful for backend scripts that need to switch to a specific user.
46 * As needed, this will update the Civi session and CMS data.
48 * @param array{flow: ?string, useSession: ?bool, principal: ?array, cred: ?string,} $details
49 * Describe the authentication process with these properties:
51 * - string $flow (default 'script');
52 * The type of authentication flow being used
53 * Ex: 'param', 'header', 'auto'
54 * - bool $useSession (default FALSE)
55 * If TRUE, then the authentication should be persistent (in a session variable).
56 * If FALSE, then the authentication should be ephemeral (single page-request).
58 * And then ONE of these properties to describe the user/principal:
61 * The credential, as formatted in the 'Authorization' header.
62 * Ex: 'Bearer 12345', 'Basic ASDFFDSA=='
64 * Description of a validated principal.
65 * Must include 'contactId', 'userId', xor 'user'
66 * @return array{contactId: int, userId: ?int, flow: string, credType: string, useSession: bool}
67 * An array describing the authenticated session.
68 * @throws \Civi\Authx\AuthxException
70 function authx_login(array $details): array {
71 $defaults = ['flow' => 'script', 'useSession' => FALSE];
72 $details = array_merge($defaults, $details);
73 $auth = new \Civi\Authx\
Authenticator();
74 $auth->setRejectMode('exception');
75 $auth->auth(NULL, array_merge($defaults, $details));
76 return \CRM_Core_Session
::singleton()->get("authx");
80 * @return \Civi\Authx\AuthxInterface
82 function _authx_uf() {
83 $class = 'Civi\\Authx\\' . CIVICRM_UF
;
84 return class_exists($class) ?
new $class() : new \Civi\Authx\
None();
88 * For parameter-based authentication, this option will hide parameters.
89 * This is mostly a precaution, hedging against the possibility that some routes
90 * make broad use of $_GET or $_PARAMS.
94 function _authx_redact(array $keys) {
95 foreach ($keys as $key) {
96 unset($_POST[$key], $_GET[$key], $_REQUEST[$key]);
101 * Reload the current page-view.
103 * @param string $route
104 * @param string $queryString
106 function _authx_reload($route, $queryString) {
107 parse_str($queryString, $query);
108 foreach (array_keys($query) as $key) {
109 if (CRM_Utils_String
::startsWith($key, '_authx')) {
113 $url = CRM_Utils_System
::url($route, $query, TRUE, NULL, FALSE, CRM_Core_Config
::singleton()->userSystem
->isFrontEndPage());
114 CRM_Utils_System
::redirect($url);
118 * Implements hook_civicrm_config().
120 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/
122 function authx_civicrm_config(&$config) {
123 _authx_civix_civicrm_config($config);
127 * Implements hook_civicrm_install().
129 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
131 function authx_civicrm_install() {
132 _authx_civix_civicrm_install();
136 * Implements hook_civicrm_postInstall().
138 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall
140 function authx_civicrm_postInstall() {
141 _authx_civix_civicrm_postInstall();
145 * Implements hook_civicrm_uninstall().
147 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_uninstall
149 function authx_civicrm_uninstall() {
150 _authx_civix_civicrm_uninstall();
154 * Implements hook_civicrm_enable().
156 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
158 function authx_civicrm_enable() {
159 _authx_civix_civicrm_enable();
163 * Implements hook_civicrm_disable().
165 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_disable
167 function authx_civicrm_disable() {
168 _authx_civix_civicrm_disable();
172 * Implements hook_civicrm_upgrade().
174 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_upgrade
176 function authx_civicrm_upgrade($op, CRM_Queue_Queue
$queue = NULL) {
177 return _authx_civix_civicrm_upgrade($op, $queue);
181 * Implements hook_civicrm_entityTypes().
183 * Declare entity types provided by this module.
185 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_entityTypes
187 function authx_civicrm_entityTypes(&$entityTypes) {
188 _authx_civix_civicrm_entityTypes($entityTypes);
192 * Implements hook_civicrm_permission().
194 * @see CRM_Utils_Hook::permission()
196 function authx_civicrm_permission(&$permissions) {
197 $permissions['authenticate with password'] = E
::ts('AuthX: Authenticate to services with password');
198 $permissions['authenticate with api key'] = E
::ts('AuthX: Authenticate to services with API key');
201 // --- Functions below this ship commented out. Uncomment as required. ---
204 * Implements hook_civicrm_preProcess().
206 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_preProcess
208 //function authx_civicrm_preProcess($formName, &$form) {
213 * Implements hook_civicrm_navigationMenu().
215 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_navigationMenu
217 //function authx_civicrm_navigationMenu(&$menu) {
218 // _authx_civix_insert_navigation_menu($menu, 'Mailings', array(
219 // 'label' => E::ts('New subliminal message'),
220 // 'name' => 'mailing_subliminal_message',
221 // 'url' => 'civicrm/mailing/subliminal',
222 // 'permission' => 'access CiviMail',
223 // 'operator' => 'OR',
226 // _authx_civix_navigationMenu($menu);