Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating User on ownCloud using php curl http post

There is all the relevant information present in broken form in the following links on owncloud related websites and from stackoverflow itself:

  1. User Provisioning Api - Owncloud
  2. PHP + curl, HTTP POST sample code
  3. Create user on ownCloud using Ajax Jquery
  4. User Provisioning - php Authentication error

I am trying to do something very simple :

  1. I have setup an owncloud server in my localhost,
  2. I have an html page that takes in string values of user name and password
  3. I send the page request to be processed by the following php script.

    <?php
    echo "Begun processing credentials , first it will be stored in local variables" . "<br/>";
    
    // Loading into local variables
    $userName = $_POST['username'];
    $RRpassword = $_POST['password'];
    
    echo "Hello " . $userName . "<br/>";
    echo "Your password is " . $RRpassword . "<br/>";
    
    // Add data, to owncloud post array and then Send the http request for creating a new user
    $ownCloudPOSTArray = array('username' => $userName, 'password' => $RRpassword );
    
    $url = 'http://localhost/owncloud/ocs/v1.php/cloud/users';
    $ch = curl_init($url);
    
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $ownCloudPOSTArray);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $response = curl_exec($ch);
    curl_close($ch);
    
    echo "<br/>Created a new user in owncloud";
    ?>
    

I get the output like:

Begun processing credentials , first it will be stored in local variables
Hello Frank
Your password is frankspassword
failure 997 Unauthorised
Created a new user in owncloud

I also tried to login to own cloud using following php script:

// Login As Admin
$ownAdminname = 'ownAdmin';
$ownAdminpassword = 'ownAdminPassword';

$ch = curl_init('http://localhost/owncloud');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$ownAdminname:$ownAdminpassword");
$output = curl_exec($ch);
curl_close($ch);
echo $output;

Even this one fails.

So in short it doesn't work. I am also unable to login via similar script to owncloud. What is the proper way to do this ? What settings am I missing ? Can someone help please ?

like image 642
Syed Alam Abbas Avatar asked Oct 19 '22 18:10

Syed Alam Abbas


2 Answers

Since this question pertains to owncloud specifically, I created an account and posted a question linking this one to it in owncloud forum.

There I was suggested by the owncloud master @RealRancor, the following,

Just had another look, maybe its just easy to replace:

$url = 'http://localhost/owncloud/ocs/v1.php/cloud/users';

with

$url = 'http://adminuser:adminpass@localhost/owncloud/ocs/v1.php/cloud/users';

as shown in the documentation.

And amazingly it worked like a charm. So here is the entire modified php script:

<pre>
&lt;?php
echo "Begun processing credentials , first it will be stored in local variables" . "<br/>";

// Loading into local variables
$userName = $_POST['username'];
$RRpassword = $_POST['password'];

echo "Hello " . $userName . "<br/>";
echo "Your password is " . $RRpassword . "<br/>";

 // Login Credentials as Admin
 $ownAdminname = 'ownAdmin';
 $ownAdminpassword = 'ufi2016%%';


// Add data, to owncloud post array and then Send the http request for creating a new user
$url = 'http://' . $ownAdminname . ':' . $ownAdminpassword . '@localhost/owncloud/ocs/v1.php/cloud/users';
echo "Created URL is " . $url . "<br/>"; 

$ownCloudPOSTArray = array('userid' => $userName, 'password' => $RRpassword );

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ownCloudPOSTArray);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo "Response from curl :" . $response;
echo "<br/>Created a new user in owncloud<br/>";

// Add to a group called 'Users'
$groupUrl = $url . '/' . $userName . '/' . 'groups';
echo "Created groups URL is " . $groupUrl . "<br/>";

$ownCloudPOSTArrayGroup = array('groupid' => 'Users');

$ch = curl_init($groupUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ownCloudPOSTArrayGroup);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo "Response from curl :" . $response;
echo "<br/>Added the new user to default group in owncloud";

?>
</pre>

And here is the output:

Begun processing credentials , first it will be stored in local variables
Hello Frank
Your password is frankspassword
Created URL is http://ownAdmin:ufi2016%%@localhost/owncloud/ocs/v1.php/cloud/users
Response from curl : ok 100 
Created a new user in owncloud
Created groups URL is http://ownAdmin:ufi2016%%@localhost/owncloud/ocs/v1.php/cloud/users/Frank/groups
Response from curl : ok 100 
Added the new user to default group in owncloud
like image 102
Syed Alam Abbas Avatar answered Oct 29 '22 01:10

Syed Alam Abbas


The owncloud documentation states that authentication is done by means of a basic HTTP Authentication header. What you are currently doing is sending the credentials as parameters to the API call. You need to add the following line:

curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $RRpassword);

There's also a typo in CURLOPT_RETURNTRANSFER ($curl instead of $ch).

like image 41
MarkM Avatar answered Oct 28 '22 23:10

MarkM