In accordance with:
How to securely authorize a user through the Javascript SDK from Facebook
Send signed_request fields to your server which is accepted in authResponse using javascript sdk
Then, on the server side, for verification, follow the procedure described in the documentation :
Once you have captured the signed request, you need to complete three steps:
- Divide the signed request into two parts, marked with the letter '.' (e.g. 238fsdfsd.oijdoifjsidf899)
- Decode the first part - encoded signature - from base64url
- Decode the second part - the "payload" - from base64url, and then decode the resulting JSON object
Here is an example in PHP:
function parse_signed_request($signed_request) { list($encoded_sig, $payload) = explode('.', $signed_request, 2); $secret = "appsecret"; // Use your app secret here // decode the data $sig = base64_url_decode($encoded_sig); $data = json_decode(base64_url_decode($payload), true); // confirm the signature $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true); if ($sig !== $expected_sig) { error_log('Bad Signed JSON signature!'); return null; } return $data; } function base64_url_decode($input) { return base64_decode(strtr($input, '-_', '+/')); }
This will create a JSON object that looks something like this:
{ "oauth_token": "{user-access-token}", "algorithm": "HMAC-SHA256", "expires": 1291840400, "issued_at": 1291836800, "user_id": "218471" }
After receiving user_id, this particular user can be placed in a session, although there must be other checks for proper authorization.
As a second check, you can check issued_at to see if it is longer than 10 minutes.
Taken from here .
However, there may be scenarios in which your app_secret may be compromised. To take care of this case, you should follow step No. 3, since the code exchange for access_token can occur only once and within 10 minutes of its release. If the user does not have an account with your site, then in any case, you will need step No. 3 to use access_token to retrieve other necessary user data, such as name, email address, etc. from FB.
To update the token, your server can make the next call
GET /oauth/access_token? grant_type=fb_exchange_token& client_id={app-id}& client_secret={app-secret}& fb_exchange_token={short-lived-token}
Link