Add all files
This commit is contained in:
123
extensions/Others/Security/Services/SecurityAuthService.php
Normal file
123
extensions/Others/Security/Services/SecurityAuthService.php
Normal file
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace Paymenter\Extensions\Others\Security\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Paymenter\Extensions\Others\Security\Models\LoginAttempt;
|
||||
use Paymenter\Extensions\Others\Security\Models\BlockedIP;
|
||||
use Paymenter\Extensions\Others\Security\Models\SuspendedAccount;
|
||||
use Paymenter\Extensions\Others\Security\Services\VPNDetectionService;
|
||||
|
||||
class SecurityAuthService
|
||||
{
|
||||
public static function attemptLogin(array $credentials, bool $remember = false, string $ipAddress = null): array
|
||||
{
|
||||
$email = $credentials['email'] ?? '';
|
||||
$ipAddress = $ipAddress ?? request()->ip();
|
||||
|
||||
if (BlockedIP::isIPBlocked($ipAddress)) {
|
||||
$block = BlockedIP::getActiveBlock($ipAddress);
|
||||
|
||||
LoginAttempt::recordAttempt(
|
||||
$ipAddress,
|
||||
$email,
|
||||
false,
|
||||
request()->userAgent(),
|
||||
null,
|
||||
'Login blocked - IP blocked: ' . $block->reason
|
||||
);
|
||||
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => 'Your IP address has been blocked. Reason: ' . $block->reason,
|
||||
'type' => 'ip_blocked'
|
||||
];
|
||||
}
|
||||
|
||||
if (VPNDetectionService::checkAndBlockVPN($ipAddress, $email)) {
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => 'Access denied. VPN/Proxy connections are not allowed.',
|
||||
'type' => 'vpn_blocked'
|
||||
];
|
||||
}
|
||||
|
||||
$user = \App\Models\User::where('email', $email)->first();
|
||||
if ($user && SuspendedAccount::isUserSuspended($user->id)) {
|
||||
$suspension = SuspendedAccount::getActiveSuspension($user->id);
|
||||
|
||||
LoginAttempt::recordAttempt(
|
||||
$ipAddress,
|
||||
$email,
|
||||
false,
|
||||
request()->userAgent(),
|
||||
null,
|
||||
'Login blocked - Account suspended: ' . $suspension->reason
|
||||
);
|
||||
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => 'Your account has been suspended. Reason: ' . $suspension->reason,
|
||||
'type' => 'account_suspended'
|
||||
];
|
||||
}
|
||||
|
||||
if (!Auth::attempt($credentials, $remember)) {
|
||||
LoginAttempt::recordAttempt(
|
||||
$ipAddress,
|
||||
$email,
|
||||
false,
|
||||
request()->userAgent(),
|
||||
null,
|
||||
'Failed login attempt - Invalid credentials'
|
||||
);
|
||||
|
||||
BlockedIP::recordFailedAttempt($ipAddress);
|
||||
static::autoBlockIfNeeded($ipAddress);
|
||||
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => 'These credentials do not match our records.',
|
||||
'type' => 'invalid_credentials'
|
||||
];
|
||||
}
|
||||
|
||||
LoginAttempt::recordAttempt(
|
||||
$ipAddress,
|
||||
$email,
|
||||
true,
|
||||
request()->userAgent(),
|
||||
null,
|
||||
'Successful login'
|
||||
);
|
||||
|
||||
return [
|
||||
'success' => true,
|
||||
'message' => 'Login successful',
|
||||
'type' => 'success'
|
||||
];
|
||||
}
|
||||
|
||||
protected static function autoBlockIfNeeded(string $ipAddress): void
|
||||
{
|
||||
try {
|
||||
$extension = app(\Paymenter\Extensions\Others\Security\Security::class);
|
||||
$config = $extension->getConfig();
|
||||
|
||||
$maxAttempts = 5;
|
||||
$blockDuration = 60;
|
||||
|
||||
foreach ($config as $setting) {
|
||||
if ($setting['name'] === 'max_login_attempts') {
|
||||
$maxAttempts = $setting['default'] ?? 5;
|
||||
}
|
||||
if ($setting['name'] === 'ip_block_duration') {
|
||||
$blockDuration = $setting['default'] ?? 60;
|
||||
}
|
||||
}
|
||||
|
||||
BlockedIP::autoBlockIfNeeded($ipAddress, $maxAttempts, $blockDuration);
|
||||
} catch (\Exception $e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user