-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from leepeuker/login
Improve login
- Loading branch information
Showing
17 changed files
with
237 additions
and
112 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,31 @@ | ||
<?php declare(strict_types=1); | ||
|
||
use Phinx\Migration\AbstractMigration; | ||
|
||
final class AddUserAuthTokenTable extends AbstractMigration | ||
{ | ||
public function down() : void | ||
{ | ||
$this->execute( | ||
<<<SQL | ||
DROP TABLE `user_auth_token` | ||
SQL | ||
); | ||
} | ||
|
||
public function up() : void | ||
{ | ||
$this->execute( | ||
<<<SQL | ||
CREATE TABLE `user_auth_token` ( | ||
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, | ||
`token` VARCHAR(255) NOT NULL, | ||
`expiration_date` DATETIME NOT NULL, | ||
`created_at` DATETIME NOT NULL DEFAULT NOW(), | ||
PRIMARY KEY (`id`), | ||
UNIQUE (`token`) | ||
) COLLATE="utf8mb4_unicode_ci" ENGINE=InnoDB | ||
SQL | ||
); | ||
} | ||
} |
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
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,116 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Movary\Application\User\Service; | ||
|
||
use Movary\Application\User\Exception\InvalidPassword; | ||
use Movary\Application\User\Repository; | ||
use Movary\ValueObject\DateTime; | ||
|
||
class Authentication | ||
{ | ||
private const AUTHENTICATION_COOKIE_NAME = 'id'; | ||
|
||
private const MAX_EXPIRATION_AGE_IN_DAYS = 30; | ||
|
||
public function __construct(private readonly Repository $repository) | ||
{ | ||
} | ||
|
||
public function deleteToken(string $token) : void | ||
{ | ||
$this->repository->deleteAuthToken($token); | ||
} | ||
|
||
public function isUserAuthenticated() : bool | ||
{ | ||
$token = filter_input(INPUT_COOKIE, self::AUTHENTICATION_COOKIE_NAME); | ||
|
||
if (empty($token) === false && $this->isValidToken($token) === true) { | ||
return true; | ||
} | ||
|
||
if (empty($token) === false) { | ||
unset($_COOKIE[self::AUTHENTICATION_COOKIE_NAME]); | ||
setcookie(self::AUTHENTICATION_COOKIE_NAME, '', -1); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function login(string $password, bool $rememberMe) : void | ||
{ | ||
if ($this->isUserAuthenticated() === true) { | ||
return; | ||
} | ||
|
||
$user = $this->repository->fetchAdminUser(); | ||
|
||
if (password_verify($password, $user->getPasswordHash()) === false) { | ||
throw InvalidPassword::create(); | ||
} | ||
|
||
$authTokenExpirationDate = $this->createExpirationDate(); | ||
$cookieExpiration = 0; | ||
|
||
if ($rememberMe === true) { | ||
$authTokenExpirationDate = $this->createExpirationDate(self::MAX_EXPIRATION_AGE_IN_DAYS); | ||
$cookieExpiration = (int)$authTokenExpirationDate->format('U'); | ||
} | ||
|
||
$token = $this->generateToken(DateTime::createFromString((string)$authTokenExpirationDate)); | ||
|
||
setcookie(self::AUTHENTICATION_COOKIE_NAME, $token, $cookieExpiration); | ||
} | ||
|
||
public function logout() : void | ||
{ | ||
$token = filter_input(INPUT_COOKIE, 'id'); | ||
|
||
if ($token !== null) { | ||
$this->deleteToken($token); | ||
unset($_COOKIE[self::AUTHENTICATION_COOKIE_NAME]); | ||
setcookie(self::AUTHENTICATION_COOKIE_NAME, '', -1); | ||
} | ||
|
||
session_regenerate_id(); | ||
} | ||
|
||
private function createExpirationDate(int $days = 1) : DateTime | ||
{ | ||
$timestamp = strtotime('+' . $days . ' day'); | ||
|
||
if ($timestamp === false) { | ||
throw new \RuntimeException('Could not generate timestamp for auth token expiration date.'); | ||
} | ||
|
||
return DateTime::createFromString(date('Y-m-d H:i:s', $timestamp)); | ||
} | ||
|
||
private function generateToken(?DateTime $expirationDate = null) : string | ||
{ | ||
if ($expirationDate === null) { | ||
$expirationDate = $this->createExpirationDate(); | ||
} | ||
|
||
$token = bin2hex(random_bytes(16)); | ||
|
||
$this->repository->createAuthToken($token, $expirationDate); | ||
|
||
return $token; | ||
} | ||
|
||
private function isValidToken(string $token) : bool | ||
{ | ||
$tokenExpirationDate = $this->repository->findAuthTokenExpirationDate($token); | ||
|
||
if ($tokenExpirationDate === null || $tokenExpirationDate->isAfter(DateTime::create()) === false) { | ||
if ($tokenExpirationDate !== null) { | ||
$this->repository->deleteAuthToken($token); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
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
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
Oops, something went wrong.