-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a494bb6
commit ec8afef
Showing
5 changed files
with
249 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Fleetbase\Http\Controllers\Internal\v1; | ||
|
||
use Fleetbase\Http\Controllers\Controller; | ||
use Illuminate\Http\Request; | ||
use Fleetbase\Support\TwoFactorAuth; | ||
|
||
/** | ||
* Class TwoFaController | ||
* | ||
* @package Fleetbase\Http\Controllers\Internal\v1 | ||
*/ | ||
class TwoFaController extends Controller | ||
{ | ||
/** | ||
* TwoFactorAuth instance. | ||
* | ||
* @var \Fleetbase\Support\TwoFactorAuth | ||
*/ | ||
protected $twoFactorAuth; | ||
|
||
/** | ||
* TwoFaController constructor. | ||
* | ||
* @param \Fleetbase\Support\TwoFactorAuth $twoFactorAuth | ||
*/ | ||
public function __construct(TwoFactorAuth $twoFactorAuth) | ||
{ | ||
$this->twoFactorAuth = $twoFactorAuth; | ||
} | ||
|
||
/** | ||
* Save Two-Factor Authentication settings. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function saveSettings(Request $request) | ||
{ | ||
return TwoFactorAuth::saveSettings($request); | ||
} | ||
|
||
/** | ||
* Get Two-Factor Authentication settings. | ||
* | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function getSettings() | ||
{ | ||
return TwoFactorAuth::getSettings(); | ||
} | ||
|
||
/** | ||
* Verify Two-Factor Authentication code. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function verifyTwoFactor(Request $request) | ||
{ | ||
return TwoFactorAuth::verifyTwoFactor($request); | ||
} | ||
} |
91 changes: 0 additions & 91 deletions
91
src/Http/Controllers/Internal/v1/TwoFaSettingController.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
namespace Fleetbase\Support; | ||
|
||
use Fleetbase\Models\VerificationCode; | ||
use Aloha\Twilio\Support\Laravel\Facade as Twilio; | ||
use Fleetbase\Models\Setting; | ||
use Illuminate\Support\Facades\RateLimiter; | ||
use Illuminate\Validation\ValidationException; | ||
|
||
/** | ||
* Class TwoFactorAuth | ||
* | ||
* @package Fleetbase\Support | ||
*/ | ||
class TwoFactorAuth | ||
{ | ||
/** | ||
* Save Two-Factor Authentication settings. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Illuminate\Http\JsonResponse | ||
* @throws \Exception | ||
*/ | ||
public static function saveSettings($request) | ||
{ | ||
$twoFaSettings = $request->input('twoFaSettings'); | ||
if (!is_array($twoFaSettings)) { | ||
throw new \Exception('Invalid 2FA settings data.'); | ||
} | ||
Setting::configure('2fa', $twoFaSettings); | ||
|
||
return response()->json([ | ||
'status' => 'ok', | ||
'message' => '2Fa settings successfully saved.', | ||
]); | ||
} | ||
|
||
/** | ||
* Get Two-Factor Authentication settings. | ||
* | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public static function getSettings() | ||
{ | ||
$twoFaSettings = Setting::lookup('2fa', ['enabled' => false, 'method' => 'authenticator_app']); | ||
|
||
return response()->json($twoFaSettings); | ||
} | ||
|
||
/** | ||
* Verify Two-Factor Authentication code. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public static function verifyTwoFactor($request) | ||
{ | ||
if (!RateLimiter::attempt(self::throttleKey($request), self::throttleMaxAttempts(), self::throttleDecayMinutes())) { | ||
throw ValidationException::withMessages([ | ||
'code' => ['Too many verification attempts. Please try again later.'], | ||
])->status(429); | ||
} | ||
|
||
$user = auth()->user(); | ||
$codeToVerify = $request->input('code'); | ||
|
||
$latestCode = VerificationCode::where('subject_uuid', $user->uuid) | ||
->where('subject_type', get_class($user)) | ||
->where('for', 'phone_verification') | ||
->latest() | ||
->first(); | ||
|
||
if (!$latestCode || $latestCode->code !== $codeToVerify || $latestCode->isExpired()) { | ||
RateLimiter::hit(self::throttleKey($request)); | ||
|
||
return response()->json([ | ||
'status' => 'error', | ||
'message' => 'Invalid or expired verification code.', | ||
], 401); | ||
} | ||
|
||
self::sendVerificationSuccessSms($user); | ||
|
||
return response()->json([ | ||
'status' => 'success', | ||
'message' => 'Verification Successful', | ||
]); | ||
} | ||
|
||
/** | ||
* Get the throttle key based on the request's IP. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return string | ||
*/ | ||
protected static function throttleKey($request) | ||
{ | ||
return 'verify_two_factor_' . $request->ip(); | ||
} | ||
|
||
/** | ||
* Get the maximum number of attempts allowed in the throttle. | ||
* | ||
* @return int | ||
*/ | ||
protected static function throttleMaxAttempts() | ||
{ | ||
return 5; | ||
} | ||
|
||
/** | ||
* Get the decay time in minutes for the throttle. | ||
* | ||
* @return int | ||
*/ | ||
protected static function throttleDecayMinutes() | ||
{ | ||
return 2; | ||
} | ||
|
||
/** | ||
* Send a success SMS after successful verification. | ||
* | ||
* @param mixed $user | ||
*/ | ||
private static function sendVerificationSuccessSms($user) | ||
{ | ||
Twilio::message($user->phone, 'Your Fleetbase verification was successful. Welcome!'); | ||
} | ||
|
||
public static function isEnabled() | ||
{ | ||
return Setting::lookup('2fa', ['enabled']); | ||
} | ||
|
||
public static function start() | ||
{ | ||
return true; | ||
} | ||
} |
Oops, something went wrong.