Skip to content

Commit 6d3786b

Browse files
committed
feat: Integrate PSR LoggerInterface and add logging
1 parent 03fe278 commit 6d3786b

23 files changed

+437
-60
lines changed

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"suggest": {
4141
"ext-bcmath": "Needed to implement bolt protocol",
4242
"ext-sysvsem": "Needed for enabling connection pooling between processes",
43-
"composer-runtime-api": "Install composer 2 for auto detection of version in user agent"
43+
"composer-runtime-api": "Install composer 2 for auto detection of version in user agent",
44+
"psr/log": "Needed to enable logging"
4445
},
4546
"require-dev": {
4647
"phpunit/phpunit": "^10.0",
@@ -51,12 +52,12 @@
5152
"friendsofphp/php-cs-fixer": "3.15.0",
5253
"psalm/plugin-phpunit": "^0.18",
5354
"monolog/monolog": "^2.2",
54-
"psr/log": "^1.1",
5555
"symfony/uid": "^5.0",
5656
"symfony/var-dumper": "^5.0",
5757
"cache/integration-tests": "dev-master",
5858
"kubawerlos/php-cs-fixer-custom-fixers": "3.13.*",
59-
"rector/rector": "^1.0"
59+
"rector/rector": "^1.0",
60+
"psr/log": "^3.0"
6061
},
6162
"autoload": {
6263
"psr-4": {

src/Authentication/Authenticate.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use function explode;
1717

18+
use Laudis\Neo4j\Common\Neo4jLogger;
1819
use Laudis\Neo4j\Contracts\AuthenticateInterface;
1920
use Psr\Http\Message\UriInterface;
2021

@@ -32,47 +33,47 @@ final class Authenticate
3233
*
3334
* @pure
3435
*/
35-
public static function basic(string $username, string $password): BasicAuth
36+
public static function basic(string $username, string $password, ?Neo4jLogger $logger = null): BasicAuth
3637
{
37-
return new BasicAuth($username, $password);
38+
return new BasicAuth($username, $password, $logger);
3839
}
3940

4041
/**
4142
* Authenticate using a kerberos token.
4243
*
4344
* @pure
4445
*/
45-
public static function kerberos(string $token): KerberosAuth
46+
public static function kerberos(string $token, ?Neo4jLogger $logger = null): KerberosAuth
4647
{
47-
return new KerberosAuth($token);
48+
return new KerberosAuth($token, $logger);
4849
}
4950

5051
/**
5152
* Authenticate using a OpenID Connect token.
5253
*
5354
* @pure
5455
*/
55-
public static function oidc(string $token): OpenIDConnectAuth
56+
public static function oidc(string $token, ?Neo4jLogger $logger = null): OpenIDConnectAuth
5657
{
57-
return new OpenIDConnectAuth($token);
58+
return new OpenIDConnectAuth($token, $logger);
5859
}
5960

6061
/**
6162
* Don't authenticate at all.
6263
*
6364
* @pure
6465
*/
65-
public static function disabled(): NoAuth
66+
public static function disabled(?Neo4jLogger $logger = null): NoAuth
6667
{
67-
return new NoAuth();
68+
return new NoAuth($logger);
6869
}
6970

7071
/**
7172
* Authenticate from information found in the url.
7273
*
7374
* @pure
7475
*/
75-
public static function fromUrl(UriInterface $uri): AuthenticateInterface
76+
public static function fromUrl(UriInterface $uri, ?Neo4jLogger $logger = null): AuthenticateInterface
7677
{
7778
/**
7879
* @psalm-suppress ImpureMethodCall Uri is a pure object:
@@ -86,9 +87,9 @@ public static function fromUrl(UriInterface $uri): AuthenticateInterface
8687
$explode = explode(':', $userInfo);
8788
[$user, $pass] = $explode;
8889

89-
return self::basic($user, $pass);
90+
return self::basic($user, $pass, $logger);
9091
}
9192

92-
return self::disabled();
93+
return self::disabled($logger);
9394
}
9495
}

src/Authentication/BasicAuth.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
use Bolt\protocol\V5_3;
2323
use Bolt\protocol\V5_4;
2424
use Exception;
25+
use Laudis\Neo4j\Common\Neo4jLogger;
2526
use Laudis\Neo4j\Common\ResponseHelper;
2627
use Laudis\Neo4j\Contracts\AuthenticateInterface;
2728
use Psr\Http\Message\RequestInterface;
2829
use Psr\Http\Message\UriInterface;
30+
use Psr\Log\LogLevel;
2931

3032
/**
3133
* Authenticates connections using a basic username and password.
@@ -37,14 +39,16 @@ final class BasicAuth implements AuthenticateInterface
3739
*/
3840
public function __construct(
3941
private readonly string $username,
40-
private readonly string $password
42+
private readonly string $password,
43+
private readonly ?Neo4jLogger $logger,
4144
) {}
4245

4346
/**
4447
* @psalm-mutation-free
4548
*/
4649
public function authenticateHttp(RequestInterface $request, UriInterface $uri, string $userAgent): RequestInterface
4750
{
51+
$this->logger?->log(LogLevel::DEBUG, 'Authenticating using BasicAuth');
4852
$combo = base64_encode($this->username.':'.$this->password);
4953

5054
/**
@@ -64,8 +68,10 @@ public function authenticateHttp(RequestInterface $request, UriInterface $uri, s
6468
public function authenticateBolt(V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol, string $userAgent): array
6569
{
6670
if (method_exists($protocol, 'logon')) {
71+
$this->logger?->log(LogLevel::DEBUG, 'HELLO', ['user_agent' => $userAgent]);
6772
$protocol->hello(['user_agent' => $userAgent]);
6873
$response = ResponseHelper::getResponse($protocol);
74+
$this->logger?->log(LogLevel::DEBUG, 'LOGON', ['scheme' => 'basic', 'principal' => $this->username]);
6975
$protocol->logon([
7076
'scheme' => 'basic',
7177
'principal' => $this->username,
@@ -76,6 +82,7 @@ public function authenticateBolt(V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol, string $
7682
/** @var array{server: string, connection_id: string, hints: list} */
7783
return $response->content;
7884
} else {
85+
$this->logger?->log(LogLevel::DEBUG, 'HELLO', ['user_agent' => $userAgent, 'scheme' => 'basic', 'principal' => $this->username]);
7986
$protocol->hello([
8087
'user_agent' => $userAgent,
8188
'scheme' => 'basic',

src/Authentication/KerberosAuth.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
use Bolt\protocol\V5_3;
2121
use Bolt\protocol\V5_4;
2222
use Exception;
23+
use Laudis\Neo4j\Common\Neo4jLogger;
2324
use Laudis\Neo4j\Common\ResponseHelper;
2425
use Laudis\Neo4j\Contracts\AuthenticateInterface;
2526
use Psr\Http\Message\RequestInterface;
2627
use Psr\Http\Message\UriInterface;
28+
use Psr\Log\LogLevel;
2729

2830
use function sprintf;
2931

@@ -36,14 +38,16 @@ final class KerberosAuth implements AuthenticateInterface
3638
* @psalm-external-mutation-free
3739
*/
3840
public function __construct(
39-
private readonly string $token
41+
private readonly string $token,
42+
private readonly ?Neo4jLogger $logger,
4043
) {}
4144

4245
/**
4346
* @psalm-mutation-free
4447
*/
4548
public function authenticateHttp(RequestInterface $request, UriInterface $uri, string $userAgent): RequestInterface
4649
{
50+
$this->logger?->log(LogLevel::DEBUG, 'Authenticating using KerberosAuth');
4751
/**
4852
* @psalm-suppress ImpureMethodCall Request is a pure object:
4953
*
@@ -61,8 +65,10 @@ public function authenticateHttp(RequestInterface $request, UriInterface $uri, s
6165
public function authenticateBolt(V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol, string $userAgent): array
6266
{
6367
if (method_exists($protocol, 'logon')) {
68+
$this->logger?->log(LogLevel::DEBUG, 'HELLO', ['user_agent' => $userAgent]);
6469
$protocol->hello(['user_agent' => $userAgent]);
6570
$response = ResponseHelper::getResponse($protocol);
71+
$this->logger?->log(LogLevel::DEBUG, 'LOGON', ['scheme' => 'kerberos', 'principal' => '']);
6672
$protocol->logon([
6773
'scheme' => 'kerberos',
6874
'principal' => '',
@@ -73,6 +79,7 @@ public function authenticateBolt(V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol, string $
7379
/** @var array{server: string, connection_id: string, hints: list} */
7480
return $response->content;
7581
} else {
82+
$this->logger?->log(LogLevel::DEBUG, 'HELLO', ['user_agent' => $userAgent, 'scheme' => 'kerberos', 'principal' => '']);
7683
$protocol->hello([
7784
'user_agent' => $userAgent,
7885
'scheme' => 'kerberos',

src/Authentication/NoAuth.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
use Bolt\protocol\V5_3;
2121
use Bolt\protocol\V5_4;
2222
use Exception;
23+
use Laudis\Neo4j\Common\Neo4jLogger;
2324
use Laudis\Neo4j\Common\ResponseHelper;
2425
use Laudis\Neo4j\Contracts\AuthenticateInterface;
2526
use Psr\Http\Message\RequestInterface;
2627
use Psr\Http\Message\UriInterface;
28+
use Psr\Log\LogLevel;
2729

2830
use function sprintf;
2931

@@ -32,11 +34,19 @@
3234
*/
3335
final class NoAuth implements AuthenticateInterface
3436
{
37+
/**
38+
* @pure
39+
*/
40+
public function __construct(
41+
private readonly ?Neo4jLogger $logger
42+
) {}
43+
3544
/**
3645
* @psalm-mutation-free
3746
*/
3847
public function authenticateHttp(RequestInterface $request, UriInterface $uri, string $userAgent): RequestInterface
3948
{
49+
$this->logger?->log(LogLevel::DEBUG, 'Authentication disabled');
4050
/**
4151
* @psalm-suppress ImpureMethodCall Request is a pure object:
4252
*
@@ -53,8 +63,10 @@ public function authenticateHttp(RequestInterface $request, UriInterface $uri, s
5363
public function authenticateBolt(V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol, string $userAgent): array
5464
{
5565
if (method_exists($protocol, 'logon')) {
66+
$this->logger->log(LogLevel::DEBUG, 'HELLO', ['user_agent' => $userAgent]);
5667
$protocol->hello(['user_agent' => $userAgent]);
5768
$response = ResponseHelper::getResponse($protocol);
69+
$this->logger->log(LogLevel::DEBUG, 'LOGON', ['scheme' => 'none']);
5870
$protocol->logon([
5971
'scheme' => 'none',
6072
]);
@@ -63,6 +75,7 @@ public function authenticateBolt(V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol, string $
6375
/** @var array{server: string, connection_id: string, hints: list} */
6476
return $response->content;
6577
} else {
78+
$this->logger->log(LogLevel::DEBUG, 'HELLO', ['user_agent' => $userAgent, 'scheme' => 'none']);
6679
$protocol->hello([
6780
'user_agent' => $userAgent,
6881
'scheme' => 'none',

src/Authentication/OpenIDConnectAuth.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Bolt\protocol\V5_3;
2121
use Bolt\protocol\V5_4;
2222
use Exception;
23+
use Laudis\Neo4j\Common\Neo4jLogger;
2324
use Laudis\Neo4j\Common\ResponseHelper;
2425
use Laudis\Neo4j\Contracts\AuthenticateInterface;
2526
use Psr\Http\Message\RequestInterface;
@@ -33,14 +34,16 @@ final class OpenIDConnectAuth implements AuthenticateInterface
3334
* @psalm-external-mutation-free
3435
*/
3536
public function __construct(
36-
private readonly string $token
37+
private readonly string $token,
38+
private readonly ?Neo4jLogger $logger
3739
) {}
3840

3941
/**
4042
* @psalm-mutation-free
4143
*/
4244
public function authenticateHttp(RequestInterface $request, UriInterface $uri, string $userAgent): RequestInterface
4345
{
46+
$this->logger?->log(LogLevel::DEBUG, 'Authenticating using OpenIDConnectAuth');
4447
/**
4548
* @psalm-suppress ImpureMethodCall Request is a pure object:
4649
*
@@ -58,8 +61,10 @@ public function authenticateHttp(RequestInterface $request, UriInterface $uri, s
5861
public function authenticateBolt(V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol, string $userAgent): array
5962
{
6063
if (method_exists($protocol, 'logon')) {
64+
$this->logger?->log(LogLevel::DEBUG, 'HELLO', ['user_agent' => $userAgent]);
6165
$protocol->hello(['user_agent' => $userAgent]);
6266
$response = ResponseHelper::getResponse($protocol);
67+
$this->logger?->log(LogLevel::DEBUG, 'LOGON', ['scheme' => 'bearer']);
6368
$protocol->logon([
6469
'scheme' => 'bearer',
6570
'credentials' => $this->token,
@@ -69,6 +74,7 @@ public function authenticateBolt(V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol, string $
6974
/** @var array{server: string, connection_id: string, hints: list} */
7075
return $response->content;
7176
} else {
77+
$this->logger?->log(LogLevel::DEBUG, 'HELLO', ['user_agent' => $userAgent, 'scheme' => 'bearer']);
7278
$protocol->hello([
7379
'user_agent' => $userAgent,
7480
'scheme' => 'bearer',

0 commit comments

Comments
 (0)