Skip to content

Commit 9a05cfd

Browse files
committed
Merge pull request #5 from pr0head/master
Added options <username> and <password> for http-authorization on rem…
2 parents 65d2a12 + aed75c8 commit 9a05cfd

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.idea
12
build
23
vendor
34
composer.lock

lib/GameNet/Jabber/RpcClient.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ class RpcClient
7171
* @var int
7272
*/
7373
protected $timeout;
74+
/**
75+
* @var string
76+
*/
77+
protected $username;
78+
/**
79+
* @var string
80+
*/
81+
protected $password;
7482

7583
public function __construct(array $options)
7684
{
@@ -84,8 +92,17 @@ public function __construct(array $options)
8492

8593
$this->server = $options['server'];
8694
$this->host = $options['host'];
95+
$this->username = isset($options['username']) ? $options['username'] : '';
96+
$this->password = isset($options['password']) ? $options['password'] : '';
8797
$this->debug = isset($options['debug']) ? (bool)$options['debug'] : false;
8898
$this->timeout = isset($options['timeout']) ? (int)$options['timeout'] : 5;
99+
100+
if ($this->username && !$this->password) {
101+
throw new \InvalidArgumentException("Password cannot be empty if username was defined");
102+
}
103+
if (!$this->username && $this->password) {
104+
throw new \InvalidArgumentException("Username cannot be empty if password was defined");
105+
}
89106
}
90107

91108
/**
@@ -110,6 +127,12 @@ public function getTimeout()
110127

111128
protected function sendRequest($command, array $params)
112129
{
130+
if ($this->username && $this->password) {
131+
$params = [
132+
['user' => $this->username, 'server' => $this->server, 'password' => $this->password], $params
133+
];
134+
}
135+
113136
$request = xmlrpc_encode_request($command, $params, ['encoding' => 'utf-8', 'escaping' => 'markup']);
114137

115138
$ch = curl_init();
@@ -122,6 +145,7 @@ protected function sendRequest($command, array $params)
122145
curl_setopt($ch, CURLOPT_POST, true);
123146
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
124147
curl_setopt($ch, CURLOPT_HTTPHEADER, ['User-Agent: GameNet', 'Content-Type: text/xml']);
148+
125149
$response = curl_exec($ch);
126150
curl_close($ch);
127151

tests/GameNet/Jabber/RpcClientTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,25 @@ public function testSetGetTimeout()
3737
$this->assertEquals(600, $client->getTimeout());
3838
}
3939

40-
private function getClient()
40+
/**
41+
* @dataProvider invalidCredentials
42+
* @expectedException InvalidArgumentException
43+
*/
44+
public function testInvalidCredentials($username, $password)
45+
{
46+
$this->getClient(['username' => $username, 'password' => $password]);
47+
}
48+
49+
public function invalidCredentials()
50+
{
51+
return [
52+
['username' => 'username', 'password' => ''],
53+
['username' => '', 'password' => 'password'],
54+
];
55+
}
56+
57+
private function getClient(array $options = [])
4158
{
42-
return new RpcClient(['server' => 'test', 'host' => 'test']);
59+
return new RpcClient(['server' => 'test', 'host' => 'test'] + $options);
4360
}
4461
}

0 commit comments

Comments
 (0)