All Projects → gsomoza → oauth2-middleware

gsomoza / oauth2-middleware

Licence: MIT license
PSR7 middleware that uses league/oauth2-client to authenticate outgoing requests

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to oauth2-middleware

Ganesha
🐘 A Circuit Breaker pattern implementation for PHP applications.
Stars: ✭ 384 (+1728.57%)
Mutual labels:  guzzle
Guzzle Jsonrpc
JSON-RPC 2.0 client for Guzzle
Stars: ✭ 87 (+314.29%)
Mutual labels:  guzzle
Guzzle Swoole
让基于 Guzzle 的项目完美无缝兼容 Swoole 协程,支持:Guzzle、Elasticsearch client——来自宇润 PHP 全家桶
Stars: ✭ 143 (+580.95%)
Mutual labels:  guzzle
Guzzle
Guzzle, an extensible PHP HTTP client
Stars: ✭ 21,384 (+101728.57%)
Mutual labels:  guzzle
Bof
The HTTP client for humans
Stars: ✭ 76 (+261.9%)
Mutual labels:  guzzle
Guzzle Rate Limiter Middleware
A rate limiter middleware for Guzzle
Stars: ✭ 94 (+347.62%)
Mutual labels:  guzzle
Sapient
Secure API Toolkit
Stars: ✭ 308 (+1366.67%)
Mutual labels:  guzzle
Guzzle Services
Provides an implementation of the Guzzle Command library that uses Guzzle service descriptions to describe web services, serialize requests, and parse responses into easy to use model structures.
Stars: ✭ 226 (+976.19%)
Mutual labels:  guzzle
Async Soap Guzzle
An asynchronous SOAP client build on top of Guzzle.
Stars: ✭ 78 (+271.43%)
Mutual labels:  guzzle
Snorlax
A lightweight REST client that gives you full control of your resources
Stars: ✭ 129 (+514.29%)
Mutual labels:  guzzle
Guzzlette
🌀 Guzzle integration into Nette Framework (@nette)
Stars: ✭ 19 (-9.52%)
Mutual labels:  guzzle
Phlack
Slack Integrations for PHP
Stars: ✭ 53 (+152.38%)
Mutual labels:  guzzle
Microservices With Lumen
A Lumen based microservice ready to deploy with guzzle for consumption of api and OAuth 2
Stars: ✭ 115 (+447.62%)
Mutual labels:  guzzle
Eightpointsguzzlebundle
⛽️ Integrates Guzzle 6.x, a PHP HTTP Client, into Symfony
Stars: ✭ 407 (+1838.1%)
Mutual labels:  guzzle
Crawler
An easy to use, powerful crawler implemented in PHP. Can execute Javascript.
Stars: ✭ 2,055 (+9685.71%)
Mutual labels:  guzzle
Guzzle Cache Middleware
A HTTP Cache for Guzzle 6. It's a simple Middleware to be added in the HandlerStack.
Stars: ✭ 325 (+1447.62%)
Mutual labels:  guzzle
Guzzle retry middleware
Middleware for Guzzle v6+ that automatically retries HTTP requests on 429, 503 responses.
Stars: ✭ 90 (+328.57%)
Mutual labels:  guzzle
php-server-sdk
LaunchDarkly Server-side SDK for PHP
Stars: ✭ 31 (+47.62%)
Mutual labels:  guzzle
Yurunhttp
YurunHttp 是开源的 PHP HTTP 客户端,支持链式操作,简单易用。完美支持Curl、Swoole 协程。QQ群:17916227
Stars: ✭ 197 (+838.1%)
Mutual labels:  guzzle
Guzzle Advanced Throttle
A Guzzle middleware that can throttle requests according to (multiple) defined rules. It is also possible to define a caching strategy, e.g. get the response from cache when the rate limit is exceeded or always get a cached value to spare your rate limits. Using wildcards in host names is also supported.
Stars: ✭ 120 (+471.43%)
Mutual labels:  guzzle

OAuth2 client middleware for league/oauth2-client

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version

Author License

PSR7 middleware that uses league/oauth2-client to authenticate requests with an OAuth2 server.

Installation

composer require somoza/oauth2-client-middleware

Usage

The current implementation indirectly depends on Guzzle 6 because it's a direct dependency of league/oauth2-client.

Using Guzzle:

use Somoza\OAuth2Middleware\OAuth2Middleware;
use Somoza\OAuth2Middleware\TokenService\Bearer;

$stack = new \GuzzleHttp\HandlerStack();
$stack->setHandler(new CurlHandler());
$client = new \GuzzleHttp\Client(['handler' => $stack]);

// instantiate a provider, see league/oauth2-client docs
$provider = new GenericProvider(
    [
        'clientId' => 'your_client_id',
        'clientSecret' => 'your_client_secret',
        'urlAuthorize' => 'your_authorization_url',
        'urlAccessToken' => 'your_access_token_url',
        'urlResourceOwnerDetails' => 'your_resource_owner_url', 
    ], 
    [ 'httpClient' => $client ] // or don't pass it and let the oauth2-client create its own Guzzle client
);

// attach our oauth2 middleware
$bearerMiddleware = new OAuth2Middleware(
    new Bearer($provider), // use the Bearer token type
    [ // ignore (do not attempt to authorize) the following URLs
        $provider->getBaseAuthorizationUrl(),
        $provider->getBaseAccessTokenUrl(),
    ]
);
$stack->push($bearerMiddleware);

// if you want to debug, it might be useful to attach a PSR7 logger here

Caching the Access Token

A callback can be assigned to the middleware in order to save the access token for future use. Make sure you know about the security implications of storing an access token (do it at your own risk).

Example:

use Somoza\OAuth2Middleware\OAuth2Middleware;
use Somoza\OAuth2Middleware\TokenService\Bearer;
use League\OAuth2\Client\Token\AccessToken;

// see previous example for initialization
$tokenStore = new EncryptedCache(); // you can use whatever you want here
$token = null;
if ($tokenStore->contains($userId)) {
    $tokenData = json_decode($cache->fetch($userId));
    $token = new AccessToken($tokenData);
}

$bearerMiddleware = new OAuth2Middleware(
    new Bearer(
        $provider, // defined as in the "Usage" example
        $token, 
        function (AccessToken $newToken, AccessToken $oldToken) 
          use ($tokenStore, $userId) {
            // called whenever a new AccessToken is fetched
            $tokenStore->save($userId, $newToken->jsonSerialize());
        }
    ), 
);

$stack->push($bearerMiddleware);

License

MIT - see LICENSE.md

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].