google/auth
Official Google Auth library for PHP. Implements OAuth 2.0 and Application Default Credentials (ADC) to authenticate and authorize requests to Google APIs. Designed for Composer installs and use across local, server, and Google Cloud environments.
ApplicationDefaultCredentials (ADC) pattern simplifies credential management across environments (local, GCP, hybrid cloud).AuthTokenMiddleware, AccessToken) are decoupled, allowing selective adoption (e.g., only JWT verification without full ADC).Illuminate\Cache) out of the box (PSR-6 support exists but needs manual bridging).UserRefreshCredentials.| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| Credential Leakage | High | Validate external JSON keys; use environment variables (GOOGLE_APPLICATION_CREDENTIALS). |
| Token Expiry Handling | Medium | Implement retry logic with exponential backoff for 401 Unauthorized. |
| Guzzle Version Lock | Medium | Pin Guzzle ^7.0 in composer.json to avoid breaking changes. |
| Cache Inconsistency | Low | Use FileSystemCacheItemPool with proper locking (semaphore support exists). |
| Deprecation Risk | Low | Monitor Google’s PHP client library roadmap. |
drive) or fine-grained scopes (e.g., drive.readonly)? This impacts token caching and revocation handling.GOOGLE_APPLICATION_CREDENTIALS).Monolog would need integration.Illuminate\Support\Facades\Http). The library’s middleware integrates seamlessly.symfony/cache, predis) work but require manual binding to Laravel’s cache manager.composer require google/auth
ApplicationDefaultCredentials in API services.// app/Providers/AuthServiceProvider.php
public function register()
{
$this->app->singleton(\Google\Auth\CredentialsInterface::class, function () {
return \Google\Auth\ApplicationDefaultCredentials::getCredentials([
'https://www.googleapis.com/auth/drive.readonly'
]);
});
}
$cache = new \Google\Auth\Cache\RedisCacheItemPool(
new \Predis\Client(['scheme' => 'tcp', 'host' => 'redis'])
);
$credentials = ApplicationDefaultCredentials::getCredentials($scopes, cache: $cache);
GuzzleHttp\RetryMiddleware).AccessToken::verify() for user auth (e.g., Firebase Auth).| Component | Laravel Version | Notes |
|---|---|---|
| Guzzle 7+ | 9.x+ | Laravel 8.x may need Guzzle 6 polyfill. |
| PHP 8.0+ | 8.0+ | Library drops PHP 5.4–5.5 support. |
| PSR-6 Cache | Any | Requires manual Laravel cache adapter. |
| Google API PHP Client | Optional | Use this library instead of google/apiclient. |
GOOGLE_APPLICATION_CREDENTIALS for local testing.MemoryCacheItemPool for dev caching.AuthTokenMiddleware hooks).401 Unauthorized errors.GOOGLE_APPLICATION_CREDENTIALS and restart workers.google/auth for breaking changes (e.g., Guzzle 7+ requirements).Log facade.$credentials = ApplicationDefaultCredentials::getCredentials($scopes);
$credentials->onAuthTokenCreated(function ($token) {
\Log::debug('Google Auth Token Refreshed', ['scopes' => $scopes]);
});
InvalidCredentials: Verify GOOGLE_APPLICATION_CREDENTIALS path or JSON key validity.TokenExpired: Check cache TTL or network connectivity to Google’s token endpoint.google/auth’s AccessToken::verify() to validate tokens.$client->getEmitter()->attach(new \GuzzleHttp\Middleware::tap(function ($request) {
\Log::debug('Google API Request', ['url' => $request->getUri()]);
}));
| Failure Scenario | Impact
How can I help you explore Laravel packages today?