-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathOpenIDConnectAuth.php
More file actions
83 lines (66 loc) · 2.49 KB
/
OpenIDConnectAuth.php
File metadata and controls
83 lines (66 loc) · 2.49 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
<?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\RequestInterface;
use Psr\Http\Message\UriInterface;
use Psr\Log\LogLevel;
use function sprintf;
class OpenIDConnectAuth 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 OpenIDConnectAuth');
return $request->withHeader('Authorization', 'Bearer '.$this->token)
->withHeader('User-Agent', $userAgent);
}
/**
* @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);
$this->logger?->log(LogLevel::DEBUG, 'HELLO', ['user_agent' => $userAgent]);
$helloMetadata = BoltHelloMetadata::withUtcPatchIfSupported($connection, ['user_agent' => $userAgent]);
$factory->createHelloMessage($helloMetadata)->send()->getResponse();
$this->logger?->log(LogLevel::DEBUG, 'LOGON', ['scheme' => 'bearer']);
$response = $factory->createLogonMessage([
'scheme' => 'bearer',
'credentials' => $this->token,
])->send()->getResponse();
/**
* @var array{server: string, connection_id: string, hints: list, patch_bolt?: list<string>}
*/
return $response->content;
}
public function toString(UriInterface $uri): string
{
return sprintf('OpenId %s@%s:%s', $this->token, $uri->getHost(), $uri->getPort() ?? '');
}
/**
* Helper to create the message factory.
*/
public function createMessageFactory(BoltConnection $connection): BoltMessageFactory
{
return new BoltMessageFactory($connection, $this->logger);
}
}