oauth2-server/src/Middleware/AuthenticationServerMiddleware.php

56 lines
1.6 KiB
PHP
Raw Normal View History

2016-01-17 23:10:26 +05:30
<?php
namespace League\OAuth2\Server\Middleware;
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Server;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
2016-02-12 18:49:47 +05:30
use Zend\Diactoros\Stream;
2016-01-17 23:10:26 +05:30
class AuthenticationServerMiddleware
{
/**
* @var \League\OAuth2\Server\Server
*/
private $server;
/**
* AuthenticationServerMiddleware constructor.
*
* @param \League\OAuth2\Server\Server $server
*/
public function __construct(Server $server)
{
$this->server = $server;
}
/**
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Psr\Http\Message\ResponseInterface $response
* @param callable $next
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
try {
2016-01-20 04:46:12 +05:30
$response = $this->server->respondToRequest($request, $response);
2016-01-17 23:10:26 +05:30
} catch (OAuthServerException $exception) {
return $exception->generateHttpResponse($response);
} catch (\Exception $exception) {
2016-02-12 18:49:47 +05:30
$body = new Stream('php://temp', 'r+');
$body->write($exception->getMessage());
2016-01-20 15:28:45 +05:30
2016-02-12 18:49:47 +05:30
return $response->withStatus(500)->withBody($body);
2016-01-17 23:10:26 +05:30
}
if (in_array($response->getStatusCode(), [400, 401, 500])) {
return $response;
}
// Pass the request and response on to the next responder in the chain
return $next($request, $response);
}
}