如何在服务器端的移动应用程序中验证FBLogin

时间:2013-02-03 20:16:12

标签: facebook facebook-graph-api cordova

我正在开发一个基于Phonegap的应用程序,它使用Facebook OAuth进行SSO登录。如何验证服务器端的FB.login javascript响应?成功从Facebook移动重定向到我的应用程序后,我得到userId和accessToken。检查accesToken以获取服务器上用户的访问权限就足够了吗?

https://graph.facebook.com/me?fields=id&access_token=...

我的问题是JS&移动和网络上的PHP混合: 用户在移动设备上使用FB.login(Javascript)并成功返回(客户端),因为用户已经通过网络连接。现在,我可以检查数据库中的FB ID,以通过AJAX请求加载用户凭据。如何使AJAX请求安全。原型将登录通过有效FB的每个人,该FB也会在我的数据库中退出。因此,系统是开放的,无需在FB(服务器)上重新验证即可获得会话。我的想法是检查accessToken是否有效,但不确定这是否足够......我认为不是......也许检查IP呢?

1 个答案:

答案 0 :(得分:0)

使用SignedRequest代替accessToken接缝是我需要的正确选择,例如PHP解决方案,使用app secret。

private function check_signed_request($signed_request) {

    $secret = Semods_Utils::getSetting('socialdna.facebook_secret','');
    list($encoded_sig, $payload) = explode('.', $signed_request, 2);

    // decode the data
    $sig = $this->base64_url_decode($encoded_sig);
    $data = json_decode($this->base64_url_decode($payload), true);

    if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
        throw new Exception('Unknown algorithm. Expected HMAC-SHA256');
    }

    // Adding the verification of the signed_request below
    $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
    if ($sig !== $expected_sig) {
        throw new Exception('Bad Signed JSON signature!');            
    }

    return true;
}

private function base64_url_decode($input) {
    return base64_decode(strtr($input, '-_', '+/'));
}

请参阅:https://developers.facebook.com/docs/howtos/login/signed-request/

<强>问题 我没有收到signed_request,这些是未定义的:

response.authResponse.signed_request
response.authResponse.signedRequest

并且这个只包含“......”

response.authResponse.sig

有人有想法吗?

修改 好的.sig接缝只能由我的插件提供,硬编码:所以修复应该在那里https://github.com/davejohnson/phonegap-plugin-facebook-connect

但报道: https://github.com/davejohnson/phonegap-plugin-facebook-connect/issues/245