1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Alexandre |
5
|
|
|
* Date: 18/02/2018 |
6
|
|
|
* Time: 18:08 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace OAuth2\Flows; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use OAuth2\Endpoints\AuthorizationEndpoint; |
13
|
|
|
use OAuth2\Endpoints\TokenEndpoint; |
14
|
|
|
use OAuth2\Storages\AccessTokenStorageInterface; |
15
|
|
|
|
16
|
|
|
class ImplicitFlow implements FlowInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var AccessTokenStorageInterface |
20
|
|
|
*/ |
21
|
|
|
private $accessTokenStorage; |
22
|
|
|
|
23
|
|
|
public function __construct(AccessTokenStorageInterface $accessTokenStorage) |
24
|
|
|
{ |
25
|
|
|
$this->accessTokenStorage = $accessTokenStorage; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
function getResponseTypes(): array |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
return ['token']; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
function handleAuthorizationRequest(AuthorizationEndpoint $authorizationEndpoint, array $requestData): array |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$accessToken = $this->accessTokenStorage->generate( |
36
|
|
|
implode(' ', $authorizationEndpoint->getScopes()), |
37
|
|
|
$authorizationEndpoint->getClient()->getIdentifier(), |
38
|
|
|
$authorizationEndpoint->getResourceOwner()->getIdentifier() |
39
|
|
|
); |
40
|
|
|
$data = [ |
41
|
|
|
'access_token' => $accessToken->getToken(), |
42
|
|
|
'token_type' => $accessToken->getType(), |
43
|
|
|
]; |
44
|
|
|
$lifetime = $this->accessTokenStorage->getLifetime(); |
45
|
|
|
if (!is_null($lifetime)) { |
46
|
|
|
$data['expires_in'] = $lifetime; |
47
|
|
|
} |
48
|
|
|
if (!$this->arrayEqual($authorizationEndpoint->getScopes(), $requestData['scope'] ?? null)) { |
49
|
|
|
$data['scope'] = implode(' ', $authorizationEndpoint->getScopes()); |
50
|
|
|
} |
51
|
|
|
return $data; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
function getDefaultResponseMode(): string |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
return 'fragment'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
function getUnsupportedResponseModes(): array |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
return ['query']; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function arrayEqual($a, $b) |
65
|
|
|
{ |
66
|
|
|
return ( |
67
|
|
|
is_array($a) |
68
|
|
|
&& is_array($b) |
69
|
|
|
&& count($a) == count($b) |
70
|
|
|
&& array_diff($a, $b) === array_diff($b, $a) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
function getGrantTypes(): array |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
return []; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
function handleAccessTokenRequest(TokenEndpoint $tokenEndpoint, array $requestData): array |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
return []; |
82
|
|
|
} |
83
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.