oauth2-server/src/Middleware/AuthorizationServerMiddleware.php

58 lines
1.8 KiB
PHP
Raw Normal View History

2016-01-17 23:10:26 +05:30
<?php
2016-04-17 17:36:05 +05:30
/**
* @author Alex Bilbie <hello@alexbilbie.com>
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
*
* @link https://github.com/thephpleague/oauth2-server
*/
2016-01-17 23:10:26 +05:30
namespace League\OAuth2\Server\Middleware;
2016-04-17 17:24:25 +05:30
use League\OAuth2\Server\AuthorizationServer;
2016-04-17 17:36:17 +05:30
use League\OAuth2\Server\Exception\OAuthServerException;
2016-01-17 23:10:26 +05:30
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
2016-04-17 17:24:25 +05:30
class AuthorizationServerMiddleware
2016-01-17 23:10:26 +05:30
{
/**
2016-04-17 17:24:25 +05:30
* @var \League\OAuth2\Server\AuthorizationServer
2016-01-17 23:10:26 +05:30
*/
private $server;
/**
2016-04-17 17:24:25 +05:30
* AuthorizationServerMiddleware constructor.
2016-01-17 23:10:26 +05:30
*
2016-04-17 17:24:25 +05:30
* @param \League\OAuth2\Server\AuthorizationServer $server
2016-01-17 23:10:26 +05:30
*/
2016-04-17 17:24:25 +05:30
public function __construct(AuthorizationServer $server)
2016-01-17 23:10:26 +05:30
{
$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-04-09 20:52:22 +05:30
$response = $this->server->respondToAccessTokenRequest($request, $response);
2016-01-17 23:10:26 +05:30
} catch (OAuthServerException $exception) {
return $exception->generateHttpResponse($response);
2016-03-10 22:52:10 +05:30
// @codeCoverageIgnoreStart
2016-01-17 23:10:26 +05:30
} catch (\Exception $exception) {
2016-05-11 17:43:58 +05:30
return (new OAuthServerException($exception->getMessage(), 0, 'unknown_error', 500))
->generateHttpResponse($response);
2016-03-10 22:52:10 +05:30
// @codeCoverageIgnoreEnd
2016-01-17 23:10:26 +05:30
}
// Pass the request and response on to the next responder in the chain
return $next($request, $response);
}
}