1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SocialConnect project |
4
|
|
|
* @author: Patsura Dmitry https://github.com/ovr <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace SocialConnect\OAuth2\Provider; |
8
|
|
|
|
9
|
|
|
use SocialConnect\Common\Http\Client\Client; |
10
|
|
|
use SocialConnect\Provider\AccessTokenInterface; |
11
|
|
|
use SocialConnect\Provider\Exception\InvalidAccessToken; |
12
|
|
|
use SocialConnect\Provider\Exception\InvalidResponse; |
13
|
|
|
use SocialConnect\OAuth2\AccessToken; |
14
|
|
|
use SocialConnect\Common\Entity\User; |
15
|
|
|
use SocialConnect\Common\Hydrator\ObjectMap; |
16
|
|
|
|
17
|
|
|
class WordPress extends \SocialConnect\OAuth2\AbstractProvider |
18
|
|
|
{ |
19
|
|
|
const NAME = 'wordpress'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
3 |
|
public function getBaseUri() |
25
|
|
|
{ |
26
|
3 |
|
return 'https://public-api.wordpress.com/rest/v1/'; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
2 |
|
public function getAuthorizeUri() |
33
|
|
|
{ |
34
|
2 |
|
return 'https://public-api.wordpress.com/oauth2/authorize'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
2 |
|
public function getRequestTokenUri() |
41
|
|
|
{ |
42
|
2 |
|
return 'https://public-api.wordpress.com/oauth2/token'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
3 |
|
public function getName() |
49
|
|
|
{ |
50
|
3 |
|
return self::NAME; |
51
|
|
|
} |
52
|
|
|
|
53
|
3 |
|
public function parseToken($body) |
54
|
|
|
{ |
55
|
3 |
|
if (empty($body)) { |
56
|
|
|
throw new InvalidAccessToken('Provider response with empty body'); |
57
|
|
|
} |
58
|
|
|
|
59
|
3 |
|
$result = json_decode($body, true); |
60
|
3 |
|
if ($result) { |
61
|
1 |
|
return new AccessToken($result); |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
throw new InvalidAccessToken('Server response with not valid/empty JSON'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
2 |
|
public function getIdentity(AccessTokenInterface $accessToken) |
72
|
|
|
{ |
73
|
2 |
|
$response = $this->httpClient->request( |
74
|
2 |
|
$this->getBaseUri() . 'me/', |
75
|
2 |
|
[], |
76
|
2 |
|
Client::GET, |
77
|
|
|
[ |
78
|
2 |
|
'Authorization' => "Bearer {$accessToken->getToken()}" |
79
|
|
|
] |
80
|
|
|
); |
81
|
|
|
|
82
|
2 |
|
if (!$response->isSuccess()) { |
83
|
1 |
|
throw new InvalidResponse( |
84
|
1 |
|
'API response with error code', |
85
|
1 |
|
$response |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
$result = $response->json(); |
90
|
1 |
|
if (!$result) { |
91
|
1 |
|
throw new InvalidResponse( |
92
|
1 |
|
'API response is not a valid JSON object', |
93
|
1 |
|
$response |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$hydrator = new ObjectMap( |
98
|
|
|
[ |
99
|
|
|
'ID' => 'id', |
100
|
|
|
'avatar_URL' => 'pictureURL', |
101
|
|
|
] |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
return $hydrator->hydrate(new User(), $result); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|