-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
284 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,37 @@ | ||
{ | ||
"name": "battis/user-session", | ||
"description": "User session management for Slim Framework", | ||
"license": "MIT", | ||
"autoload": { | ||
"psr-4": { | ||
"Battis\\UserSession\\": "src" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Battis\\UserSession\\Tests\\": "tests" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Seth Battis", | ||
"email": "seth@battis.net" | ||
} | ||
], | ||
"license": "MIT", | ||
"require": { | ||
"bryanjhv/slim-session": "^4.0", | ||
"slim/http": "^1.2", | ||
"slim/php-view": "^3.0", | ||
"slim/psr7": "^1.0" | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
"autoload": { | ||
"psr-4": { | ||
"Battis\\UserSession\\": "src" | ||
} | ||
}, | ||
"require-dev": { | ||
"battis/data-utilities": "^1.1", | ||
"ext-simplexml": "*", | ||
"php-di/php-di": "^6.4", | ||
"phpspec/prophecy-phpunit": "^2.0", | ||
"phpunit/phpunit": "^9.0" | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Battis\\UserSession\\Tests\\": "tests" | ||
} | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Battis\UserSession\Tests\Actions; | ||
|
||
use Battis\UserSession\Manager; | ||
use Battis\UserSession\Repositories\UserRepositoryInterface; | ||
use Battis\UserSession\Tests\Fixtures\Reusable\User; | ||
use Battis\UserSession\Tests\Fixtures\Reusable\UserRepository; | ||
use Battis\UserSession\Tests\TestCase; | ||
|
||
class HandleLoginTest extends TestCase | ||
{ | ||
public function testHandleLogin() | ||
{ | ||
$app = $this->getAppInstance(); | ||
|
||
$user = new User(); | ||
|
||
/** @var UserRepository $userRepo */ | ||
$userRepo = $app->getContainer()->get(UserRepositoryInterface::class); | ||
$userRepo->addUser($user); | ||
|
||
/** #var Manager $manager */ | ||
$manager = $app->getContainer()->get(Manager::class); | ||
|
||
$response = $app->handle($this->createRequest('POST', Manager::DEFAULT_LOGIN_PATH)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Battis\UserSession\Tests\Actions; | ||
|
||
use Battis\UserSession\Manager; | ||
use Battis\UserSession\Tests\Fixtures\Reusable\User; | ||
use Battis\UserSession\Tests\TestCase; | ||
use SimpleXMLElement; | ||
|
||
class ShowLoginViewTest extends TestCase | ||
{ | ||
public function testLoginView() | ||
{ | ||
$app = $this->getAppInstance(); | ||
$response = $app->handle($this->createRequest('GET', Manager::DEFAULT_LOGIN_PATH)); | ||
$payload = (string) $response->getBody(); | ||
$xml = new SimpleXMLElement($payload); | ||
$this->assertNotEmpty($payload); | ||
$this->assertCount(0, $xml->{'non-document-error'}); | ||
$this->assertCount(0, $xml->error); | ||
$this->assertStringContainsString('<form class="form login" method="post" action="/auth/login">', $payload); | ||
} | ||
|
||
public function testLoginViewWithActiveuser() | ||
{ | ||
$app = $this->getAppInstance(); | ||
|
||
/** @var Manager */ | ||
$manager = $app->getContainer()->get(Manager::class); | ||
$manager->startUserSession(new User()); | ||
|
||
$response = $app->handle($this->createRequest('GET', Manager::DEFAULT_LOGIN_PATH)); | ||
$this->assertLocationHeader(Manager::DEFAULT_REDIRECT, $response); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Battis\UserSession\Tests\Fixtures\Reusable; | ||
|
||
use Battis\UserSession\Manager; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Slim\Views\PhpRenderer; | ||
|
||
/** | ||
* Render a web page using a template with the same name as the | ||
* endpoint. Current user and query and URL params are passed as | ||
* template variables. | ||
*/ | ||
class PageRenderer | ||
{ | ||
private $renderer; | ||
private $manager; | ||
|
||
public function __construct(PhpRenderer $renderer, Manager $manager) | ||
{ | ||
$this->renderer = $renderer; | ||
$this->manager = $manager; | ||
} | ||
|
||
public function __invoke( | ||
ServerRequestInterface $request, | ||
ResponseInterface $response, | ||
array $args = [] | ||
) { | ||
return $this->renderer->render( | ||
$response, | ||
basename($request->getUri()->getPath()) . ".php", | ||
array_merge($args, $request->getQueryParams(), [ | ||
"user" => $this->manager->getCurrentUser(), | ||
]) | ||
); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
tests/Fixtures/ManagerTest/Session.php → tests/Fixtures/Reusable/Session.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Battis\UserSession\Tests\Fixtures\Reusable; | ||
|
||
use Battis\UserSession\Entities\UserEntityInterface; | ||
|
||
class User implements UserEntityInterface | ||
{ | ||
public $id; | ||
public string $username; | ||
public string $password; | ||
public string $hash; | ||
|
||
public function __construct($id = null, string $password = null) | ||
{ | ||
$this->id = $id ?? random_int(1, 1000); | ||
$this->username = md5(time() . 'username'); | ||
$this->password = $password ?? md5(time() . 'password'); | ||
$this->hash = password_hash($this->password, PASSWORD_DEFAULT); | ||
} | ||
|
||
public function getIdentifier() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function passwordVerify(string $password): bool | ||
{ | ||
return password_verify($password, $this->hash); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Battis\UserSession\Tests\Fixtures\Reusable; | ||
|
||
use Battis\UserSession\Entities\UserEntityInterface; | ||
use Battis\UserSession\Repositories\UserRepositoryInterface; | ||
|
||
class UserRepository implements UserRepositoryInterface | ||
{ | ||
private array $users = []; | ||
|
||
public function addUser(User $user) | ||
{ | ||
$this->users[$user->username] = $user; | ||
} | ||
|
||
public function getUserEntityByUsername(string $username): ?UserEntityInterface | ||
{ | ||
return $this->users[$username] ?? null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
use Battis\UserSession\Repositories\UserRepositoryInterface; | ||
use Battis\UserSession\Tests\Fixtures\Reusable\UserRepository; | ||
|
||
return [ | ||
UserRepositoryInterface::class => fn() => new UserRepository(), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
use DI\Container; | ||
use Slim\App; | ||
|
||
/** @var App $app */ | ||
/** @var Container $container */ | ||
|
||
// prepare the $container with any middleware you might plan on using | ||
|
||
// use the Slim body-parsing middleware for convenience | ||
$app->addBodyParsingMiddleware(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
use Battis\UserSession; | ||
use Battis\UserSession\Tests\Fixtures\Reusable\PageRenderer; | ||
use DI\Container; | ||
use Slim\App; | ||
|
||
/** @var App $app */ | ||
/** @var Container $container */ | ||
|
||
// assign the UserSession endpoint(s) to the UserSession\Controller | ||
$app->group(UserSession\Controller::ENDPOINT, UserSession\Controller::class); | ||
|
||
$app | ||
->get("/home", PageRenderer::class) | ||
->add(UserSession\Middleware\Session::class); | ||
$app->redirect("/", "/home"); | ||
$app | ||
->get("/protected", PageRenderer::class) | ||
->add(UserSession\Middleware\RequireAuthentication::class); |
Oops, something went wrong.