Skip to content

Commit 7500780

Browse files
Updated changes to implement different types of auth.
1 parent 2357e02 commit 7500780

File tree

17 files changed

+114
-237
lines changed

17 files changed

+114
-237
lines changed

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +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>
16+
<directory suffix=".php">src/helpers</directory>
1717
<file>src/Bolt.php</file>
1818
</whitelist>
1919
</filter>

src/Bolt.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -185,35 +185,33 @@ private function packProtocolVersions(): string
185185

186186
/**
187187
* Send INIT message
188-
* @param auth\Auth $auth
189-
* @return bool|array
188+
* @param array $extra You can use helpers\Auth to generate required array
189+
* @return array
190190
* @throws Exception
191191
* @version <3
192192
*/
193-
public function init(\Bolt\auth\Auth $auth)
193+
public function init(array $extra)
194194
{
195-
if (!$this->connection->connect())
196-
return false;
197-
198-
if (!$this->handshake())
199-
return false;
200-
201-
if (self::$debug)
202-
echo 'INIT';
195+
if ($this->connection->connect() && $this->handshake()) {
196+
if (self::$debug)
197+
echo 'INIT';
198+
return $this->protocol->init($extra, $this->routing);
199+
}
203200

204-
return $this->protocol->init($auth->getCredentials(), $this->routing) ?? false;
201+
// I don't think it will reach this point, but otherwise I've to end method with return
202+
throw new Exception('INIT message wasn\'t successful');
205203
}
206204

207205
/**
208206
* Send HELLO message
209-
* @param auth\Auth $auth
210-
* @return bool|array
207+
* @param array $extra You can use helpers\Auth to generate required array
208+
* @return array
211209
* @throws Exception
212210
* @version >=3
213211
*/
214-
public function hello(\Bolt\auth\Auth $auth)
212+
public function hello(array $extra)
215213
{
216-
return $this->init($auth);
214+
return $this->init($extra);
217215
}
218216

219217
/**

src/auth/Auth.php

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/auth/Basic.php

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/auth/Bearer.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/auth/None.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/helpers/Auth.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Bolt\helpers;
4+
5+
/**
6+
* Class Auth
7+
* Helper to generate array of extra parameters for INIT/HELLO message
8+
*
9+
* @author Michal Stefanak
10+
* @link https://github.com/neo4j-php/Bolt
11+
* @package Bolt\helpers
12+
*/
13+
class Auth
14+
{
15+
/**
16+
* @var string
17+
*/
18+
public static $userAgent = 'bolt-php';
19+
20+
/**
21+
* None authorization
22+
* @return array
23+
*/
24+
public static function none(): array
25+
{
26+
return [
27+
'user_agent' => self::$userAgent,
28+
'scheme' => 'none'
29+
];
30+
}
31+
32+
/**
33+
* Basic authorization with username and password
34+
* @param string $username
35+
* @param string $password
36+
* @return array
37+
*/
38+
public static function basic(string $username, string $password): array
39+
{
40+
return [
41+
'user_agent' => self::$userAgent,
42+
'scheme' => 'basic',
43+
'principal' => $username,
44+
'credentials' => $password
45+
];
46+
}
47+
48+
/**
49+
* OIDC authorization with token
50+
* @param string $token
51+
* @return array
52+
*/
53+
public static function bearer(string $token): array
54+
{
55+
return [
56+
'user_agent' => self::$userAgent,
57+
'scheme' => 'bearer',
58+
'credentials' => $token
59+
];
60+
}
61+
}

src/protocol/IProtocol.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface IProtocol
1717
* @param mixed ...$args
1818
* @return array
1919
*/
20-
public function init(...$args): ?array;
20+
public function init(...$args): array;
2121

2222
/**
2323
* Send RUN message

src/protocol/V1.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ class V1 extends AProtocol
1818
{
1919

2020
/**
21+
* Send INIT message
2122
* @param mixed ...$args
2223
* @return array
2324
* @throws Exception
2425
*/
25-
public function init(...$args): ?array
26+
public function init(...$args): array
2627
{
2728
if (count($args) < 1) {
2829
throw new PackException('Wrong arguments count');
@@ -40,10 +41,11 @@ public function init(...$args): ?array
4041
throw new MessageException($output['message'] . ' (' . $output['code'] . ')');
4142
}
4243

43-
return $signature == self::SUCCESS ? $output : null;
44+
return $output;
4445
}
4546

4647
/**
48+
* Send RUN message
4749
* @param mixed ...$args
4850
* @return array
4951
* @throws Exception
@@ -66,6 +68,7 @@ public function run(...$args): array
6668
}
6769

6870
/**
71+
* Send PULL_ALL message
6972
* @param mixed ...$args
7073
* @return array
7174
* @throws Exception
@@ -90,6 +93,7 @@ public function pullAll(...$args): array
9093
}
9194

9295
/**
96+
* Send DISCARD_ALL message
9397
* @param mixed ...$args
9498
* @return bool
9599
* @throws Exception
@@ -119,6 +123,9 @@ private function ackFailure(): bool
119123
}
120124

121125
/**
126+
* Send RESET message
127+
* The RESET message requests that the connection should be set back to its initial READY state, as if an INIT had just successfully completed.
128+
*
122129
* @return bool
123130
* @throws Exception
124131
*/

src/protocol/V3.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@ class V3 extends V2
1818
{
1919

2020
/**
21-
* @param mixed ...$args
22-
* @return array
23-
* @throws Exception
21+
* @inheritDoc
22+
* @deprecated Replaced with HELLO message
2423
*/
25-
public function init(...$args): ?array
24+
public function init(...$args): array
2625
{
2726
$args[] = null;
2827
return $this->hello(...$args);
2928
}
3029

3130
/**
31+
* Send HELLO message
3232
* @param mixed ...$args
3333
* @return array
3434
* @throws Exception
3535
*/
36-
public function hello(...$args): ?array
36+
public function hello(...$args): array
3737
{
3838
if (count($args) < 1) {
3939
throw new PackException('Wrong arguments count');
@@ -46,7 +46,7 @@ public function hello(...$args): ?array
4646
throw new MessageException($output['message'] . ' (' . $output['code'] . ')');
4747
}
4848

49-
return $signature == self::SUCCESS ? $output : null;
49+
return $output;
5050
}
5151

5252
/**

0 commit comments

Comments
 (0)