1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\ApiBundle\Controller; |
13
|
|
|
|
14
|
|
|
use FOS\RestBundle\Decoder\DecoderProviderInterface; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
17
|
|
|
use OAuth2\OAuth2; |
18
|
|
|
use OAuth2\OAuth2ServerException; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @see \FOS\OAuthServerBundle\Controller\TokenController |
24
|
|
|
* @see \FOS\RestBundle\EventListener\BodyListener |
25
|
|
|
* |
26
|
|
|
* @author Łukasz Chruściel <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class TokenController |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var OAuth2 |
32
|
|
|
*/ |
33
|
|
|
private $server; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var DecoderProviderInterface |
37
|
|
|
*/ |
38
|
|
|
private $decoderProvider; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param OAuth2 $server |
42
|
|
|
* @param DecoderProviderInterface $decoderProvider |
43
|
|
|
*/ |
44
|
|
|
public function __construct(OAuth2 $server, DecoderProviderInterface $decoderProvider) |
45
|
|
|
{ |
46
|
|
|
$this->server = $server; |
47
|
|
|
$this->decoderProvider = $decoderProvider; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Request $request |
52
|
|
|
* |
53
|
|
|
* @return Response |
54
|
|
|
*/ |
55
|
|
|
public function tokenAction(Request $request) |
56
|
|
|
{ |
57
|
|
|
$this->convertContentToRequestParameters($request); |
58
|
|
|
|
59
|
|
|
try { |
60
|
|
|
return $this->server->grantAccessToken($request); |
61
|
|
|
} catch (OAuth2ServerException $e) { |
62
|
|
|
return $e->getHttpResponse(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Additional action to keep default behaviour of FosRestBundle. Main aim is to turn off data normalization. |
68
|
|
|
* |
69
|
|
|
* @param Request $request |
70
|
|
|
*/ |
71
|
|
|
private function convertContentToRequestParameters(Request $request) |
72
|
|
|
{ |
73
|
|
|
$format = $request->getFormat($request->headers->get('Content-Type', '')) ?: $request->getRequestFormat(); |
74
|
|
|
$content = $request->getContent(); |
75
|
|
|
|
76
|
|
|
if (empty($content)) { |
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (!$this->decoderProvider->supports($format)) { |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$decoder = $this->decoderProvider->getDecoder($format); |
85
|
|
|
$data = $decoder->decode($content); |
|
|
|
|
86
|
|
|
|
87
|
|
|
if (!is_array($data)) { |
88
|
|
|
throw new BadRequestHttpException(sprintf('Invalid %s message received', $format)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$request->request = new ParameterBag($data); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.