Skip to content

Commit

Permalink
Merge branch 'feature/authentication' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreGerault committed Jun 14, 2021
2 parents 624e166 + e6baf8c commit 82a78f7
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 6 deletions.
22 changes: 22 additions & 0 deletions src/AGerault/Authentication/Authenticator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace AGerault\Framework\Authentication;

use AGerault\Framework\Contracts\Authentication\AuthenticatableInterface;
use AGerault\Framework\Contracts\Authentication\AuthenticatorInterface;

class Authenticator implements AuthenticatorInterface
{

public function check(AuthenticatableInterface $authenticatable, array $credentials): bool
{
return true;
}

public function attempt(AuthenticatableInterface $authenticatable, array $credentials): ?AuthenticatableInterface
{
return $credentials['login'] === $authenticatable->login()
&& password_verify($credentials['password'], $authenticatable->password()) ? $authenticatable : null;
}

}
15 changes: 15 additions & 0 deletions src/AGerault/Contracts/Authentication/AuthenticatableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace AGerault\Framework\Contracts\Authentication;

interface AuthenticatableInterface
{
/**
* A unique identifier to fetch a user
*/
public function key(): int|string;

public function login(): string;

public function password(): string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace AGerault\Framework\Contracts\Authentication;

interface AuthenticatableProviderInterface
{
public function fetch(string|int $key): AuthenticatableInterface;
}
16 changes: 16 additions & 0 deletions src/AGerault/Contracts/Authentication/AuthenticatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace AGerault\Framework\Contracts\Authentication;

interface AuthenticatorInterface
{
/**
* Check whether the credentials match for a given user
*/
public function check(AuthenticatableInterface $authenticatable, array $credentials): bool;

/**
* Try to log in a given user based on credentials
*/
public function attempt(AuthenticatableInterface $authenticatable, array $credentials): ?AuthenticatableInterface;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace AGerault\Framework\Contracts\Authentication\Exceptions;

use Exception;

class AuthenticatableNotFoundException extends Exception
{
}
8 changes: 8 additions & 0 deletions src/AGerault/Contracts/Session/SessionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,13 @@

interface SessionInterface
{
public function has(string $key): bool;

public function get(string $key): mixed;

public function put(string $key, mixed $payload): void;

public function clear(): void;

public function forget(string $key): void;
}
1 change: 0 additions & 1 deletion src/AGerault/Core/HttpRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public function __construct(UrlMatcherInterface $matcher)
$this->matcher = $matcher;
}


public function handle(ServerRequestInterface $request): ResponseInterface
{
$route = $this->matcher->match(
Expand Down
9 changes: 4 additions & 5 deletions src/AGerault/Services/ServiceContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ class ServiceContainer implements ServiceContainerInterface
*/
public function get(string $id): mixed
{
// If we have no instances of this id, let's build one
if (array_key_exists($id, $this->factories)) {
return call_user_func($this->factories[$id]);
}

if (!$this->has($id)) {
$instance = $this->resolve($id);

Expand Down Expand Up @@ -217,6 +212,10 @@ private function resolve(string $id): object

$this->getDefinition($id);

if (array_key_exists($id, $this->factories)) {
return call_user_func($this->factories[$id]);
}

// If the constructor of the class is null, no dependencies are required
if ($reflectionClass->getConstructor() === null) {
return $reflectionClass->newInstance();
Expand Down
38 changes: 38 additions & 0 deletions src/AGerault/Session/Session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace AGerault\Framework\Session;

use AGerault\Framework\Contracts\Session\SessionInterface;

class Session implements SessionInterface
{
public function __construct()
{
session_start();
}

public function has(string $key): bool
{
return isset($_SESSION[$key]);
}

public function get(string $key): mixed
{
return $_SESSION[$key] ?? null;
}

public function put(string $key, mixed $payload): void
{
$_SESSION[$key] = $payload;
}

public function clear(): void
{
session_destroy();
}

public function forget(string $key): void
{
unset($_SESSION[$key]);
}
}

0 comments on commit 82a78f7

Please sign in to comment.