-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloudAPI_Example.php
More file actions
99 lines (74 loc) · 2.9 KB
/
Copy pathCloudAPI_Example.php
File metadata and controls
99 lines (74 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
//settings
$username = "<email>";
$password = "<pwd>";
$api = "https://api.starleaf.com/v1/";
//make the cookie file location somewhere writeable
//for linux server etc change to '/tmp/cookie.txt'
$cookiefile = dirname(__FILE__).'/cookie.txt';
//request challenge, salt and iterations
$challenge_url = $api."challenge?username=".urlencode($username);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$challenge_url);
$result=curl_exec($ch);
curl_close($ch);
$challenge_values = json_decode($result, true);
//convert hex representations to binary
$bin_salt = hex2bin($challenge_values['salt']);
$bin_challenge = hex2bin($challenge_values['challenge']);
//generate key, 256 bit binary number
$key = hash_pbkdf2 ( "sha256" ,$password, $bin_salt , $challenge_values['iterations'], 32, true );
//generate response
$response = hash_hmac( "sha256" , $bin_challenge, $key);
//put them into an array
$authenticate_data = array( "username" => $username, "response" => $response );
//turn it into JSON
$authenticate_data_string = json_encode( $authenticate_data );
//send authenticate POST request
$authenticate_url = $api."authenticate";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$authenticate_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $authenticate_data_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
$result=curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
//gives NONE on success otherwise prints out failure message
var_dump(json_decode($result,true));
//gives 204 on success
echo("Got HTTP response code of :".$code."\n");
//list my account features
$features_url = $api."features";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $features_url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
$result=curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
var_dump(json_decode($result,true));
echo("Got HTTP response code of :".$code."\n");
//list my account users
$users_url = $api."users";
echo ("GETting $users_url\n");
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $users_url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
$result=curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
var_dump(json_decode($result,true));
echo("Got HTTP response code of :".$code."\n");
?>