authentikate/database/seeders/AuthenticationTokenSeeder.php
Javier Feliz 038ee47fa3
All checks were successful
linter / quality (push) Successful in 3m8s
tests / ci (push) Successful in 13m56s
Claude be codin
2025-07-31 00:56:12 -04:00

87 lines
3.6 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\Application;
use App\Models\AuthenticationToken;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;
class AuthenticationTokenSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$user = User::where('email', 'me@javierfeliz.com')->first();
if (!$user) {
$this->command->error('Default user not found. Please run the main seeder first.');
return;
}
$applications = Application::all();
if ($applications->isEmpty()) {
$this->command->error('No applications found. Please run the application seeder first.');
return;
}
// Sample user agents from different browsers and devices
$userAgents = [
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0',
'Mozilla/5.0 (X11; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Safari/605.1.15',
'Mozilla/5.0 (iPhone; CPU iPhone OS 17_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Mobile/15E148 Safari/604.1',
'Mozilla/5.0 (iPad; CPU OS 17_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Mobile/15E148 Safari/604.1',
'Mozilla/5.0 (Linux; Android 14; SM-G991B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.6099.43 Mobile Safari/537.36',
];
// Sample IP addresses (using private/local ranges for testing)
$ipAddresses = [
'192.168.1.101',
'192.168.1.102',
'10.0.0.15',
'10.0.0.23',
'172.16.0.10',
'172.16.0.25',
'127.0.0.1',
'::1',
];
// Create 10-15 tokens for different applications at different times
$tokenCount = rand(10, 15);
$selectedApps = $applications->random(min($tokenCount, $applications->count()));
foreach ($selectedApps as $index => $app) {
// Create 1-3 tokens per app to simulate multiple sessions
$tokensPerApp = rand(1, 3);
for ($i = 0; $i < $tokensPerApp; $i++) {
$issuedAt = now()->subDays(rand(0, 30))->subHours(rand(0, 23))->subMinutes(rand(0, 59));
$expiresAt = $issuedAt->copy()->addDays(rand(7, 90)); // Tokens expire 7-90 days after issue
AuthenticationToken::create([
'user_id' => $user->id,
'application_id' => $app->id,
'token' => Str::random(64), // Simulate an OAuth token
'user_agent' => fake()->randomElement($userAgents),
'ip' => fake()->randomElement($ipAddresses),
'issued_at' => $issuedAt,
'expires_at' => $expiresAt,
'created_at' => $issuedAt,
'updated_at' => $issuedAt,
]);
}
}
$this->command->info("Created authentication tokens for {$user->email}");
}
}