1313
1414namespace Laudis \Neo4j \Authentication ;
1515
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 ;
1624use Exception ;
17- use Laudis \Neo4j \Bolt \BoltConnection ;
1825use Laudis \Neo4j \Bolt \BoltMessageFactory ;
1926use Laudis \Neo4j \Common \Neo4jLogger ;
2027use Laudis \Neo4j \Contracts \AuthenticateInterface ;
21- use Laudis \Neo4j \Enum \ConnectionProtocol ;
28+ use Laudis \Neo4j \Exception \Neo4jException ;
29+ use Psr \Http \Message \RequestInterface ;
2230use Psr \Http \Message \UriInterface ;
31+ use Psr \Log \LogLevel ;
2332
2433use function sprintf ;
2534
@@ -30,21 +39,30 @@ public function __construct(
3039 ) {
3140 }
3241
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+
3349 /**
3450 * @throws Exception
3551 *
3652 * @return array{server: string, connection_id: string, hints: list}
3753 */
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
3955 {
40- $ factory = $ this ->createMessageFactory ($ connection );
56+ $ factory = $ this ->createMessageFactory ($ protocol );
4157
42- if ($ connection -> getProtocol ()-> compare (ConnectionProtocol:: BOLT_V5_1 ()) >= 0 ) {
58+ if (method_exists ( $ protocol , ' logon ' ) ) {
4359 $ helloMetadata = ['user_agent ' => $ userAgent ];
4460
45- $ factory ->createHelloMessage ($ helloMetadata )->send ()->getResponse ();
61+ $ factory ->createHelloMessage ($ helloMetadata )->send ();
62+ $ response = self ::getResponse ($ protocol );
4663
47- $ response = $ factory ->createLogonMessage (['scheme ' => 'none ' ])->send ()->getResponse ();
64+ $ factory ->createLogonMessage (['scheme ' => 'none ' ])->send ();
65+ self ::getResponse ($ protocol );
4866
4967 /** @var array{server: string, connection_id: string, hints: list} */
5068 return $ response ->content ;
@@ -55,19 +73,36 @@ public function authenticateBolt(BoltConnection $connection, string $userAgent):
5573 'scheme ' => 'none ' ,
5674 ];
5775
58- $ response = $ factory ->createHelloMessage ($ helloMetadata )->send ()-> getResponse ();
76+ $ factory ->createHelloMessage ($ helloMetadata )->send ();
5977
6078 /** @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 ();
6297 }
6398
6499 public function toString (UriInterface $ uri ): string
65100 {
66101 return sprintf ('No Auth %s:%s ' , $ uri ->getHost (), $ uri ->getPort () ?? '' );
67102 }
68103
69- private function createMessageFactory (BoltConnection $ connection ): BoltMessageFactory
104+ private function createMessageFactory (V4_4 | V5 | V5_1 | V5_2 | V5_3 | V5_4 $ protocol ): BoltMessageFactory
70105 {
71- return new BoltMessageFactory ($ connection , $ this ->logger );
106+ return new BoltMessageFactory ($ protocol , $ this ->logger );
72107 }
73108}
0 commit comments