|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Alex Bilbie <[email protected]> |
|
4
|
|
|
* @copyright Copyright (c) Alex Bilbie |
|
5
|
|
|
* @license http://mit-license.org/ |
|
6
|
|
|
* |
|
7
|
|
|
* @link https://github.com/thephpleague/oauth2-server |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace League\OAuth2\Server\Middleware; |
|
11
|
|
|
|
|
12
|
|
|
use League\OAuth2\Server\AuthorizationServer; |
|
13
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException; |
|
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
15
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
16
|
|
|
|
|
17
|
|
|
class Psr7AuthorizationServerMiddleware |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var AuthorizationServer |
|
21
|
|
|
*/ |
|
22
|
|
|
private $server; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param AuthorizationServer $server |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(AuthorizationServer $server) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->server = $server; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param ServerRequestInterface $request |
|
34
|
|
|
* @param ResponseInterface $response |
|
35
|
|
|
* @param callable $next |
|
36
|
|
|
* |
|
37
|
|
|
* @return ResponseInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
|
40
|
|
|
{ |
|
41
|
|
|
try { |
|
42
|
|
|
$response = $this->server->respondToAccessTokenRequest($request, $response); |
|
43
|
|
|
} catch (OAuthServerException $exception) { |
|
44
|
|
|
return $exception->generateHttpResponse($response); |
|
45
|
|
|
// @codeCoverageIgnoreStart |
|
46
|
|
|
} catch (\Exception $exception) { |
|
47
|
|
|
return (new OAuthServerException($exception->getMessage(), 0, 'unknown_error', 500)) |
|
48
|
|
|
->generateHttpResponse($response); |
|
49
|
|
|
// @codeCoverageIgnoreEnd |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// Pass the request and response on to the next responder in the chain |
|
53
|
|
|
return $next($request, $response); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|