Skip to content

Commit

Permalink
Merge pull request #15 from kainuk/ajaxcurl
Browse files Browse the repository at this point in the history
Add a connector that authenticates with the new authx extension
  • Loading branch information
kainuk authored Aug 26, 2021
2 parents ce573ff + 66caa09 commit 3425c37
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
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`

0 comments on commit 3425c37

Please sign in to comment.