-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathKerberosAuth.php
More file actions
107 lines (87 loc) · 3.02 KB
/
Copy pathKerberosAuth.php
File metadata and controls
107 lines (87 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
declare(strict_types=1);
/*
* This file is part of the Neo4j PHP Client and Driver package.
*
* (c) Nagels <https://nagels.tech>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Laudis\Neo4j\Authentication;
use Bolt\enum\Signature;
use Bolt\protocol\Response;
use Bolt\protocol\V4_4;
use Bolt\protocol\V5;
use Bolt\protocol\V5_1;
use Bolt\protocol\V5_2;
use Bolt\protocol\V5_3;
use Bolt\protocol\V5_4;
use Exception;
use Laudis\Neo4j\Bolt\BoltMessageFactory;
use Laudis\Neo4j\Common\Neo4jLogger;
use Laudis\Neo4j\Contracts\AuthenticateInterface;
use Laudis\Neo4j\Exception\Neo4jException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;
use Psr\Log\LogLevel;
use function sprintf;
/**
* Authenticates connections using a kerberos token.
*/
final class KerberosAuth implements AuthenticateInterface
{
public function __construct(
private readonly string $token,
private readonly ?Neo4jLogger $logger,
) {
}
public function authenticateHttp(RequestInterface $request, UriInterface $uri, string $userAgent): RequestInterface
{
$this->logger?->log(LogLevel::DEBUG, 'Authenticating using KerberosAuth');
return $request->withHeader('Authorization', 'Kerberos '.$this->token)
->withHeader('User-Agent', $userAgent);
}
/**
* @throws Exception
*
* @return array{server: string, connection_id: string, hints: list}
*/
public function authenticateBolt(V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol, string $userAgent): array
{
$factory = $this->createMessageFactory($protocol);
$this->logger?->log(LogLevel::DEBUG, 'HELLO', ['user_agent' => $userAgent]);
$factory->createHelloMessage(['user_agent' => $userAgent])->send();
$response = self::getResponse($protocol);
$this->logger?->log(LogLevel::DEBUG, 'LOGON', ['scheme' => 'kerberos', 'principal' => '']);
$factory->createLogonMessage([
'scheme' => 'kerberos',
'principal' => '',
'credentials' => $this->token,
])->send();
self::getResponse($protocol);
/**
* @var array{server: string, connection_id: string, hints: list}
*/
return $response->content;
}
public static function getResponse(V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol): Response
{
$response = $protocol->getResponse();
if ($response->signature === Signature::FAILURE) {
throw Neo4jException::fromBoltResponse($response);
}
return $response;
}
public function toString(UriInterface $uri): string
{
return sprintf('Kerberos %s@%s:%s', $this->token, $uri->getHost(), $uri->getPort() ?? '');
}
/**
* Helper to create the message factory.
*/
private function createMessageFactory(V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol): BoltMessageFactory
{
return new BoltMessageFactory($protocol, $this->logger);
}
}