Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a connector that authenticates with the new authx extension #15

Merged
merged 3 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions CMRF/Connection/CurlAuthX.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/**
* Remote CiviCRM connection based on CURL
* Uses the new CiviCRM auth extension. Authentication
* is done with X-Civi-Auth em X-Civi-Key headers
*
* @author Björn Endres, SYSTOPIA (endres@systopia.de)
*/

namespace CMRF\Connection;

use CMRF\Core\Call as Call;
use CMRF\Core\Connection as Connection;

class CurlAuthX extends Connection {

public function getType() {
return 'curlauthx';
}

public function isReady() {
// TODO: check for CURL
return TRUE;
}

/**
* execute the given call synchroneously
*
* return call status
*/
public function executeCall(Call $call) {
$profile = $this->getProfile();

$request = $this->getAPI3Params($call);
$post_data = "entity=" . $call->getEntity();
$post_data .= "&action=" . $call->getAction();
$post_data .= "&version=3";
$post_data .= "&json=" . urlencode(json_encode($request));

$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_URL, $profile['url']);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSLVERSION, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
"X-Requested-With: XMLHttpRequest",
"X-Civi-Auth: Bearer {$profile['api_key']}",
"X-Civi-Key: {$profile['site_key']}"
]);

$response = curl_exec($curl);
if (curl_error($curl)){
$call->setStatus(Call::STATUS_FAILED, curl_error($curl));
return NULL;
} else {
$reply = json_decode($response, true);
if ($reply===NULL) {
$call->setStatus(Call::STATUS_FAILED, curl_error($curl));
return NULL;
} else {
$status = Call::STATUS_DONE;
if (isset($reply['is_error']) && $reply['is_error']) {
$status = Call::STATUS_FAILED;
}
$call->setReply($reply, $status);
return $reply;
}
}
}
}
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
You can find the general documentation at https://github.com/CiviMRF/documentation.

### The CurlAuthX connector
In the latest versions of CiviCRM, the endpoint `https://my-civi.org/sites/all/modules/civicrm/extern/rest.php` is not supported anymore. It is replaced by a new extension `AuthX` that provides a lot of different authentication options. The CurlAuthX connector uses just one of them.

The AuthX does not have a graphical interface (yet). You can configure it with the `cv` utility.

* enable the new authx extension `cv ext:enable authx`
* choose the authentication method `cv ev 'Civi::settings()->set("authx_xheader_cred",["api_key"]);'`
* make sitekey mandatory `cv ev 'Civi::settings()->set("authx_guards",["site_key"]);'`
* refresh the cache `cv flush`