2020use Bolt \protocol \V5_3 ;
2121use Bolt \protocol \V5_4 ;
2222use Exception ;
23+ use Laudis \Neo4j \Bolt \BoltMessageFactory ;
2324use Laudis \Neo4j \Common \Neo4jLogger ;
2425use Laudis \Neo4j \Common \ResponseHelper ;
2526use Laudis \Neo4j \Contracts \AuthenticateInterface ;
3435 */
3536final class KerberosAuth implements AuthenticateInterface
3637{
37- /**
38- * @psalm-external-mutation-free
39- */
4038 public function __construct (
4139 private readonly string $ token ,
4240 private readonly ?Neo4jLogger $ logger ,
@@ -47,11 +45,6 @@ public function authenticateHttp(RequestInterface $request, UriInterface $uri, s
4745 {
4846 $ this ->logger ?->log(LogLevel::DEBUG , 'Authenticating using KerberosAuth ' );
4947
50- /**
51- * @psalm-suppress ImpureMethodCall Request is a pure object:
52- *
53- * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message-meta.md#why-value-objects
54- */
5548 return $ request ->withHeader ('Authorization ' , 'Kerberos ' .$ this ->token )
5649 ->withHeader ('User-Agent ' , $ userAgent );
5750 }
@@ -63,36 +56,40 @@ public function authenticateHttp(RequestInterface $request, UriInterface $uri, s
6356 */
6457 public function authenticateBolt (V4_4 |V5 |V5_1 |V5_2 |V5_3 |V5_4 $ protocol , string $ userAgent ): array
6558 {
66- if (method_exists ($ protocol , 'logon ' )) {
67- $ this ->logger ?->log(LogLevel::DEBUG , 'HELLO ' , ['user_agent ' => $ userAgent ]);
68- $ protocol ->hello (['user_agent ' => $ userAgent ]);
69- $ response = ResponseHelper::getResponse ($ protocol );
70- $ this ->logger ?->log(LogLevel::DEBUG , 'LOGON ' , ['scheme ' => 'kerberos ' , 'principal ' => '' ]);
71- $ protocol ->logon ([
72- 'scheme ' => 'kerberos ' ,
73- 'principal ' => '' ,
74- 'credentials ' => $ this ->token ,
75- ]);
76- ResponseHelper::getResponse ($ protocol );
77-
78- /** @var array{server: string, connection_id: string, hints: list} */
79- return $ response ->content ;
80- } else {
81- $ this ->logger ?->log(LogLevel::DEBUG , 'HELLO ' , ['user_agent ' => $ userAgent , 'scheme ' => 'kerberos ' , 'principal ' => '' ]);
82- $ protocol ->hello ([
83- 'user_agent ' => $ userAgent ,
84- 'scheme ' => 'kerberos ' ,
85- 'principal ' => '' ,
86- 'credentials ' => $ this ->token ,
87- ]);
88-
89- /** @var array{server: string, connection_id: string, hints: list} */
90- return ResponseHelper::getResponse ($ protocol )->content ;
91- }
59+ $ factory = $ this ->createMessageFactory ($ protocol );
60+
61+ $ this ->logger ?->log(LogLevel::DEBUG , 'HELLO ' , ['user_agent ' => $ userAgent ]);
62+
63+ $ factory ->createHelloMessage (['user_agent ' => $ userAgent ])->send ();
64+
65+ $ response = ResponseHelper::getResponse ($ protocol );
66+
67+ $ this ->logger ?->log(LogLevel::DEBUG , 'LOGON ' , ['scheme ' => 'kerberos ' , 'principal ' => '' ]);
68+
69+ $ factory ->createLogonMessage ([
70+ 'scheme ' => 'kerberos ' ,
71+ 'principal ' => '' ,
72+ 'credentials ' => $ this ->token ,
73+ ])->send ();
74+
75+ ResponseHelper::getResponse ($ protocol );
76+
77+ /**
78+ * @var array{server: string, connection_id: string, hints: list}
79+ */
80+ return $ response ->content ;
9281 }
9382
9483 public function toString (UriInterface $ uri ): string
9584 {
9685 return sprintf ('Kerberos %s@%s:%s ' , $ this ->token , $ uri ->getHost (), $ uri ->getPort () ?? '' );
9786 }
87+
88+ /**
89+ * Helper to create the message factory.
90+ */
91+ private function createMessageFactory (V4_4 |V5 |V5_1 |V5_2 |V5_3 |V5_4 $ protocol ): BoltMessageFactory
92+ {
93+ return new BoltMessageFactory ($ protocol , $ this ->logger );
94+ }
9895}
0 commit comments