X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/44330bdd24a7dca1c85ab2068d335f089d4cff6d..refs/pull/2902/head:/app/Auth/Access/UserTokenService.php diff --git a/app/Auth/Access/UserTokenService.php b/app/Auth/Access/UserTokenService.php index 40f363ee1..565dcb948 100644 --- a/app/Auth/Access/UserTokenService.php +++ b/app/Auth/Access/UserTokenService.php @@ -1,23 +1,27 @@ -getEntryByToken($token); @@ -61,7 +71,7 @@ class UserTokenService } if ($this->entryExpired($entry)) { - throw new UserTokenExpiredException("Token of id {$token->id} has expired.", $entry->user_id); + throw new UserTokenExpiredException("Token of id {$entry->id} has expired.", $entry->user_id); } return $entry->user_id; @@ -69,40 +79,47 @@ class UserTokenService /** * Creates a unique token within the email confirmation database. + * * @return string */ - protected function generateToken() : string + protected function generateToken(): string { - $token = str_random(24); + $token = Str::random(24); while ($this->tokenExists($token)) { - $token = str_random(25); + $token = Str::random(25); } + return $token; } /** * Generate and store a token for the given user. + * * @param User $user + * * @return string */ - protected function createTokenForUser(User $user) : string + protected function createTokenForUser(User $user): string { $token = $this->generateToken(); $this->db->table($this->tokenTable)->insert([ - 'user_id' => $user->id, - 'token' => $token, + 'user_id' => $user->id, + 'token' => $token, 'created_at' => Carbon::now(), - 'updated_at' => Carbon::now() + 'updated_at' => Carbon::now(), ]); + return $token; } /** * Check if the given token exists. + * * @param string $token + * * @return bool */ - protected function tokenExists(string $token) : bool + protected function tokenExists(string $token): bool { return $this->db->table($this->tokenTable) ->where('token', '=', $token)->exists(); @@ -110,7 +127,9 @@ class UserTokenService /** * Get a token entry for the given token. + * * @param string $token + * * @return object|null */ protected function getEntryByToken(string $token) @@ -122,13 +141,14 @@ class UserTokenService /** * Check if the given token entry has expired. + * * @param stdClass $tokenEntry + * * @return bool */ - protected function entryExpired(stdClass $tokenEntry) : bool + protected function entryExpired(stdClass $tokenEntry): bool { return Carbon::now()->subHours($this->expiryTime) ->gt(new Carbon($tokenEntry->created_at)); } - -} \ No newline at end of file +}