Skip to content

Commit 2357e02

Browse files
Changed init/hello. Added new classes for different Auth.
1 parent 5e1354a commit 2357e02

File tree

15 files changed

+272
-103
lines changed

15 files changed

+272
-103
lines changed

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<directory suffix=".php">src/connection</directory>
1414
<directory suffix=".php">src/PackStream</directory>
1515
<directory suffix=".php">src/protocol</directory>
16+
<directory suffix=".php">src/auth</directory>
1617
<file>src/Bolt.php</file>
1718
</whitelist>
1819
</filter>

src/Bolt.php

Lines changed: 54 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class Bolt
2626
* @var IPacker
2727
*/
2828
private $packer;
29-
29+
3030
/**
3131
* @var IUnpacker
3232
*/
@@ -53,13 +53,13 @@ final class Bolt
5353
private $version;
5454

5555
/**
56-
* @var string
56+
* @var array|null
5757
*/
58-
private $scheme = 'basic';
58+
private $routing = null;
5959

6060
/**
6161
* Print debug info
62-
* @var bool
62+
* @var bool
6363
*/
6464
public static $debug = false;
6565

@@ -89,7 +89,7 @@ public function setProtocolVersions(...$v): Bolt
8989
* @return Bolt
9090
* @throws Exception
9191
*/
92-
public function setPackStreamVersion(int $version = 1)
92+
public function setPackStreamVersion(int $version = 1): Bolt
9393
{
9494
$packerClass = "\\Bolt\\PackStream\\v" . $version . "\\Packer";
9595
if (!class_exists($packerClass)) {
@@ -107,17 +107,7 @@ public function setPackStreamVersion(int $version = 1)
107107
}
108108

109109
/**
110-
* @param string $scheme
111-
* @return Bolt
112-
*/
113-
public function setScheme(string $scheme = 'basic')
114-
{
115-
if (in_array($scheme, ['none', 'basic', 'kerberos']))
116-
$this->scheme = $scheme;
117-
return $this;
118-
}
119-
120-
/**
110+
* Version is available after successful connection with init/hello message
121111
* @return float
122112
*/
123113
public function getProtocolVersion(): float
@@ -195,19 +185,12 @@ private function packProtocolVersions(): string
195185

196186
/**
197187
* Send INIT message
198-
* @version <3
199-
* @param string $name should conform to "Name/Version" https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent
200-
* @param string $user
201-
* @param string $password
202-
* @param array $routing routing::Dictionary(address::String)
203-
<pre>null - the server should not carry out routing
204-
[] - the server should carry out routing
205-
['address' => 'ip:port'] - the server should carry out routing according to the given routing context</pre>
206-
* @param array $metadata Server success response metadata
207-
* @return bool
188+
* @param auth\Auth $auth
189+
* @return bool|array
208190
* @throws Exception
191+
* @version <3
209192
*/
210-
public function init(string $name, string $user, string $password, array $routing = null, array &$metadata = []): bool
193+
public function init(\Bolt\auth\Auth $auth)
211194
{
212195
if (!$this->connection->connect())
213196
return false;
@@ -218,44 +201,49 @@ public function init(string $name, string $user, string $password, array $routin
218201
if (self::$debug)
219202
echo 'INIT';
220203

221-
$metadata = $this->protocol->init($name, $this->scheme, $user, $password, $routing);
222-
return !empty($metadata);
204+
return $this->protocol->init($auth->getCredentials(), $this->routing) ?? false;
223205
}
224206

225207
/**
226208
* Send HELLO message
227-
* @internal INIT alias
228-
* @version >=3
229-
* @param string $name
230-
* @param string $user
231-
* @param string $password
232-
* @param array $routing routing::Dictionary(address::String)
233-
<pre>null - the server should not carry out routing
234-
[] - the server should carry out routing
235-
['address' => 'ip:port'] - the server should carry out routing according to the given routing context</pre>
236-
* @param array $metadata Server success response metadata
237-
* @return bool
209+
* @param auth\Auth $auth
210+
* @return bool|array
238211
* @throws Exception
212+
* @version >=3
213+
*/
214+
public function hello(\Bolt\auth\Auth $auth)
215+
{
216+
return $this->init($auth);
217+
}
218+
219+
/**
220+
* Set routing table for HELLO message
221+
* @param array|null $routing routing::Dictionary(address::String)
222+
* <pre>null - the server should not carry out routing
223+
* [] - the server should carry out routing
224+
* ['address' => 'ip:port'] - the server should carry out routing according to the given routing context</pre>
225+
* @return Bolt
239226
*/
240-
public function hello(string $name, string $user, string $password, array $routing = null, array &$metadata = []): bool
227+
public function setRouting(?array $routing = null): Bolt
241228
{
242-
return $this->init($name, $user, $password, $routing, $metadata);
229+
$this->routing = $routing;
230+
return $this;
243231
}
244232

245233
/**
246234
* Send RUN message
247235
* @param string $statement
248236
* @param array $parameters
249237
* @param array $extra extra::Dictionary(bookmarks::List<String>, tx_timeout::Integer, tx_metadata::Dictionary, mode::String, db:String)
250-
<pre>The bookmarks is a list of strings containg some kind of bookmark identification e.g [“neo4j-bookmark-transaction:1”, “neo4j-bookmark-transaction:2”]
251-
The tx_timeout is an integer in that specifies a transaction timeout in ms.
252-
The tx_metadata is a dictionary that can contain some metadata information, mainly used for logging.
253-
The mode specifies what kind of server the RUN message is targeting. For write access use "w" and for read access use "r". Defaults to write access if no mode is sent.
254-
The db specifies the database name for multi-database to select where the transaction takes place. If no db is sent or empty string it implies that it is the default database.</pre>
238+
* <pre>The bookmarks is a list of strings containg some kind of bookmark identification e.g [“neo4j-bookmark-transaction:1”, “neo4j-bookmark-transaction:2”]
239+
* The tx_timeout is an integer in that specifies a transaction timeout in ms.
240+
* The tx_metadata is a dictionary that can contain some metadata information, mainly used for logging.
241+
* The mode specifies what kind of server the RUN message is targeting. For write access use "w" and for read access use "r". Defaults to write access if no mode is sent.
242+
* The db specifies the database name for multi-database to select where the transaction takes place. If no db is sent or empty string it implies that it is the default database.</pre>
255243
* @return array
256244
* @throws Exception
257245
*/
258-
public function run(string $statement, array $parameters = [], array $extra = [])
246+
public function run(string $statement, array $parameters = [], array $extra = []): array
259247
{
260248
if (self::$debug)
261249
echo 'RUN: ' . $statement;
@@ -264,13 +252,13 @@ public function run(string $statement, array $parameters = [], array $extra = []
264252

265253
/**
266254
* Send PULL_ALL message
267-
* @version <4
268255
* @param int $n The n specifies how many records to fetch. n=-1 will fetch all records.
269256
* @param int $qid The qid (query identification) specifies the result of which statement the operation should be carried out. (Explicit Transaction only). qid=-1 can be used to denote the last executed statement and if no ``.
270257
* @return array
271258
* @throws Exception
259+
* @version <4
272260
*/
273-
public function pullAll(int $n = -1, int $qid = -1)
261+
public function pullAll(int $n = -1, int $qid = -1): array
274262
{
275263
if (self::$debug)
276264
echo 'PULL';
@@ -279,27 +267,27 @@ public function pullAll(int $n = -1, int $qid = -1)
279267

280268
/**
281269
* Send PULL message
282-
* @version >=4
283-
* @internal PULL_ALL alias
284270
* @param int $n The n specifies how many records to fetch. n=-1 will fetch all records.
285271
* @param int $qid The qid (query identification) specifies the result of which statement the operation should be carried out. (Explicit Transaction only). qid=-1 can be used to denote the last executed statement and if no ``.
286272
* @return array Array of records. Last array element is success message.
287273
* @throws Exception
274+
* @version >=4
275+
* @internal PULL_ALL alias
288276
*/
289-
public function pull(int $n = -1, int $qid = -1)
277+
public function pull(int $n = -1, int $qid = -1): array
290278
{
291279
return $this->pullAll($n, $qid);
292280
}
293281

294282
/**
295283
* Send DISCARD_ALL message
296-
* @version <4
297284
* @param int $n The n specifies how many records to throw away. n=-1 will throw away all records.
298285
* @param int $qid The qid (query identification) specifies the result of which statement the operation should be carried out. (Explicit Transaction only). qid=-1 can be used to denote the last executed statement and if no ``.
299286
* @return bool
300287
* @throws Exception
288+
* @version <4
301289
*/
302-
public function discardAll(int $n = -1, int $qid = -1)
290+
public function discardAll(int $n = -1, int $qid = -1): bool
303291
{
304292
if (self::$debug)
305293
echo 'DISCARD';
@@ -308,12 +296,12 @@ public function discardAll(int $n = -1, int $qid = -1)
308296

309297
/**
310298
* Send DISCARD message
311-
* @version >=4
312-
* @internal DISCARD_ALL alias
313299
* @param int $n The n specifies how many records to throw away. n=-1 will throw away all records.
314300
* @param int $qid The qid (query identification) specifies the result of which statement the operation should be carried out. (Explicit Transaction only). qid=-1 can be used to denote the last executed statement and if no ``.
315301
* @return bool
316302
* @throws Exception
303+
* @version >=4
304+
* @internal DISCARD_ALL alias
317305
*/
318306
public function discard(int $n = -1, int $qid = -1): bool
319307
{
@@ -322,15 +310,15 @@ public function discard(int $n = -1, int $qid = -1): bool
322310

323311
/**
324312
* Send BEGIN message
325-
* @version >=3
326313
* @param array $extra extra::Dictionary(bookmarks::List<String>, tx_timeout::Integer, tx_metadata::Dictionary, mode::String, db:String)
327-
<pre>The bookmarks is a list of strings containg some kind of bookmark identification e.g [“neo4j-bookmark-transaction:1”, “neo4j-bookmark-transaction:2”]
328-
The tx_timeout is an integer in that specifies a transaction timeout in ms.
329-
The tx_metadata is a dictionary that can contain some metadata information, mainly used for logging.
330-
The mode specifies what kind of server the RUN message is targeting. For write access use "w" and for read access use "r". Defaults to write access if no mode is sent.
331-
The db specifies the database name for multi-database to select where the transaction takes place. If no db is sent or empty string it implies that it is the default database.</pre>
314+
* <pre>The bookmarks is a list of strings containg some kind of bookmark identification e.g [“neo4j-bookmark-transaction:1”, “neo4j-bookmark-transaction:2”]
315+
* The tx_timeout is an integer in that specifies a transaction timeout in ms.
316+
* The tx_metadata is a dictionary that can contain some metadata information, mainly used for logging.
317+
* The mode specifies what kind of server the RUN message is targeting. For write access use "w" and for read access use "r". Defaults to write access if no mode is sent.
318+
* The db specifies the database name for multi-database to select where the transaction takes place. If no db is sent or empty string it implies that it is the default database.</pre>
332319
* @return bool
333320
* @throws Exception
321+
* @version >=3
334322
*/
335323
public function begin(array $extra = []): bool
336324
{
@@ -341,9 +329,9 @@ public function begin(array $extra = []): bool
341329

342330
/**
343331
* Send COMMIT message
344-
* @version >=3
345332
* @return bool
346333
* @throws Exception
334+
* @version >=3
347335
*/
348336
public function commit(): bool
349337
{
@@ -354,9 +342,9 @@ public function commit(): bool
354342

355343
/**
356344
* Send ROLLBACK message
357-
* @version >=3
358345
* @return bool
359346
* @throws Exception
347+
* @version >=3
360348
*/
361349
public function rollback(): bool
362350
{
@@ -379,11 +367,11 @@ public function reset(): bool
379367

380368
/**
381369
* Send ROUTE message to instruct the server to return the current routing table.
382-
* @version >=4.3 In previous versions there was no explicit message for this and a procedure had to be invoked using Cypher through the RUN and PULL messages.
383370
* @param array|null $routing
384371
* @param array $bookmarks
385372
* @param array|string|null $extra
386373
* @return array|null
374+
* @version >=4.3 In previous versions there was no explicit message for this and a procedure had to be invoked using Cypher through the RUN and PULL messages.
387375
*/
388376
public function route(?array $routing = null, array $bookmarks = [], $extra = null): ?array
389377
{

src/auth/Auth.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Bolt\auth;
4+
5+
/**
6+
* Class Auth
7+
*
8+
* @author Michal Stefanak
9+
* @link https://github.com/neo4j-php/Bolt
10+
* @package Bolt\auth
11+
*/
12+
abstract class Auth
13+
{
14+
/**
15+
* @var string
16+
*/
17+
protected $userAgent;
18+
19+
/**
20+
* @var string
21+
*/
22+
protected $scheme;
23+
24+
/**
25+
* @param string $userAgent
26+
*/
27+
public function __construct(string $userAgent = 'bolt-php')
28+
{
29+
$this->userAgent = $userAgent;
30+
}
31+
32+
/**
33+
* Returns structured array for init/hello message
34+
* @return array
35+
*/
36+
abstract public function getCredentials(): array;
37+
}

src/auth/Basic.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Bolt\auth;
4+
5+
/**
6+
* Class Basic
7+
*
8+
* @author Michal Stefanak
9+
* @link https://github.com/neo4j-php/Bolt
10+
* @package Bolt\auth
11+
*/
12+
class Basic extends Auth
13+
{
14+
/**
15+
* @inheritdoc
16+
*/
17+
protected $scheme = 'basic';
18+
19+
/**
20+
* @var string
21+
*/
22+
private $username;
23+
24+
/**
25+
* @var string
26+
*/
27+
private $password;
28+
29+
/**
30+
* @param string $username
31+
* @param string $password
32+
*/
33+
public function setCredentials(string $username, string $password)
34+
{
35+
$this->username = $username;
36+
$this->password = $password;
37+
}
38+
39+
/**
40+
* @inheritDoc
41+
*/
42+
public function getCredentials(): array
43+
{
44+
return [
45+
'user_agent' => $this->userAgent,
46+
'scheme' => $this->scheme,
47+
'principal' => $this->username,
48+
'credentials' => $this->password
49+
];
50+
}
51+
}

0 commit comments

Comments
 (0)