Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit 7f7a796

Browse files
authored
Merge pull request repejota#100 from repejota/feature/auth
Add Token authorization
2 parents cc0d299 + c01ebf8 commit 7f7a796

3 files changed

Lines changed: 68 additions & 4 deletions

File tree

examples/connectauthtoken.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
require_once __DIR__.'/../vendor/autoload.php';
3+
4+
$connectionOptions = new \Nats\ConnectionOptions();
5+
$connectionOptions->setHost('localhost')->setPort(4222)->setToken('supersecrettoken');
6+
$c = new Nats\Connection($connectionOptions);
7+
$c->connect();
8+
$c->close();

src/Nats/ConnectionOptions.php

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ class ConnectionOptions
3737
*/
3838
private $pass = null;
3939

40+
/**
41+
* Token to connect.
42+
*
43+
* @var string
44+
*/
45+
private $token = null;
46+
4047
/**
4148
* Language of this client.
4249
*
@@ -105,6 +112,10 @@ public function __toString()
105112
$a['pass'] = $this->pass;
106113
}
107114

115+
if (empty($this->token) === false) {
116+
$a['auth_token'] = $this->token;
117+
}
118+
108119
return json_encode($a);
109120
}
110121

@@ -169,7 +180,7 @@ public function setPort($port)
169180
public function getUser()
170181
{
171182
return $this->user;
172-
}//end getUser()
183+
}
173184

174185

175186
/**
@@ -184,7 +195,7 @@ public function setUser($user)
184195
$this->user = $user;
185196

186197
return $this;
187-
}//end setUser()
198+
}
188199

189200

190201
/**
@@ -195,8 +206,7 @@ public function setUser($user)
195206
public function getPass()
196207
{
197208
return $this->pass;
198-
}//end getPass()
199-
209+
}
200210

201211
/**
202212
* Set password.
@@ -212,6 +222,29 @@ public function setPass($pass)
212222
return $this;
213223
}
214224

225+
/**
226+
* Get token.
227+
*
228+
* @return string
229+
*/
230+
public function getToken()
231+
{
232+
return $this->token;
233+
}
234+
235+
/**
236+
* Set token.
237+
*
238+
* @param string $token Token.
239+
*
240+
* @return $this
241+
*/
242+
public function setToken($token)
243+
{
244+
$this->token = $token;
245+
246+
return $this;
247+
}
215248

216249
/**
217250
* Get language.

test/ConnectionOptionsTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,29 @@ public function testSettersAndGetters()
2424
$this->assertEquals(4222, $options->getPort());
2525
$this->assertEquals('user', $options->getUser());
2626
$this->assertEquals('password', $options->getPass());
27+
$this->assertNull($options->getToken());
28+
$this->assertEquals('lang', $options->getLang());
29+
$this->assertEquals('version', $options->getVersion());
30+
$this->assertTrue($options->isVerbose());
31+
$this->assertTrue($options->isPedantic());
32+
$this->assertTrue($options->isReconnect());
33+
}
34+
35+
/**
36+
* Test Connection Options getters and setters using auth token.
37+
*
38+
* @return void
39+
*/
40+
public function testAuthToken()
41+
{
42+
$options = new ConnectionOptions();
43+
$options->setHost('host')->setPort(4222)->setToken('token')->setLang('lang')->setVersion('version')->setVerbose(true)->setPedantic(true)->setReconnect(true);
44+
45+
$this->assertEquals('host', $options->getHost());
46+
$this->assertEquals(4222, $options->getPort());
47+
$this->assertEquals('token', $options->getToken());
48+
$this->assertNull($options->getUser());
49+
$this->assertNull($options->getPass());
2750
$this->assertEquals('lang', $options->getLang());
2851
$this->assertEquals('version', $options->getVersion());
2952
$this->assertTrue($options->isVerbose());

0 commit comments

Comments
 (0)