1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TechWilk\Rota\AuthProvider\Callback; |
4
|
|
|
|
5
|
|
|
use Facebook\Exceptions\FacebookResponseException; |
6
|
|
|
use Facebook\Exceptions\FacebookSDKException; |
7
|
|
|
use Facebook\Facebook; |
8
|
|
|
use TechWilk\Rota\AuthProvider\CallbackInterface; |
9
|
|
|
use TechWilk\Rota\EmailAddress; |
10
|
|
|
|
11
|
|
|
class FacebookAuth implements CallbackInterface |
12
|
|
|
{ |
13
|
|
|
protected $facebook; |
14
|
|
|
protected $enabled; |
15
|
|
|
|
16
|
|
|
protected $appId; |
17
|
|
|
protected $permissions; |
18
|
|
|
|
19
|
|
|
protected $router; |
20
|
|
|
|
21
|
|
|
protected $user; |
22
|
|
|
|
23
|
|
|
public function __construct(Facebook $facebook, $appId, $baseUrl, $router, $enabled = true) |
24
|
|
|
{ |
25
|
|
|
$this->facebook = $facebook; |
26
|
|
|
$this->enabled = (bool) $enabled; |
27
|
|
|
|
28
|
|
|
$this->appId = $appId; |
29
|
|
|
$this->router = $router; |
30
|
|
|
$this->baseUrl = $baseUrl; |
|
|
|
|
31
|
|
|
|
32
|
|
|
$this->permissions = ['email']; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function isEnabled() |
36
|
|
|
{ |
37
|
|
|
return $this->enabled; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getCallbackUrl() |
41
|
|
|
{ |
42
|
|
|
$helper = $this->facebook->getRedirectLoginHelper(); |
43
|
|
|
|
44
|
|
|
$path = $this->router->pathFor('login-callback', ['provider' => $this->getAuthProviderSlug()]); |
45
|
|
|
|
46
|
|
|
$url = $this->baseUrl.$path; |
47
|
|
|
|
48
|
|
|
return $helper->getLoginUrl($url, $this->permissions); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function verifyCallback($args) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$helper = $this->facebook->getRedirectLoginHelper(); |
54
|
|
|
|
55
|
|
|
try { |
56
|
|
|
$accessToken = $helper->getAccessToken(); |
57
|
|
|
} catch (FacebookResponseException $e) { |
58
|
|
|
// When Graph returns an error |
59
|
|
|
echo 'Graph returned an error: '.$e->getMessage(); |
60
|
|
|
|
61
|
|
|
return false; |
62
|
|
|
} catch (FacebookSDKException $e) { |
63
|
|
|
// When validation fails or other local issues |
64
|
|
|
echo 'Facebook SDK returned an error: '.$e->getMessage(); |
65
|
|
|
|
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
if (!isset($accessToken)) { |
|
|
|
|
70
|
|
|
if ($helper->getError()) { |
|
|
|
|
71
|
|
|
header('HTTP/1.0 401 Unauthorized'); |
72
|
|
|
echo 'Error: '.$helper->getError()."\n"; |
73
|
|
|
echo 'Error Code: '.$helper->getErrorCode()."\n"; |
74
|
|
|
echo 'Error Reason: '.$helper->getErrorReason()."\n"; |
75
|
|
|
echo 'Error Description: '.$helper->getErrorDescription()."\n"; |
76
|
|
|
} else { |
77
|
|
|
header('HTTP/1.0 400 Bad Request'); |
78
|
|
|
echo 'Bad request'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// The OAuth 2.0 client handler helps us manage access tokens |
85
|
|
|
$oAuth2Client = $this->facebook->getOAuth2Client(); |
86
|
|
|
|
87
|
|
|
// Get the access token metadata from /debug_token |
88
|
|
|
$tokenMetadata = $oAuth2Client->debugToken($accessToken); |
89
|
|
|
|
90
|
|
|
// Validation (these will throw FacebookSDKException's when they fail) |
91
|
|
|
$tokenMetadata->validateAppId($this->appId); // Replace {app-id} with your app id |
92
|
|
|
// If you know the user ID this access token belongs to, you can validate it here |
93
|
|
|
//$tokenMetadata->validateUserId('123'); |
|
|
|
|
94
|
|
|
$tokenMetadata->validateExpiration(); |
95
|
|
|
|
96
|
|
View Code Duplication |
if (!$accessToken->isLongLived()) { |
|
|
|
|
97
|
|
|
// Exchanges a short-lived access token for a long-lived one |
98
|
|
|
try { |
99
|
|
|
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken); |
100
|
|
|
} catch (FacebookSDKException $e) { |
101
|
|
|
echo '<p>Error getting long-lived access token: '.$helper->getMessage()."</p>\n\n"; |
|
|
|
|
102
|
|
|
|
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$_SESSION['fb_access_token'] = (string) $accessToken; |
108
|
|
|
|
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getAuthProviderSlug() |
113
|
|
|
{ |
114
|
|
|
return 'facebook'; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getUserId() |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$accessToken = $_SESSION['fb_access_token']; |
120
|
|
|
|
121
|
|
|
try { |
122
|
|
|
// Returns a `Facebook\FacebookResponse` object |
123
|
|
|
$response = $this->facebook->get('/me?fields=id,name,email', $accessToken); |
124
|
|
|
} catch (Facebook\Exceptions\FacebookResponseException $e) { |
|
|
|
|
125
|
|
|
echo 'Graph returned an error: '.$e->getMessage(); |
126
|
|
|
exit; |
|
|
|
|
127
|
|
|
} catch (Facebook\Exceptions\FacebookSDKException $e) { |
|
|
|
|
128
|
|
|
echo 'Facebook SDK returned an error: '.$e->getMessage(); |
129
|
|
|
exit; |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$user = $response->getGraphUser(); |
133
|
|
|
|
134
|
|
|
$this->user = $user; |
135
|
|
|
|
136
|
|
|
return $user->getId(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function getMeta() |
140
|
|
|
{ |
141
|
|
|
if (empty($this->user)) { |
142
|
|
|
$this->getUserId(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return [ |
146
|
|
|
'name' => $this->user->getName(), |
147
|
|
|
'email' => new EmailAddress($this->user->getEmail()), |
148
|
|
|
]; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: