-
Notifications
You must be signed in to change notification settings - Fork 0
/
GrantTypeMiddleware.php
52 lines (44 loc) · 1.77 KB
/
GrantTypeMiddleware.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
48
49
50
51
52
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2019 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace OAuth2Framework\Component\TokenEndpoint;
use function Safe\sprintf;
use OAuth2Framework\Component\Core\Message\OAuth2Error;
use OAuth2Framework\Component\Core\Util\RequestBodyParser;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
final class GrantTypeMiddleware implements MiddlewareInterface
{
private GrantTypeManager $grantTypeManager;
public function __construct(GrantTypeManager $grantTypeManager)
{
$this->grantTypeManager = $grantTypeManager;
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
try {
$parameters = RequestBodyParser::parseFormUrlEncoded($request);
if (!\array_key_exists('grant_type', $parameters)) {
throw new \InvalidArgumentException('The "grant_type" parameter is missing.');
}
$grant_type = $parameters['grant_type'];
if (!$this->grantTypeManager->has($grant_type)) {
throw new \InvalidArgumentException(sprintf('The grant type "%s" is not supported by this server.', $grant_type));
}
$type = $this->grantTypeManager->get($grant_type);
$request = $request->withAttribute('grant_type', $type);
return $handler->handle($request);
} catch (\InvalidArgumentException $e) {
throw OAuth2Error::invalidRequest($e->getMessage(), [], $e);
}
}
}