Skip to content

Commit 5017c5f

Browse files
brendtTreggats
andauthored
feat: authenticator (#493)
Co-authored-by: Tonko Mulder <[email protected]>
1 parent e4bb059 commit 5017c5f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1417
-31
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ vendor/
66
src/Tempest/database.sqlite
77
tests/Fixtures/database.sqlite
88
tests/Unit/Console/test-console.log
9+
src/Tempest/Database/src/database.sqlite
910
.env
1011
composer.lock
1112
debug.log
1213
tempest.log
1314
public/static
14-
tests/Unit/Log
15+
tests/Unit/Log

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"symplify/monorepo-builder": "^11.2"
5151
},
5252
"replace": {
53+
"tempest/auth": "self.version",
5354
"tempest/cache": "self.version",
5455
"tempest/clock": "self.version",
5556
"tempest/command-bus": "self.version",
@@ -71,6 +72,7 @@
7172
"minimum-stability": "dev",
7273
"autoload": {
7374
"psr-4": {
75+
"Tempest\\Auth\\": "src/Tempest/Auth/src/",
7476
"Tempest\\Cache\\": "src/Tempest/Cache/src/",
7577
"Tempest\\Clock\\": "src/Tempest/Clock/src/",
7678
"Tempest\\CommandBus\\": "src/Tempest/CommandBus/src",
@@ -105,6 +107,7 @@
105107
},
106108
"autoload-dev": {
107109
"psr-4": {
110+
"Tempest\\Auth\\Tests\\": "src/Tempest/Auth/tests",
108111
"Tempest\\Cache\\Tests\\": "src/Tempest/Cache/tests",
109112
"Tempest\\Clock\\Tests\\": "src/Tempest/Clock/tests",
110113
"Tempest\\CommandBus\\Tests\\": "src/Tempest/CommandBus/tests",

src/Tempest/Auth/.gitattributes

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Exclude build/test files from the release
2+
.github/ export-ignore
3+
tests/ export-ignore
4+
.gitattributes export-ignore
5+
.gitignore export-ignore
6+
phpunit.xml export-ignore
7+
README.md export-ignore
8+
9+
# Configure diff output for .php and .phar files.
10+
*.php diff=php

src/Tempest/Auth/LICENCE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2024 Brent Roose [email protected]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

src/Tempest/Auth/composer.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "tempest/auth",
3+
"description": "A flexible authentication package for Tempest, providing user authentication and authorization.",
4+
"require": {
5+
"php": "^8.3",
6+
"tempest/core": "dev-main",
7+
"tempest/http": "dev-main",
8+
"tempest/database": "dev-main"
9+
},
10+
"autoload": {
11+
"psr-4": {
12+
"Tempest\\Auth\\": "src"
13+
}
14+
},
15+
"license": "MIT",
16+
"minimum-stability": "dev"
17+
}

src/Tempest/Auth/phpunit.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.4/phpunit.xsd" bootstrap="vendor/autoload.php" executionOrder="depends,defects" beStrictAboutOutputDuringTests="true" displayDetailsOnPhpunitDeprecations="true" failOnPhpunitDeprecation="false" failOnRisky="true" failOnWarning="true">
3+
<testsuites>
4+
<testsuite name="Tempest Auth">
5+
<directory>tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
<source restrictNotices="true" restrictWarnings="true" ignoreIndirectDeprecations="true">
9+
<include>
10+
<directory>src</directory>
11+
</include>
12+
</source>
13+
</phpunit>

src/Tempest/Auth/src/Allow.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Auth;
6+
7+
use Attribute;
8+
use UnitEnum;
9+
10+
#[Attribute(Attribute::TARGET_METHOD)]
11+
final readonly class Allow
12+
{
13+
public function __construct(
14+
/** @var string|UnitEnum|class-string<\Tempest\Auth\Authorizer> $permission */
15+
public string|UnitEnum $permission,
16+
) {
17+
}
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Auth;
6+
7+
use Tempest\Core\KernelEvent;
8+
use Tempest\EventBus\EventHandler;
9+
use Tempest\Http\Router;
10+
11+
final readonly class AuthBootstrap
12+
{
13+
public function __construct(
14+
private Router $router
15+
) {
16+
}
17+
18+
#[EventHandler(KernelEvent::BOOTED)]
19+
public function __invoke(): void
20+
{
21+
$this->router->addMiddleware(AuthorizerMiddleware::class);
22+
}
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Auth;
6+
7+
final class AuthConfig
8+
{
9+
public function __construct(
10+
/** @var class-string<\Tempest\Auth\Authenticator> */
11+
public string $authenticatorClass = SessionAuthenticator::class,
12+
13+
/** @var class-string<\Tempest\Database\DatabaseModel> */
14+
public string $userModelClass = User::class,
15+
) {
16+
}
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Auth;
6+
7+
interface Authenticator
8+
{
9+
public function login(CanAuthenticate $user): void;
10+
11+
public function logout(): void;
12+
13+
public function currentUser(): ?CanAuthenticate;
14+
}

0 commit comments

Comments
 (0)