Skip to content

Commit

Permalink
[FEATURE] Add authentication and debug package
Browse files Browse the repository at this point in the history
  • Loading branch information
vertexvaar committed Oct 14, 2023
1 parent 9af3dd4 commit 55aa815
Show file tree
Hide file tree
Showing 23 changed files with 856 additions and 297 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"vertexvaar/bluesprints": "@dev",
"vertexvaar/bluefluid": "@dev",
"psr/container": "^2.0",
"vertexvaar/bluecontainer": "@dev"
"vertexvaar/bluecontainer": "@dev",
"vertexvaar/blueauth": "@dev",
"vertexvaar/bluedebug": "@dev"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 4 additions & 0 deletions config/system.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
'files' => 0660,
'folders' => 0770,
],
'cookie' => [
'authName' => 'bluedist_auth',
'domain' => 'local.bluedist.com',
],
];
23 changes: 23 additions & 0 deletions packages/blueauth/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "vertexvaar/blueauth",
"description": "Authentication component for bluesprints",
"type": "library",
"license": "GPL-3.0+",
"authors": [
{
"name": "Oliver Eglseder",
"email": "[email protected]",
"homepage": "http://vxvr.de/",
"role": "Developer"
}
],
"autoload": {
"psr-4": {
"VerteXVaaR\\BlueAuth\\": "src/"
}
},
"require": {
"php": "8.2.*",
"vertexvaar/bluesprints": "*"
}
}
12 changes: 12 additions & 0 deletions packages/blueauth/config/middlewares.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use VerteXVaaR\BlueAuth\Middleware\AuthenticationMiddleware;

return [
'vertexvaar/blueauth/authentication' => [
'service' => AuthenticationMiddleware::class,
'before' => ['vertexvaar/bluesprints/routing']
]
];
7 changes: 7 additions & 0 deletions packages/blueauth/config/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
_defaults:
autowire: true
autoconfigure: true

VerteXVaaR\BlueAuth\:
resource: '../src/*'
51 changes: 51 additions & 0 deletions packages/blueauth/src/Middleware/AuthenticationMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace VerteXVaaR\BlueAuth\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use VerteXVaaR\BlueSprints\Environment\Config;
use VerteXVaaR\BlueSprints\Environment\Paths;

use function CoStack\Lib\concat_paths;
use function file_exists;

readonly class AuthenticationMiddleware implements MiddlewareInterface
{
public function __construct(
private Config $config,
private Paths $paths
) {
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$authenticated = false;
$username = null;

$cookies = $request->getCookieParams();
$cookieAuthName = $this->config->cookieAuthName ?: 'bluedist_auth';
$authCookie = $cookies[$cookieAuthName] ?? null;

if (null !== $authCookie) {
$authPath = concat_paths($this->paths->database, 'auth');
$cookieFile = concat_paths($authPath, $authCookie);
if (file_exists($cookieFile)) {
$sessionValues = require $cookieFile;
if ($sessionValues['authenticated'] ?? false) {
$authenticated = true;
$username = $sessionValues['username'] ?? null;
}
}
}

$request = $request->withAttribute('authenticated', $authenticated);
$request = $request->withAttribute('username', $username);

return $handler->handle($request);
}
}
Loading

0 comments on commit 55aa815

Please sign in to comment.