Implemented Facebook login
This commit is contained in:
@@ -181,6 +181,115 @@ class WrenchOauth extends BaseController
|
||||
}
|
||||
return $local_out;
|
||||
}
|
||||
|
||||
private function facebookOAuthCodeExchange($in, &$local_out) {
|
||||
/*
|
||||
https://developers.facebook.com/docs/facebook-login/guides/advanced/manual-flow/#exchangecode
|
||||
Step 1. Get access token by code
|
||||
|
||||
GET https://graph.facebook.com/v17.0/oauth/access_token?
|
||||
client_id={app-id}
|
||||
&redirect_uri={redirect-uri}
|
||||
&client_secret={app-secret}
|
||||
&code={code-parameter}
|
||||
|
||||
https://developers.facebook.com/docs/facebook-login/guides/access-tokens/get-long-lived
|
||||
Step 2. Get long-lived token by access token
|
||||
|
||||
curl -i -X GET "https://graph.facebook.com/{graph-api-version}/oauth/access_token?
|
||||
grant_type=fb_exchange_token&
|
||||
client_id={app-id}&
|
||||
client_secret={app-secret}&
|
||||
fb_exchange_token={your-access-token}"
|
||||
*/
|
||||
|
||||
// Step 1. Get access token
|
||||
$data = [
|
||||
"client_id" => $this->getSiteConfigurations("facebook.facebook_client_id"),
|
||||
"client_secret" => $this->getSiteConfigurations("facebook.facebook_client_secret"),
|
||||
"redirect_uri" => $in["redirect_uri"],
|
||||
"code" => $in["code"]
|
||||
];
|
||||
$ch = curl_init();
|
||||
|
||||
$url = "https://graph.facebook.com/v17.0/oauth/access_token?" . http_build_query($data);
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_POST, 0);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
|
||||
// Receive server response ...
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
$server_output = curl_exec($ch);
|
||||
|
||||
curl_close($ch);
|
||||
|
||||
/*
|
||||
{
|
||||
"access_token": "EAAJogfssZCgYBOz16ZBqoASjMwZAGNfcISyfjSmPdQDJON3NLxnlSYmludsX1S6Hp9ZC7ZAeUIZBHLnC7HGIh3KRoxksrqdLO5lp1aNhXvB4ecUlRzUe1OOhaf0CAyuYkE4iJfDJr3Q05gjChWjVYr3gj0502kSCvZAxnwsbsyRtlFcXTzzXeJKqrnwAxZB9EzVkXcimas2ZBRdQ4mcutSZBLNYZCdvCcnIL61ypZCwlASvIfRlaenaZAgp4LnCf06w4ZD",
|
||||
"token_type": "bearer",
|
||||
"expires_in": 5181637
|
||||
}
|
||||
*/
|
||||
|
||||
$local_out = json_decode($server_output,true);
|
||||
|
||||
if (!is_array($local_out) || !array_key_exists("message",$local_out)) {
|
||||
$local_out["message"] = "Received from Facebook token API: ".$server_output;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////
|
||||
// IT LOOKS LIKE THE STEP 2 IS NOT NEEDED - BOTH TOKENS HAVE THE SAME EXPIRATION LENGTH
|
||||
///////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/*
|
||||
// Step 2. Get long-lived token
|
||||
$data = [
|
||||
"grant_type" => "fb_exchange_token",
|
||||
"client_id" => $this->getSiteConfigurations("facebook.facebook_client_id"),
|
||||
"client_secret" => $this->getSiteConfigurations("facebook.facebook_client_secret"),
|
||||
"fb_exchange_token" => $local_out["access_token"]
|
||||
];
|
||||
$ch = curl_init();
|
||||
$url = "https://graph.facebook.com/v17.0/oauth/access_token" . http_build_query($data);
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_POST, 0);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
|
||||
// Receive server response ...
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
$server_output = curl_exec($ch);
|
||||
|
||||
curl_close($ch);
|
||||
|
||||
$local_out = json_decode($server_output,true);
|
||||
//*/
|
||||
}
|
||||
|
||||
private function facebookOAuthGetUser($access_token, &$local_out) {
|
||||
// https://developers.facebook.com/docs/graph-api/reference/user/
|
||||
$urlInfp = "https://graph.facebook.com/me?fields=id,email,name,first_name,last_name&access_token=" . $access_token;
|
||||
|
||||
$ch = curl_init();
|
||||
|
||||
curl_setopt($ch, CURLOPT_URL,$urlInfp);
|
||||
//curl_setopt($ch, CURLOPT_POST, 1);
|
||||
//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
|
||||
// Receive server response ...
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
$server_output = curl_exec($ch);
|
||||
|
||||
curl_close($ch);
|
||||
|
||||
$local_out = json_decode($server_output,true);
|
||||
|
||||
if (!is_array($local_out) || !array_key_exists("message",$local_out)) {
|
||||
$local_out["message"] = "Received from Google token API: ".$server_output;
|
||||
}
|
||||
return $local_out;
|
||||
}
|
||||
|
||||
private function prepareOauthEndPointData($endpoint, $in, &$call_backend=true,&$local_out=[]){
|
||||
log_message('critical', "Started prepareOauthEndPointData -> ".$endpoint );
|
||||
switch ($endpoint) {
|
||||
@@ -199,6 +308,19 @@ class WrenchOauth extends BaseController
|
||||
$this->provisionGoogleAccount($local_out["user_info"] , $local_out);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'FACEBOOK':
|
||||
log_message('critical', "Reading prepareOauthEndPointData -> ".$endpoint );
|
||||
$this->facebookOAuthCodeExchange($in, $local_out);
|
||||
log_message('critical', "LET SEEEEE TOKEN prepareOauthEndPointData -> ".$local_out["access_token"] );
|
||||
if ( isset($local_out["access_token"])){
|
||||
log_message('critical', "XXXXXXXXXXXXXXXXX prepareOauthEndPointData -> ".$endpoint );
|
||||
$l_out =[];
|
||||
$local_out["user_info"] = $this->facebookOAuthGetUser($local_out["access_token"], $l_out);
|
||||
log_message('critical', "prepareOauthEndPointDataL FINAL DATALOGIN DATA STEP 889993".serialize( $local_out["user_info"] ) );
|
||||
$this->provisionFacebookAccount($local_out["user_info"] , $local_out);
|
||||
}
|
||||
break;
|
||||
}
|
||||
log_message('critical', "prepareOauthEndPointDataL FINAL DATA".serialize($local_out) );
|
||||
$call_backend=false;
|
||||
@@ -225,19 +347,18 @@ class WrenchOauth extends BaseController
|
||||
return $in;
|
||||
}
|
||||
|
||||
/*
|
||||
*essage";s:326:"Received from Google token API: {
|
||||
"id": "112113008943138678578",
|
||||
"email": "jubaworker@gmail.com",
|
||||
"verified_email": true,
|
||||
"name": "Juba Juba",
|
||||
"given_name": "Juba",
|
||||
"family_name": "Juba",
|
||||
"picture": "https://lh3.googleusercontent.com/a/AAcHTtcVmXN4sjpZiUCZI5X6AwJWUhi46g4VRJqtIqW2G2cb=s96-c",
|
||||
"locale": "en"
|
||||
}
|
||||
|
||||
*/
|
||||
/*
|
||||
*essage";s:326:"Received from Google token API: {
|
||||
"id": "112113008943138678578",
|
||||
"email": "jubaworker@gmail.com",
|
||||
"verified_email": true,
|
||||
"name": "Juba Juba",
|
||||
"given_name": "Juba",
|
||||
"family_name": "Juba",
|
||||
"picture": "https://lh3.googleusercontent.com/a/AAcHTtcVmXN4sjpZiUCZI5X6AwJWUhi46g4VRJqtIqW2G2cb=s96-c",
|
||||
"locale": "en"
|
||||
}
|
||||
*/
|
||||
private function provisionGoogleAccount($user, &$out)
|
||||
{
|
||||
log_message('critical', "YYYYYYYYYYYYYYYYYYY prepareOauthEndPointData -> " );
|
||||
@@ -254,7 +375,7 @@ class WrenchOauth extends BaseController
|
||||
$data['login_channel'] = LOGIN_GOOGLE;
|
||||
$data['sessionid'] = rand(10000, 99999) . "A" . rand(10000, 99999);
|
||||
|
||||
// $this->load->model('backend_model');
|
||||
// $this->load->model('backend_model');
|
||||
$out = array();
|
||||
|
||||
$wrenchboard = new \App\Models\BackendModel();
|
||||
@@ -265,10 +386,54 @@ class WrenchOauth extends BaseController
|
||||
|
||||
} else {
|
||||
// Cannot proceed - backend error?
|
||||
// $msg = (is_array($out) && array_key_exists('status',$out) && $out['status']!='')
|
||||
// ? $out ['status'] : json_encode($out);
|
||||
// $this->session->set_flashdata('login_message','Cannot proceed - error: '.$msg);
|
||||
// redirect('login');
|
||||
// $msg = (is_array($out) && array_key_exists('status',$out) && $out['status']!='')
|
||||
// ? $out ['status'] : json_encode($out);
|
||||
// $this->session->set_flashdata('login_message','Cannot proceed - error: '.$msg);
|
||||
// redirect('login');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
{
|
||||
"id": "10154230206933215",
|
||||
"email": "acidumirae@gmail.com",
|
||||
"name": "Anatolii Okhotnikov",
|
||||
"first_name": "Anatolii",
|
||||
"last_name": "Okhotnikov"
|
||||
}
|
||||
*/
|
||||
private function provisionFacebookAccount($user, &$out)
|
||||
{
|
||||
log_message('critical', "YYYYYYYYYYYYYYYYYYY prepareOauthEndPointData -> " );
|
||||
$name = (string) $user["name"];
|
||||
if (trim($name) == "") {
|
||||
$name = strtok($user["email"], "@");
|
||||
}
|
||||
|
||||
$data['fb_id'] = $user["id"];
|
||||
$data['action'] = WRENCHBOARD_FACEBOOK_LOGIN;
|
||||
$data['firstname'] = $user["first_name"];
|
||||
$data['lastname'] = $user["last_name"];
|
||||
$data['email'] = $user["email"];
|
||||
$data['login_channel'] = LOGIN_FACEBOOK;
|
||||
$data['sessionid'] = rand(10000, 99999) . "A" . rand(10000, 99999);
|
||||
|
||||
// $this->load->model('backend_model');
|
||||
$out = array();
|
||||
|
||||
$wrenchboard = new \App\Models\BackendModel();
|
||||
$ret = $wrenchboard->wrenchboard_api($data, $out);
|
||||
$out['internal_return'] = $ret;
|
||||
if ($ret == PHP_LOGIN_OK) {
|
||||
|
||||
|
||||
} else {
|
||||
// Cannot proceed - backend error?
|
||||
// $msg = (is_array($out) && array_key_exists('status',$out) && $out['status']!='')
|
||||
// ? $out ['status'] : json_encode($out);
|
||||
// $this->session->set_flashdata('login_message','Cannot proceed - error: '.$msg);
|
||||
// redirect('login');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user