Skip to content

Commit

Permalink
Merge pull request #11 from RobDWaller/1.0.0
Browse files Browse the repository at this point in the history
1.0.0
  • Loading branch information
RobDWaller authored Mar 9, 2020
2 parents 5f6f6c7 + ce19446 commit 09ca492
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ infection.log
coverage.xml
Dockerfile
docker-compose.yml
.phpunit.result.cache
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
language: php
php:
- 7.1
- 7.2
- 7.3
- 7.4
install:
- composer validate --strict
- travis_retry composer install --no-interaction --no-suggest --prefer-source --dev
script:
- composer pipeline
Expand Down
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

PSR-JWT is a middleware library which allows you to authorise JSON Web Tokens contained in a web request. It is [PSR-7](https://www.php-fig.org/psr/psr-7/) and [PSR-15](https://www.php-fig.org/psr/psr-15/) compliant and built on top of [ReallySimpleJWT](https://github.com/RobDWaller/ReallySimpleJWT).

The library also allows you to generate JSON Web Tokens and the PSR-7 PSR-15 compliant middleware can be added to any compatible framework, such as [Slim PHP](http://www.slimframework.com/).
The library also allows you to generate JSON Web Tokens and the PSR-7 / PSR-15 compliant middleware can be added to any compatible framework, such as [Slim PHP](http://www.slimframework.com/).

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 authorisation in [RFC 6750](https://tools.ietf.org/html/rfc6750).

Expand All @@ -14,7 +14,7 @@ For more information on JSON Web Tokens please read [RFC 7519](https://tools.iet
- [Slim PHP Example Implementation](#slim-php-example-implementation)
- [Generate JSON Web Token](#generate-json-web-token)
- [Parse and Validate JSON Web Token](#parse-and-validate-json-web-token)
- [Retrieve Token From the Request](retrieve-token-from-the-request)
- [Retrieve Token From Request](#retrieve-token-from-request)
- [Advanced Usage](#advanced-usage)
- [Handlers](#handlers)
- [Create Custom Handler](#create-custom-handler)
Expand Down Expand Up @@ -72,7 +72,7 @@ $app->get('/jwt', function (Request $request, Response $response) {
})->add(\PsrJwt\Factory\JwtMiddleware::html('Secret123!456$', 'jwt', 'Authorisation Failed'));
```

### Generate a JSON Web Token
### Generate JSON Web Token

To generate JSON Web Tokens PsrJwt offers a wrapper for the library [ReallySimpleJWT](https://github.com/RobDWaller/ReallySimpleJWT). You can create an instance of the ReallySimpleJWT builder by calling the built in factory method.

Expand Down Expand Up @@ -108,11 +108,11 @@ $parser->parse();

For more information on creating, parsing and validating tokens please read the [ReallySimpleJWT](https://github.com/RobDWaller/ReallySimpleJWT/blob/master/readme.md) documentation.

### Retrieve Token From the Request
### Retrieve Token From Request

If you would like to retrieve the JSON Web Token from the request outside of the normal middleware authorisation flow you can use the request helper class.

It allows you to retrive the token itself or just access the token's payload or header.
It allows you to retrieve the token itself or just access the token's payload or header.

```php
require 'vendor/autoload.php';
Expand Down Expand Up @@ -171,6 +171,7 @@ Next you will need to extend the `PsrJwt\Auth\Authorise` class as this will give
```php
// An example JWT Authorisation Handler.
use PsrJwt\Auth\Authorise;
use PsrJwt\JwtAuthMiddleware;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -196,6 +197,15 @@ class MyHandler extends Authorise implements RequestHandlerInterface
);
}
}

// Add Handler to Middleware.
$middleware = new JwtAuthMiddleware(new MyHandler('secret', 'token-key'));

// Add Middleware to Slim PHP route.
$app->get('/my/route', function (ServerRequestInterface $request, ResponseInterface $response) {
$response->getBody()->write("OK!");
return $response;
})->add($middleware);
```

## License
Expand Down
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@
}
],
"require": {
"php": ">=7.1.0",
"rbdwllr/reallysimplejwt": "^2.0",
"php": ">=7.2.0",
"rbdwllr/reallysimplejwt": "^3.0",
"psr/http-message": "^1.0",
"psr/http-server-middleware": "^1.0",
"nyholm/psr7": "^1.1"
"nyholm/psr7": "^1.2"
},
"require-dev": {
"phpunit/phpunit": "^7.0",
"phpunit/phpunit": "^8.0",
"phpstan/phpstan": "^0.11",
"phpstan/phpstan-mockery": "^0.11",
"phpmd/phpmd": "2.6.*",
"phpmd/phpmd": "^2.7",
"squizlabs/php_codesniffer": "^3.0",
"mockery/mockery": "^1.2",
"infection/infection": "^0.12.2",
"phploc/phploc": "^4.0",
"mockery/mockery": "^1.3",
"infection/infection": "^0.14",
"phploc/phploc": "^5.0",
"sebastian/phpcpd": "^4.0"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.5/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
Expand Down
2 changes: 2 additions & 0 deletions src/Factory/Jwt.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use ReallySimpleJWT\Validate;
use ReallySimpleJWT\Encode;
use ReallySimpleJWT\Parse;
use ReallySimpleJWT\Secret;
use ReallySimpleJWT\Jwt as RSJwt;

/**
Expand All @@ -28,6 +29,7 @@ public function builder(): Build
return new Build(
'JWT',
new Validate(),
new Secret(),
new Encode()
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Auth/AuthoriseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ public function testGetTokenNoToken()
$result = $method->invokeArgs($auth, [$request]);
}

public function tearDown()
public function tearDown(): void
{
m::close();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Handler/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public function testAuthoriseUnauthorized()
$this->assertSame('<h1>Fail!</h1>', $result->getBody()->__toString());
}

public function tearDown()
public function tearDown(): void
{
m::close();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/JwtAuthMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public function testInvokeFail()
$this->assertSame('Unauthorized: Signature is invalid.', $result->getReasonPhrase());
}

public function tearDown()
public function tearDown(): void
{
m::close();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Parser/BearerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testParseInvalid()
$this->assertEmpty($result);
}

public function tearDown()
public function tearDown(): void
{
m::close();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Parser/BodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function testParseBodyObjectNoObject()
$this->assertSame('', $result);
}

public function tearDown()
public function tearDown(): void
{
m::close();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Parser/CookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function testParse()
$this->assertSame('abc.def.ghi', $result);
}

public function tearDown()
public function tearDown(): void
{
m::close();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Parser/ParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function testFindTokenFail()
$this->assertEmpty($result);
}

public function tearDown()
public function tearDown(): void
{
m::close();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Parser/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function testParse()
$this->assertSame('abc.def.ghi', $result);
}

public function tearDown()
public function tearDown(): void
{
m::close();
}
Expand Down

0 comments on commit 09ca492

Please sign in to comment.