/symfony-skills:security-authenticationfrom symfony-skills
Configures Symfony Security: firewalls, authenticators, User entity, password hashing, access control, and voters. Activates on login, authentication, authorization, roles, or password mentions.
Ships in symfony-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: 'users')]
#[ORM\UniqueConstraint(name: 'uniq_users_email', fields: ['email'])]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id, ORM\Column(type: UuidType::NAME, unique: true)]
private Uuid $id;
#[ORM\Column(length: 180, unique: true)]
private string $email;
/** @var string[] */
#[ORM\Column]
private array $roles = [];
#[ORM\Column] // the *hash*, never the plaintext
private string $password;
public function __construct(string $email)
{
$this->id = Uuid::v7();
$this->email = $email;
}
public function getUserIdentifier(): string { return $this->email; }
/** @return string[] */
public function getRoles(): array
{
$roles = $this->roles;
$roles[] = 'ROLE_USER'; // everyone is at least ROLE_USER
return array_unique($roles);
}
public function getPassword(): string { return $this->password; }
public function setHashedPassword(string $hash): void { $this->password = $hash; }
public function eraseCredentials(): void { /* no plaintext held */ }
}
Use the hasher service; configure auto so Symfony picks the best algorithm (currently bcrypt/argon2) and can rehash on upgrade.
# config/packages/security.yaml
security:
password_hashers:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
// Hashing at registration
$user->setHashedPassword($this->passwordHasher->hashPassword($user, $plainPassword));
// ❌ NEVER
$user->setPassword(md5($plain)); // broken
$user->setPassword(hash('sha256', $plain)); // unsalted, fast = crackable
security:
providers:
app_user_provider:
entity: { class: App\Entity\User, property: email }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
api:
pattern: ^/api
stateless: true # APIs are stateless (token auth)
provider: app_user_provider
jwt: ~ # see jwt-authentication
main:
lazy: true
provider: app_user_provider
access_control:
- { path: ^/api/login, roles: PUBLIC_ACCESS }
- { path: ^/api/admin, roles: ROLE_ADMIN }
- { path: ^/api, roles: ROLE_USER }
access_control is coarse-grained (path + role). For per-object decisions, use voters.
final class ApiKeyAuthenticator extends AbstractAuthenticator
{
public function __construct(private readonly UserRepository $users) {}
public function supports(Request $request): ?bool
{
return $request->headers->has('X-API-KEY');
}
public function authenticate(Request $request): Passport
{
$apiKey = $request->headers->get('X-API-KEY');
if (!$apiKey) {
throw new CustomUserMessageAuthenticationException('No API key provided');
}
return new SelfValidatingPassport(
new UserBadge($apiKey, fn (string $key) => $this->users->findByApiKey($key)),
);
}
public function onAuthenticationFailure(Request $request, AuthenticationException $e): Response
{
return new JsonResponse(['title' => 'Authentication failed'], Response::HTTP_UNAUTHORIZED);
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
{
return null; // continue the request
}
}
Authorization that depends on the specific object (owner, tenant, state) belongs in a voter, not in access_control or scattered if checks.
/** @extends Voter<string, Order> */
final class OrderVoter extends Voter
{
public const VIEW = 'ORDER_VIEW';
public const CANCEL = 'ORDER_CANCEL';
protected function supports(string $attribute, mixed $subject): bool
{
return in_array($attribute, [self::VIEW, self::CANCEL], true) && $subject instanceof Order;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
return match ($attribute) {
self::VIEW => $subject->getCustomerEmail() === $user->getUserIdentifier(),
self::CANCEL => $subject->getCustomerEmail() === $user->getUserIdentifier()
&& $subject->getStatus() === OrderStatus::Pending,
default => false,
};
}
}
// In the controller / handler
$this->denyAccessUnlessGranted(OrderVoter::CANCEL, $order);
stateless: true for API firewalls — no session cookie.#[IsGranted] attribute or denyAccessUnlessGranted).$request for identity — read the authenticated user from the token / #[CurrentUser].md5/sha1/sha256 — use the password_hasher (auto).eraseCredentials() clears any plaintext.access_control or inline if — use a voter for object-level rules.stateless: true.#[CurrentUser].403 for unauthenticated requests (or 401 for forbidden) — 401 = who are you, 403 = not allowed.ROLE_USER default in getRoles() — every authenticated user should have it.form_login / the built-in authenticators, or json_login for APIs.What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub fatonh/symfony-skills --plugin symfony-skills17 skills in this plugin
/api-platform-resourcesUse when exposing data through API Platform 4 — defining #[ApiResource], operations, DTO resources, state providers/processors, serialization groups, filters, and pagination. Use when the task mentions API Platform, ApiResource, state processor, or exposing an entity as a REST/GraphQL endpoint.
/bundle-organizationUse when structuring a Symfony project's src/ directory, organizing code into modules, configuring services, or deciding where a class belongs. Covers the modular monolith layout, service configuration, and config/ conventions for Symfony 7 (Flex, no app bundle).
/console-commandsUse when creating or refactoring Symfony console commands — never extends Command. On Symfony 8.1+ put #[AsCommand] on a method of a service class (even for a single command); on 7.x use the invokable attribute class. Also covers typed CLI inputs via value resolvers and #[MapInput] DTOs. Use when the task mentions console command, #[AsCommand], bin/console, #[Argument], #[Option], or a CLI task.
/doctrine-migrationsUse when changing the database schema in Symfony — generating Doctrine migrations, writing reversible up()/down(), data migrations, and deploy-safe (zero-downtime) changes. Use when the task mentions migration, schema change, ALTER TABLE, or doctrine:migrations.
/doctrine-ormUse when creating Doctrine ORM 3 entities, associations, repositories, enums, or UUID identifiers in Symfony. Defines entity conventions, attribute mapping, fetch strategy, and repository structure. Use when the task mentions entity, Doctrine, repository, association, or mapping.
/doctrine-query-optimizationUse when writing Doctrine queries, fixing N+1 problems, paginating large result sets, building read models, or tuning DQL/query builder performance. Use when the task mentions N+1, slow query, pagination, DQL, hydration, or read model.
Other plugins' skills for the same job