diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index e0e7a3ba5..57ccab4ae 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -29,6 +29,7 @@ public function run(): void DisableTwoStepConfirmationSeeder::class, SentinelSeeder::class, CaSslCertSeeder::class, + PersonalAccessTokenSeeder::class, ]); } } diff --git a/database/seeders/PersonalAccessTokenSeeder.php b/database/seeders/PersonalAccessTokenSeeder.php new file mode 100644 index 000000000..38a45219c --- /dev/null +++ b/database/seeders/PersonalAccessTokenSeeder.php @@ -0,0 +1,115 @@ +environment('production')) { + $this->command->warn('Skipping PersonalAccessTokenSeeder in production environment'); + + return; + } + + // Get the first user (usually the admin user created during setup) + $user = User::find(0); + + if (! $user) { + $this->command->warn('No user found. Please run UserSeeder first.'); + + return; + } + + // Get the user's first team + $team = $user->teams()->first(); + + if (! $team) { + $this->command->warn('No team found for user. Cannot create API tokens.'); + + return; + } + + // Define test tokens with different scopes + $testTokens = [ + [ + 'name' => 'Development Root Token', + 'token' => 'root', + 'abilities' => ['root'], + ], + [ + 'name' => 'Development Read Token', + 'token' => 'read', + 'abilities' => ['read'], + ], + [ + 'name' => 'Development Read Sensitive Token', + 'token' => 'read-sensitive', + 'abilities' => ['read', 'read:sensitive'], + ], + [ + 'name' => 'Development Write Token', + 'token' => 'write', + 'abilities' => ['write'], + ], + [ + 'name' => 'Development Write Sensitive Token', + 'token' => 'write-sensitive', + 'abilities' => ['write', 'write:sensitive'], + ], + [ + 'name' => 'Development Deploy Token', + 'token' => 'deploy', + 'abilities' => ['deploy'], + ], + ]; + + // First, remove all existing development tokens for this user + $deletedCount = PersonalAccessToken::where('tokenable_id', $user->id) + ->where('tokenable_type', get_class($user)) + ->whereIn('name', array_column($testTokens, 'name')) + ->delete(); + + if ($deletedCount > 0) { + $this->command->info("Removed {$deletedCount} existing development token(s)."); + } + + // Now create fresh tokens + foreach ($testTokens as $tokenData) { + // Create the token with a simple format: Bearer {scope} + // The token format in the database is the hash of the plain text token + $plainTextToken = $tokenData['token']; + + PersonalAccessToken::create([ + 'tokenable_type' => get_class($user), + 'tokenable_id' => $user->id, + 'name' => $tokenData['name'], + 'token' => hash('sha256', $plainTextToken), + 'abilities' => $tokenData['abilities'], + 'team_id' => $team->id, + ]); + + $this->command->info("Created token '{$tokenData['name']}' with Bearer token: {$plainTextToken}"); + } + + $this->command->info(''); + $this->command->info('Test API tokens created successfully!'); + $this->command->info('You can use these tokens in development as:'); + $this->command->info(' Bearer root - Root access'); + $this->command->info(' Bearer read - Read only access'); + $this->command->info(' Bearer read-sensitive - Read with sensitive data access'); + $this->command->info(' Bearer write - Write access'); + $this->command->info(' Bearer write-sensitive - Write with sensitive data access'); + $this->command->info(' Bearer deploy - Deploy access'); + } +}