ecphp/api-gw-authentication-bundle
Installation
composer require ecphp/api-gw-authentication-bundle
Add to config/bundles.php:
return [
// ...
Ecphp\ApiGatewayAuthenticationBundle\EcphpApiGatewayAuthenticationBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console config:dump-reference EcphpApiGatewayAuthenticationBundle
Update config/packages/ecphp_api_gw_authentication.yaml with your API Gateway credentials (client ID, secret, and endpoint).
First Use Case
Protect a route with the api_gateway_auth firewall:
# config/packages/security.yaml
firewalls:
api:
pattern: ^/api
stateless: true
provider: api_gateway_auth
guard:
authenticators:
- Ecphp\ApiGatewayAuthenticationBundle\Security\ApiGatewayAuthenticator
Request Flow
ApiGatewayAuthenticator.Authorization header (format: Bearer <token>).User Provider Integration
Ecphp\ApiGatewayAuthenticationBundle\Security\User\ApiGatewayUserProvider to map API Gateway responses to Laravel/Symfony users:
class CustomUserProvider extends ApiGatewayUserProvider
{
public function loadUserByIdentifier($identifier): UserInterface
{
// Fetch user from your DB or external service
return new User($identifier, $this->getPasswordEncoder());
}
}
security.yaml:
providers:
api_gateway_auth:
id: Ecphp\ApiGatewayAuthenticationBundle\Security\User\CustomUserProvider
Token Refresh
ApiGatewayTokenManager to refresh tokens:
$tokenManager = $container->get('ecphp_api_gw_auth.token_manager');
$refreshedToken = $tokenManager->refreshToken($expiredToken);
loadUserByIdentifier method.Token Validation Failures
Ecphp\ApiGatewayAuthenticationBundle\Exception\TokenValidationException.Header Mismatches
Authorization header in the format Bearer <token>. Misconfigured proxies or load balancers may strip/modify headers.$request->headers->set('Authorization', $request->server->get('HTTP_AUTHORIZATION'));
User Provider Gaps
loadUserByIdentifier returns null, the authenticator fails silently.UserNotFoundException or log a warning:
if (null === $user) {
throw new UserNotFoundException('User not found for identifier: '.$identifier);
}
Enable Verbose Logging
Add to config/packages/monolog.yaml:
handlers:
api_gw:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.api_gw.log"
level: debug
channels: ["api_gateway_auth"]
Test Locally Use a mock API Gateway endpoint (e.g., Mockoon) to simulate responses during development.
Custom Token Validation
Override Ecphp\ApiGatewayAuthenticationBundle\Security\ApiGatewayAuthenticator to add logic:
protected function validateToken($token): bool
{
// Custom validation (e.g., check token issuer)
return parent::validateToken($token) && $this->isIssuerValid($token);
}
Event Listeners
Listen for api_gateway.auth.success and api_gateway.auth.failure events to log or modify responses:
// src/EventListener/AuthListener.php
public function onAuthSuccess(AuthSuccessEvent $event)
{
// Log or audit the authenticated user
}
Register in services.yaml:
services:
App\EventListener\AuthListener:
tags:
- { name: kernel.event_listener, event: api_gateway.auth.success, method: onAuthSuccess }
Configuration Overrides Dynamically override settings (e.g., token TTL) via environment variables:
# config/packages/ecphp_api_gw_authentication.yaml
ecphp_api_gw_authentication:
token_ttl: '%env(int:API_GW_TOKEN_TTL)%'
How can I help you explore Laravel packages today?