Skip to content

Commit

Permalink
Revert the changes, instead creating a new Laravel 12 compatible vers…
Browse files Browse the repository at this point in the history
…ion.
  • Loading branch information
jasonvarga committed Feb 24, 2025
1 parent c203d2b commit ee5e722
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 9 deletions.
98 changes: 98 additions & 0 deletions src/Auth/Passwords/LaravelTwelveTokenRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Statamic\Auth\Passwords;

use Illuminate\Auth\Passwords\DatabaseTokenRepository;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Carbon;
use Statamic\Facades\YAML;

/** @deprecated */
class LaravelTwelveTokenRepository extends DatabaseTokenRepository
{
protected $path;

public function __construct(
protected Filesystem $files,
protected HasherContract $hasher,
protected string $table,
protected string $hashKey,
protected int $expires = 3600,
protected int $throttle = 60
) {
$this->path = storage_path("statamic/password_resets/$table.yaml");
}

public function create(CanResetPasswordContract $user)
{
$email = $user->getEmailForPasswordReset();

$token = $this->createNewToken();

$this->insert($this->getPayload($email, $token));

return $token;
}

protected function insert($payload)
{
$resets = $this->getResets();

$resets[$payload['email']] = [
'token' => $payload['token'],
'created_at' => $payload['created_at']->timestamp,
];

$this->putResets($resets);
}

public function delete(CanResetPasswordContract $user)
{
$this->putResets(
$this->getResets()->forget($user->email())
);
}

public function deleteExpired()
{
$this->putResets($this->getResets()->reject(function ($item, $email) {
return $this->tokenExpired($item['created_at']);
}));
}

public function exists(CanResetPasswordContract $user, $token)
{
$record = $this->getResets()->get($user->email());

return $record &&
! $this->tokenExpired(Carbon::createFromTimestamp($record['created_at'], config('app.timezone')))
&& $this->hasher->check($token, $record['token']);
}

public function recentlyCreatedToken(CanResetPasswordContract $user)
{
$record = $this->getResets()->get($user->email());

return $record && parent::tokenRecentlyCreated($record['created_at']);
}

protected function getResets()
{
if (! $this->files->exists($this->path)) {
return collect();
}

return collect(YAML::parse($this->files->get($this->path)));
}

protected function putResets($resets)
{
if (! $this->files->isDirectory($dir = dirname($this->path))) {
$this->files->makeDirectory($dir);
}

$this->files->put($this->path, YAML::dump($resets->all()));
}
}
13 changes: 12 additions & 1 deletion src/Auth/Passwords/PasswordBrokerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,18 @@ protected function createTokenRepository(array $config)
$key = base64_decode(substr($key, 7));
}

return new TokenRepository(
if (version_compare(app()->version(), '12', '<')) {
return new TokenRepository(
$this->app['files'],
$this->app['hash'],
$config['table'],
$key,
$config['expire'],
$config['throttle'] ?? 0
);
}

return new LaravelTwelveTokenRepository(
$this->app['files'],
$this->app['hash'],
$config['table'],
Expand Down
20 changes: 12 additions & 8 deletions src/Auth/Passwords/TokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@

class TokenRepository extends DatabaseTokenRepository
{
protected $files;
protected $hasher;
protected $hashKey;
protected $expires;
protected $path;

public function __construct(
protected Filesystem $files,
protected HasherContract $hasher,
protected string $table,
protected string $hashKey,
protected int $expires = 3600,
protected int $throttle = 60
) {
public function __construct(Filesystem $files, HasherContract $hasher, $table, $hashKey, $expires = 60, $throttle = 60)
{
$this->files = $files;
$this->hasher = $hasher;
$this->hashKey = $hashKey;
$this->expires = $expires * 60;
$this->throttle = $throttle;

$this->path = storage_path("statamic/password_resets/$table.yaml");
}

Expand Down

0 comments on commit ee5e722

Please sign in to comment.