@@ -22,9 +22,14 @@ class TwoFactorAuthenticationController extends Controller implements TwoFactorA
2222 */
2323 public function setupTwoFactorAuthentication (Request $ request )
2424 {
25- $ secret_key = $ this ->base32EncodedString (config ('2fa-config.number_of_digits ' ));
2625 $ user = User::find ($ request ->user ()->id );
27- $ user ->two_factor_secret_key = $ secret_key ;
26+
27+ if (!$ user ->two_factor_secret_key ) {
28+ $ user ->two_factor_secret_key = $ this ->base32EncodedString (config ('2fa-config.number_of_digits ' ));
29+ } else {
30+ $ secret_key = $ user ->two_factor_secret_key ;
31+ }
32+
2833 $ user ->update ();
2934 $ totp = new TOTP (
3035 config ('2fa-config.account_name ' ),
@@ -48,7 +53,7 @@ public function setupTwoFactorAuthentication(Request $request)
4853 */
4954 public function enableTwoFactorAuthentication (Request $ request )
5055 {
51- $ user = User::find ($ request ->user ()->id );
56+ $ user = User::find ($ request ->user ()->id );
5257 $ user ->is_two_factor_enabled = 1 ;
5358 $ user ->update ();
5459
@@ -63,7 +68,7 @@ public function enableTwoFactorAuthentication(Request $request)
6368 public function verifyTwoFactorAuthentication (Request $ request )
6469 {
6570 if ($ request ->session ()->has ('2fa:user:id ' )) {
66- $ secret = getenv ('HMAC_SECRET ' );
71+ $ secret = getenv ('HMAC_SECRET ' );
6772 $ signature = hash_hmac ('sha256 ' , decrypt ($ request ->session ()->get ('2fa:user:id ' )), $ secret );
6873
6974 if (md5 ($ signature ) !== md5 ($ request ->signature )) {
@@ -84,8 +89,7 @@ public function verifyTwoFactorAuthentication(Request $request)
8489 * @return string
8590 */
8691 private function base32EncodedString ($ length = 30 ):
87- string
88- {
92+ string {
8993 return Base32::encode ($ this ->strRandom ($ length ));
9094 }
9195
@@ -97,8 +101,7 @@ private function base32EncodedString($length = 30):
97101 * @return string
98102 */
99103 private function strRandom ($ length = 30 ):
100- string
101- {
104+ string {
102105 $ string = '' ;
103106
104107 while (($ len = strlen ($ string )) < $ length ) {
0 commit comments