forked from gothinkster/slim-php-realworld-example-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptionalAuth.php
47 lines (39 loc) · 1.26 KB
/
OptionalAuth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
namespace Conduit\Middleware;
use Interop\Container\ContainerInterface;
use Slim\DeferredCallable;
class OptionalAuth
{
/**
* @var \Interop\Container\ContainerInterface
*/
private $container;
/**
* OptionalAuth constructor.
*
* @param \Interop\Container\ContainerInterface $container
*
* @internal param \Slim\Middleware\JwtAuthentication $jwt
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
/**
* OptionalAuth middleware invokable class to verify JWT token when present in Request
*
* @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request
* @param \Psr\Http\Message\ResponseInterface $response PSR7 response
* @param callable $next Next middleware
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function __invoke($request, $response, $next)
{
if ($request->hasHeader('HTTP_AUTHORIZATION')) {
$callable = new DeferredCallable($this->container->get('jwt'), $this->container);
return call_user_func($callable, $request, $response, $next);
}
return $next($request, $response);
}
}