Newer
Older
<?php
/* ----------------------------------------------------------------------------
* INAF - National Institute for Astrophysics
* IRA - Radioastronomical Institute - Bologna
* OATS - Astronomical Observatory - Trieste
* ----------------------------------------------------------------------------
*
* Copyright (C) 2016 Istituto Nazionale di Astrofisica
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License Version 3 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/* Facebook callback page */
// Retrieve Facebook configuration
$Facebook = $AUTHENTICATION_METHODS['Facebook'];
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
$fb = new Facebook\Facebook([
'app_id' => $Facebook['id'],
'app_secret' => $Facebook['secret'],
'default_graph_version' => $Facebook['version'],
]);
$helper = $fb->getRedirectLoginHelper();
if (isset($_GET['state'])) {
$helper->getPersistentDataHandler()->set('state', $_GET['state']);
}
try {
$accessToken = $helper->getAccessToken();
} catch (Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
http_response_code(500);
die('Graph returned an error: ' . $e->getMessage());
} catch (Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
http_response_code(500);
die('Facebook SDK returned an error: ' . $e->getMessage());
}
if (!isset($accessToken)) {
if ($helper->getError()) {
$errorMessage = "Error: " . $helper->getError() . "<br>";
$errorMessage = $errorMessage . "Error Code: " . $helper->getErrorCode() . "<br>";
$errorMessage = $errorMessage . "Error Reason: " . $helper->getErrorReason() . "<br>";
$errorMessage = $errorMessage . "Error Description: " . $helper->getErrorDescription();
} else {
$errorMessage = "Bad request";
}
http_response_code(500);
die($errorMessage);
}
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=id,first_name,last_name,email', $accessToken);
} catch (Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch (Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$_SESSION['fb_access_token'] = (string) $accessToken;
$fbUser = $response->getGraphUser();
$typedId = $fbUser["id"];
// Search if the user is already registered into RAP using the Facebook ID.
$user = $userHandler->findUserByIdentity(RAP\Identity::FACEBOOK, $typedId);
$user = new RAP\User();
$identity = new RAP\Identity(RAP\Identity::FACEBOOK);
$identity->email = $fbUser["email"];
$identity->name = $fbUser["first_name"];
$identity->surname = $fbUser["last_name"];
$identity->typedId = $typedId;
$user->addIdentity($identity);
$userHandler->saveUser($user);
$auditLog->info("LOGIN,Facebook," . $user->id);
$callbackHandler->manageLoginRedirect($user, $session);