-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathBasicAuth.php
More file actions
90 lines (74 loc) · 2.81 KB
/
BasicAuth.php
File metadata and controls
90 lines (74 loc) · 2.81 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
<?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 Exception;
use Laudis\Neo4j\Bolt\BoltConnection;
use Laudis\Neo4j\Bolt\BoltMessageFactory;
use Laudis\Neo4j\Common\Neo4jLogger;
use Laudis\Neo4j\Contracts\AuthenticateInterface;
use Psr\Http\Message\UriInterface;
/**
* Authenticates connections using a basic username and password.
*/
final class BasicAuth implements AuthenticateInterface
{
public function __construct(
private readonly string $username,
private readonly string $password,
private readonly ?Neo4jLogger $logger,
) {
}
/**
* @throws Exception
*
* @return array{server: string, connection_id: string, hints: list, patch_bolt?: list<string>}
*/
public function authenticateBolt(BoltConnection $connection, string $userAgent): array
{
$factory = $this->createMessageFactory($connection);
$protocol = $connection->protocol();
if (method_exists($protocol, 'logon')) {
$helloMetadata = BoltHelloMetadata::withUtcPatchIfSupported($connection, ['user_agent' => $userAgent]);
$responseHello = $factory->createHelloMessage($helloMetadata)->send()->getResponse();
$credentials = [
'scheme' => 'basic',
'principal' => $this->username,
'credentials' => $this->password,
];
$response = $factory->createLogonMessage($credentials)->send()->getResponse();
/** @var array{server: string, connection_id: string, hints: list, patch_bolt?: list<string>} */
return array_merge($responseHello->content, $response->content);
}
$helloMetadata = BoltHelloMetadata::withUtcPatchIfSupported($connection, [
'user_agent' => $userAgent,
'scheme' => 'basic',
'principal' => $this->username,
'credentials' => $this->password,
]);
$response = $factory->createHelloMessage($helloMetadata)->send()->getResponse();
/** @var array{server: string, connection_id: string, hints: list, patch_bolt?: list<string>} */
return $response->content;
}
/**
* @throws Exception
*/
public function toString(UriInterface $uri): string
{
return sprintf('Basic %s:%s@%s:%s', $this->username, '######', $uri->getHost(), $uri->getPort() ?? '');
}
/**
* Helper to create message factory.
*/
private function createMessageFactory(BoltConnection $connection): BoltMessageFactory
{
return new BoltMessageFactory($connection, $this->logger);
}
}