簡體   English   中英

如何在服務器端的移動應用中驗證FBLogin

[英]How to validate FBLogin in mobile app on server side

我正在開發一個基於Phonegap的應用程序,該應用程序使用Facebook OAuth進行SSO登錄。 如何在服務器端驗證FB.login JavaScript響應? 從Facebook移動成功重定向回我的應用程序后,我得到了userId和accessToken。 檢查accesToken以獲得服務器上用戶的訪問權限就足夠了嗎?

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

我的問題是在移動設備和Web上混合使用JS和PHP:用戶在移動設備上使用FB.login(Javascript)並成功返回(客戶端),因為用戶已經通過Web連接了。 現在,我可以檢查數據庫中的FB ID來通過AJAX請求加載用戶憑據。 如何使AJAX請求安全。 原型將登錄通過有效FB的所有人,該FB也將退出我的數據庫。 因此,系統無需等待在FB(服務器)上重新驗證即可打開以獲取會話。 我的想法是檢查accessToken是否有效,但不確定這是否足夠...我認為不是...也許也檢查IP?

使用SignedRequest而不是accessToken接縫是我需要的正確選擇,例如使用應用程序密碼的PHP解決方案。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM