webthink / guzzle-jwt
This package contains useful classes and interfaces for requiring a JWT token
Installs: 25 905
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 2
Forks: 1
Open Issues: 1
Requires
- php: >=7.1
- ext-json: *
- guzzlehttp/guzzle: 6.*
Requires (Dev)
- phpunit/phpunit: ^7.5
- symfony/cache: 3.* | 4.*
- symfony/http-foundation: 3.* | 4.*
- webthink/codesniffer: ^2.0
This package is auto-updated.
Last update: 2022-03-23 09:42:01 UTC
README
Goal of this package is to provide a set of useful classes in order to use a JWT authentication with Guzzle.
Install
You can install the package using composer
composer require webthink/guzzle-jwt
Usage
Basic Usage
As a first step you need to implement your own Authenticator.
class MyAuthenticator implements AuthenticatorInterface { public function authenticate($username, $password) { // code.. and return a TokenInterface. } }
Then you need to append into your handler stack the middleware provided by the package.
// Your handler stack $stack = HandlerStack::create(); // Add middleware $stack->append(new Middleware($myAuthenticator)); $httpClient = new Client([ 'handler' => $stack, 'jwt' => [ 'username' => 'username', 'password' => 'password', ], ]); $response = $httClient->get('/my_api_that_requires_jwt_token');
NOTE
I will not proceed with creating classes that implement the AuthenticatorInterface
. The AuthenticatorInterface
can be implemented by the developer who uses the package.
Storages
Storages are a way of caching JWT between multiple requests. Depending on the storage the token can for using it for more than one HTTP Request.
In order to use the storage you will need either to implement an Authenticator of your own that will use the storage
or use the StoreAuthenticator
$myAuthenticator = new MyAuthenticator(); $storeAuthenticator = new StoreAuthenticator($myAuthenticator, new MemoryStorage());
Then you can pass the store authenticator in the middleware and follow the procedure described above.
Contributing
Feel free to comment on anything you might believe it's going in the wrong direction or even better contribute with a PR.
Todo
- Increase UnitTests.
- Investigate the possibility of including some authenticators.
Credits
License
The MIT License (MIT). Please see License File for more information.