|
1
|
|
|
<?php namespace Arcanedev\Socialite\OAuth\Two; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\Socialite\Base\OAuthTwoProvider; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class FacebookProvider |
|
7
|
|
|
* |
|
8
|
|
|
* @package Arcanedev\Socialite\OAuth\Two |
|
9
|
|
|
* @author ARCANEDEV <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class FacebookProvider extends OAuthTwoProvider |
|
12
|
|
|
{ |
|
13
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
14
|
|
|
| Properties |
|
15
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
16
|
|
|
*/ |
|
17
|
|
|
/** |
|
18
|
|
|
* The base Facebook Graph URL. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $graphUrl = 'https://graph.facebook.com'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The Graph API version for the request. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $version = 'v2.5'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The user fields being requested. |
|
33
|
|
|
* |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $fields = ['name', 'email', 'gender', 'verified']; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The scopes being requested. |
|
40
|
|
|
* |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $scopes = ['email']; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Display the dialog in a popup view. |
|
47
|
|
|
* |
|
48
|
|
|
* @var bool |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $popup = false; |
|
51
|
|
|
|
|
52
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
53
|
|
|
| Main Functions |
|
54
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
55
|
|
|
*/ |
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function getAuthUrl($state) |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->buildAuthUrlFromBase( |
|
62
|
|
|
"https://www.facebook.com/{$this->version}/dialog/oauth", $state |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function getTokenUrl() |
|
70
|
|
|
{ |
|
71
|
|
|
return "{$this->graphUrl}/oauth/access_token"; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get the access token for the given code. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $code |
|
78
|
|
|
* |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getAccessToken($code) |
|
82
|
|
|
{ |
|
83
|
|
|
$response = $this->getHttpClient()->get($this->getTokenUrl(), [ |
|
84
|
|
|
'query' => $this->getTokenFields($code), |
|
85
|
|
|
]); |
|
86
|
|
|
|
|
87
|
|
|
return $this->parseAccessToken($response->getBody()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function parseAccessToken($body) |
|
94
|
|
|
{ |
|
95
|
|
|
parse_str($body); |
|
96
|
|
|
|
|
97
|
|
|
return $access_token; |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function getUserByToken($token) |
|
104
|
|
|
{ |
|
105
|
|
|
$appSecretProof = hash_hmac('sha256', $token, $this->clientSecret); |
|
106
|
|
|
$response = $this->getHttpClient()->get("{$this->graphUrl}/{$this->version}/me?access_token={$token}&appsecret_proof={$appSecretProof}&fields=" . implode(',', $this->fields), [ |
|
107
|
|
|
'headers' => [ |
|
108
|
|
|
'Accept' => 'application/json', |
|
109
|
|
|
], |
|
110
|
|
|
]); |
|
111
|
|
|
|
|
112
|
|
|
return json_decode($response->getBody(), true); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* {@inheritdoc} |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function mapUserToObject(array $user) |
|
119
|
|
|
{ |
|
120
|
|
|
$avatarUrl = "{$this->graphUrl}/{$this->version}/{$user['id']}/picture"; |
|
121
|
|
|
|
|
122
|
|
|
return (new User)->setRaw($user)->map([ |
|
123
|
|
|
'id' => $user['id'], |
|
124
|
|
|
'nickname' => null, |
|
125
|
|
|
'name' => isset($user['name']) ? $user['name'] : null, |
|
126
|
|
|
'email' => isset($user['email']) ? $user['email'] : null, |
|
127
|
|
|
'avatar' => $avatarUrl . '?type=normal', |
|
128
|
|
|
'avatar_original' => $avatarUrl . '?width=1920', |
|
129
|
|
|
]); |
|
130
|
|
|
} |
|
131
|
|
|
/** |
|
132
|
|
|
* {@inheritdoc} |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function getCodeFields($state = null) |
|
135
|
|
|
{ |
|
136
|
|
|
$fields = parent::getCodeFields($state); |
|
137
|
|
|
|
|
138
|
|
|
if ($this->popup) { |
|
139
|
|
|
$fields['display'] = 'popup'; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $fields; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Set the user fields to request from Facebook. |
|
147
|
|
|
* |
|
148
|
|
|
* @param array $fields |
|
149
|
|
|
* |
|
150
|
|
|
* @return self |
|
151
|
|
|
*/ |
|
152
|
|
|
public function fields(array $fields) |
|
153
|
|
|
{ |
|
154
|
|
|
$this->fields = $fields; |
|
155
|
|
|
|
|
156
|
|
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Set the dialog to be displayed as a popup. |
|
161
|
|
|
* |
|
162
|
|
|
* @return self |
|
163
|
|
|
*/ |
|
164
|
|
|
public function asPopup() |
|
165
|
|
|
{ |
|
166
|
|
|
$this->popup = true; |
|
167
|
|
|
|
|
168
|
|
|
return $this; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.