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\OAuth2\AccessToken; |
10
|
|
|
use SocialConnect\Provider\AccessTokenInterface; |
11
|
|
|
use SocialConnect\Provider\Exception\InvalidAccessToken; |
12
|
|
|
use SocialConnect\Provider\Exception\InvalidResponse; |
13
|
|
|
use SocialConnect\Common\Entity\User; |
14
|
|
|
use SocialConnect\Common\Http\Client\Client; |
15
|
|
|
use SocialConnect\Common\Hydrator\ObjectMap; |
16
|
|
|
|
17
|
|
|
class Facebook extends \SocialConnect\OAuth2\AbstractProvider |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* By default AbstractProvider use POST method, FB does not accept POST and return HTML page ᕙ(⇀‸↼‶)ᕗ |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $requestHttpMethod = Client::GET; |
25
|
|
|
|
26
|
3 |
|
public function getBaseUri() |
27
|
|
|
{ |
28
|
3 |
|
return 'https://graph.facebook.com/v2.8/'; |
29
|
|
|
} |
30
|
|
|
|
31
|
2 |
|
public function getAuthorizeUri() |
32
|
|
|
{ |
33
|
2 |
|
return 'https://www.facebook.com/dialog/oauth'; |
34
|
|
|
} |
35
|
|
|
|
36
|
2 |
|
public function getRequestTokenUri() |
37
|
|
|
{ |
38
|
2 |
|
return 'https://graph.facebook.com/oauth/access_token'; |
39
|
|
|
} |
40
|
|
|
|
41
|
3 |
|
public function getName() |
42
|
|
|
{ |
43
|
3 |
|
return 'facebook'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
3 |
|
public function parseToken($body) |
50
|
|
|
{ |
51
|
3 |
|
if (empty($body)) { |
52
|
|
|
throw new InvalidAccessToken('Provider response with empty body'); |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
|
$result = json_decode($body, true); |
56
|
3 |
|
if ($result) { |
57
|
1 |
|
return new AccessToken($result); |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
throw new InvalidAccessToken('Provider response with not valid JSON'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
2 |
|
public function getIdentity(AccessTokenInterface $accessToken) |
67
|
|
|
{ |
68
|
2 |
|
$response = $this->httpClient->request( |
69
|
2 |
|
$this->getBaseUri() . 'me', |
70
|
|
|
[ |
71
|
2 |
|
'access_token' => $accessToken->getToken(), |
72
|
2 |
|
'fields' => $this->getFieldsInline() |
73
|
2 |
|
] |
74
|
2 |
|
); |
75
|
|
|
|
76
|
2 |
|
if (!$response->isSuccess()) { |
77
|
1 |
|
throw new InvalidResponse( |
78
|
1 |
|
'API response with error code', |
79
|
|
|
$response |
80
|
1 |
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
$result = $response->json(); |
84
|
1 |
|
if (!$result) { |
85
|
1 |
|
throw new InvalidResponse( |
86
|
1 |
|
'API response is not a valid JSON object', |
87
|
|
|
$response |
88
|
1 |
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$hydrator = new ObjectMap( |
92
|
|
|
[ |
93
|
|
|
'id' => 'id', |
94
|
|
|
'first_name' => 'firstname', |
95
|
|
|
'last_name' => 'lastname', |
96
|
|
|
'email' => 'email', |
97
|
|
|
'gender' => 'sex', |
98
|
|
|
'link' => 'url', |
99
|
|
|
'locale' => 'locale', |
100
|
|
|
'name' => 'fullname', |
101
|
|
|
'timezone' => 'timezone', |
102
|
|
|
'updated_time' => 'dateModified', |
103
|
|
|
'verified' => 'verified' |
104
|
|
|
] |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
/** @var User $user */ |
108
|
|
|
$user = $hydrator->hydrate(new User(), $result); |
109
|
|
|
$user->emailVerified = true; |
110
|
|
|
|
111
|
|
|
if ($result->picture) { |
112
|
|
|
$user->pictureURL = $result->picture->data->url; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $user; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|