-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
36 changed files
with
582 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
namespace App\Auth; | ||
|
||
use Illuminate\Support\Str; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
use Illuminate\Cache\RateLimiter; | ||
use Illuminate\Auth\Events\Lockout; | ||
use Illuminate\Support\Facades\Lang; | ||
use Illuminate\Validation\ValidationException; | ||
|
||
/** | ||
* @author Taylor Otwell | ||
* @link https://github.com/laravel/ui/blob/2.x/auth-backend/ThrottlesLogins.php | ||
*/ | ||
trait ThrottlesLogins | ||
{ | ||
/** | ||
* Determine if the user has too many failed login attempts. | ||
* | ||
* @param Request $request | ||
* @return bool | ||
*/ | ||
protected function hasTooManyLoginAttempts(Request $request): bool | ||
{ | ||
return $this->limiter()->tooManyAttempts( | ||
$this->throttleKey($request), | ||
$this->maxAttempts() | ||
); | ||
} | ||
|
||
/** | ||
* Increment the login attempts for the user. | ||
* | ||
* @param Request $request | ||
* @return void | ||
*/ | ||
protected function incrementLoginAttempts(Request $request): void | ||
{ | ||
$this->limiter()->hit( | ||
$this->throttleKey($request), | ||
$this->decayMinutes() * 60 | ||
); | ||
} | ||
|
||
/** | ||
* Redirect the user after determining they are locked out. | ||
* | ||
* @param Request $request | ||
* @return void | ||
* | ||
* @throws ValidationException | ||
*/ | ||
protected function sendLockoutResponse(Request $request): void | ||
{ | ||
$seconds = $this->limiter()->availableIn( | ||
$this->throttleKey($request) | ||
); | ||
|
||
throw ValidationException::withMessages([ | ||
'email' => [Lang::get('auth.throttle', [ | ||
'attempts' => $this->maxAttempts(), | ||
'seconds' => $seconds, | ||
'minutes' => ceil($seconds / 60), | ||
])], | ||
])->status(Response::HTTP_TOO_MANY_REQUESTS); | ||
} | ||
|
||
/** | ||
* Clear the login locks for the given user credentials. | ||
* | ||
* @param Request $request | ||
* @return void | ||
*/ | ||
protected function clearLoginAttempts(Request $request): void | ||
{ | ||
$this->limiter()->clear($this->throttleKey($request)); | ||
} | ||
|
||
/** | ||
* Fire an event when a lockout occurs. | ||
* | ||
* @param Request $request | ||
* @return void | ||
*/ | ||
protected function fireLockoutEvent(Request $request): void | ||
{ | ||
event(new Lockout($request)); | ||
} | ||
|
||
/** | ||
* Get the throttle key for the given request. | ||
* | ||
* @param Request $request | ||
* @return string | ||
*/ | ||
protected function throttleKey(Request $request): string | ||
{ | ||
return Str::lower($request->input('email')).'|'.$request->ip(); | ||
} | ||
|
||
/** | ||
* Get the rate limiter instance. | ||
* | ||
* @return RateLimiter | ||
*/ | ||
protected function limiter(): RateLimiter | ||
{ | ||
return app(RateLimiter::class); | ||
} | ||
|
||
/** | ||
* Get the maximum number of attempts to allow. | ||
* | ||
* @return int | ||
*/ | ||
public function maxAttempts(): int | ||
{ | ||
return property_exists($this, 'maxAttempts') ? $this->maxAttempts : 5; | ||
} | ||
|
||
/** | ||
* Get the number of minutes to throttle for. | ||
* | ||
* @return int | ||
*/ | ||
public function decayMinutes(): int | ||
{ | ||
return property_exists($this, 'decayMinutes') ? $this->decayMinutes : 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Auth; | ||
|
||
use Illuminate\Http\Exceptions\HttpResponseException; | ||
use App\Auth\ThrottlesLogins; | ||
use App\Http\Requests\Auth\Login; | ||
use App\Http\Controllers\Controller; | ||
use Illuminate\Support\Facades\Lang; | ||
use App\Http\Resources\User as UserResource; | ||
use Illuminate\Contracts\Auth\Factory as Auth; | ||
use Illuminate\Validation\ValidationException; | ||
|
||
class LoginController extends Controller | ||
{ | ||
use ThrottlesLogins; | ||
|
||
/** | ||
* Max number of login attempts allowed. | ||
* | ||
* @var integer | ||
*/ | ||
protected int $maxAttempts = 5; | ||
|
||
/** | ||
* Number of minutes login attempts are throttled for. | ||
* | ||
* @var integer | ||
*/ | ||
protected int $decayMinutes = 5; | ||
|
||
/** | ||
* Handle an authentication attempt. | ||
* | ||
* @param Login $request | ||
* @param Auth $auth | ||
* @return UserResource | ||
*@throws HttpResponseException | ||
*/ | ||
public function __invoke(Login $request, Auth $auth) | ||
{ | ||
if ($this->hasTooManyLoginAttempts($request)) { | ||
$this->fireLockoutEvent($request); | ||
|
||
$this->sendLockoutResponse($request); | ||
} | ||
|
||
if ($auth->attempt($request->only('email', 'password'))) { | ||
$request->session()->regenerate(); | ||
|
||
$this->clearLoginAttempts($request); | ||
|
||
return new UserResource($auth->user()); | ||
} | ||
|
||
$this->incrementLoginAttempts($request); | ||
|
||
throw ValidationException::withMessages(['email' => [Lang::get('auth.failed')]])->status(422); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use Illuminate\Http\JsonResponse; | ||
|
||
class SystemConfigController extends Controller | ||
{ | ||
public function __invoke() | ||
{ | ||
return new JsonResponse([ | ||
'auth_type' => auth_type() | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\Auth; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class Login extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'email' => ['required'], | ||
'password' => ['required'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace App\Support\Enums; | ||
|
||
enum Auth: string | ||
{ | ||
case Form = 'form'; | ||
case Google = 'google'; | ||
|
||
public function title(): string | ||
{ | ||
return match ($this) { | ||
self::Form => 'Form', | ||
self::Google => 'Google' | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.