Skip to content

Commit

Permalink
implemented 2fa validation
Browse files Browse the repository at this point in the history
  • Loading branch information
TemuulenBM committed Jan 4, 2024
1 parent ec8afef commit 1ca998d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Http/Controllers/Internal/v1/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function login(LoginRequest $request)

// Check if 2FA enabled
if (TwoFactorAuth::isEnabled()) {
$twoFaSession = TwoFactorAuth::start();
$twoFaSession = TwoFactorAuth::start($user);
return response()->json(['two_fa_session' => $twoFaSession]);
}

Expand Down
26 changes: 26 additions & 0 deletions src/Http/Controllers/Internal/v1/TwoFaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,30 @@ public function verifyTwoFactor(Request $request)
{
return TwoFactorAuth::verifyTwoFactor($request);
}

public function checkTwoFactor(Request $request)
{
$identity = $request->input('identity');
$isTwoFaEnabled = TwoFactorAuth::isEnabled();
$twoFaSession = null;
$isTwoFaValidated = false;
$error = null;

if ($isTwoFaEnabled) {
$twoFaSession = TwoFactorAuth::start($identity);

if ($twoFaSession === null) {
$error = 'No user found using identity provided';
} else {
$isTwoFaValidated = TwoFactorAuth::isTwoFactorSessionValidated($twoFaSession);
}
}

return response()->json([
'isTwoFaEnabled' => $isTwoFaEnabled,
'isTwoFaValidated' => $isTwoFaValidated,
'twoFaSession' => $twoFaSession,
'error' => $error
]);
}
}
45 changes: 42 additions & 3 deletions src/Support/TwoFactorAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
use Fleetbase\Models\VerificationCode;
use Aloha\Twilio\Support\Laravel\Facade as Twilio;
use Fleetbase\Models\Setting;
use Fleetbase\Models\User;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;

/**
Expand Down Expand Up @@ -129,13 +132,49 @@ private static function sendVerificationSuccessSms($user)
Twilio::message($user->phone, 'Your Fleetbase verification was successful. Welcome!');
}

/**
* Check if Two-Factor Authentication is enabled.
*
* @return bool
*/
public static function isEnabled()
{
return Setting::lookup('2fa', ['enabled']);
$twoFaSettings = Setting::lookup('2fa');

// dd(data_get($twoFaSettings, 'enabled'));

return isset($twoFaSettings['enabled']) ? (bool)$twoFaSettings['enabled'] : false;

// return data_get($twoFaSettings, 'enabled');
}

/**
* Start the Two-Factor Authentication process and return the session key.
*
* @return string
*/
public static function start(string $identity): ?string
{
$twoFaSession = Str::random(40);

$user = User::where(function ($query) use ($identity) {
$query->where('email', $identity)->orWhere('phone', $identity);
})->first();

if ($user) {
Cache::put('two_fa_session:' . $user->uuid, true, now()->addMinutes(10));
return $twoFaSession;
}

return null;
}

public static function start()
public static function isTwoFactorSessionValidated(?string $twoFaSession = null): bool
{
return true;
if ($twoFaSession === null) {
return false;
}
// do check here
return false;
}
}
1 change: 1 addition & 0 deletions src/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ function ($router, $controller) {
function ($router, $controller) {
$router->post('settings', $controller('saveSettings'));
$router->get('settings', $controller('getSettings'));
$router->get('settings', $controller('checkTwoFactor'));
}
);
$router->fleetbaseRoutes('api-events');
Expand Down

0 comments on commit 1ca998d

Please sign in to comment.