Skip to content

Commit

Permalink
Configurable response factory
Browse files Browse the repository at this point in the history
PSR-17 response factory can be provided in configuration
  • Loading branch information
dakujem committed Nov 25, 2019
1 parent 2257aac commit 329edfb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,18 @@ $app->add(new Tuupola\Middleware\JwtAuthentication([
]));
```

### Response Factory

A custom PSR-17 compatible response factory can be provided. If none is provided, PSR-17 implementation auto-discovery is used. Response factory is used to create a new 401 response.

```php
$app = new Slim\App;

$app->add(new Tuupola\Middleware\JwtAuthentication([
"responseFactory" => new MyResponseFactory,
]));
```

### Rules

The optional `rules` parameter allows you to pass in rules which define whether the request should be authenticated or not. A rule is a callable which receives the request as parameter. If any of the rules returns boolean `false` the request will not be authenticated.
Expand Down
17 changes: 13 additions & 4 deletions src/JwtAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@
use InvalidArgumentException;
use Exception;
use Firebase\JWT\JWT;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use RuntimeException;
use Tuupola\Middleware\DoublePassTrait;
use Tuupola\Http\Factory\ResponseFactory;
use Tuupola\Middleware\JwtAuthentication\RequestMethodRule;
use Tuupola\Middleware\JwtAuthentication\RequestPathRule;
Expand Down Expand Up @@ -85,7 +85,8 @@ final class JwtAuthentication implements MiddlewareInterface
"ignore" => null,
"before" => null,
"after" => null,
"error" => null
"error" => null,
"responseFactory" => null,
];

public function __construct(array $options = [])
Expand Down Expand Up @@ -139,7 +140,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
$token = $this->fetchToken($request);
$decoded = $this->decodeToken($token);
} catch (RuntimeException | DomainException $exception) {
$response = (new ResponseFactory)->createResponse(401);
$factory = $this->options['responseFactory'] ?? new ResponseFactory;
$response = $factory->createResponse(401);
return $this->processError($response, [
"message" => $exception->getMessage(),
"uri" => (string)$request->getUri()
Expand All @@ -158,7 +160,6 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface

/* Modify $request before calling next middleware. */
if (is_callable($this->options["before"])) {
$response = (new ResponseFactory)->createResponse(200);
$beforeRequest = $this->options["before"]($request, $params);
if ($beforeRequest instanceof ServerRequestInterface) {
$request = $beforeRequest;
Expand Down Expand Up @@ -452,4 +453,12 @@ private function rules(array $rules): void
$this->rules->push($callable);
}
}

/**
* Set the response factory.
*/
private function responseFactory(ResponseFactoryInterface $factory = null): void
{
$this->options["responseFactory"] = $factory;
}
}

0 comments on commit 329edfb

Please sign in to comment.