google/auth
Official Google Auth library for PHP. Implements OAuth 2.0 and Application Default Credentials (ADC) for authenticating to Google APIs and Google Cloud. Install via Composer and use with HTTP clients like Guzzle to authorize API calls.
HttpClient facade or GuzzleHttp\Client).HttpClient facade (v9+) via custom middleware or stack manipulation.GoogleAuth::drive()) for cleaner Laravel syntax.| Risk Area | Mitigation Strategy |
|---|---|
| Guzzle Version Mismatch | Laravel 9+ uses Guzzle 7; package supports Guzzle 6/7. Test compatibility early. |
| Credential Management | Store GOOGLE_APPLICATION_CREDENTIALS in Laravel’s .env or Vault (avoid hardcoding). |
| Token Refresh Logic | Use package’s built-in caching (PSR-6) to avoid manual token management. |
| IAP/Cloud Run Scenarios | Validate Proxy-Authorization headers in middleware (see getProxyIdTokenMiddleware). |
| Deprecation Risk | Monitor Google’s PHP client deprecations. |
.env, Workload Identity Federation, or IAP?quotaProject)?false?ApplicationDefaultCredentials for unit tests (use Mockery or Google\Auth\Testbed).HttpClient (Guzzle 7) and Illuminate\Support\Facades\Http.Phase 1: Core Integration
composer require google/auth
bootstrap/app.php or a Service Provider:
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . env('GOOGLE_CREDENTIALS_PATH'));
// app/Http/Middleware/GoogleAuthMiddleware.php
public function handle(Request $request, Closure $next) {
$stack = HandlerStack::create();
$stack->push(ApplicationDefaultCredentials::getMiddleware(['https://www.googleapis.com/auth/drive']));
$client = new Client(['handler' => $stack]);
// Use $client for requests...
}
HttpClient with a custom stack:
$client = Http::withOptions([
'handler' => fn () => HandlerStack::create()->push(
ApplicationDefaultCredentials::getMiddleware(['scope'])
),
]);
Phase 2: Advanced Features
ServiceAccountCredentials.getProxyIdTokenMiddleware for protected endpoints.$cache = Cache::store('redis')->getStore();
$credentials = ApplicationDefaultCredentials::getCredentials(['scope'], cache: $cache);
Phase 3: Laravel-Specific Abstractions
// app/Facades/GoogleAuth.php
public static function drive() {
return Http::withOptions([...])->get('drive/v3/files');
}
php artisan google:auth:refresh).| Component | Compatibility Notes |
|---|---|
| Laravel HTTP Client | Works with Http::macro() or custom middleware. |
| Lumen | Same approach; use GuzzleHttp\Client directly. |
| Laravel Forge/Vapor | Store credentials in environment variables or secrets. |
| Livewire/Inertia | Use for backend API calls (not frontend auth). |
| Queues/Jobs | ADC works in queues; ensure credentials are accessible in worker environment. |
roles/storage.objectAdmin).GOOGLE_APPLICATION_CREDENTIALS.Google\Auth\Testbed.google/auth for breaking changes (e.g., Guzzle 7+ features).Google_Auth_Exception).Proxy-Authorization headers.Google\Auth\AccessToken::verify() to debug JWTs.$client->getEmitter()->attach(new \GuzzleHttp\Middleware::tap(function ($request) {
\Log::debug($request);
}));
google-auth-library-php.FileSystemCacheItemPool) suffices.GOOGLE_APPLICATION_CREDENTIALS is set per instance.429 responses (use Guzzle’s retry middleware).| Failure Scenario | Mitigation |
|---|---|
| Credential file missing | Validate GOOGLE_APPLICATION_CREDENTIALS in AppServiceProvider. |
| Token refresh failure | Retry with jitter; log errors to Sentry/ |
How can I help you explore Laravel packages today?