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\Storages\AccessTokenStorageInterface; |
14
|
|
|
|
15
|
|
|
class ImplicitFlow implements FlowInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var AccessTokenStorageInterface |
19
|
|
|
*/ |
20
|
|
|
private $accessTokenStorage; |
21
|
|
|
|
22
|
|
|
public function __construct(AccessTokenStorageInterface $accessTokenStorage) |
23
|
|
|
{ |
24
|
|
|
$this->accessTokenStorage = $accessTokenStorage; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
function getResponseTypes(): array |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
return ['token']; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
function handleAuthorizationRequest(AuthorizationEndpoint $authorizationEndpoint, array $requestData): array |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$accessToken = $this->accessTokenStorage->generate( |
35
|
|
|
implode(' ', $authorizationEndpoint->getScopes()), |
36
|
|
|
$authorizationEndpoint->getClient()->getIdentifier(), |
37
|
|
|
$authorizationEndpoint->getResourceOwner()->getIdentifier() |
38
|
|
|
); |
39
|
|
|
$data = [ |
40
|
|
|
'access_token' => $accessToken->getToken(), |
41
|
|
|
'token_type' => $accessToken->getType(), |
42
|
|
|
]; |
43
|
|
|
$lifetime = $this->accessTokenStorage->getLifetime(); |
44
|
|
|
if (!is_null($lifetime)) { |
45
|
|
|
$data['expires_in'] = $lifetime; |
46
|
|
|
} |
47
|
|
|
if (!$this->arrayEqual($authorizationEndpoint->getScopes(), $requestData['scope'] ?? null)) { |
48
|
|
|
$data['scope'] = implode(' ', $authorizationEndpoint->getScopes()); |
49
|
|
|
} |
50
|
|
|
return $data; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
function getDefaultResponseMode(): string |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
return 'fragment'; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
function getUnsupportedResponseModes(): array |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
return ['query']; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function arrayEqual($a, $b) |
64
|
|
|
{ |
65
|
|
|
return ( |
66
|
|
|
is_array($a) |
67
|
|
|
&& is_array($b) |
68
|
|
|
&& count($a) == count($b) |
69
|
|
|
&& array_diff($a, $b) === array_diff($b, $a) |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
} |
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.