-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Add AuthenticationController and acceptance test for fixed …
…admin login
- Loading branch information
1 parent
55aa815
commit a8a8eee
Showing
9 changed files
with
189 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
packages/blueauth/src/Controller/AuthenticationController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VerteXVaaR\BlueAuth\Controller; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
use RuntimeException; | ||
use VerteXVaaR\BlueSprints\Environment\Config; | ||
use VerteXVaaR\BlueSprints\Environment\Paths; | ||
use VerteXVaaR\BlueSprints\Mvc\AbstractController; | ||
use VerteXVaaR\BlueSprints\Mvc\Repository; | ||
use VerteXVaaR\BlueSprints\Mvc\TemplateRenderer; | ||
use VerteXVaaR\BlueSprints\Utility\Strings; | ||
|
||
use function array_key_exists; | ||
use function CoStack\Lib\concat_paths; | ||
use function file_exists; | ||
use function file_put_contents; | ||
use function getenv; | ||
use function json_encode; | ||
use function mkdir; | ||
use function setcookie; | ||
use function unlink; | ||
|
||
class AuthenticationController extends AbstractController | ||
{ | ||
public function __construct( | ||
Repository $repository, | ||
TemplateRenderer $templateRenderer, | ||
private readonly Paths $paths, | ||
private readonly Config $config, | ||
) { | ||
parent::__construct($repository, $templateRenderer); | ||
} | ||
|
||
public function login(ServerRequestInterface $request): void | ||
{ | ||
} | ||
|
||
public function logout(ServerRequestInterface $request): void | ||
{ | ||
$sessionIdentifier = $request->getAttribute('session'); | ||
if ($sessionIdentifier) { | ||
$sessionFile = concat_paths(getenv('VXVR_BS_ROOT'), $this->paths->database, 'auth', $sessionIdentifier); | ||
if (file_exists($sessionFile)) { | ||
unlink($sessionFile); | ||
} | ||
setcookie($this->config->cookieAuthName ?: 'bluesprints_auth', '', -1, '/'); | ||
} | ||
$this->redirect('/'); | ||
} | ||
|
||
public function authenticate(ServerRequestInterface $request): void | ||
{ | ||
$this->renderTemplate = false; | ||
$body = $request->getParsedBody(); | ||
if (array_key_exists('username', $body) && array_key_exists('password', $body)) { | ||
$username = $body['username']; | ||
$password = $body['password']; | ||
if ($username === 'admin' && $password === 'password') { | ||
$path = concat_paths(getenv('VXVR_BS_ROOT'), $this->paths->database, 'auth'); | ||
$sessionIdentifier = Strings::generateUuid(); | ||
$session = [ | ||
'id' => $sessionIdentifier, | ||
'username' => $username, | ||
'authenticated' => true, | ||
]; | ||
if (!mkdir($path, $this->config->folderPermissions, true) && !is_dir($path)) { | ||
throw new RuntimeException(sprintf('Directory "%s" was not created', $path)); | ||
} | ||
file_put_contents(concat_paths($path, $sessionIdentifier), json_encode($session)); | ||
setcookie($this->config->cookieAuthName ?: 'bluesprints_auth', $sessionIdentifier); | ||
$this->redirect('/'); | ||
} | ||
} | ||
$this->redirect('/login'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace acceptance; | ||
|
||
use VerteXVaaR\BlueDistTest\AcceptanceTester; | ||
|
||
use function ini_get; | ||
|
||
class LoginCest | ||
{ | ||
|
||
public function _before(AcceptanceTester $I) | ||
{ | ||
} | ||
|
||
// tests | ||
public function loginAndLogout(AcceptanceTester $I) | ||
{ | ||
$I->amOnPage('/'); | ||
|
||
if (ini_get('xdebug.mode') === 'debug') { | ||
$I->setCookie('XDEBUG_SESSION', 'XDEBUG_ECLIPSE'); | ||
} | ||
|
||
$I->see('User (anonymous session)'); | ||
|
||
$I->amOnPage('/login'); | ||
$I->submitForm('#login', [ | ||
'username' => 'admin', | ||
'password' => 'password' | ||
]); | ||
// Requires bluesprints debug package | ||
$I->see('User (authenticated): admin'); | ||
|
||
$I->amOnPage('/logout'); | ||
|
||
// Requires bluesprints debug package | ||
$I->see('User (anonymous session)'); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
view/Layout/VerteXVaaR/BlueAuth/Controller/AuthenticationController/Html.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!doctype html> | ||
|
||
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>{pageTitle}</title> | ||
</head> | ||
|
||
<body> | ||
<f:render section="Content"/> | ||
</body> | ||
</html> |
File renamed without changes.
28 changes: 28 additions & 0 deletions
28
...rteXVaaR/BlueAuth/Controller/AuthenticationController/AuthenticationController/Login.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" data-namespace-typo3-fluid="true"> | ||
<f:layout name="Html"/> | ||
|
||
<f:section name="Content"> | ||
<section> | ||
<h1>Login</h1> | ||
<form method="post" action="/login" id="login"> | ||
<table> | ||
<tr> | ||
<td> | ||
<label>Username | ||
<input type="text" name="username"/> | ||
</label> | ||
</td> | ||
<td> | ||
<label>Password | ||
<input type="password" name="password"/> | ||
</label> | ||
</td> | ||
<td> | ||
<button type="submit">Login</button> | ||
</td> | ||
</tr> | ||
</table> | ||
</form> | ||
</section> | ||
</f:section> | ||
</html> |