Skip to content

Commit

Permalink
feat: Integrate PSR LoggerInterface and add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
exaby73 committed Sep 25, 2024
1 parent 03fe278 commit 6d3786b
Show file tree
Hide file tree
Showing 23 changed files with 437 additions and 60 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"suggest": {
"ext-bcmath": "Needed to implement bolt protocol",
"ext-sysvsem": "Needed for enabling connection pooling between processes",
"composer-runtime-api": "Install composer 2 for auto detection of version in user agent"
"composer-runtime-api": "Install composer 2 for auto detection of version in user agent",
"psr/log": "Needed to enable logging"
},
"require-dev": {
"phpunit/phpunit": "^10.0",
Expand All @@ -51,12 +52,12 @@
"friendsofphp/php-cs-fixer": "3.15.0",
"psalm/plugin-phpunit": "^0.18",
"monolog/monolog": "^2.2",
"psr/log": "^1.1",
"symfony/uid": "^5.0",
"symfony/var-dumper": "^5.0",
"cache/integration-tests": "dev-master",
"kubawerlos/php-cs-fixer-custom-fixers": "3.13.*",
"rector/rector": "^1.0"
"rector/rector": "^1.0",
"psr/log": "^3.0"
},
"autoload": {
"psr-4": {
Expand Down
23 changes: 12 additions & 11 deletions src/Authentication/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use function explode;

use Laudis\Neo4j\Common\Neo4jLogger;
use Laudis\Neo4j\Contracts\AuthenticateInterface;
use Psr\Http\Message\UriInterface;

Expand All @@ -32,47 +33,47 @@ final class Authenticate
*
* @pure
*/
public static function basic(string $username, string $password): BasicAuth
public static function basic(string $username, string $password, ?Neo4jLogger $logger = null): BasicAuth
{
return new BasicAuth($username, $password);
return new BasicAuth($username, $password, $logger);
}

/**
* Authenticate using a kerberos token.
*
* @pure
*/
public static function kerberos(string $token): KerberosAuth
public static function kerberos(string $token, ?Neo4jLogger $logger = null): KerberosAuth
{
return new KerberosAuth($token);
return new KerberosAuth($token, $logger);
}

/**
* Authenticate using a OpenID Connect token.
*
* @pure
*/
public static function oidc(string $token): OpenIDConnectAuth
public static function oidc(string $token, ?Neo4jLogger $logger = null): OpenIDConnectAuth
{
return new OpenIDConnectAuth($token);
return new OpenIDConnectAuth($token, $logger);
}

/**
* Don't authenticate at all.
*
* @pure
*/
public static function disabled(): NoAuth
public static function disabled(?Neo4jLogger $logger = null): NoAuth
{
return new NoAuth();
return new NoAuth($logger);
}

/**
* Authenticate from information found in the url.
*
* @pure
*/
public static function fromUrl(UriInterface $uri): AuthenticateInterface
public static function fromUrl(UriInterface $uri, ?Neo4jLogger $logger = null): AuthenticateInterface
{
/**
* @psalm-suppress ImpureMethodCall Uri is a pure object:
Expand All @@ -86,9 +87,9 @@ public static function fromUrl(UriInterface $uri): AuthenticateInterface
$explode = explode(':', $userInfo);
[$user, $pass] = $explode;

return self::basic($user, $pass);
return self::basic($user, $pass, $logger);
}

return self::disabled();
return self::disabled($logger);
}
}
9 changes: 8 additions & 1 deletion src/Authentication/BasicAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
use Bolt\protocol\V5_3;
use Bolt\protocol\V5_4;
use Exception;
use Laudis\Neo4j\Common\Neo4jLogger;
use Laudis\Neo4j\Common\ResponseHelper;
use Laudis\Neo4j\Contracts\AuthenticateInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;
use Psr\Log\LogLevel;

/**
* Authenticates connections using a basic username and password.
Expand All @@ -37,14 +39,16 @@ final class BasicAuth implements AuthenticateInterface
*/
public function __construct(
private readonly string $username,
private readonly string $password
private readonly string $password,
private readonly ?Neo4jLogger $logger,
) {}

/**
* @psalm-mutation-free
*/
public function authenticateHttp(RequestInterface $request, UriInterface $uri, string $userAgent): RequestInterface
{
$this->logger?->log(LogLevel::DEBUG, 'Authenticating using BasicAuth');

Check failure on line 51 in src/Authentication/BasicAuth.php

View workflow job for this annotation

GitHub Actions / Lint & Analyse

UnusedMethodCall

src/Authentication/BasicAuth.php:51:25: UnusedMethodCall: The call to Laudis\Neo4j\Common\Neo4jLogger::log is not used (see https://psalm.dev/209)
$combo = base64_encode($this->username.':'.$this->password);

/**
Expand All @@ -64,8 +68,10 @@ public function authenticateHttp(RequestInterface $request, UriInterface $uri, s
public function authenticateBolt(V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol, string $userAgent): array
{
if (method_exists($protocol, 'logon')) {
$this->logger?->log(LogLevel::DEBUG, 'HELLO', ['user_agent' => $userAgent]);

Check failure on line 71 in src/Authentication/BasicAuth.php

View workflow job for this annotation

GitHub Actions / Lint & Analyse

UnusedMethodCall

src/Authentication/BasicAuth.php:71:29: UnusedMethodCall: The call to Laudis\Neo4j\Common\Neo4jLogger::log is not used (see https://psalm.dev/209)
$protocol->hello(['user_agent' => $userAgent]);
$response = ResponseHelper::getResponse($protocol);
$this->logger?->log(LogLevel::DEBUG, 'LOGON', ['scheme' => 'basic', 'principal' => $this->username]);

Check failure on line 74 in src/Authentication/BasicAuth.php

View workflow job for this annotation

GitHub Actions / Lint & Analyse

UnusedMethodCall

src/Authentication/BasicAuth.php:74:29: UnusedMethodCall: The call to Laudis\Neo4j\Common\Neo4jLogger::log is not used (see https://psalm.dev/209)
$protocol->logon([
'scheme' => 'basic',
'principal' => $this->username,
Expand All @@ -76,6 +82,7 @@ public function authenticateBolt(V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol, string $
/** @var array{server: string, connection_id: string, hints: list} */
return $response->content;
} else {
$this->logger?->log(LogLevel::DEBUG, 'HELLO', ['user_agent' => $userAgent, 'scheme' => 'basic', 'principal' => $this->username]);

Check failure on line 85 in src/Authentication/BasicAuth.php

View workflow job for this annotation

GitHub Actions / Lint & Analyse

UnusedMethodCall

src/Authentication/BasicAuth.php:85:29: UnusedMethodCall: The call to Laudis\Neo4j\Common\Neo4jLogger::log is not used (see https://psalm.dev/209)
$protocol->hello([
'user_agent' => $userAgent,
'scheme' => 'basic',
Expand Down
9 changes: 8 additions & 1 deletion src/Authentication/KerberosAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
use Bolt\protocol\V5_3;
use Bolt\protocol\V5_4;
use Exception;
use Laudis\Neo4j\Common\Neo4jLogger;
use Laudis\Neo4j\Common\ResponseHelper;
use Laudis\Neo4j\Contracts\AuthenticateInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;
use Psr\Log\LogLevel;

use function sprintf;

Expand All @@ -36,14 +38,16 @@ final class KerberosAuth implements AuthenticateInterface
* @psalm-external-mutation-free
*/
public function __construct(
private readonly string $token
private readonly string $token,
private readonly ?Neo4jLogger $logger,
) {}

/**
* @psalm-mutation-free
*/
public function authenticateHttp(RequestInterface $request, UriInterface $uri, string $userAgent): RequestInterface
{
$this->logger?->log(LogLevel::DEBUG, 'Authenticating using KerberosAuth');

Check failure on line 50 in src/Authentication/KerberosAuth.php

View workflow job for this annotation

GitHub Actions / Lint & Analyse

UnusedMethodCall

src/Authentication/KerberosAuth.php:50:25: UnusedMethodCall: The call to Laudis\Neo4j\Common\Neo4jLogger::log is not used (see https://psalm.dev/209)
/**
* @psalm-suppress ImpureMethodCall Request is a pure object:
*
Expand All @@ -61,8 +65,10 @@ public function authenticateHttp(RequestInterface $request, UriInterface $uri, s
public function authenticateBolt(V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol, string $userAgent): array
{
if (method_exists($protocol, 'logon')) {
$this->logger?->log(LogLevel::DEBUG, 'HELLO', ['user_agent' => $userAgent]);

Check failure on line 68 in src/Authentication/KerberosAuth.php

View workflow job for this annotation

GitHub Actions / Lint & Analyse

UnusedMethodCall

src/Authentication/KerberosAuth.php:68:29: UnusedMethodCall: The call to Laudis\Neo4j\Common\Neo4jLogger::log is not used (see https://psalm.dev/209)
$protocol->hello(['user_agent' => $userAgent]);
$response = ResponseHelper::getResponse($protocol);
$this->logger?->log(LogLevel::DEBUG, 'LOGON', ['scheme' => 'kerberos', 'principal' => '']);

Check failure on line 71 in src/Authentication/KerberosAuth.php

View workflow job for this annotation

GitHub Actions / Lint & Analyse

UnusedMethodCall

src/Authentication/KerberosAuth.php:71:29: UnusedMethodCall: The call to Laudis\Neo4j\Common\Neo4jLogger::log is not used (see https://psalm.dev/209)
$protocol->logon([
'scheme' => 'kerberos',
'principal' => '',
Expand All @@ -73,6 +79,7 @@ public function authenticateBolt(V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol, string $
/** @var array{server: string, connection_id: string, hints: list} */
return $response->content;
} else {
$this->logger?->log(LogLevel::DEBUG, 'HELLO', ['user_agent' => $userAgent, 'scheme' => 'kerberos', 'principal' => '']);

Check failure on line 82 in src/Authentication/KerberosAuth.php

View workflow job for this annotation

GitHub Actions / Lint & Analyse

UnusedMethodCall

src/Authentication/KerberosAuth.php:82:29: UnusedMethodCall: The call to Laudis\Neo4j\Common\Neo4jLogger::log is not used (see https://psalm.dev/209)
$protocol->hello([
'user_agent' => $userAgent,
'scheme' => 'kerberos',
Expand Down
13 changes: 13 additions & 0 deletions src/Authentication/NoAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
use Bolt\protocol\V5_3;
use Bolt\protocol\V5_4;
use Exception;
use Laudis\Neo4j\Common\Neo4jLogger;
use Laudis\Neo4j\Common\ResponseHelper;
use Laudis\Neo4j\Contracts\AuthenticateInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;
use Psr\Log\LogLevel;

use function sprintf;

Expand All @@ -32,11 +34,19 @@
*/
final class NoAuth implements AuthenticateInterface
{
/**
* @pure
*/
public function __construct(
private readonly ?Neo4jLogger $logger
) {}

/**
* @psalm-mutation-free
*/
public function authenticateHttp(RequestInterface $request, UriInterface $uri, string $userAgent): RequestInterface
{
$this->logger?->log(LogLevel::DEBUG, 'Authentication disabled');

Check failure on line 49 in src/Authentication/NoAuth.php

View workflow job for this annotation

GitHub Actions / Lint & Analyse

UnusedMethodCall

src/Authentication/NoAuth.php:49:25: UnusedMethodCall: The call to Laudis\Neo4j\Common\Neo4jLogger::log is not used (see https://psalm.dev/209)
/**
* @psalm-suppress ImpureMethodCall Request is a pure object:
*
Expand All @@ -53,8 +63,10 @@ public function authenticateHttp(RequestInterface $request, UriInterface $uri, s
public function authenticateBolt(V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol, string $userAgent): array
{
if (method_exists($protocol, 'logon')) {
$this->logger->log(LogLevel::DEBUG, 'HELLO', ['user_agent' => $userAgent]);

Check failure on line 66 in src/Authentication/NoAuth.php

View workflow job for this annotation

GitHub Actions / Lint & Analyse

PossiblyNullReference

src/Authentication/NoAuth.php:66:28: PossiblyNullReference: Cannot call method log on possibly null value (see https://psalm.dev/083)
$protocol->hello(['user_agent' => $userAgent]);
$response = ResponseHelper::getResponse($protocol);
$this->logger->log(LogLevel::DEBUG, 'LOGON', ['scheme' => 'none']);
$protocol->logon([
'scheme' => 'none',
]);
Expand All @@ -63,6 +75,7 @@ public function authenticateBolt(V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol, string $
/** @var array{server: string, connection_id: string, hints: list} */
return $response->content;
} else {
$this->logger->log(LogLevel::DEBUG, 'HELLO', ['user_agent' => $userAgent, 'scheme' => 'none']);
$protocol->hello([
'user_agent' => $userAgent,
'scheme' => 'none',
Expand Down
8 changes: 7 additions & 1 deletion src/Authentication/OpenIDConnectAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Bolt\protocol\V5_3;
use Bolt\protocol\V5_4;
use Exception;
use Laudis\Neo4j\Common\Neo4jLogger;
use Laudis\Neo4j\Common\ResponseHelper;
use Laudis\Neo4j\Contracts\AuthenticateInterface;
use Psr\Http\Message\RequestInterface;
Expand All @@ -33,14 +34,16 @@ final class OpenIDConnectAuth implements AuthenticateInterface
* @psalm-external-mutation-free
*/
public function __construct(
private readonly string $token
private readonly string $token,
private readonly ?Neo4jLogger $logger
) {}

/**
* @psalm-mutation-free
*/
public function authenticateHttp(RequestInterface $request, UriInterface $uri, string $userAgent): RequestInterface
{
$this->logger?->log(LogLevel::DEBUG, 'Authenticating using OpenIDConnectAuth');

Check warning on line 46 in src/Authentication/OpenIDConnectAuth.php

View workflow job for this annotation

GitHub Actions / Lint & Analyse

MixedArgument

src/Authentication/OpenIDConnectAuth.php:46:29: MixedArgument: Argument 1 of Laudis\Neo4j\Common\Neo4jLogger::log cannot be mixed, expecting string (see https://psalm.dev/030)
/**
* @psalm-suppress ImpureMethodCall Request is a pure object:
*
Expand All @@ -58,8 +61,10 @@ public function authenticateHttp(RequestInterface $request, UriInterface $uri, s
public function authenticateBolt(V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol, string $userAgent): array
{
if (method_exists($protocol, 'logon')) {
$this->logger?->log(LogLevel::DEBUG, 'HELLO', ['user_agent' => $userAgent]);

Check warning on line 64 in src/Authentication/OpenIDConnectAuth.php

View workflow job for this annotation

GitHub Actions / Lint & Analyse

MixedArgument

src/Authentication/OpenIDConnectAuth.php:64:33: MixedArgument: Argument 1 of Laudis\Neo4j\Common\Neo4jLogger::log cannot be mixed, expecting string (see https://psalm.dev/030)
$protocol->hello(['user_agent' => $userAgent]);
$response = ResponseHelper::getResponse($protocol);
$this->logger?->log(LogLevel::DEBUG, 'LOGON', ['scheme' => 'bearer']);

Check warning on line 67 in src/Authentication/OpenIDConnectAuth.php

View workflow job for this annotation

GitHub Actions / Lint & Analyse

MixedArgument

src/Authentication/OpenIDConnectAuth.php:67:33: MixedArgument: Argument 1 of Laudis\Neo4j\Common\Neo4jLogger::log cannot be mixed, expecting string (see https://psalm.dev/030)
$protocol->logon([
'scheme' => 'bearer',
'credentials' => $this->token,
Expand All @@ -69,6 +74,7 @@ public function authenticateBolt(V4_4|V5|V5_1|V5_2|V5_3|V5_4 $protocol, string $
/** @var array{server: string, connection_id: string, hints: list} */
return $response->content;
} else {
$this->logger?->log(LogLevel::DEBUG, 'HELLO', ['user_agent' => $userAgent, 'scheme' => 'bearer']);

Check warning on line 77 in src/Authentication/OpenIDConnectAuth.php

View workflow job for this annotation

GitHub Actions / Lint & Analyse

MixedArgument

src/Authentication/OpenIDConnectAuth.php:77:33: MixedArgument: Argument 1 of Laudis\Neo4j\Common\Neo4jLogger::log cannot be mixed, expecting string (see https://psalm.dev/030)
$protocol->hello([
'user_agent' => $userAgent,
'scheme' => 'bearer',
Expand Down
Loading

0 comments on commit 6d3786b

Please sign in to comment.