uecode/api-key-bundle
A Symfony bundle that adds API key authentication to your application, letting you generate, store, and validate keys for users or clients without building the plumbing yourself.
This is a Symfony 2 bundle, not a Composer-installable Laravel package. A Laravel dev can't composer require it into an app. Treat it as reference material for building API-key auth, or use it only inside a legacy Symfony 2 codebase.
Minimum steps to stand it up in its native environment:
friendsofsymfony/user-bundle (FOSUserBundle), and PHP ~5.5+. The last release was 2016-01-14, so anything newer (Symfony 3+/4+/5+) will fight the composer constraints.composer require uecode/api-key-bundle
app/AppKernel.php:
new Uecode\Bundle\ApiKeyBundle\UecodeApiKeyBundle(),
ApiKey entity mapping a key string to a FOSUser user (Doctrine annotations/YAML), and generate a migration.# app/config/config.yml
uecode_api_key:
api_key_entity: AppBundle\Entity\ApiKey
# header defaults to X-Api-Key
security:
firewalls:
api:
pattern: ^/api
stateless: true
uecode_api_key: true
ApiKey row tied to a user, and call it with curl -H "X-Api-Key: <key>" https://app/api/me.First place to look: the bundle's DependencyInjection/ config and its security-listener class. That's where the header name, provider wiring, and lookup behavior live — everything else is Symfony security boilerplate.
$key = bin2hex(random_bytes(32));
$apiKey = new ApiKey();
$apiKey->setKey(hash('sha256', $key)); // store a hash
$apiKey->setUser($user);
$em->persist($apiKey); $em->flush();
// return $key once to the caller
ApiKey with ManyToOne to User, plus label, lastUsedAt, revokedAt, expiresAt. This is how you support rotation and revocation — the bundle won't do it for you.uecode_api_key firewall with stateless: true, and never enable form_login on the same firewall.scopes column and enforce it in a kernel.request listener or voter rather than in the bundle.curl -i http://localhost:8000/api/orders -H "X-Api-Key: $KEY"
Assert 200 with a valid key, 401 with garbage, and that revoked keys are rejected.api_keys table: id, user_id, name, key_hash, last_used_at, expires_at, revoked_at.AuthenticateApiKey middleware that reads X-Api-Key, hashes it, looks up key_hash, checks expiry/revocation, and Auth::login($user).createToken) if you just need personal access tokens.X-Api-Key by default), but the listener runs early in the firewall chain. If auth silently fails, check: (1) the firewall pattern matches the URL, (2) the firewall is marked stateless, (3) the user provider points at the right entity/property, (4) the security listener priority hasn't been displaced by another bundle.security.firewalls.api logging, drop a temporary listener at kernel.request to dump the resolved user, and verify the Doctrine query actually hits an ApiKey row. Most 401s are a misconfigured provider property or a trailing \r\n in the header from a sloppy client.ApiKey entity and the security factory if you need custom lookup. Everything else (header name, provider) is config. There's very little surface area, which is why the bundle is small — and why you'll be writing most of the meaningful logic yourself.How can I help you explore Laravel packages today?