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 AuthorizationServerMiddleware |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var \League\OAuth2\Server\AuthorizationServer |
21
|
|
|
*/ |
22
|
|
|
private $server; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* AuthorizationServerMiddleware constructor. |
26
|
|
|
* |
27
|
|
|
* @param \League\OAuth2\Server\AuthorizationServer $server |
28
|
|
|
*/ |
29
|
|
|
public function __construct(AuthorizationServer $server) |
30
|
|
|
{ |
31
|
|
|
$this->server = $server; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request |
36
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
37
|
|
|
* @param callable $next |
38
|
|
|
* |
39
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
40
|
|
|
*/ |
41
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
42
|
|
|
{ |
43
|
|
|
try { |
44
|
|
|
$response = $this->server->respondToAccessTokenRequest($request, $response); |
45
|
|
|
} catch (OAuthServerException $exception) { |
46
|
|
|
return $exception->generateHttpResponse($response); |
47
|
|
|
// @codeCoverageIgnoreStart |
48
|
|
|
} catch (\Exception $exception) { |
49
|
|
|
$response->getBody()->write($exception->getMessage()); |
50
|
|
|
|
51
|
|
|
return $response->withStatus(500); |
52
|
|
|
// @codeCoverageIgnoreEnd |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Pass the request and response on to the next responder in the chain |
56
|
|
|
return $next($request, $response); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|