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