Repository navigation
Expand file tree
/
Copy pathoauth2_openstack.inc.php
More file actions
55 lines (42 loc) · 2.1 KB
/
Copy pathoauth2_openstack.inc.php
File metadata and controls
55 lines (42 loc) · 2.1 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
<?php
function openstack_getAccessToken(){
if ( !isset($this->cloud['auth']) ){
$_SESSION['errorData']['Error'][]="No authorization data set. Cannot connect to openstack cloud.";
return 0;
}
// request openstack token via REST
//
$auth_data = $this->cloud['auth'];
$data = array ("auth" => array( "tenantName" => "'".$auth_data["OS_TENANT_NAME"]."'",
"passwordCredentials" => array ( "username" => "'".$auth_data["OS_USERNAME"]."'",
"password" => "'".$auth_data["OS_PASSWORD"]."'")));
$url = $auth_data["OS_AUTH_URL"]."/tokens";
$data_string = json_encode($data);
if (!strlen($data_string)){
$_SESSION['errorData']['Error'][]="Curl: cannot POST openstack token request. Data to send is empty";
return 0;
}
logger("OS TOKEN request: ".$cmd);
$cmd = "curl -H \"Content-Type: application/json\" -X POST -d '".json_encode($data)."' $url";
subprocess($cmd,$r_str,$stdErr);
$r = json_decode($r_str, true);
//var_dump($r);
//print "<br/> CMD --> $cmd <br/>";
//print "<br/><br/><br/>----------------------------------------------------R2 ---------> $r";
// curl_init returns 401. Instead, using curl via subprocess
//$headers = array('Content-Type: application/json');
//list($r,$info) = post($data_string,$url,$headers);
if (!isset($r['access']["token"])){
if ($_SESSION['errorData']['Error']){
$_SESSION['errorData']['Error'] = "Cannot access to the requestes cloud. Authorization failed.";
logger("ERROR: Cannot access to the requestes cloud. Authorization failed. Response: $r");
}
return 0;
}
return $r['access']["token"];
}
function openstack_isTokenExpired($token){
$token_expires = strtotime($token["expires"]);
date_default_timezone_set("UTC");
return $token_expires < time();
}