Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/belgattitude/soluble-wallit
PSR-15 JWT Middleware
https://github.com/belgattitude/soluble-wallit
expressive jwt psr-15 psr-7
Last synced: about 2 months ago
JSON representation
PSR-15 JWT Middleware
- Host: GitHub
- URL: https://github.com/belgattitude/soluble-wallit
- Owner: belgattitude
- License: mit
- Created: 2017-05-07T21:28:00.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-10-26T15:02:18.000Z (over 6 years ago)
- Last Synced: 2024-10-24T17:50:57.421Z (3 months ago)
- Topics: expressive, jwt, psr-15, psr-7
- Language: PHP
- Homepage:
- Size: 146 KB
- Stars: 1
- Watchers: 4
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# soluble-wallit
[![PHP Version](http://img.shields.io/badge/php-7.1+-ff69b4.svg)](https://packagist.org/packages/soluble/wallit)
[![Build Status](https://travis-ci.org/belgattitude/soluble-wallit.svg?branch=master)](https://travis-ci.org/belgattitude/soluble-wallit)
[![codecov](https://codecov.io/gh/belgattitude/soluble-wallit/branch/master/graph/badge.svg)](https://codecov.io/gh/belgattitude/soluble-wallit)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/belgattitude/soluble-wallit/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/belgattitude/soluble-wallit/?branch=master)
[![Latest Stable Version](https://poser.pugx.org/soluble/wallit/v/stable.svg)](https://packagist.org/packages/soluble/wallit)
[![Total Downloads](https://poser.pugx.org/soluble/wallit/downloads.png)](https://packagist.org/packages/soluble/wallit)
[![License](https://poser.pugx.org/soluble/wallit/license.png)](https://packagist.org/packages/soluble/wallit)PSR-15 Middleware for dealing with JWT generation and checks.
Status: **Experimental**. Not yet used in production, if interested take a look at the issues
and feel welcome to make suggestions or open P/R :)## Requirements
* PHP 7.1
## Recommended
* [zend-expressive 3.0](https://github.com/zendframework/zend-expressive) (or any PSR-7 & PSR-15 compatible framework)
> For zend-expressive 2.0 use the 0.3 release.
## Install
```bash
$ composer require soluble-wallit
```## Configure
Copy [soluble-wallit.config.php.dist](https://github.com/belgattitude/soluble-wallit/blob/master/config/soluble-wallit.config.php.dist) in your autoload directory.
```bash
cp ./vendor/soluble/wallit/config/soluble-wallit.config.php.dist ./config/autoload/soluble-wallit.config.local.php
```Edit the config file and add your token keys
## Register (zend-expressive 3.0)
Ensure `Soluble\Wallit\Config\ConfigProvider::class` is registered in the `./config/config.php` file.
```php
'data/config-cache.php',
];$aggregator = new ConfigAggregator([
new ArrayProvider($cacheConfig),
// Register the Soluble Wallit ConfigProvider
Soluble\Wallit\Config\ConfigProvider::class,
new PhpFileProvider('config/autoload/{{,*.}global,{,*.}local}.php'),
new PhpFileProvider('config/development.config.php'),
], $cacheConfig['config_cache_path']);return $aggregator->getMergedConfig();
```## Usage (zend-expressive 3.0)
To quickly browse the examples, see the [smoke tests directory](https://github.com/belgattitude/soluble-wallit/tree/master/tests/server/expressive/src/App/Handler).
### Example 1
Create a PSR-15 handler to generate a JWT token upon successful authentication:
```php
jwtService = $jwtService;
}public function handle(ServerRequestInterface $request): ResponseInterface
{
$method = $request->getMethod();
if ($method !== 'POST') {
throw new \RuntimeException('TODO - Handle error your way ;)');
}$body = $request->getParsedBody();
$login = $body['login'] ?? '';
$password = $body['password'] ?? '';if ($login === 'demo' && $password === 'demo') {
$token = $this->jwtService->createToken([
JwtClaims::ID => Uuid::uuid1(),
'login' => $login
]);return new JsonResponse([
'access_token' => (string) $token,
'token_type' => 'example',
]);
}return (new JsonResponse([
'success' => false
]))->withStatus(StatusCodeInterface::STATUS_UNAUTHORIZED);
}
}```
Its related factory can be:
```php
get(JwtService::class)
);
}
}
```Add a route in `./config/routes.php`
```php
post('/auth', App\Handler\AuthHandler::class, 'auth');
```
### Example 2: Check JWTSimply pipe or add the `JwtAuthMiddleware::class` to the desired route.
As an example in the `./config/routes.php` file :
```php
get('/admin', [
JwtAuthMiddleware::class,
App\Handler\AdminHandler::class
], 'admin');```
### Example 3: Retrive the token
The token is available as a request attribute: `$request->getAttribute(JwtAuthMiddleware::class)`.
```php
template = $template;
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
$token = $this->getTokenFromRequest($request);
return new HtmlResponse($this->template->render('pages::admin', [
'token' => $token,
'login' => $token->getClaim('login')
]));
}
protected function getTokenFromRequest(ServerRequestInterface $request): Token
{
return $request->getAttribute(JwtAuthMiddleware::class);
}
}
```
## Standards* [fig/http-message-util](https://github.com/php-fig/http-message-util) Utility classes and constants for use with PSR-7 (psr/http-message)
* [psr/http-message](http://www.php-fig.org/psr/psr-7/) Common interface for HTTP messages (PHP FIG PSR-7)
* [psr/container](http://www.php-fig.org/psr/psr-11/) Common Container Interface (PHP FIG PSR-11)
* [PSR 4 Autoloader](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md)
* [PSR 2 Coding Style Guide](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)