1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SocialConnect project |
4
|
|
|
* @author: Patsura Dmitry https://github.com/ovr <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace SocialConnect\OAuth2; |
8
|
|
|
|
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use SocialConnect\Auth\AbstractBaseProvider; |
11
|
|
|
use SocialConnect\Auth\AccessTokenInterface; |
12
|
|
|
use SocialConnect\Auth\Provider\Exception\InvalidAccessToken; |
13
|
|
|
use SocialConnect\Auth\Provider\Exception\InvalidResponse; |
14
|
|
|
use SocialConnect\Common\Entity\User; |
15
|
|
|
use SocialConnect\Common\Http\Client\Client; |
16
|
|
|
|
17
|
|
|
abstract class AbstractProvider extends AbstractBaseProvider |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* HTTP method for access token request |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $requestHttpMethod = Client::POST; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
abstract public function getAuthorizeUri(); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
abstract public function getRequestTokenUri(); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Default parameters for auth url, can be redeclared inside implementation of the Provider |
38
|
|
|
* |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
1 |
|
public function getAuthUrlParameters() |
42
|
|
|
{ |
43
|
|
|
return array( |
44
|
1 |
|
'client_id' => $this->consumer->getKey(), |
45
|
1 |
|
'redirect_uri' => $this->getRedirectUrl(), |
46
|
1 |
|
'response_type' => 'code', |
47
|
1 |
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
1 |
|
public function makeAuthUrl() |
54
|
|
|
{ |
55
|
1 |
|
$urlParameters = $this->getAuthUrlParameters(); |
56
|
|
|
|
57
|
1 |
|
if (count($this->scope) > 0) { |
58
|
|
|
$urlParameters['scope'] = $this->getScopeInline(); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
if (count($this->fields) > 0) { |
62
|
|
|
$urlParameters['fields'] = $this->getFieldsInline(); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
return $this->getAuthorizeUri() . '?' . http_build_query($urlParameters); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Parse access token from response's $body |
70
|
|
|
* |
71
|
|
|
* @param string|bool $body |
72
|
|
|
* @return AccessToken |
73
|
|
|
* @throws InvalidAccessToken |
74
|
|
|
*/ |
75
|
|
|
public function parseToken($body) |
76
|
|
|
{ |
77
|
|
|
if (empty($body)) { |
78
|
|
|
throw new InvalidAccessToken('Provider response with empty body'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
parse_str($body, $token); |
82
|
|
|
|
83
|
|
|
if (!is_array($token) || !isset($token['access_token'])) { |
84
|
|
|
throw new InvalidAccessToken('Provider API returned an unexpected response'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return new AccessToken($token); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $code |
92
|
|
|
* @return AccessToken |
93
|
|
|
* @throws InvalidResponse |
94
|
|
|
*/ |
95
|
1 |
|
public function getAccessToken($code) |
96
|
|
|
{ |
97
|
1 |
|
if (!is_string($code)) { |
98
|
1 |
|
throw new InvalidArgumentException('Parameter $code must be a string'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$parameters = array( |
102
|
|
|
'client_id' => $this->consumer->getKey(), |
103
|
|
|
'client_secret' => $this->consumer->getSecret(), |
104
|
|
|
'code' => $code, |
105
|
|
|
'grant_type' => 'authorization_code', |
106
|
|
|
'redirect_uri' => $this->getRedirectUrl() |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
$response = $this->service->getHttpClient()->request( |
110
|
|
|
$this->getRequestTokenUri(), |
111
|
|
|
$parameters, |
112
|
|
|
$this->requestHttpMethod, |
113
|
|
|
[ |
114
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded' |
115
|
|
|
] |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
if (!$response->isSuccess()) { |
119
|
|
|
throw new InvalidResponse( |
120
|
|
|
'API response with error code', |
121
|
|
|
$response |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$body = $response->getBody(); |
126
|
|
|
return $this->parseToken($body); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param array $parameters |
132
|
|
|
* @return AccessToken |
133
|
|
|
*/ |
134
|
|
|
public function getAccessTokenByRequestParameters(array $parameters) |
135
|
|
|
{ |
136
|
|
|
return $this->getAccessToken($parameters['code']); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|