|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://dukt.net/social/ |
|
4
|
|
|
* @copyright Copyright (c) 2018, Dukt |
|
5
|
|
|
* @license https://github.com/dukt/social/blob/v2/LICENSE.md |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace dukt\social\loginproviders; |
|
9
|
|
|
|
|
10
|
|
|
use dukt\social\base\LoginProvider; |
|
11
|
|
|
use dukt\social\helpers\SocialHelper; |
|
12
|
|
|
use GuzzleHttp\Client; |
|
13
|
|
|
use dukt\social\models\Token; |
|
14
|
|
|
use League\OAuth2\Client\Provider\FacebookUser; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Facebook represents the Facebook login provider. |
|
18
|
|
|
* |
|
19
|
|
|
* @author Dukt <[email protected]> |
|
20
|
|
|
* @since 1.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
class Facebook extends LoginProvider |
|
23
|
|
|
{ |
|
24
|
|
|
// Public Methods |
|
25
|
|
|
// ========================================================================= |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @inheritdoc |
|
29
|
|
|
*/ |
|
30
|
|
|
public function getName(): string |
|
31
|
|
|
{ |
|
32
|
|
|
return 'Facebook'; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @inheritdoc |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getManagerUrl() |
|
39
|
|
|
{ |
|
40
|
|
|
return 'https://developers.facebook.com/apps'; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @inheritdoc |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getScopeDocsUrl() |
|
47
|
|
|
{ |
|
48
|
|
|
return 'https://developers.facebook.com/docs/facebook-login/permissions'; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritdoc |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getProfile(Token $token) |
|
55
|
|
|
{ |
|
56
|
|
|
$client = $this->getClient($token); |
|
57
|
|
|
|
|
58
|
|
|
$fields = implode(',', $this->getProfileFields()); |
|
59
|
|
|
|
|
60
|
|
|
$options = [ |
|
61
|
|
|
'query' => [ |
|
62
|
|
|
'fields' => $fields |
|
63
|
|
|
] |
|
64
|
|
|
]; |
|
65
|
|
|
|
|
66
|
|
|
$response = $client->request('GET', '/me', $options); |
|
67
|
|
|
|
|
68
|
|
|
if (!$response) { |
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$data = json_decode($response->getBody(), true); |
|
73
|
|
|
|
|
74
|
|
|
if (!$data) { |
|
75
|
|
|
return null; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return new FacebookUser($data); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get the redirect URI. |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getRedirectUri(): string |
|
87
|
|
|
{ |
|
88
|
|
|
$url = SocialHelper::siteActionUrl('social/login-accounts/callback'); |
|
89
|
|
|
$parsedUrl = parse_url($url); |
|
90
|
|
|
|
|
91
|
|
|
if (isset($parsedUrl['query'])) { |
|
92
|
|
|
parse_str($parsedUrl['query'], $query); |
|
93
|
|
|
|
|
94
|
|
|
$query = http_build_query($query); |
|
95
|
|
|
|
|
96
|
|
|
return $parsedUrl['scheme'].'://'.$parsedUrl['host'].$parsedUrl['path'].'?'.$query; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $url; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @inheritdoc |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getOauthProviderConfig(): array |
|
106
|
|
|
{ |
|
107
|
|
|
$config = parent::getOauthProviderConfig(); |
|
108
|
|
|
|
|
109
|
|
|
if (empty($config['options']['graphApiVersion'])) { |
|
110
|
|
|
$config['options']['graphApiVersion'] = 'v3.0'; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $config; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @inheritdoc |
|
118
|
|
|
* |
|
119
|
|
|
* @return \League\OAuth2\Client\Provider\Facebook |
|
120
|
|
|
* @throws \yii\base\InvalidConfigException |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getOauthProvider(): \League\OAuth2\Client\Provider\Facebook |
|
123
|
|
|
{ |
|
124
|
|
|
$config = $this->getOauthProviderConfig(); |
|
125
|
|
|
|
|
126
|
|
|
return new \League\OAuth2\Client\Provider\Facebook($config['options']); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @inheritdoc |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getDefaultUserFieldMapping(): array |
|
133
|
|
|
{ |
|
134
|
|
|
return [ |
|
135
|
|
|
'id' => '{{ profile.getId() }}', |
|
136
|
|
|
'email' => '{{ profile.getEmail() }}', |
|
137
|
|
|
'username' => '{{ profile.getEmail() }}', |
|
138
|
|
|
'photo' => '{{ profile.getPictureUrl() }}', |
|
139
|
|
|
]; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
// Protected Methods |
|
143
|
|
|
// ========================================================================= |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @inheritdoc |
|
147
|
|
|
*/ |
|
148
|
|
|
protected function getDefaultOauthScope(): array |
|
149
|
|
|
{ |
|
150
|
|
|
return [ |
|
151
|
|
|
'email', |
|
152
|
|
|
]; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @inheritdoc |
|
157
|
|
|
*/ |
|
158
|
|
|
protected function getDefaultProfileFields(): array |
|
159
|
|
|
{ |
|
160
|
|
|
return [ |
|
161
|
|
|
'id', |
|
162
|
|
|
'name', |
|
163
|
|
|
'first_name', |
|
164
|
|
|
'last_name', |
|
165
|
|
|
'email', |
|
166
|
|
|
'picture.type(large){url,is_silhouette}', |
|
167
|
|
|
'cover{source}', |
|
168
|
|
|
'gender', |
|
169
|
|
|
'locale', |
|
170
|
|
|
'link', |
|
171
|
|
|
]; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
// Private Methods |
|
175
|
|
|
// ========================================================================= |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Returns the authenticated Guzzle client. |
|
179
|
|
|
* |
|
180
|
|
|
* @param Token $token |
|
181
|
|
|
* |
|
182
|
|
|
* @return Client |
|
183
|
|
|
*/ |
|
184
|
|
|
private function getClient(Token $token): Client |
|
185
|
|
|
{ |
|
186
|
|
|
$headers = []; |
|
187
|
|
|
|
|
188
|
|
|
if ($token) { |
|
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
$headers['Authorization'] = 'Bearer '.$token->token; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
$options = [ |
|
194
|
|
|
'base_uri' => 'https://graph.facebook.com/', |
|
195
|
|
|
'headers' => $headers |
|
196
|
|
|
]; |
|
197
|
|
|
|
|
198
|
|
|
return new Client($options); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|