|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Statamic\Auth\Passwords; |
| 4 | + |
| 5 | +use Illuminate\Auth\Passwords\DatabaseTokenRepository; |
| 6 | +use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; |
| 7 | +use Illuminate\Contracts\Hashing\Hasher as HasherContract; |
| 8 | +use Illuminate\Filesystem\Filesystem; |
| 9 | +use Illuminate\Support\Carbon; |
| 10 | +use Statamic\Facades\YAML; |
| 11 | + |
| 12 | +/** @deprecated */ |
| 13 | +class LaravelTwelveTokenRepository extends DatabaseTokenRepository |
| 14 | +{ |
| 15 | + protected $path; |
| 16 | + |
| 17 | + public function __construct( |
| 18 | + protected Filesystem $files, |
| 19 | + protected HasherContract $hasher, |
| 20 | + protected string $table, |
| 21 | + protected string $hashKey, |
| 22 | + protected int $expires = 3600, |
| 23 | + protected int $throttle = 60 |
| 24 | + ) { |
| 25 | + $this->path = storage_path("statamic/password_resets/$table.yaml"); |
| 26 | + } |
| 27 | + |
| 28 | + public function create(CanResetPasswordContract $user) |
| 29 | + { |
| 30 | + $email = $user->getEmailForPasswordReset(); |
| 31 | + |
| 32 | + $token = $this->createNewToken(); |
| 33 | + |
| 34 | + $this->insert($this->getPayload($email, $token)); |
| 35 | + |
| 36 | + return $token; |
| 37 | + } |
| 38 | + |
| 39 | + protected function insert($payload) |
| 40 | + { |
| 41 | + $resets = $this->getResets(); |
| 42 | + |
| 43 | + $resets[$payload['email']] = [ |
| 44 | + 'token' => $payload['token'], |
| 45 | + 'created_at' => $payload['created_at']->timestamp, |
| 46 | + ]; |
| 47 | + |
| 48 | + $this->putResets($resets); |
| 49 | + } |
| 50 | + |
| 51 | + public function delete(CanResetPasswordContract $user) |
| 52 | + { |
| 53 | + $this->putResets( |
| 54 | + $this->getResets()->forget($user->email()) |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + public function deleteExpired() |
| 59 | + { |
| 60 | + $this->putResets($this->getResets()->reject(function ($item, $email) { |
| 61 | + return $this->tokenExpired($item['created_at']); |
| 62 | + })); |
| 63 | + } |
| 64 | + |
| 65 | + public function exists(CanResetPasswordContract $user, $token) |
| 66 | + { |
| 67 | + $record = $this->getResets()->get($user->email()); |
| 68 | + |
| 69 | + return $record && |
| 70 | + ! $this->tokenExpired(Carbon::createFromTimestamp($record['created_at'], config('app.timezone'))) |
| 71 | + && $this->hasher->check($token, $record['token']); |
| 72 | + } |
| 73 | + |
| 74 | + public function recentlyCreatedToken(CanResetPasswordContract $user) |
| 75 | + { |
| 76 | + $record = $this->getResets()->get($user->email()); |
| 77 | + |
| 78 | + return $record && parent::tokenRecentlyCreated($record['created_at']); |
| 79 | + } |
| 80 | + |
| 81 | + protected function getResets() |
| 82 | + { |
| 83 | + if (! $this->files->exists($this->path)) { |
| 84 | + return collect(); |
| 85 | + } |
| 86 | + |
| 87 | + return collect(YAML::parse($this->files->get($this->path))); |
| 88 | + } |
| 89 | + |
| 90 | + protected function putResets($resets) |
| 91 | + { |
| 92 | + if (! $this->files->isDirectory($dir = dirname($this->path))) { |
| 93 | + $this->files->makeDirectory($dir); |
| 94 | + } |
| 95 | + |
| 96 | + $this->files->put($this->path, YAML::dump($resets->all())); |
| 97 | + } |
| 98 | +} |
0 commit comments