66use App \User ;
77use Base32 \Base32 ;
88use Illuminate \Http \Request ;
9+ use Illuminate \Support \Facades \Schema ;
910use OTPHP \TOTP ;
1011use Thecodework \TwoFactorAuthentication \AuthenticatesUsersWith2FA ;
1112use Thecodework \TwoFactorAuthentication \Contracts \TwoFactorAuthenticationInterface ;
13+ use Thecodework \TwoFactorAuthentication \Exceptions \TwoFactorAuthenticationExceptions ;
14+ use Thecodework \TwoFactorAuthentication \TwoFactorAuthenticationServiceProvider ;
1215
1316class 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 ) {
0 commit comments