1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mechpave\ImgurClient\Authorization; |
4
|
|
|
|
5
|
|
|
use Mechpave\ImgurClient\Http\HttpClientInterface; |
6
|
|
|
use Mechpave\ImgurClient\ImgurClient; |
7
|
|
|
use Mechpave\ImgurClient\Mapper\TokenMapper; |
8
|
|
|
use Mechpave\ImgurClient\Model\AuthorizationModel; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class AuthorizationHandler |
13
|
|
|
* @package Mechpave\ImgurClient\Authorization |
14
|
|
|
*/ |
15
|
|
|
class AuthorizationHandler implements AuthorizationHandlerInterface |
16
|
|
|
{ |
17
|
|
|
use ContainerAwareTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ImgurClient |
21
|
|
|
*/ |
22
|
|
|
protected $client; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var HttpClientInterface |
26
|
|
|
*/ |
27
|
|
|
protected $httpClient; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var TokenMapper |
31
|
|
|
*/ |
32
|
|
|
protected $tokenMapper; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* AuthorizationHandler constructor. |
36
|
|
|
* @param ImgurClient $client |
37
|
|
|
* @param HttpClientInterface $httpClient |
38
|
|
|
* @param TokenMapper $tokenMapper |
39
|
|
|
*/ |
40
|
8 |
|
public function __construct( |
41
|
|
|
ImgurClient $client, |
42
|
|
|
HttpClientInterface $httpClient, |
43
|
|
|
TokenMapper $tokenMapper) |
44
|
|
|
{ |
45
|
8 |
|
$this->client = $client; |
46
|
8 |
|
$this->httpClient = $httpClient; |
47
|
8 |
|
$this->tokenMapper = $tokenMapper; |
48
|
8 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
*/ |
53
|
1 |
|
public function getAuthorizationUrl( |
54
|
|
|
$responseType = AuthorizationModel::RESPONSE_CODE, |
55
|
|
|
$state = null |
56
|
|
|
) { |
57
|
1 |
|
$uri = AuthorizationModel::ENDPOINT_BASE . AuthorizationModel::ENDPOINT_AUTHORIZATION; |
58
|
|
|
|
59
|
|
|
$queryParams = [ |
60
|
1 |
|
'client_id' => $this->client->getClientId(), |
61
|
1 |
|
'response_type' => $responseType, |
62
|
|
|
]; |
63
|
|
|
|
64
|
1 |
|
if ($state) { |
|
|
|
|
65
|
1 |
|
$queryParams['state'] = $state; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
$uri .= '?' . http_build_query($queryParams); |
69
|
|
|
|
70
|
1 |
|
return $uri; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritdoc |
75
|
|
|
*/ |
76
|
2 |
|
public function requestAccessToken( |
77
|
|
|
$code, |
78
|
|
|
$grantType = AuthorizationModel::GRANT_CODE |
79
|
|
|
) { |
80
|
|
|
$postData = [ |
81
|
2 |
|
'client_id' => $this->client->getClientId(), |
82
|
2 |
|
'client_secret' => $this->client->getClientSecret(), |
83
|
2 |
|
'grant_type' => $grantType, |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
switch ($grantType) { |
87
|
2 |
|
case AuthorizationModel::GRANT_CODE: |
88
|
1 |
|
$postData['code'] = $code; |
89
|
1 |
|
break; |
90
|
1 |
|
case AuthorizationModel::GRANT_PIN: |
91
|
|
|
$postData['pin'] = $code; |
92
|
|
|
break; |
93
|
|
|
default: |
94
|
1 |
|
throw new \InvalidArgumentException('Invalid grant_type provided'); |
95
|
|
|
break; |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
$response = $this->httpClient->post( |
99
|
1 |
|
AuthorizationModel::ENDPOINT_BASE . AuthorizationModel::ENDPOINT_TOKEN, |
100
|
|
|
$postData |
101
|
|
|
); |
102
|
|
|
|
103
|
1 |
|
return $this->tokenMapper->buildToken($response->getBody()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritdoc |
108
|
|
|
*/ |
109
|
2 |
|
public function refreshToken() |
110
|
|
|
{ |
111
|
2 |
|
if (!$this->client->getToken()) { |
112
|
1 |
|
throw new \Exception('Token is not set'); |
113
|
|
|
} |
114
|
1 |
|
$response = $this->httpClient->post( |
115
|
1 |
|
AuthorizationModel::ENDPOINT_BASE . AuthorizationModel::ENDPOINT_TOKEN, |
116
|
|
|
[ |
117
|
1 |
|
'refresh_token' => $this->client->getToken()->getRefreshToken(), |
118
|
1 |
|
'client_id' => $this->client->getClientId(), |
119
|
1 |
|
'client_secret' => $this->client->getClientSecret(), |
120
|
1 |
|
'grant_type' => AuthorizationModel::GRANT_REFRESH, |
121
|
|
|
] |
122
|
|
|
); |
123
|
|
|
|
124
|
1 |
|
return $this->tokenMapper->buildToken($response->getBody()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @inheritdoc |
129
|
|
|
* @throws \Exception |
130
|
|
|
*/ |
131
|
3 |
|
public function checkTokenExpired() |
132
|
|
|
{ |
133
|
3 |
|
$token = $this->client->getToken(); |
134
|
|
|
|
135
|
3 |
|
if (!$token) { |
136
|
1 |
|
throw new \Exception('Token is not set'); |
137
|
|
|
} |
138
|
|
|
|
139
|
2 |
|
return $token->getExpiresAt() < time(); |
140
|
|
|
} |
141
|
|
|
} |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: