Skip to content

Instantly share code, notes, and snippets.

@l0gicgate
Last active March 14, 2019 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save l0gicgate/6854103a91f507186a46434289c948cb to your computer and use it in GitHub Desktop.
Save l0gicgate/6854103a91f507186a46434289c948cb to your computer and use it in GitHub Desktop.
<?php
namespace Project\Services\Authentication;
// This file should reside in your project root's `src\Services` sub-directory
use Illuminate\Hashing\BcryptHasher;
use Psr\Log\LoggerInterface;
use Project\Repositories\UserRepository;
class AuthenticationService {
private $hasher;
private $logger;
private $userRepository;
public function __construct(BcryptHasher $hasher, LoggerInterface $logger, UserRepository $userRepository)
{
$this->hasher = $hasher;
$this->logger = $logger;
$this->userRepository = $userRepository;
}
/**
* @throws AuthenticationServiceException
*/
public function authenticate(string $username, string $password, string $ipAddress = null): void
{
... do your authentication logic ...
if ($a === true) {
throw new AuthenticationServiceException('Message 1');
} else if ($b === false) {
throw new AuthenticationServiceException('Message 2');
}
... do your logging logic ...
}
}
<?php
namespace Project\Services\Authentication;
use Exception;
class AuthenticationServiceException extends Exception
{
}
<?php
// This file should reside in your project root's `app` sub-directory
return [
Database::class => DI\factory(function () {
$dsn = getenv('DB_TYPE') . ':host=' . getenv('DB_HOST') . ';dbname=' . getenv('DB_NAME');
$db_user = getenv('DB_USERNAME');
$db_password = getenv('DB_PASSWORD');
return new Database($dsn, $db_user, $db_password);
}),
LoggerInterface::class => DI\factory(function () {
$loggerApp = new Logger('APP');
$loggerApp->pushHandler(new RotatingFileHandler(__DIR__ . '/../tmp/logs/app.log', Logger::DEBUG));
return $loggerApp;
}),
];
<?php
use Project\MyApp;
use Project\Actions\RequestAuthenticationAction;
require __DIR__ . '/../vendor/autoload.php';
$app = new MyApp();
// Middleware
$app->add(new RKA\Middleware\IpAddress(true, ['10.0.0.1', '10.0.0.2']));
// Routes
$app->post('/authentication', RequestAuthenticationAction::class);
$app->run();
<?php
namespace Project;
// This file should reside in `src` sub-directory of your project
use DI\Bridge\Slim\App;
class MyApp extends App {
protected function configureContainer(ContainerBuilder $builder)
{
$builder->addDefinitions(__DIR__ . '/../app/config.php');
}
}
<?php
namespace Project\Actions;
// This file should reside in your project root's `src\Actions` sub-directory
use Project\Services\Authentication\AuthenticationService;
use Project\Services\Authentication\AuthenticationServiceException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Rakit\Validation\Validator;
class RequestAuthenticationAction {
private $authenticationService;
private $validator;
public function __construct(AuthenticationService $authenticationService, Validator $validator)
{
$this->authenticationService = $authenticationService;
$this->validator = $validator;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $args)
{
$username = $request->getParsedBodyParam('username', null);
$password = $request->getParsedBodyparam('password', null);
$ipAddress = $request->getAttribute('ip_address');
... do your validation logic with $this->validator ...
try {
$this->authenticationService->authenticate($username, $password, $ipAddress)
} catch (AuthenticationServiceException $e) {
$response->withJson(['error' => $e->getMessage()], 400);
}
return $response->withJson(['success' => true], 200);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment