Installation
Add the bundle to your composer.json:
composer require chrisns/gae-auth-bundle
Enable it in config/bundles.php:
return [
// ...
Chrisns\GaeAuthBundle\GaeAuthBundle::class => ['all' => true],
];
Configuration
Update config/packages/gae_auth.yaml (create if missing):
gae_auth:
enabled: true
service_account_key: '%env(GAE_SERVICE_ACCOUNT_KEY)%'
project_id: '%env(GAE_PROJECT_ID)%'
First Use Case Authenticate a user via Google App Engine (GAE) Identity Platform:
use Chrisns\GaeAuthBundle\Service\GaeAuthService;
$authService = $this->container->get(GaeAuthService::class);
$user = $authService->authenticate($request);
User Registration
Extend FOSUserBundle's registration flow to validate GAE tokens:
// src/EventListener/GaeAuthListener.php
use Chrisns\GaeAuthBundle\Event\GaeAuthEvent;
public function onRegistration(GaeAuthEvent $event) {
$token = $event->getToken();
if (!$this->validateGaeToken($token)) {
throw new \RuntimeException('Invalid GAE token');
}
}
Login Flow
Use the bundle’s GaeAuthListener to handle GAE auth in security.yaml:
security:
firewalls:
main:
pattern: ^/
stateless: true
anonymous: true
provider: fos_userbundle
entry_point: chrisns_gae_auth
User Sync
Sync GAE user data with your User entity:
$gaeUserData = $authService->fetchUserData($token);
$user->setGoogleId($gaeUserData['sub']);
$user->setEmail($gaeUserData['email']);
$user->save();
GaeAuthService::validateToken() for custom endpoints.GaeAuthListener:
gae_auth:
role_mapping:
'gae:admin' => 'ROLE_ADMIN'
Token Expiry
GAE tokens expire. Cache validated tokens (e.g., with Symfony\Component\Cache\Adapter\AdapterInterface):
$cache = $this->container->get('cache.app');
$token = $cache->get('gae_token_' . $userId);
Service Account Key
service_account_key. Use environment variables or Symfony’s %kernel.project_dir%/config/gae_key.json.identitytoolkit.GetAccountInfo scope.FOSUserBundle Conflict
FOSUserBundle, override its UserManager to integrate GAE logic:
$userManager->setGaeAuthService($authService);
config/packages/monolog.yaml:
handlers:
gae_auth:
type: stream
path: '%kernel.logs_dir%/gae_auth.log'
level: debug
Custom Claims
Extend GaeAuthService to handle custom GAE claims:
public function getCustomClaim($token, string $claimName) {
$decoded = $this->decodeToken($token);
return $decoded['custom_claims'][$claimName] ?? null;
}
Multi-Provider
Combine with other auth providers (e.g., OAuth) via a custom Authenticator:
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
public function supports(Request $request) {
return $request->headers->has('X-GAE-Token');
}
Webhook Validation For server-to-server auth, validate tokens via GAE’s Identity Toolkit API:
$client = new \Google_Client();
$client->setAuthConfig($serviceAccountKey);
$response = $client->getAccountInfo($token);
How can I help you explore Laravel packages today?