-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from kainuk/ajaxcurl
Add a connector that authenticates with the new authx extension
- Loading branch information
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
|