1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Phisch\OAuth\Server; |
4
|
|
|
|
5
|
|
|
use Phisch\OAuth\Server\Exception\AuthorizationServerException; |
6
|
|
|
use Phisch\OAuth\Server\Grant\GrantInterface; |
7
|
|
|
use Phisch\OAuth\Server\Response\ResponseBuilderInterface; |
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
|
11
|
|
|
class Server |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var GrantInterface[] |
15
|
|
|
*/ |
16
|
|
|
private $grants = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var ResponseBuilderInterface |
20
|
|
|
*/ |
21
|
|
|
private $responseBuilder; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* AuthorizationServer constructor. |
25
|
|
|
* @param GrantInterface[] $grants |
26
|
|
|
* @param ResponseBuilderInterface $responseBuilder |
27
|
|
|
*/ |
28
|
|
|
public function __construct(array $grants, ResponseBuilderInterface $responseBuilder) |
29
|
|
|
{ |
30
|
|
|
$this->grants = $grants; |
31
|
|
|
$this->responseBuilder = $responseBuilder; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param Request $request |
36
|
|
|
* @return mixed |
37
|
|
|
*/ |
38
|
|
|
public function tokenEndpoint(Request $request) |
39
|
|
|
{ |
40
|
|
|
try { |
41
|
|
|
foreach ($this->grants as $grant) { |
42
|
|
|
if ($grant->supports($request)) { |
43
|
|
|
return $grant->handle($request, $this->responseBuilder); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
throw new AuthorizationServerException( |
47
|
|
|
'The requested grant_type is unsupported.', |
48
|
|
|
null, |
49
|
|
|
null, |
50
|
|
|
'unsupported_grant_type' |
51
|
|
|
); |
52
|
|
|
} catch (AuthorizationServerException $exception) { |
53
|
|
|
return $this->responseBuilder->error($exception); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param Request $request |
60
|
|
|
* @return Response|null |
61
|
|
|
*/ |
62
|
|
|
public function validateAuthorization(Request $request) |
63
|
|
|
{ |
64
|
|
|
try { |
65
|
|
|
$this->getGrant($request)->validate($request); |
66
|
|
|
} catch (AuthorizationServerException $exception) { |
67
|
|
|
return $this->responseBuilder->error($exception); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function handleAuthorization(Request $request) |
72
|
|
|
{ |
73
|
|
|
try { |
74
|
|
|
return $this->getGrant($request)->handle($request, $this->responseBuilder); |
75
|
|
|
} catch (AuthorizationServerException $exception) { |
76
|
|
|
return $this->responseBuilder->error($exception); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function cancelAuthorization(Request $request) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
//return $this->getGrant($request)->cancel($request, $this->responseBuilder); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $request |
87
|
|
|
* @return mixed|GrantInterface |
88
|
|
|
*/ |
89
|
|
|
private function getGrant($request) |
90
|
|
|
{ |
91
|
|
|
foreach ($this->grants as $grant) { |
92
|
|
|
if ($grant->supports($request)) { |
93
|
|
|
return $grant; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
// TODO: throw invalid grant exception |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.