dormilich/http-oauth
PSR-compatible OAuth2 Client Credentials extension for dormilich/http-client. Automatically fetches and caches access tokens via a token client/provider, then adds Authorization headers to outgoing requests. Works with PSR-18/17 HTTP clients and PSR-16 cache.
Pros:
Cons:
SimpleCache), which may require additional setup (e.g., Redis, database) for production-grade token storage.GuzzleHttp\Client (PSR-18) and Illuminate\Http\Client can wrap dormilich/http-client, but may require adapter shims for PSR-17 factories.Illuminate\Cache (PSR-16 compatible) can replace SimpleCache with minimal config.TokenClient, TokenProvider, and credential providers, reducing boilerplate.dormilich/http-client integrates smoothly (may need middleware adapters).dormilich/http-client coexist with Laravel’s HttpClient without conflicts?DomainProvider) integrate with Laravel’s routing?| Component | Laravel Equivalent | Integration Notes |
|---|---|---|
| PSR-18 HTTP Client | GuzzleHttp\Client or HttpClient |
Use Laravel’s HttpClient facade or wrap Guzzle. |
| PSR-17 Factories | Symfony\Contracts\HttpClient |
May need adapter (e.g., GuzzleHttp\Psr7). |
| PSR-16 Cache | Illuminate\Cache |
Replace SimpleCache with Cache::store(). |
| Service Container | Laravel’s DI | Bind TokenClient, TokenProvider as singletons. |
HttpClient (PSR-18) + dormilich/http-client as a middleware layer.Illuminate\Cache) for low-latency token storage.Log facade to track token refreshes/errors.Phase 1: Proof of Concept
dormilich/http-oauth:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(TokenClient::class, fn() => new TokenClient(
new DefaultProvider(new ClientCredentials(env('CLIENT_ID'), env('CLIENT_SECRET'), env('AUTH_URL'))),
new GuzzleHttp\Client(),
new GuzzleHttp\Psr7\HttpFactory(),
new GuzzleHttp\Psr7\StreamFactory(),
Cache::store('redis')
));
}
Phase 2: Credential Provider Expansion
DomainProvider for multi-API support:
$domainProvider = new DomainProvider();
$domainProvider->add($credentials, ['api.example.com', 'api.staging.example.com']);
$chainProvider = new ChainProvider([$domainProvider, new DefaultProvider($fallbackCredentials)]);
Phase 3: Error Handling & Observability
// app/Http/Middleware/OAuthRefreshMiddleware.php
public function handle($request, Closure $next)
{
try {
return $next($request);
} catch (OAuthException $e) {
app(TokenProvider::class)->refresh();
return $next($request);
}
}
Log::channel('oauth').GuzzleHttp\Client for broader compatibility with dormilich/http-client.HttpClient, create a PSR-18 adapter (e.g., SymfonyHttpClientAdapter).HttpClient or Guzzle for PSR-18 compliance.dormilich/http-oauth and dependencies.TokenClient/TokenProvider to Laravel’s container.dormilich/http-client may lag behind Laravel/Guzzle updates.Log::debug() to inspect token payloads and cache hits/misses.DomainProvider to verify domain matching.How can I help you explore Laravel packages today?