...

Code Block
languagephp
linenumberstrue
<?php
// Login to the site and request the access token
$curl = curl_init('http://your-domain.com/restws/session/token');
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "restws_webservice:webservice_password"); // Your web service user credentials goes here.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$token = curl_exec($curl);
// The token looks like "q71OBx05wtECfjA0KmXf6wiktewrywNhkMZv-OcfyOA%"

// #1 Verify the mapping does not exist by sending a GET request to the system, searching by uid
$curl = curl_init("http://your-domain.com/authmap.json?uid=300");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-CSRF-Token: $token", "Content-Type: application/json"));
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "restws_webservice:webservice_password"); //Your credentials goes here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

$json = curl_exec($curl);
$response = json_decode($json);

// A list of users matching criteria will be returned. If this list is empty, a user does not have an associated mapping value.
$list = $response->list;

// #2 Send the creation request, via POST
if (empty($list)) {
  $authmap = array(
    "uid" => 300,
    "authname" => "ethosce_authname",
    "module" => "ethoscesimplesamlphp_ext_moduleauth",
  );
  $curl = curl_init("http://your-domain.com/authmap");
  curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
  curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-CSRF-Token: $token", "Content-Type: application/json"));
  curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($curl, CURLOPT_USERPWD, "restws_webservice:webservice_password"); //Your credentials goes here
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($authmap));

  $json = curl_exec($curl);
  // An array detailing the new user entity is returned if successful
  $authmapInfo = json_decode($json);

...