Skip to content

Commit

Permalink
cuarentena changes
Browse files Browse the repository at this point in the history
  • Loading branch information
augusthur committed Mar 21, 2020
1 parent ffedf62 commit d7203d8
Show file tree
Hide file tree
Showing 10 changed files with 263 additions and 94 deletions.
22 changes: 22 additions & 0 deletions app/dependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

use Illuminate\Container\Container as IlluminateContainer;
use Illuminate\Database\Connection;
use Illuminate\Database\Connectors\ConnectionFactory;
use Illuminate\Database\ConnectionResolver;
use Illuminate\Database\Eloquent\Model as Eloquent;


return function (ContainerBuilder $containerBuilder) {
$containerBuilder->addDefinitions([
LoggerInterface::class => function (ContainerInterface $c) {
Expand All @@ -24,5 +31,20 @@

return $logger;
},
// Database connection
Connection::class => function (ContainerInterface $c) {
$settings = $c->get('settings');
$factory = new ConnectionFactory(new IlluminateContainer());
$connection = $factory->make(settings['db']);
$connection->disableQueryLog();
$resolver = new ConnectionResolver();
$resolver->addConnection('default', $connection);
$resolver->setDefaultConnection('default');
Eloquent::setConnectionResolver($resolver);
return $connection;
},
PDO::class => function (ContainerInterface $c) {
return $c->get(Connection::class)->getPdo();
},
]);
};
12 changes: 11 additions & 1 deletion app/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@
'settings' => [
'displayErrorDetails' => true, // Should be set to false in production
'logger' => [
'name' => 'slim-app',
'name' => 'app',
'path' => isset($_ENV['docker']) ? 'php://stdout' : __DIR__ . '/../logs/app.log',
'level' => Logger::DEBUG,
],
'db' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'test',
'username' => 'dev',
'password' => 'dev',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],
],
]);
};
19 changes: 10 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@
],
"homepage": "https://virtuagora.org",
"require": {
"php": ">=7.1",
"ext-json": "*",
"firebase/php-jwt": "^5.0",
"google/recaptcha": "^1.2",
"grimzy/laravel-mysql-spatial": "^2.1",
"guzzlehttp/guzzle": "^6.2",
"hansott/psr7-cookies": "^3.0",
"illuminate/database": "^5.8",
"illuminate/mail": "^5.8",
"monolog/monolog": "^1.24",
"php": ">=7.1",
"opis/json-schema": "^1.0",
"php-di/php-di": "^6.0",
"slim/psr7": "^0.5",
"slim/slim": "^4.1",
"illuminate/database": "^5.8",
"illuminate/mail": "^5.8",
"grimzy/laravel-mysql-spatial": "^2.1",
"firebase/php-jwt": "^5.0",
"guzzlehttp/guzzle": "^6.2",
"google/recaptcha": "^1.2",
"opis/json-schema": "^1.0"
"slim/slim": "^4.1"
},
"config": {
"process-timeout": 0,
Expand Down
64 changes: 61 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

119 changes: 119 additions & 0 deletions src/Auth/Session/JWTSessionHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace App\Auth\SessionManager;

use App\Auth\Requester;
use App\Auth\SubjectInterface as Subject;
use Psr\Http\Message\ServerRequestInterface as Request;
use Firebase\JWT\JWT;
use HansOtt\PSR7Cookies\SetCookie;
use Exception;

class JWTSessionHandler implements SessionHandler
{
private array $options;
private $publicKey;
private $privateKey;

public function __construct(array $options = [])
{

$this->options = array_merge([
'header' => 'Authorization',
'regexp' => "/Bearer\s+(.*)$/i",
'cookie' => 'token',
'algorithm' => 'RS512',
], $options);
if (in_array($options['algorithm'], ['RS256', 'RS384', 'RS512'])) {
$this->privateKey = openssl_get_privatekey($options['privateKey']);
$this->publicKey = openssl_get_pubickey($options['publicKey']);
} else {
$this->privateKey = $this->publicKey = $options['secret'];
}
}

public function authenticate(Request $request): Requester
{
$token = $this->fetchToken($request);
if (is_null($token)) {
return new Requester('Annonymous');
} else {
$claims = $this->decodeToken($token);
return new Requester(
$claims->type,
$claims->id,
$claims->name,
$claims->roles
);
}
}

public function signIn(Response $response, Actor $agent)
{
$claims = [
'id' => $subject->getId(),
'type' => $subject->getType(),
'name' => $subject->getDisplayName(),
'roles' => $subject->getRolesList(),
];
$token = $this->encodeToken($claims);
$cookie = new SetCookie('token', $token, time() + 3600, '', '', true, true);
return $cookie->addToResponse($response);
}

public function signOut(Request $request, Response $response)
{
return $response;
}

private function fetchToken(Request $request): string
{
/* Check for token in header. */
$header = $request->getHeaderLine($this->options["header"]);
if (false === empty($header)) {
if (preg_match($this->options["regexp"], $header, $matches)) {
return $matches[1];
}
}
/* Token not found in header try a cookie. */
$cookieParams = $request->getCookieParams();
if (isset($cookieParams[$this->options["cookie"]])) {
if (
preg_match(
$this->options["regexp"],
$cookieParams[$this->options["cookie"]],
$matches
)
) {
return $matches[1];
}
return $cookieParams[$this->options["cookie"]];
};
/* If everything fails return null. */
return null;
}

private function decodeToken(string $token): object
{
try {
$decoded = JWT::decode(
$token, $this->publicKey, [$this->options["algorithm"]]
);
return $decoded;
} catch (Exception $exception) {
throw $exception;
}
}

private function encodeToken(array $claims): string
{
try {
$encoded = JWT::encode(
$claims, $this->privateKey, $this->options['algorithm']
);
return $encoded;
} catch (Exception $exception) {
throw $exception;
}
}
}
74 changes: 0 additions & 74 deletions src/Auth/Session/JWTSessionManager.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Auth/Session/SessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface SessionHandler
{
public function authenticate(Request $request): Actor;

public function signIn(Request $request, Actor $subject): Response; // response??
public function signIn(Response $response, Actor $subject): Response;

public function signOut();
public function signOut(Request $request, Response $response): Response;
}
Loading

0 comments on commit d7203d8

Please sign in to comment.