Skip to content

Commit 8204238

Browse files
committed
New feature added which helps users to define User model. #6
1 parent 06feab2 commit 8204238

File tree

6 files changed

+69
-11
lines changed

6 files changed

+69
-11
lines changed

config/2fa-config.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,15 @@
2828
* To Support Google Authenticator
2929
*/
3030
'number_of_digits' => 8,
31+
32+
/*
33+
* Explitcitly Define Table name for the model.
34+
*/
35+
'table' => 'users',
36+
37+
/*
38+
* User Model
39+
* By Default `\App\User` Model is defined.
40+
*/
41+
'model' => '\App\User',
3142
];

database/migrations/2017_01_20_160000_add_two_factor_authentication_required_fields.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class AddTwoFactorAuthenticationRequiredFields extends Migration
1313
*/
1414
public function up()
1515
{
16-
Schema::table('users', function (Blueprint $table) {
16+
Schema::table(config('2fa-config.table'), function (Blueprint $table) {
1717
$table->smallInteger('is_two_factor_enabled')->default(0)->before('created_at');
1818
$table->string('two_factor_secret_key')->nullable()->default(NULL)->after('is_two_factor_enabled');
1919
});
@@ -26,7 +26,7 @@ public function up()
2626
*/
2727
public function down()
2828
{
29-
Schema::table('users', function (Blueprint $table) {
29+
Schema::table(config('2fa-config.table'), function (Blueprint $table) {
3030
$table->dropColumn('is_two_factor_enabled');
3131
$table->dropColumn('two_factor_secret_key');
3232
});

src/AuthenticatesUsersWith2FA.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Auth;
77
use Illuminate\Http\Request;
88
use OTPHP\TOTP;
9+
use Thecodework\TwoFactorAuthentication\TwoFactorAuthenticationServiceProvider;
910
use Validator;
1011

1112
trait AuthenticatesUsersWith2FA
@@ -48,9 +49,10 @@ protected function authenticated(Request $request, $user)
4849
*/
4950
public function verifyToken(Request $request)
5051
{
52+
$userModel = TwoFactorAuthenticationServiceProvider::getUserModelInstance();
5153
// Pulling encrypted user id from session and getting user details
5254
$userId = $request->session()->get('2fa:user:id');
53-
$this->user = User::find(decrypt($userId));
55+
$this->user = $userModel->find(decrypt($userId));
5456

5557
// If token is not valid then custom validation error message will be shown.
5658
$messages = [
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Thecodework\TwoFactorAuthentication\Exceptions;
4+
5+
class TwoFactorAuthenticationExceptions extends \Exception {
6+
7+
/**
8+
* Column Not Found
9+
*
10+
*/
11+
public static function columnNotFound()
12+
{
13+
$table = config('2fa-config.table');
14+
return new static("Could not locate required column `two_factor_secret_key` or `is_two_factor_enabled` in `$table` table. Make sure the migrations ran properly.");
15+
}
16+
}

src/Http/Controllers/TwoFactorAuthenticationController.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,43 @@
66
use App\User;
77
use Base32\Base32;
88
use Illuminate\Http\Request;
9+
use Illuminate\Support\Facades\Schema;
910
use OTPHP\TOTP;
1011
use Thecodework\TwoFactorAuthentication\AuthenticatesUsersWith2FA;
1112
use Thecodework\TwoFactorAuthentication\Contracts\TwoFactorAuthenticationInterface;
13+
use Thecodework\TwoFactorAuthentication\Exceptions\TwoFactorAuthenticationExceptions;
14+
use Thecodework\TwoFactorAuthentication\TwoFactorAuthenticationServiceProvider;
1215

1316
class TwoFactorAuthenticationController extends Controller implements TwoFactorAuthenticationInterface
1417
{
1518
use AuthenticatesUsersWith2FA;
1619

20+
/**
21+
* User Model
22+
*/
23+
protected $userModel;
24+
25+
/**
26+
* Assigns $usersModel Property a Model instance.
27+
*/
28+
public function __construct()
29+
{
30+
$this->userModel = TwoFactorAuthenticationServiceProvider::getUserModelInstance();
31+
}
1732
/**
1833
* Setup two factor authentication.
1934
*
2035
* @param \Illuminate\Http\Request
2136
* @param \Illuminate\Http\Response
37+
* @throws \Thecodework\TwoFactorAuthentications\Exceptions\TwoFactorAuthenticationExceptions
2238
*/
2339
public function setupTwoFactorAuthentication(Request $request)
2440
{
25-
$user = User::find($request->user()->id);
41+
$user = $this->userModel->find($request->user()->id);
42+
if (!Schema::hasColumn(config('2fa-config.table'), 'two_factor_secret_key') ||
43+
!Schema::hasColumn(config('2fa-config.table'), 'is_two_factor_enabled')) {
44+
throw TwoFactorAuthenticationExceptions::columnNotFound();
45+
}
2646
$user->two_factor_secret_key = $user->two_factor_secret_key ?? $this->base32EncodedString(config('2fa-config.number_of_digits'));
2747
$user->update();
2848

@@ -53,7 +73,7 @@ public function setupTwoFactorAuthentication(Request $request)
5373
*/
5474
public function enableTwoFactorAuthentication(Request $request)
5575
{
56-
$user = User::find($request->user()->id);
76+
$user = $this->userModel->find($request->user()->id);
5777
$user->is_two_factor_enabled = 1;
5878
$user->update();
5979

@@ -78,7 +98,7 @@ public function enableTwoFactorAuthentication(Request $request)
7898
*/
7999
public function disableTwoFactorAuthentication(Request $request)
80100
{
81-
$user = User::find($request->user()->id);
101+
$user = $this->userModel->find($request->user()->id);
82102
$user->is_two_factor_enabled = 0;
83103
$user->two_factor_secret_key = null;
84104
$user->update();
@@ -103,7 +123,7 @@ public function disableTwoFactorAuthentication(Request $request)
103123
public function verifyTwoFactorAuthentication(Request $request)
104124
{
105125
if ($request->session()->has('2fa:user:id')) {
106-
$secret = getenv('HMAC_SECRET');
126+
$secret = getenv('HMAC_SECRET');
107127
$signature = hash_hmac('sha256', decrypt($request->session()->get('2fa:user:id')), $secret);
108128

109129
if (md5($signature) !== md5($request->signature)) {
@@ -124,8 +144,7 @@ public function verifyTwoFactorAuthentication(Request $request)
124144
* @return string
125145
*/
126146
private function base32EncodedString($length = 30):
127-
string
128-
{
147+
string {
129148
return Base32::encode($this->strRandom($length));
130149
}
131150

@@ -137,8 +156,7 @@ private function base32EncodedString($length = 30):
137156
* @return string
138157
*/
139158
private function strRandom($length = 30):
140-
string
141-
{
159+
string{
142160
$string = '';
143161

144162
while (($len = strlen($string)) < $length) {

src/TwoFactorAuthenticationServiceProvider.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Thecodework\TwoFactorAuthentication;
44

55
use Illuminate\Support\ServiceProvider;
6+
use Illuminate\Database\Eloquent\Model;
67

78
class TwoFactorAuthenticationServiceProvider extends ServiceProvider
89
{
@@ -33,4 +34,14 @@ public function boot()
3334
__DIR__ . '/../resources/views/' => resource_path('views/vendor/2fa'),
3435
], 'views');
3536
}
37+
38+
public static function determineUserModel(): string
39+
{
40+
return $userModel = config('2fa-config.model');
41+
}
42+
43+
public static function getUserModelInstance(): Model {
44+
$userModelClassName = self::determineUserModel();
45+
return new $userModelClassName();
46+
}
3647
}

0 commit comments

Comments
 (0)