-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTwoFactorAuthenticationController.php
176 lines (153 loc) · 5.06 KB
/
TwoFactorAuthenticationController.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
namespace Thecodework\TwoFactorAuthentication\Http\Controllers;
use Endroid\QrCode\QrCode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Schema;
use OTPHP\TOTP;
use ParagonIE\ConstantTime\Base32;
use Thecodework\TwoFactorAuthentication\AuthenticatesUsersWith2FA;
use Thecodework\TwoFactorAuthentication\Contracts\TwoFactorAuthenticationInterface;
use Thecodework\TwoFactorAuthentication\Exceptions\TwoFactorAuthenticationExceptions;
use Thecodework\TwoFactorAuthentication\TwoFactorAuthenticationServiceProvider;
use Session;
class TwoFactorAuthenticationController extends Controller implements TwoFactorAuthenticationInterface
{
use AuthenticatesUsersWith2FA;
/**
* User Model.
*/
protected $TwoFAModel;
/**
* Assigns $usersModel Property a Model instance.
* Set authenticated users data to $user Property.
*/
public function __construct()
{
$this->TwoFAModel = TwoFactorAuthenticationServiceProvider::getTwoFAModelInstance();
$this->middleware(function ($request, $next) {
$this->setUser(\Auth::guard(config('2fa-config.guard'))->user());
return $next($request);
});
}
/**
* Setup two factor authentication.
*
* @param \Illuminate\Http\Request
* @param \Illuminate\Http\Response
*
* @throws \Thecodework\TwoFactorAuthentications\Exceptions\TwoFactorAuthenticationExceptions
*
* @return mixed
*/
public function setupTwoFactorAuthentication(Request $request)
{
$user = $this->getUser();
$totp = TOTP::create(
$this->base32EncodedString(),
config('2fa-config.period'),
config('2fa-config.digest_algorithm'),
config('2fa-config.number_of_digits')
);
session(['totp' => $totp]);
$totp->setLabel(config('2fa-config.account_name'));
$this->updateUserWithProvisionedUri($totp->getProvisioningUri());
$qrCode = new QrCode($totp->getProvisioningUri());
$barcode = $qrCode->writeDataUri();
if ($request->ajax()) {
return $barcode;
}
return view('2fa::setup', compact('barcode', 'user'));
}
/**
* Disable 2FA.
*
* @param \Illuminate\Http\Request
*
* @return mixed
*/
public function enableTwoFactorAuthentication(Request $request)
{
$GOTP = $request->session()->get('totp');
$UOTP = $request->input('pass_code');
if (isset($GOTP) && $GOTP->verify($UOTP)) {
$user = $this->getUser();
$user->is_two_factor_enabled = 1;
$user->update();
if ($request->ajax()) {
return [
'data' => [
'message' => 'success',
'description' => '2FA Enabled',
],
];
}
return redirect(config('2fa-config.redirect_to'));
}else{
return redirect('setup-2fa');
}
}
/**
* Enable 2FA.
*
* @param \Illuminate\Http\Request
*
* @return mixed
*/
public function disableTwoFactorAuthentication(Request $request)
{
$user = $this->getUser();
$user->is_two_factor_enabled = 0;
$user->two_factor_provisioned_uri = null;
$user->update();
if ($request->ajax()) {
return [
'data' => [
'message' => 'success',
'description' => '2FA Disabled',
],
];
}
return redirect(config('2fa-config.redirect_to'));
}
/**
* Verify Two Factor Authentication.
*
* @param \Illuminate\Http\Request $request
*/
public function verifyTwoFactorAuthentication(Request $request)
{
if ($request->session()->has('2fa:user:id')) {
$secret = getenv('HMAC_SECRET');
$signature = hash_hmac('sha256', decrypt($request->session()->get('2fa:user:id')), $secret);
if (md5($signature) !== md5($request->signature)) {
return redirect()->intended('login');
}
return view('2fa::verify');
}
return redirect()->back(); //shoud be configurable
}
/**
* Encode Random String to 32 Base Transfer Encoding.
*
* @return string
*/
private function base32EncodedString(): string
{
return trim(Base32::encodeUpper(random_bytes(128)), '=');
}
/**
* Update User data with 2FA generated Key.
*
* @return void
*/
private function updateUserWithProvisionedUri($twoFactorProvisionedUri)
{
$user = $this->TwoFAModel->find($this->getUser()->id);
if (!Schema::hasColumn(config('2fa-config.table'), 'two_factor_provisioned_uri') ||
!Schema::hasColumn(config('2fa-config.table'), 'is_two_factor_enabled')) {
throw TwoFactorAuthenticationExceptions::columnNotFound();
}
$user->two_factor_provisioned_uri = $twoFactorProvisionedUri;
$user->update();
}
}