1 | <?php |
||
42 | class FacebookRedirectLoginHelper |
||
43 | { |
||
44 | /** |
||
45 | * @const int The length of CSRF string to validate the login link. |
||
46 | */ |
||
47 | const CSRF_LENGTH = 32; |
||
48 | |||
49 | /** |
||
50 | * @var OAuth2Client The OAuth 2.0 client service. |
||
51 | */ |
||
52 | protected $oAuth2Client; |
||
53 | |||
54 | /** |
||
55 | * @var UrlDetectionInterface The URL detection handler. |
||
56 | */ |
||
57 | protected $urlDetectionHandler; |
||
58 | |||
59 | /** |
||
60 | * @var PersistentDataInterface The persistent data handler. |
||
61 | */ |
||
62 | protected $persistentDataHandler; |
||
63 | |||
64 | /** |
||
65 | * @var PseudoRandomStringGeneratorInterface The cryptographically secure pseudo-random string generator. |
||
66 | */ |
||
67 | protected $pseudoRandomStringGenerator; |
||
68 | |||
69 | /** |
||
70 | * @param OAuth2Client $oAuth2Client The OAuth 2.0 client service. |
||
71 | * @param PersistentDataInterface|null $persistentDataHandler The persistent data handler. |
||
72 | * @param UrlDetectionInterface|null $urlHandler The URL detection handler. |
||
73 | * @param PseudoRandomStringGeneratorInterface|null $prsg The cryptographically secure pseudo-random string generator. |
||
74 | */ |
||
75 | public function __construct(OAuth2Client $oAuth2Client, PersistentDataInterface $persistentDataHandler = null, UrlDetectionInterface $urlHandler = null, PseudoRandomStringGeneratorInterface $prsg = null) |
||
82 | |||
83 | /** |
||
84 | * Returns the persistent data handler. |
||
85 | * |
||
86 | * @return PersistentDataInterface |
||
87 | */ |
||
88 | public function getPersistentDataHandler() |
||
89 | { |
||
90 | return $this->persistentDataHandler; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Returns the URL detection handler. |
||
95 | * |
||
96 | * @return UrlDetectionInterface |
||
97 | */ |
||
98 | public function getUrlDetectionHandler() |
||
99 | { |
||
100 | return $this->urlDetectionHandler; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Returns the cryptographically secure pseudo-random string generator. |
||
105 | * |
||
106 | * @return PseudoRandomStringGeneratorInterface |
||
107 | */ |
||
108 | public function getPseudoRandomStringGenerator() |
||
109 | { |
||
110 | return $this->pseudoRandomStringGenerator; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Stores CSRF state and returns a URL to which the user should be sent to in order to continue the login process with Facebook. |
||
115 | * |
||
116 | * @param string $redirectUrl The URL Facebook should redirect users to after login. |
||
117 | * @param array $scope List of permissions to request during login. |
||
118 | * @param array $params An array of parameters to generate URL. |
||
119 | * @param string $separator The separator to use in http_build_query(). |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | private function makeUrl($redirectUrl, array $scope, array $params = [], $separator = '&') |
||
124 | { |
||
125 | $state = $this->persistentDataHandler->get('state') ?: $this->pseudoRandomStringGenerator->getPseudoRandomString(static::CSRF_LENGTH); |
||
126 | $this->persistentDataHandler->set('state', $state); |
||
127 | |||
128 | return $this->oAuth2Client->getAuthorizationUrl($redirectUrl, $state, $scope, $params, $separator); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Returns the URL to send the user in order to login to Facebook. |
||
133 | * |
||
134 | * @param string $redirectUrl The URL Facebook should redirect users to after login. |
||
135 | * @param array $scope List of permissions to request during login. |
||
136 | * @param string $separator The separator to use in http_build_query(). |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | public function getLoginUrl($redirectUrl, array $scope = [], $separator = '&') |
||
144 | |||
145 | /** |
||
146 | * Returns the URL to send the user in order to log out of Facebook. |
||
147 | * |
||
148 | * @param AccessToken|string $accessToken The access token that will be logged out. |
||
149 | * @param string $next The url Facebook should redirect the user to after a successful logout. |
||
150 | * @param string $separator The separator to use in http_build_query(). |
||
151 | * |
||
152 | * @return string |
||
153 | * |
||
154 | * @throws FacebookSDKException |
||
155 | */ |
||
156 | public function getLogoutUrl($accessToken, $next, $separator = '&') |
||
173 | |||
174 | /** |
||
175 | * Returns the URL to send the user in order to login to Facebook with permission(s) to be re-asked. |
||
176 | * |
||
177 | * @param string $redirectUrl The URL Facebook should redirect users to after login. |
||
178 | * @param array $scope List of permissions to request during login. |
||
179 | * @param string $separator The separator to use in http_build_query(). |
||
180 | * |
||
181 | * @return string |
||
182 | */ |
||
183 | public function getReRequestUrl($redirectUrl, array $scope = [], $separator = '&') |
||
189 | |||
190 | /** |
||
191 | * Returns the URL to send the user in order to login to Facebook with user to be re-authenticated. |
||
192 | * |
||
193 | * @param string $redirectUrl The URL Facebook should redirect users to after login. |
||
194 | * @param array $scope List of permissions to request during login. |
||
195 | * @param string $separator The separator to use in http_build_query(). |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | public function getReAuthenticationUrl($redirectUrl, array $scope = [], $separator = '&') |
||
205 | |||
206 | /** |
||
207 | * Takes a valid code from a login redirect, and returns an AccessToken entity. |
||
208 | * |
||
209 | * @param string|null $redirectUrl The redirect URL. |
||
210 | * |
||
211 | * @return AccessToken|null |
||
212 | * |
||
213 | * @throws FacebookSDKException |
||
214 | */ |
||
215 | public function getAccessToken($redirectUrl = null) |
||
230 | |||
231 | /** |
||
232 | * Validate the request against a cross-site request forgery. |
||
233 | * |
||
234 | * @throws FacebookSDKException |
||
235 | */ |
||
236 | protected function validateCsrf() |
||
253 | |||
254 | /** |
||
255 | * Resets the CSRF so that it doesn't get reused. |
||
256 | */ |
||
257 | private function resetCsrf() |
||
261 | |||
262 | /** |
||
263 | * Return the code. |
||
264 | * |
||
265 | * @return string|null |
||
266 | */ |
||
267 | protected function getCode() |
||
271 | |||
272 | /** |
||
273 | * Return the state. |
||
274 | * |
||
275 | * @return string|null |
||
276 | */ |
||
277 | protected function getState() |
||
281 | |||
282 | /** |
||
283 | * Return the error code. |
||
284 | * |
||
285 | * @return string|null |
||
286 | */ |
||
287 | public function getErrorCode() |
||
291 | |||
292 | /** |
||
293 | * Returns the error. |
||
294 | * |
||
295 | * @return string|null |
||
296 | */ |
||
297 | public function getError() |
||
301 | |||
302 | /** |
||
303 | * Returns the error reason. |
||
304 | * |
||
305 | * @return string|null |
||
306 | */ |
||
307 | public function getErrorReason() |
||
311 | |||
312 | /** |
||
313 | * Returns the error description. |
||
314 | * |
||
315 | * @return string|null |
||
316 | */ |
||
317 | public function getErrorDescription() |
||
321 | |||
322 | /** |
||
323 | * Returns a value from a GET param. |
||
324 | * |
||
325 | * @param string $key |
||
326 | * |
||
327 | * @return string|null |
||
328 | */ |
||
329 | private function getInput($key) |
||
333 | } |
||
334 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: