Installation
composer require coresite/apiauthbundle
Register Bundle
Add to app/AppKernel.php:
new CoreSite\APIAuthBundle\CoreSiteAPIAuthBundle(),
Basic Security Configuration
Configure app/config/security.yml:
firewalls:
api:
pattern: ^/api
stateless: true
simple_preauth:
authenticator: cs_apiauth_authenticator
provider: cs_apiauth_user_provider
First Use Case
/api_login_check) to authenticate users and return a token.Authorization: Bearer <token> header.Login Flow
/api_login_check with _username and _password.cs_apiauth_login and returns a token.Protected API Requests
/api/* must include the token in the Authorization header.cs_apiauth_authenticator validates the token and loads the user.User Provider Integration
CoreSite\APIAuthBundle\Security\User\APIUserProvider to fetch users from your data source (e.g., database, LDAP).services:
cs_apiauth_user_provider:
class: AppBundle\Security\User\CustomAPIUserProvider
arguments: [@doctrine.orm.entity_manager]
Token Storage
CoreSite\APIAuthBundle\Security\Authenticator\APIAuthenticator for custom token validation logic.cs_apiauth_user_handler_authentication_success to customize token generation or response formatting.security.yml or via a custom authenticator.Stateless Mode Conflicts
stateless: true). If misconfigured, token validation may fail.api firewall is stateless and api_login is anonymous.Token Storage Assumptions
public function getToken($credentials)
{
// Custom logic to fetch token from a database or cache
}
User Provider Mismatch
cs_apiauth_user_provider must return an object implementing CoreSite\APIAuthBundle\Security\User\APIUserInterface.bin/console debug:security
CSRF Protection
api_login:
pattern: ^/api/login
stateless: true
anonymous: ~
csrf_protection: true
Token Validation Errors
Authentication failure messages. Common causes:
Bearer <token>).# app/config/config.yml
framework:
router:
debug: "%kernel.debug%"
Configuration Overrides
config.yml:
cs_apiauth:
token_ttl: 3600 # Token time-to-live in seconds
token_storage: session # or 'database'/'cache'
Custom Token Format
Override CoreSite\APIAuthBundle\Security\Token\APIAuthToken to support JWT or other formats.
Multi-Tenant Support Extend the user provider to include tenant IDs in tokens:
public function loadUserByToken($token)
{
$tenantId = $this->extractTenantIdFromToken($token);
// Load user with tenant context
}
Rate Limiting
Integrate with nelmio/cors-bundle or lexik/jwt-authentication-bundle for rate limiting on token endpoints.
How can I help you explore Laravel packages today?