13
13
14
14
namespace Laudis \Neo4j \Authentication ;
15
15
16
+ use Bolt \enum \Signature ;
17
+ use Bolt \protocol \Response ;
18
+ use Bolt \protocol \V4_4 ;
19
+ use Bolt \protocol \V5 ;
20
+ use Bolt \protocol \V5_1 ;
21
+ use Bolt \protocol \V5_2 ;
22
+ use Bolt \protocol \V5_3 ;
23
+ use Bolt \protocol \V5_4 ;
16
24
use Exception ;
17
- use Laudis \Neo4j \Bolt \BoltConnection ;
18
25
use Laudis \Neo4j \Bolt \BoltMessageFactory ;
19
26
use Laudis \Neo4j \Common \Neo4jLogger ;
20
27
use Laudis \Neo4j \Contracts \AuthenticateInterface ;
21
- use Laudis \Neo4j \Enum \ConnectionProtocol ;
28
+ use Laudis \Neo4j \Exception \Neo4jException ;
29
+ use Psr \Http \Message \RequestInterface ;
22
30
use Psr \Http \Message \UriInterface ;
31
+ use Psr \Log \LogLevel ;
23
32
24
33
use function sprintf ;
25
34
@@ -30,21 +39,30 @@ public function __construct(
30
39
) {
31
40
}
32
41
42
+ public function authenticateHttp (RequestInterface $ request , UriInterface $ uri , string $ userAgent ): RequestInterface
43
+ {
44
+ $ this ->logger ?->log(LogLevel::DEBUG , 'Authentication disabled ' );
45
+
46
+ return $ request ->withHeader ('User-Agent ' , $ userAgent );
47
+ }
48
+
33
49
/**
34
50
* @throws Exception
35
51
*
36
52
* @return array{server: string, connection_id: string, hints: list}
37
53
*/
38
- public function authenticateBolt (BoltConnection $ connection , string $ userAgent ): array
54
+ public function authenticateBolt (V4_4 | V5 | V5_1 | V5_2 | V5_3 | V5_4 $ protocol , string $ userAgent ): array
39
55
{
40
- $ factory = $ this ->createMessageFactory ($ connection );
56
+ $ factory = $ this ->createMessageFactory ($ protocol );
41
57
42
- if ($ connection -> getProtocol ()-> compare (ConnectionProtocol:: BOLT_V5_1 ()) >= 0 ) {
58
+ if (method_exists ( $ protocol , ' logon ' ) ) {
43
59
$ helloMetadata = ['user_agent ' => $ userAgent ];
44
60
45
- $ factory ->createHelloMessage ($ helloMetadata )->send ()->getResponse ();
61
+ $ factory ->createHelloMessage ($ helloMetadata )->send ();
62
+ $ response = self ::getResponse ($ protocol );
46
63
47
- $ response = $ factory ->createLogonMessage (['scheme ' => 'none ' ])->send ()->getResponse ();
64
+ $ factory ->createLogonMessage (['scheme ' => 'none ' ])->send ();
65
+ self ::getResponse ($ protocol );
48
66
49
67
/** @var array{server: string, connection_id: string, hints: list} */
50
68
return $ response ->content ;
@@ -55,19 +73,36 @@ public function authenticateBolt(BoltConnection $connection, string $userAgent):
55
73
'scheme ' => 'none ' ,
56
74
];
57
75
58
- $ response = $ factory ->createHelloMessage ($ helloMetadata )->send ()-> getResponse ();
76
+ $ factory ->createHelloMessage ($ helloMetadata )->send ();
59
77
60
78
/** @var array{server: string, connection_id: string, hints: list} */
61
- return $ response ->content ;
79
+ return self ::getResponse ($ protocol )->content ;
80
+ }
81
+
82
+ public static function getResponse (V4_4 |V5 |V5_1 |V5_2 |V5_3 |V5_4 $ protocol ): Response
83
+ {
84
+ $ response = $ protocol ->getResponse ();
85
+ if ($ response ->signature === Signature::FAILURE ) {
86
+ throw Neo4jException::fromBoltResponse ($ response );
87
+ }
88
+
89
+ return $ response ;
90
+ }
91
+
92
+ public function logoff (V4_4 |V5 |V5_1 |V5_2 |V5_3 |V5_4 $ protocol ): void
93
+ {
94
+ $ factory = $ this ->createMessageFactory ($ protocol );
95
+ $ factory ->createLogoffMessage ()->send ();
96
+ $ protocol ->getResponse ();
62
97
}
63
98
64
99
public function toString (UriInterface $ uri ): string
65
100
{
66
101
return sprintf ('No Auth %s:%s ' , $ uri ->getHost (), $ uri ->getPort () ?? '' );
67
102
}
68
103
69
- private function createMessageFactory (BoltConnection $ connection ): BoltMessageFactory
104
+ private function createMessageFactory (V4_4 | V5 | V5_1 | V5_2 | V5_3 | V5_4 $ protocol ): BoltMessageFactory
70
105
{
71
- return new BoltMessageFactory ($ connection , $ this ->logger );
106
+ return new BoltMessageFactory ($ protocol , $ this ->logger );
72
107
}
73
108
}
0 commit comments