diff --git a/app/Authentication/Authenticators/LegacySafeSession.php b/app/Authentication/Authenticators/LegacySafeSession.php new file mode 100644 index 0000000..cd1398c --- /dev/null +++ b/app/Authentication/Authenticators/LegacySafeSession.php @@ -0,0 +1,65 @@ + false, + 'reason' => lang('Auth.badAttempt'), + ]); + } + + $givenPassword = (string) $credentials['password']; + unset($credentials['password']); + + $user = $this->provider->findByCredentials($credentials); + + if ($user === null) { + return new Result([ + 'success' => false, + 'reason' => lang('Auth.badAttempt'), + ]); + } + + $hash = $user->password_hash ?? null; + if (!is_string($hash) || trim($hash) === '') { + return new Result([ + 'success' => false, + 'reason' => lang('Auth.invalidPassword'), + ]); + } + + /** @var Passwords $passwords */ + $passwords = service('passwords'); + + if (!$passwords->verify($givenPassword, $hash)) { + return new Result([ + 'success' => false, + 'reason' => lang('Auth.invalidPassword'), + ]); + } + + if ($passwords->needsRehash($hash)) { + $user->password_hash = $passwords->hash($givenPassword); + $this->provider->save($user); + } + + return new Result([ + 'success' => true, + 'extraInfo' => $user, + ]); + } +} diff --git a/app/Config/Auth.php b/app/Config/Auth.php index 04c8f4b..a3a57e2 100644 --- a/app/Config/Auth.php +++ b/app/Config/Auth.php @@ -13,13 +13,13 @@ declare(strict_types=1); namespace Config; +use App\Authentication\Authenticators\LegacySafeSession; use CodeIgniter\Shield\Config\Auth as ShieldAuth; use CodeIgniter\Shield\Authentication\Actions\ActionInterface; use CodeIgniter\Shield\Authentication\AuthenticatorInterface; use CodeIgniter\Shield\Authentication\Authenticators\AccessTokens; use CodeIgniter\Shield\Authentication\Authenticators\HmacSha256; use CodeIgniter\Shield\Authentication\Authenticators\JWT; -use CodeIgniter\Shield\Authentication\Authenticators\Session; use CodeIgniter\Shield\Authentication\Passwords\CompositionValidator; use CodeIgniter\Shield\Authentication\Passwords\DictionaryValidator; use CodeIgniter\Shield\Authentication\Passwords\NothingPersonalValidator; @@ -120,7 +120,7 @@ class Auth extends ShieldAuth */ public array $authenticators = [ 'tokens' => AccessTokens::class, - 'session' => Session::class, + 'session' => LegacySafeSession::class, 'hmac' => HmacSha256::class, // 'jwt' => JWT::class, ];