Skip to content

Commit aa395af

Browse files
Merge pull request #79 from Haruki1707/2.x
[2.x] FIX: Redirection route in ConfirmTwoFactorCode middleware.
2 parents 10b658e + 48ef0b9 commit aa395af

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

src/Http/Middleware/ConfirmTwoFactorCode.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,28 @@ public function handle(
3232
return $next($request);
3333
}
3434

35+
$route = $this->getRedirectionRoute($route);
36+
3537
return $request->expectsJson()
3638
? response()->json(['message' => trans('two-factor::messages.required')], 403)
3739
: response()->redirectGuest(url()->route($route));
3840
}
3941

42+
/**
43+
* Determine the route to redirect the user.
44+
*/
45+
protected function getRedirectionRoute(string $route): string
46+
{
47+
// If the developer is forcing this middleware to always run,
48+
// then return redirection route "2fa.confirm" as default.
49+
// Otherwise, return the route as the developer set it.
50+
if (in_array(strtolower($route), ['true', 'force'], true)) {
51+
return '2fa.confirm';
52+
}
53+
54+
return $route;
55+
}
56+
4057
/**
4158
* Determine if the confirmation timeout has expired.
4259
*/

tests/Http/Middleware/ConfirmTwoFactorEnabledTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ public function test_continues_if_user_is_2fa_but_not_activated(): void
6464
}
6565

6666
public function test_asks_for_confirmation_if_forced(): void
67+
{
68+
$this->app['router']->get('intended_force', function () {
69+
return 'ok';
70+
})->name('intended')->middleware('web', 'auth', '2fa.confirm:true');
71+
72+
$this->actingAs($this->user);
73+
74+
$sessionKey = $this->app->make('config')->get('two-factor.confirm.key').'confirm.expires_at';
75+
76+
$this->session([$sessionKey => now()->addHour()->getTimestamp()]);
77+
78+
$this->getJson('intended_force')->assertJson(['message' => trans('two-factor::messages.required')]);
79+
$this->get('intended_force')->assertRedirect('confirm');
80+
}
81+
82+
public function test_asks_for_confirmation_if_forced_with_custom_route(): void
6783
{
6884
$this->app['router']->get('intended_force', function () {
6985
return 'ok';

0 commit comments

Comments
 (0)