This repository has been archived by the owner on Feb 17, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathConfirms2FACode.php
92 lines (80 loc) · 2.2 KB
/
Confirms2FACode.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace DarkGhostHunter\Laraguard\Http\Controllers;
use Illuminate\Contracts\View\View;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use function view;
use function response;
use function redirect;
use function now;
trait Confirms2FACode
{
/**
* Display the TOTP code confirmation view.
*
* @return \Illuminate\Contracts\View\View
*/
public function showConfirmForm(): View
{
return view('laraguard::confirm');
}
/**
* Confirm the given user's TOTP code.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
*/
public function confirm(Request $request): JsonResponse|Response|RedirectResponse
{
$request->validate($this->rules(), $this->validationErrorMessages());
$this->resetTotpConfirmationTimeout($request);
return $request->wantsJson()
? response()->noContent()
: redirect()->intended($this->redirectPath());
}
/**
* Reset the TOTP code timeout.
*
* @param \Illuminate\Http\Request $request
* @return void
*/
protected function resetTotpConfirmationTimeout(Request $request): void
{
$request->session()->put('2fa.totp_confirmed_at', now()->timestamp);
}
/**
* Get the TOTP code validation rules.
*
* @return array
*/
protected function rules(): array
{
return [
'2fa_code' => 'required|totp_code',
];
}
/**
* Get the password confirmation validation error messages.
*
* @return array
*/
protected function validationErrorMessages(): array
{
return [];
}
/**
* Return the path to redirect if no intended path exists.
*
* @return string
* @see \Illuminate\Foundation\Auth\RedirectsUsers
*/
public function redirectPath(): string
{
if (method_exists($this, 'redirectTo')) {
return $this->redirectTo();
}
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}
}