-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from RobDWaller/0.1.0-alpha1
0.1.0 alpha1
- Loading branch information
Showing
32 changed files
with
1,143 additions
and
783 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,64 @@ | ||
# PSR-JWT | ||
[![Build Status](https://travis-ci.org/RobDWaller/psr-jwt.svg?branch=master)](https://travis-ci.org/RobDWaller/psr-jwt) [![codecov](https://codecov.io/gh/RobDWaller/psr-jwt/branch/master/graph/badge.svg)](https://codecov.io/gh/RobDWaller/psr-jwt) | ||
|
||
A PSR-7 and PSR-15 compliant JSON Web Token Middleware Library. | ||
A PSR-7 and PSR-15 compliant JSON Web Token Middleware Library. Currently in alpha and built on top of [ReallySimpleJWT](https://github.com/RobDWaller/ReallySimpleJWT). | ||
|
||
Currently in alpha and built on top of [ReallySimpleJWT](https://github.com/RobDWaller/ReallySimpleJWT). | ||
The library allows you to create JSON Web Tokens and then validate them using PSR-15 compliant middleware which can be added to compatible frameworks. | ||
|
||
For more information on JSON Web Tokens please read [RFC 7519](https://tools.ietf.org/html/rfc7519). Also to learn more about how to pass JSON Web Tokens to web applications please read up on bearer token authorization in [RFC 6750](https://tools.ietf.org/html/rfc6750). | ||
For more information on JSON Web Tokens please read [RFC 7519](https://tools.ietf.org/html/rfc7519). Also to learn more about how to pass JSON Web Tokens to web applications please read up on bearer token authorization in [RFC 6750](https://tools.ietf.org/html/rfc6750). | ||
|
||
## Setup | ||
|
||
Via Composer on the command line: | ||
|
||
```bash | ||
composer require rbdwllr/psr-jwt | ||
``` | ||
|
||
Via composer.json: | ||
|
||
```javascript | ||
"require": { | ||
"rbdwllr/psr-jwt": "^0.1" | ||
} | ||
``` | ||
|
||
## Usage | ||
|
||
PSR-JWT can be used with any PSR-7 / PSR-15 compliant framework. Just call the middleware factory method and it will return a middleware instance that exposes two methods, `__invoke()` and `process()`. The later will work with PSR-15 compliant frameworks like Zend Expressive and the former will work with older PSR-7 compliant frameworks like Slim PHP v3. | ||
|
||
```php | ||
\PsrJwt\Factory\JwtAuth::middleware('tokenKey', 'secret'); | ||
``` | ||
|
||
The `tokenKey` is the key required to retrieve the JSON Web Token from a cookie, query parameter or the request body. By default though the library looks for tokens in bearer field of the authorization header. | ||
|
||
The `secret` is the string required to hash the JSON Web Token signature. | ||
|
||
### Slim PHP 3.0 Example Implementation | ||
|
||
```php | ||
// Can be added to any routes file in Slim, often index.php. | ||
$app->get('/jwt', function (Request $request, Response $response) { | ||
$response->getBody()->write("JSON Web Token is Valid!"); | ||
|
||
return $response; | ||
})->add(\PsrJwt\Factory\JwtAuth::middleware('jwt', 'Secret123!456$')); | ||
``` | ||
|
||
### Zend Expressive Example Implementation | ||
|
||
```php | ||
// Add to the config/pipeline.php file. | ||
$app->pipe('/api', \PsrJwt\Factory\JwtAuth::middleware('jwt', '!secReT$123*')); | ||
``` | ||
|
||
### Generate JSON Web Token | ||
|
||
To generate JSON Web Tokens PsrJwt offers a wrapper for the library ReallySimpleJWT. You can create an instance of the ReallySimpleJWT builder by calling the built in factory method. | ||
|
||
```php | ||
\PsrJwt\Factory\Jwt::builder(); | ||
``` | ||
|
||
For more information on creating tokens please read the ReallySimpleJWT documentation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
includes: | ||
- vendor/phpstan/phpstan-mockery/extension.neon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PsrJwt\Auth; | ||
|
||
class Auth | ||
{ | ||
private $code; | ||
|
||
private $message; | ||
|
||
public function __construct(int $code, string $message) | ||
{ | ||
$this->code = $code; | ||
|
||
$this->message = $message; | ||
} | ||
|
||
public function getCode(): int | ||
{ | ||
return $this->code; | ||
} | ||
|
||
public function getMessage(): string | ||
{ | ||
return $this->message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PsrJwt\Factory; | ||
|
||
use PsrJwt\JwtAuthMiddleware; | ||
use PsrJwt\Auth\Authenticate; | ||
use PsrJwt\JwtAuthInvokable; | ||
|
||
class JwtAuth | ||
{ | ||
public static function middleware(string $tokenKey, string $secret): JwtAuthMiddleware | ||
{ | ||
$auth = new Authenticate($tokenKey, $secret); | ||
|
||
return new JwtAuthMiddleware($auth); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.