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

Commit f7fc1ef

Browse files
committed
Add Token authorization
1 parent cc0d299 commit f7fc1ef

3 files changed

Lines changed: 67 additions & 5 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: 36 additions & 5 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
*
@@ -100,10 +107,12 @@ public function __toString()
100107
if (empty($this->user) === false) {
101108
$a['user'] = $this->user;
102109
}
103-
104110
if (empty($this->pass) === false) {
105111
$a['pass'] = $this->pass;
106112
}
113+
if (empty($this->token) === false) {
114+
$a['auth_token'] = $this->token;
115+
}
107116

108117
return json_encode($a);
109118
}
@@ -169,7 +178,7 @@ public function setPort($port)
169178
public function getUser()
170179
{
171180
return $this->user;
172-
}//end getUser()
181+
}
173182

174183

175184
/**
@@ -184,7 +193,7 @@ public function setUser($user)
184193
$this->user = $user;
185194

186195
return $this;
187-
}//end setUser()
196+
}
188197

189198

190199
/**
@@ -195,8 +204,7 @@ public function setUser($user)
195204
public function getPass()
196205
{
197206
return $this->pass;
198-
}//end getPass()
199-
207+
}
200208

201209
/**
202210
* Set password.
@@ -212,6 +220,29 @@ public function setPass($pass)
212220
return $this;
213221
}
214222

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

216247
/**
217248
* 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)