Skip to content

Commit

Permalink
Add better method names for Exceptions\AuthenticationFailure
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed May 9, 2024
1 parent bc00b69 commit 6a425a2
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 30 deletions.
10 changes: 5 additions & 5 deletions src/Exceptions/AuthenticationFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,31 @@ public function __construct(
/**
* Database server denied user login
*/
public static function serverDenied(): self
public static function deniedByDatabaseServer(): self
{
return new self(self::SERVER_DENIED, __('Cannot log in to the database server.'));
}

/**
* User denied by allow/deny rules
*/
public static function allowDenied(): self
public static function deniedByAllowDenyRules(): self
{
return new self(self::ALLOW_DENIED, __('Access denied!'));
}

/**
* User 'root' is denied in configuration
*/
public static function rootDenied(): self
public static function rootDeniedByConfiguration(): self
{
return new self(self::ROOT_DENIED, __('Access denied!'));
}

/**
* Empty password is denied
*/
public static function emptyDenied(): self
public static function emptyPasswordDeniedByConfiguration(): self
{
return new self(
self::EMPTY_DENIED,
Expand All @@ -65,7 +65,7 @@ public static function emptyDenied(): self
/**
* Automatically logged out due to inactivity
*/
public static function noActivity(): self
public static function loggedOutDueToInactivity(): self
{
return new self(
self::NO_ACTIVITY,
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Middleware/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private function connectToDatabaseServer(DatabaseInterface $dbi, Server $current
// Connects to the server (validates user's login)
$userConnection = $dbi->connect($currentServer, ConnectionType::User);
if ($userConnection === null) {
throw AuthenticationFailure::serverDenied();
throw AuthenticationFailure::deniedByDatabaseServer();
}

if ($controlConnection !== null) {
Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/Auth/AuthenticationCookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ public function readCredentials(): bool
SessionCache::remove('table_priv');
SessionCache::remove('proc_priv');

throw AuthenticationFailure::noActivity();
throw AuthenticationFailure::loggedOutDueToInactivity();
}

// check password cookie
Expand Down
6 changes: 3 additions & 3 deletions src/Plugins/AuthenticationPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,21 +273,21 @@ public function checkRules(): void

// Ejects the user if banished
if ($allowDenyForbidden) {
throw AuthenticationFailure::allowDenied();
throw AuthenticationFailure::deniedByAllowDenyRules();
}
}

// is root allowed?
if (! $config->selectedServer['AllowRoot'] && $config->selectedServer['user'] === 'root') {
throw AuthenticationFailure::rootDenied();
throw AuthenticationFailure::rootDeniedByConfiguration();
}

// is a login without password allowed?
if ($config->selectedServer['AllowNoPassword'] || $config->selectedServer['password'] !== '') {
return;
}

throw AuthenticationFailure::emptyDenied();
throw AuthenticationFailure::emptyPasswordDeniedByConfiguration();
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/Exceptions/AuthenticationFailureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ final class AuthenticationFailureTest extends TestCase
{
public function testAllowDenied(): void
{
$exception = AuthenticationFailure::allowDenied();
$exception = AuthenticationFailure::deniedByAllowDenyRules();
self::assertSame('allow-denied', $exception->failureType);
self::assertSame('Access denied!', $exception->getMessage());
}

public function testEmptyDenied(): void
{
$exception = AuthenticationFailure::emptyDenied();
$exception = AuthenticationFailure::emptyPasswordDeniedByConfiguration();
self::assertSame('empty-denied', $exception->failureType);
self::assertSame(
'Login without a password is forbidden by configuration (see AllowNoPassword).',
Expand All @@ -30,7 +30,7 @@ public function testEmptyDenied(): void

public function testNoActivity(): void
{
$exception = AuthenticationFailure::noActivity();
$exception = AuthenticationFailure::loggedOutDueToInactivity();
self::assertSame('no-activity', $exception->failureType);
self::assertSame(
'You have been automatically logged out due to inactivity of %s seconds.'
Expand All @@ -41,14 +41,14 @@ public function testNoActivity(): void

public function testRootDenied(): void
{
$exception = AuthenticationFailure::rootDenied();
$exception = AuthenticationFailure::rootDeniedByConfiguration();
self::assertSame('root-denied', $exception->failureType);
self::assertSame('Access denied!', $exception->getMessage());
}

public function testServerDenied(): void
{
$exception = AuthenticationFailure::serverDenied();
$exception = AuthenticationFailure::deniedByDatabaseServer();
self::assertSame('server-denied', $exception->failureType);
self::assertSame('Cannot log in to the database server.', $exception->getMessage());
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Plugins/Auth/AuthenticationConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function testAuthFails(): void

(new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, null);

$response = $this->object->showFailure(AuthenticationFailure::serverDenied());
$response = $this->object->showFailure(AuthenticationFailure::deniedByDatabaseServer());

$html = (string) $response->getBody();

Expand Down
12 changes: 6 additions & 6 deletions tests/unit/Plugins/Auth/AuthenticationCookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ public function testAuthCheckAuthFails(): void
->method('cookieDecrypt')
->willReturn('testBF');

$this->expectExceptionObject(AuthenticationFailure::noActivity());
$this->expectExceptionObject(AuthenticationFailure::loggedOutDueToInactivity());
$this->object->readCredentials();
}

Expand Down Expand Up @@ -628,7 +628,7 @@ public function testAuthFailsNoPass(): void
(new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, $responseStub);

try {
$this->object->showFailure(AuthenticationFailure::emptyDenied());
$this->object->showFailure(AuthenticationFailure::emptyPasswordDeniedByConfiguration());
} catch (Throwable $throwable) {
}

Expand Down Expand Up @@ -696,7 +696,7 @@ public function testAuthFailsDeny(): void
(new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, $responseStub);

try {
$this->object->showFailure(AuthenticationFailure::allowDenied());
$this->object->showFailure(AuthenticationFailure::deniedByAllowDenyRules());
} catch (Throwable $throwable) {
}

Expand Down Expand Up @@ -728,7 +728,7 @@ public function testAuthFailsActivity(): void
(new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, $responseStub);

try {
$this->object->showFailure(AuthenticationFailure::noActivity());
$this->object->showFailure(AuthenticationFailure::loggedOutDueToInactivity());
} catch (Throwable $throwable) {
}

Expand Down Expand Up @@ -773,7 +773,7 @@ public function testAuthFailsDBI(): void
(new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, $responseStub);

try {
$this->object->showFailure(AuthenticationFailure::serverDenied());
$this->object->showFailure(AuthenticationFailure::deniedByDatabaseServer());
} catch (Throwable $throwable) {
}

Expand Down Expand Up @@ -814,7 +814,7 @@ public function testAuthFailsErrno(): void
(new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, $responseStub);

try {
$this->object->showFailure(AuthenticationFailure::serverDenied());
$this->object->showFailure(AuthenticationFailure::deniedByDatabaseServer());
} catch (Throwable $throwable) {
}

Expand Down
6 changes: 3 additions & 3 deletions tests/unit/Plugins/Auth/AuthenticationHttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public function testAuthFails(): void
(new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, null);
ResponseRenderer::getInstance()->setAjax(false);

$response = $this->object->showFailure(AuthenticationFailure::serverDenied());
$response = $this->object->showFailure(AuthenticationFailure::deniedByDatabaseServer());

$result = (string) $response->getBody();
self::assertStringContainsString('<p>error 123</p>', $result);
Expand All @@ -284,7 +284,7 @@ public function testAuthFails(): void
(new ReflectionProperty(ResponseRenderer::class, 'instance'))->setValue(null, null);
ResponseRenderer::getInstance()->setAjax(false);

$response = $this->object->showFailure(AuthenticationFailure::serverDenied());
$response = $this->object->showFailure(AuthenticationFailure::deniedByDatabaseServer());
$result = (string) $response->getBody();
self::assertStringContainsString('Wrong username/password. Access denied.', $result);

Expand All @@ -293,7 +293,7 @@ public function testAuthFails(): void

// case 3
$GLOBALS['errno'] = 1043;
$response = $this->object->showFailure(AuthenticationFailure::serverDenied());
$response = $this->object->showFailure(AuthenticationFailure::deniedByDatabaseServer());
$result = (string) $response->getBody();
self::assertStringContainsString('Wrong username/password. Access denied.', $result);
}
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/Plugins/Auth/AuthenticationSignonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public function testAuthFailsForbidden(): void
->willThrowException(new ExitException());

try {
$this->object->showFailure(AuthenticationFailure::emptyDenied());
$this->object->showFailure(AuthenticationFailure::emptyPasswordDeniedByConfiguration());
} catch (ExitException) {
}

Expand All @@ -271,7 +271,7 @@ public function testAuthFailsDeny(): void
->willThrowException(new ExitException());

try {
$this->object->showFailure(AuthenticationFailure::allowDenied());
$this->object->showFailure(AuthenticationFailure::deniedByAllowDenyRules());
} catch (ExitException) {
}

Expand All @@ -296,7 +296,7 @@ public function testAuthFailsTimeout(): void
$config->settings['LoginCookieValidity'] = '1440';

try {
$this->object->showFailure(AuthenticationFailure::noActivity());
$this->object->showFailure(AuthenticationFailure::loggedOutDueToInactivity());
} catch (ExitException) {
}

Expand Down Expand Up @@ -333,7 +333,7 @@ public function testAuthFailsMySQLError(): void
DatabaseInterface::$instance = $dbi;

try {
$this->object->showFailure(AuthenticationFailure::serverDenied());
$this->object->showFailure(AuthenticationFailure::deniedByDatabaseServer());
} catch (ExitException) {
}

Expand Down Expand Up @@ -366,7 +366,7 @@ public function testAuthFailsConnect(): void
DatabaseInterface::$instance = $dbi;

try {
$this->object->showFailure(AuthenticationFailure::serverDenied());
$this->object->showFailure(AuthenticationFailure::deniedByDatabaseServer());
} catch (ExitException) {
}

Expand Down

0 comments on commit 6a425a2

Please sign in to comment.