-
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.
wip: ひとまず認証処理をベタ書きで実装(「ダミーのオンメモリストレージで単にログイン/ログアウトできる」というだけのセッション実装)
- Loading branch information
Showing
11 changed files
with
214 additions
and
9 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cw\LearnBear\AppSpi; | ||
|
||
interface IdentityRepositoryInterface | ||
{ | ||
public function findByUserNameAndPassword(string $username, string $password): ?string; | ||
} |
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,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cw\LearnBear\Infrastructure\Authentication; | ||
|
||
use Aura\Session\Segment; | ||
use Aura\Session\SessionFactory; | ||
use Aura\Web\WebFactory; | ||
use Cw\LearnBear\AppSpi\LoggerInterface; | ||
use Cw\LearnBear\AppSpi\SessionHandlerInterface; | ||
|
||
class CwSession implements SessionHandlerInterface | ||
{ | ||
private readonly Segment $segment; | ||
|
||
/** | ||
* @SuppressWarnings(PHPMD.Superglobals) | ||
*/ | ||
public function __construct(private readonly LoggerInterface $logger) | ||
{ | ||
$session = (new SessionFactory())->newInstance( | ||
(new WebFactory($GLOBALS))->newRequest()->cookies->getArrayCopy() | ||
); | ||
$this->segment = $session->getSegment(self::SESS_SEGMENT); | ||
} | ||
|
||
public function setAuth(string $uuid): void | ||
{ | ||
$this->segment->set('userIdentity', $uuid); | ||
} | ||
|
||
public function isNotAuthorized(): bool | ||
{ | ||
$this->logger->log('userIdentity: ' . $this->segment->get('userIdentity', 'no valid session.')); | ||
|
||
return empty($this->segment->get('userIdentity')); | ||
} | ||
|
||
public function clearAuth(): void | ||
{ | ||
$this->segment->clear(); | ||
} | ||
|
||
public function setFlashMessage(string $message, string $key): void | ||
{ | ||
$this->segment->setFlash($key, $message); | ||
} | ||
|
||
public function getFlashMessage(string $key): ?string | ||
{ | ||
$message = $this->segment->getFlash($key); | ||
$this->segment->clearFlash(); | ||
|
||
return $message; | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cw\LearnBear\Infrastructure\Authentication; | ||
|
||
use Cw\LearnBear\AppSpi\IdentityRepositoryInterface; | ||
|
||
use function password_hash; | ||
use function password_verify; | ||
|
||
use const PASSWORD_BCRYPT; | ||
|
||
class IdentityRepository implements IdentityRepositoryInterface | ||
{ | ||
/** @var array<string, mixed> */ | ||
private readonly array $dummyStorage; | ||
|
||
public function __construct() | ||
{ | ||
$this->dummyStorage = [ | ||
'hogetest' => [ | ||
'uuid' => 'ea210d8c-25b9-4f4a-b36a-a42634a9ab5c', | ||
'password' => password_hash('Fuga.1234', PASSWORD_BCRYPT), | ||
], | ||
'piyotest' => [ | ||
'uuid' => 'd31ebeeb-6825-4b0e-9dee-248b7ace9ffa', | ||
'password' => password_hash('Fuga.1234', PASSWORD_BCRYPT), | ||
], | ||
]; | ||
} | ||
|
||
public function findByUserNameAndPassword(string $username, string $password): ?string | ||
{ | ||
$identity = $this->dummyStorage[$username] ?? null; | ||
if ($identity === null) { | ||
return null; | ||
} | ||
|
||
return password_verify($password, $identity['password']) | ||
? $identity['uuid'] | ||
: 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
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cw\LearnBear\Module; | ||
|
||
use Cw\LearnBear\AppSpi\IdentityRepositoryInterface; | ||
use Cw\LearnBear\AppSpi\SessionHandlerInterface; | ||
use Cw\LearnBear\Infrastructure\Authentication\CwSession; | ||
use Cw\LearnBear\Infrastructure\Authentication\IdentityRepository; | ||
use Ray\Di\AbstractModule; | ||
|
||
class CwAuthModule extends AbstractModule | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function configure() | ||
{ | ||
$this->bind(SessionHandlerInterface::class)->to(CwSession::class); | ||
$this->bind(IdentityRepositoryInterface::class)->to(IdentityRepository::class); | ||
} | ||
} |
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