This repository was archived by the owner on Sep 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathRpcConnector.php
More file actions
159 lines (127 loc) · 5.3 KB
/
RpcConnector.php
File metadata and controls
159 lines (127 loc) · 5.3 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
declare(strict_types=1);
namespace SimpleSAML\Module\perun;
use SimpleSAML\Error\Exception;
use SimpleSAML\Logger;
use SimpleSAML\Module\perun\Exception as PerunException;
/**
* Provides interface to call Perun RPC. Note that Perun RPC should be considered as unreliable and authentication
* process should continue without connection to Perun. e.g. use LDAP instead.
*
* Example Usage:
*
* try { $attribute = RpcConnector::get('attributesManager', 'getAttribute', [ 'user' => $userId, 'attributeName' =>
* $attrName, ]); ... } catch (PerunException $pe) { ... }
*/
class RpcConnector
{
public const COOKIE_FILE = '/tmp/proxyidp_cookie.txt';
public const CONNECT_TIMEOUT = 5;
public const TIMEOUT = 15;
private $rpcUrl;
private $user;
private $password;
private $serializer;
/**
* sspmod_perun_RpcConnector constructor.
*
* @param $rpcUrl
* @param $user
* @param $password
* @param $serializer
*/
public function __construct($rpcUrl, $user, $password, $serializer)
{
$this->rpcUrl = $rpcUrl;
$this->user = $user;
$this->password = $password;
$this->serializer = $serializer;
}
public function get($manager, $method, $params = [])
{
$paramsQuery = http_build_query($params);
// replace 'paramList[0]=val0' to just 'paramList[]=val0' because perun rpc cannot consume such lists.
$paramsQuery = preg_replace('/\%5B\d+\%5D/', '%5B%5D', $paramsQuery);
$ch = curl_init();
$uri = $this->rpcUrl . $this->serializer . '/' . $manager . '/' . $method;
curl_setopt($ch, CURLOPT_URL, $uri . '?' . $paramsQuery);
curl_setopt($ch, CURLOPT_USERPWD, $this->user . ':' . $this->password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, self::COOKIE_FILE);
curl_setopt($ch, CURLOPT_COOKIEFILE, self::COOKIE_FILE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::CONNECT_TIMEOUT);
curl_setopt($ch, CURLOPT_TIMEOUT, self::TIMEOUT);
$startTime = microtime(true);
$json = curl_exec($ch);
$endTime = microtime(true);
curl_close($ch);
$responseTime = round($endTime - $startTime, 3);
Logger::debug('perun.RPC: GET call ' . $uri . ' with params: ' . $paramsQuery . ', response : ' .
$json . ' in: ' . $responseTime . 's.');
if ($json === false) {
throw new Exception(
'Cant\'t get response from Perun. Call: ' . $uri . ', Params: ' . $paramsQuery . ', Response: ' . $json
);
}
$result = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception(
'Cant\'t decode response from Perun. Call: ' . $uri . ', Params: ' . $paramsQuery . ', Response: ' . $json
);
}
if (isset($result['errorId'])) {
$name = $result['name'] ?? null;
$message = $result['message'] ?? null;
self::error($result['errorId'], $name, $message, $uri, $paramsQuery);
}
return $result;
}
public function post($manager, $method, $params = [])
{
$paramsJson = json_encode($params);
$ch = curl_init();
$uri = $this->rpcUrl . 'json/' . $manager . '/' . $method;
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_USERPWD, $this->user . ':' . $this->password);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $paramsJson);
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
['Content-Type:application/json', 'Content-Length: ' . strlen($paramsJson)]
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, self::COOKIE_FILE);
curl_setopt($ch, CURLOPT_COOKIEFILE, self::COOKIE_FILE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::CONNECT_TIMEOUT);
curl_setopt($ch, CURLOPT_TIMEOUT, self::TIMEOUT);
$startTime = microtime(true);
$json = curl_exec($ch);
$endTime = microtime(true);
curl_close($ch);
$responseTime = round($endTime - $startTime, 3);
Logger::debug('perun.RPC: POST call ' . $uri . ' with params: ' . $paramsJson . ', response : ' .
$json . ' in: ' . $responseTime . 's.');
if ($json === false) {
throw new Exception(
'Can\'t get response from Perun. Call: ' . $uri . ', Params: ' . $paramsJson . ', Error: ' . curl_error($ch)
);
}
$result = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception(
'Cant\'t decode response from Perun. Call: ' . $uri . ', Params: ' . $paramsJson . ', Response: ' . $json
);
}
if (isset($result['errorId'])) {
$name = $result['name'] ?? null;
$message = $result['message'] ?? null;
self::error($result['errorId'], $name, $message, $uri, $paramsJson);
}
return $result;
}
private static function error($id, $name, $message, $uri, $params)
{
throw new PerunException($id, $name, $message . '\ncall: ' . $uri . ', params: ' . var_export($params, true));
}
}