-
Notifications
You must be signed in to change notification settings - Fork 0
/
Auth.php
137 lines (102 loc) · 3.54 KB
/
Auth.php
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace K5\Token;
class Auth
{
public $username;
public $password;
public $contract;
public $region;
public $project;
// if client side stored $token_passed is passed in, we just return it without making the request
public function __construct($username, $password, $contract, $region = '', $project = '', $token_passed = false) {
$this->username = $username;
$this->password = $password;
$this->contract = $contract;
$this->region = $region;
$this->token_passed = $token_passed;
$this->project = $project;
}
/**
* Get the authentication token from K5 API
*
* Get the authentication token for interacting with each API call.
* By default, this token expires in 1 hour, and you can customize a local session storage (1hr)
* Some API calls (eg. Create, Delete global resources) will require global token, and needs to set the last parameter to TRUE
*
* @see _https://identity.cloud.global.fujitsu.com/v3/auth/tokens_
*
* @param $k5_username Customer's Username
* @param $k5_password Customer's Password
* @param $k5_contract_number Customer's Contract Number
* @param $global If sets TRUE, will get global token
*
* @\K5\Token\Auth()
*
* @return array
*/
public function getAuthToken()
{
if($token_passed){
return $token_passed;
}
!empty($this->region) ? $region = $this->region . '.' : $region = '';
$c = '\
curl -i -X "POST" "https://identity.'.$region.'cloud.global.fujitsu.com/v3/auth/tokens" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-d \'{
"auth": {
"identity": {
"methods": [
"password"
],
"password": {
"user": {
"domain": {
"name": "' .$this->contract. '"
},
"name": "' .$this->username. '",
"password": "' .$this->password. '"
}
}
}
}
}\'
';
$respond = shell_exec($c);
$respond = explode("\r\n",$respond);
array_filter($respond);
foreach($respond as $value){
if(strpos($value, "X-Subject-Token:") !== false){
$token = trim(substr($value,strpos($value,":")+1));
}else{
json_decode($value);
if (json_last_error() == JSON_ERROR_NONE) {
$return = $value; //the actual body data in Json format
$data = $return; //return this at the bottom of this function
}
}
}
// if(empty($token)) $token = '';
if(!$token) $token = '';
$return = json_decode($return, true);
if(!empty($this->project)){
$project_id = $this->project;
}else{
$project_id = $this->getProjectId(json_decode($data));
}
$domain_id = $this->getDomainId(json_decode($data));
return array(
"token"=>$token,
"project_id"=>$project_id,
"domain_id"=>$domain_id,
"response"=>$data
);
}
private function getProjectId($response){
return $response->token->project->id;
}
private function getDomainId($response){
return $response->token->project->domain->id;
}
}