From b05dbdc2cd396c2188bc55576b1f9731aba51ce9 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 14 Feb 2024 10:34:12 +0900 Subject: [PATCH] refactor: remove hard coded `'Y-m-d H:i:s'` See https://github.com/codeigniter4/shield/issues/608 and https://github.com/codeigniter4/CodeIgniter4/pull/8538 --- src/Authentication/Authenticators/AccessTokens.php | 2 +- src/Authentication/Authenticators/HmacSha256.php | 2 +- src/Authentication/Authenticators/Session.php | 6 +++--- src/Authorization/Traits/Authorizable.php | 2 +- src/Controllers/MagicLinkController.php | 2 +- src/Models/LoginModel.php | 6 +++--- src/Models/RememberModel.php | 9 ++++----- src/Models/TokenLoginModel.php | 2 +- src/Models/UserIdentityModel.php | 2 +- src/Models/UserModel.php | 2 +- tests/Authentication/MagicLinkTest.php | 4 ++-- tests/Controllers/MagicLinkTest.php | 2 +- 12 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/Authentication/Authenticators/AccessTokens.php b/src/Authentication/Authenticators/AccessTokens.php index 46ff20188..be9f6ba9d 100644 --- a/src/Authentication/Authenticators/AccessTokens.php +++ b/src/Authentication/Authenticators/AccessTokens.php @@ -170,7 +170,7 @@ public function check(array $credentials): Result ]); } - $token->last_used_at = Time::now()->format('Y-m-d H:i:s'); + $token->last_used_at = Time::now(); if ($token->hasChanged()) { $identityModel->save($token); diff --git a/src/Authentication/Authenticators/HmacSha256.php b/src/Authentication/Authenticators/HmacSha256.php index 2f37ba669..993ee69c2 100644 --- a/src/Authentication/Authenticators/HmacSha256.php +++ b/src/Authentication/Authenticators/HmacSha256.php @@ -186,7 +186,7 @@ public function check(array $credentials): Result ]); } - $token->last_used_at = Time::now()->format('Y-m-d H:i:s'); + $token->last_used_at = Time::now(); if ($token->hasChanged()) { $identityModel->save($token); diff --git a/src/Authentication/Authenticators/Session.php b/src/Authentication/Authenticators/Session.php index c480dae5c..fd116cbbc 100644 --- a/src/Authentication/Authenticators/Session.php +++ b/src/Authentication/Authenticators/Session.php @@ -922,17 +922,17 @@ protected function rememberUser(User $user): void $user, $selector, $this->hashValidator($validator), - $expires + $expires->format('Y-m-d H:i:s') ); $this->setRememberMeCookie($rawToken); } - private function calcExpires(): string + private function calcExpires(): Time { $timestamp = Time::now()->getTimestamp() + setting('Auth.sessionConfig')['rememberLength']; - return Time::createFromTimestamp($timestamp)->format('Y-m-d H:i:s'); + return Time::createFromTimestamp($timestamp); } private function setRememberMeCookie(string $rawToken): void diff --git a/src/Authorization/Traits/Authorizable.php b/src/Authorization/Traits/Authorizable.php index c96bcc763..e26e1a772 100644 --- a/src/Authorization/Traits/Authorizable.php +++ b/src/Authorization/Traits/Authorizable.php @@ -390,7 +390,7 @@ private function saveGroupsOrPermissions(string $type, $model, array $cache): vo $inserts[] = [ 'user_id' => $this->id, $type => $item, - 'created_at' => Time::now()->format('Y-m-d H:i:s'), + 'created_at' => Time::now(), ]; } diff --git a/src/Controllers/MagicLinkController.php b/src/Controllers/MagicLinkController.php index 4b0d48344..78fb74785 100644 --- a/src/Controllers/MagicLinkController.php +++ b/src/Controllers/MagicLinkController.php @@ -109,7 +109,7 @@ public function loginAction() 'user_id' => $user->id, 'type' => Session::ID_TYPE_MAGIC_LINK, 'secret' => $token, - 'expires' => Time::now()->addSeconds(setting('Auth.magicLinkLifetime'))->format('Y-m-d H:i:s'), + 'expires' => Time::now()->addSeconds(setting('Auth.magicLinkLifetime')), ]); /** @var IncomingRequest $request */ diff --git a/src/Models/LoginModel.php b/src/Models/LoginModel.php index 9c2ff0dca..22205e2fd 100644 --- a/src/Models/LoginModel.php +++ b/src/Models/LoginModel.php @@ -40,7 +40,7 @@ class LoginModel extends BaseModel 'identifier' => 'permit_empty|string', 'user_agent' => 'permit_empty|string', 'user_id' => 'permit_empty|integer', - 'date' => 'required|valid_date', + 'date' => 'required', ]; protected $validationMessages = []; protected $skipValidation = false; @@ -80,7 +80,7 @@ public function recordLoginAttempt( 'id_type' => $idType, 'identifier' => $identifier, 'user_id' => $userId, - 'date' => Time::now()->format('Y-m-d H:i:s'), + 'date' => Time::now(), 'success' => (int) $success, ]); @@ -121,7 +121,7 @@ public function fake(Generator &$faker): Login 'id_type' => Session::ID_TYPE_EMAIL_PASSWORD, 'identifier' => $faker->email(), 'user_id' => null, - 'date' => Time::parse('-1 day')->format('Y-m-d H:i:s'), + 'date' => Time::parse('-1 day'), 'success' => true, ]); } diff --git a/src/Models/RememberModel.php b/src/Models/RememberModel.php index 68b09bda1..c036e51e3 100644 --- a/src/Models/RememberModel.php +++ b/src/Models/RememberModel.php @@ -15,7 +15,6 @@ use CodeIgniter\I18n\Time; use CodeIgniter\Shield\Entities\User; -use DateTime; use Faker\Generator; use stdClass; @@ -45,22 +44,22 @@ public function fake(Generator &$faker): stdClass 'user_id' => 1, 'selector' => 'selector', 'hashedValidator' => 'validator', - 'expires' => Time::parse('+1 day')->format('Y-m-d H:i:s'), + 'expires' => Time::parse('+1 day'), ]; } /** * Stores a remember-me token for the user. + * + * @TODO `string $expires` → `Time $expires` */ public function rememberUser(User $user, string $selector, string $hashedValidator, string $expires): void { - $expires = new DateTime($expires); - $return = $this->insert([ 'user_id' => $user->id, 'selector' => $selector, 'hashedValidator' => $hashedValidator, - 'expires' => $expires->format('Y-m-d H:i:s'), + 'expires' => Time::parse($expires), ]); $this->checkQueryReturn($return); diff --git a/src/Models/TokenLoginModel.php b/src/Models/TokenLoginModel.php index 07c902d41..ef5328b49 100644 --- a/src/Models/TokenLoginModel.php +++ b/src/Models/TokenLoginModel.php @@ -35,7 +35,7 @@ public function fake(Generator &$faker): Login 'ip_address' => $faker->ipv4(), 'identifier' => 'token: ' . random_string('crypto', 64), 'user_id' => fake(UserModel::class)->id, - 'date' => Time::parse('-1 day')->format('Y-m-d H:i:s'), + 'date' => Time::parse('-1 day'), 'success' => true, ]); } diff --git a/src/Models/UserIdentityModel.php b/src/Models/UserIdentityModel.php index 643e1f952..2f437154b 100644 --- a/src/Models/UserIdentityModel.php +++ b/src/Models/UserIdentityModel.php @@ -442,7 +442,7 @@ public function getIdentitiesByTypes(User $user, array $types): array */ public function touchIdentity(UserIdentity $identity): void { - $identity->last_used_at = Time::now()->format('Y-m-d H:i:s'); + $identity->last_used_at = Time::now(); $return = $this->save($identity); diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index 44d4c5d4b..df991b1b5 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -376,7 +376,7 @@ public function updateActiveDate(User $user): void assert($user->last_active instanceof Time); // Safe date string for database - $last_active = $user->last_active->format('Y-m-d H:i:s'); + $last_active = $user->last_active; $this->builder() ->set('last_active', $last_active) diff --git a/tests/Authentication/MagicLinkTest.php b/tests/Authentication/MagicLinkTest.php index 38dbe6525..f8426604a 100644 --- a/tests/Authentication/MagicLinkTest.php +++ b/tests/Authentication/MagicLinkTest.php @@ -114,7 +114,7 @@ public function testMagicLinkVerifyExpired(): void 'user_id' => $user->id, 'type' => Session::ID_TYPE_MAGIC_LINK, 'secret' => 'abasdasdf', - 'expires' => Time::now()->subDays(5)->format('Y-m-d H:i:s'), + 'expires' => Time::now()->subDays(5), ]); $result = $this->get(route_to('verify-magic-link') . '?token=abasdasdf'); @@ -136,7 +136,7 @@ public function testMagicLinkVerifySuccess(): void 'user_id' => $user->id, 'type' => Session::ID_TYPE_MAGIC_LINK, 'secret' => 'abasdasdf', - 'expires' => Time::now()->addMinutes(60)->format('Y-m-d H:i:s'), + 'expires' => Time::now()->addMinutes(60), ]); $result = $this->get(route_to('verify-magic-link') . '?token=abasdasdf'); diff --git a/tests/Controllers/MagicLinkTest.php b/tests/Controllers/MagicLinkTest.php index 11f82db87..477c98248 100644 --- a/tests/Controllers/MagicLinkTest.php +++ b/tests/Controllers/MagicLinkTest.php @@ -103,7 +103,7 @@ public function testMagicLinkVerifyPendingRegistrationActivation(): void 'user_id' => $user->id, 'type' => Session::ID_TYPE_MAGIC_LINK, 'secret' => 'abasdasdf', - 'expires' => Time::now()->addMinutes(60)->format('Y-m-d H:i:s'), + 'expires' => Time::now()->addMinutes(60), ]); $result = $this->get(route_to('verify-magic-link') . '?token=abasdasdf');