Skip to content

Commit cd8b6aa

Browse files
committed
allow granular authentication customization
1 parent 6c36b08 commit cd8b6aa

File tree

4 files changed

+100
-14
lines changed

4 files changed

+100
-14
lines changed

src/Actions/AttemptToAuthenticate.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,50 @@ public function __construct(StatefulGuard $guard, LoginRateLimiter $limiter)
4545
*/
4646
public function handle($request, $next)
4747
{
48+
if (Fortify::$authenticateUsingCallback) {
49+
return $this->handleUsingCustomCallback($request, $next);
50+
}
51+
4852
if ($this->guard->attempt(
4953
$request->only(Fortify::username(), 'password'),
5054
$request->filled('remember'))
5155
) {
5256
return $next($request);
5357
}
5458

59+
$this->throwFailedAuthenticationException($request);
60+
}
61+
62+
/**
63+
* Attempt to authenticate using a custom callback.
64+
*
65+
* @param \Illuminate\Http\Request $request
66+
* @param callable $next
67+
* @return mixed
68+
*/
69+
protected function handleUsingCustomCallback($request, $next)
70+
{
71+
$user = call_user_func(Fortify::$authenticateUsingCallback, $request);
72+
73+
if (! $user) {
74+
return $this->throwFailedAuthenticationException();
75+
}
76+
77+
$this->guard->login($user, $request->filled('remember'));
78+
79+
return $next($request);
80+
}
81+
82+
/**
83+
* Throw a failed authentication validation exception.
84+
*
85+
* @param \Illuminate\Http\Request $request
86+
* @return void
87+
*
88+
* @throws \Illuminate\Validation\ValidationException
89+
*/
90+
protected function throwFailedAuthenticationException($request)
91+
{
5592
$this->limiter->increment($request);
5693

5794
throw ValidationException::withMessages([

src/Actions/RedirectIfTwoFactorAuthenticatable.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ public function __construct(StatefulGuard $guard, LoginRateLimiter $limiter)
4747
*/
4848
public function handle($request, $next)
4949
{
50-
$user = $this->validateCredentials(
51-
$request, $this->guard->getProvider()->getModel()
52-
);
50+
$user = $this->validateCredentials($request);
5351

5452
if (optional($user)->two_factor_secret &&
5553
in_array(TwoFactorAuthenticatable::class, class_uses_recursive($user))) {
@@ -63,22 +61,44 @@ public function handle($request, $next)
6361
* Attempt to validate the incoming credentials.
6462
*
6563
* @param \Illuminate\Http\Request $request
66-
* @param string $model
6764
* @return mixed
6865
*/
69-
protected function validateCredentials($request, $model)
66+
protected function validateCredentials($request)
7067
{
68+
if (Fortify::$authenticateUsingCallback) {
69+
return tap(call_user_func(Fortify::$authenticateUsingCallback, $request), function ($user) {
70+
if (! $user) {
71+
$this->throwFailedAuthenticationException($request);
72+
}
73+
});
74+
}
75+
76+
$model = $this->guard->getProvider()->getModel();
77+
7178
return tap($model::where(Fortify::username(), $request->{Fortify::username()})->first(), function ($user) use ($request) {
7279
if (! $user || ! Hash::check($request->password, $user->password)) {
73-
$this->limiter->increment($request);
74-
75-
throw ValidationException::withMessages([
76-
Fortify::username() => [trans('auth.failed')],
77-
]);
80+
$this->throwFailedAuthenticationException($request);
7881
}
7982
});
8083
}
8184

85+
/**
86+
* Throw a failed authentication validation exception.
87+
*
88+
* @param \Illuminate\Http\Request $request
89+
* @return void
90+
*
91+
* @throws \Illuminate\Validation\ValidationException
92+
*/
93+
protected function throwFailedAuthenticationException($request)
94+
{
95+
$this->limiter->increment($request);
96+
97+
throw ValidationException::withMessages([
98+
Fortify::username() => [trans('auth.failed')],
99+
]);
100+
}
101+
82102
/**
83103
* Get the two factor authentication enabled response.
84104
*

src/Fortify.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ class Fortify
2121
*
2222
* @var callable|null
2323
*/
24-
public static $loginThroughCallback;
24+
public static $authenticateThroughCallback;
25+
26+
/**
27+
* The callback that is repsonsible for validating authentication credentials, if applicable.
28+
*
29+
* @var callable|null
30+
*/
31+
public static $authenticateUsingCallback;
2532

2633
/**
2734
* Indicates if Fortify routes will be registered.
@@ -151,7 +158,29 @@ public static function requestPasswordResetLinkView($view)
151158
*/
152159
public static function loginThrough(callable $callback)
153160
{
154-
static::$loginThroughCallback = $callback;
161+
return static::authenticateThrough($callback);
162+
}
163+
164+
/**
165+
* Register a callback that is responsible for building the authentication pipeline array.
166+
*
167+
* @param callable $callback
168+
* @return void
169+
*/
170+
public static function authenticateThrough(callable $callback)
171+
{
172+
static::$authenticateThroughCallback = $callback;
173+
}
174+
175+
/**
176+
* Register a callback that is responsible for validating incoming authentication credentials.
177+
*
178+
* @param callable $callback
179+
* @return void
180+
*/
181+
public static function authenticateUsing(callable $callback)
182+
{
183+
static::$authenticateUsingCallback = $callback;
155184
}
156185

157186
/**

src/Http/Controllers/AuthenticatedSessionController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public function store(LoginRequest $request)
6868
*/
6969
protected function loginPipeline(LoginRequest $request)
7070
{
71-
if (Fortify::$loginThroughCallback) {
71+
if (Fortify::$authenticateThroughCallback) {
7272
return (new Pipeline(app()))->send($request)->through(array_filter(
73-
call_user_func(Fortify::$loginThroughCallback, $request)
73+
call_user_func(Fortify::$authenticateThroughCallback, $request)
7474
));
7575
}
7676

0 commit comments

Comments
 (0)