Facebook SDK: Is signed_request an alternative to access_code? - facebook

I hope my question doesnt sound stupid, But i really want to know this..
Is signed_request an alternative to access_code?
If so, How do i receive the signed_request for desktop clients?
I mean flow that i follow in my desktop client is:
Call https://www.facebook.com/dialog/oauth?client_id=xxxx&redirect_uri=https://www.facebook.com/connect/login_success.html&response_type=token&scope=publish_stream
Take the access_token returned from (1) and fetch user information.
So will 1. and 2. be merged in a single request call to signed_request which says it will give the JSON object of user information in it.
How to make this call to get signed_request for desktop clients?

Two problems with your theory here:
In order to get a valid access_token within the signed_request, you'll still need to authenticate and request permission from a user, so in that case it's probably simpler to use the authentication flow fully rather than trying to parse the signed_request.
As stated in the Signed Request docs you will not receive any signed_request if you are building a desktop/device app.

Related

Facebook API how to use a token

I'm sorry if this question is stupid, but how do I actually use the token after I get it (and validate it)? I'm running a Desktop application for which I'm trying to get Facebook login working. I've managed to grab the token, but I do not know how to use it in a request (for example, to get a user's e-mail). I mean, what form would a request that also contains the token look like?
This would be the correct form:
https://graph.facebook.com/me?fields=name,email&access_token=xxx

facebook graph and access token

when I do a get request to
https://graph.facebook.com/[userid]
with the access_token i get all the basic info
but when I do
https://graph.facebook.com/me
with the same token i get an error message: An active access token must be used to query information about the current user
I have tried to under stand what can cause that.
I even tried to token that comes with the signed request
I'm on classic asp, with JSON library, I want the auto to be server side if possible. the FB.api("/me" is working but it is not what i really want to achive
I am pretty sure you don´t have a user access token. Did you login the user? Of course you get the public data of every user with your first link, even without any access token. You can even put it directly in the browser and will get results. But for "/me" you have to authorize the user to your app.
See here: https://developers.facebook.com/docs/howtos/login/server-side-login/
It is the same problem as in the other thread for sure, just a different Programming language.
Remember: If the user did not accept at least the basic permissions in a dialog or redirect, he is not logged in and you will never know anything about him in the app (except for some specifics in tab apps, like language, like-status and stuff).

Confirming a Facebook Access Token?

We're using the Facebook JS SDK to authenticate users to our application, then sending that information to the server back end to log our user into the application (we support multiple forms of authentication but we can only support FB using a client side flow).
FB is able to log in correctly, I'm able to authenticate the user and everything else, but there's one wrinkle that I'm not sure how to handle, and I'm having difficulty finding anything in the documentation about it. Facebook sends back an accessToken in addition to an id. My question is, how do I verify on the server side that the accessToken is correct for the given id? I want to make sure that the data the user sends us matches what Facebook sent them, and I can't imagine this is that out of the ordinary, yet I can't seem to find any documentation on it.
In the response from Facebook should be a signed_request string that can authenticate that the data is genuine with and decoded to yield the user id and an oauth token for the user.
Passing this along with the other response to your server-side code should allow you to validate everything pretty easily.
May I suggest this answer from another question! It uses try-catch. It is probably the only way to verify that the authentication and the permissions are valid! That's how they do in their examples
Facebook OAuthException: (#1)
I dont think facebook provides any other way of checking

Is signed_request secure?

I'm creating a Facebook app. Facebook displays the app view in iframe and gives it signed_request in a POST parameter.
If someone get other's signed_request string, he can post it to the app.
curl -F "signed_request=CCuTU8c2…NjMwOTMxIn0" https://app.mydomain.xx/
Signed_request is registant to tampering. On the other hand, the app accepts the data which isn't tempered.
Should Facebook apps check something for it? For example, the value of issued_at. I wonder how to handle signed_request. Facebook's PHP SDK sets it to cookie. It's OK?
This cannot be faked, if you check it properly. It is a JSON data object that has been encrypted with your application-secret. An attacker would not be able to encrypt with this key, cos you never let anyone know it, right? --if you have ever leaked your application-secret, your seriously boned in lots of other ways; a faked signed_request is the last of your worries.
You need to decrypt the data and parse the resultant JSON to ensure it matches the request you have received. If an attacker sends a signed-request that he has sniffed from a previous use, then he can't just send it with an arbitrary request, as it will only match the request from which it was stolen. But it is the responsibility of your code to perform this check. It is possible that this may be a feature of your Facebook API client library, depending on the library and configuration used.
Please see the signed request page at Facebook Developers
And for further reading on Facebook security, please see the excellent Developing Secure Facebook Apps document at OWASP.

Decrypt OAuth 2.0 access token

Is it possible to decrypt Facebook's new OAuth 2.0 access_token ?
I need to somehow get user_id and app_id from the access_token.
PS:
I need to get the user_id and app_id ONLY from the access_token as Facebook Linter used to do.
As others have already pointed out, the access_token is a unique random string, so it cannot be decrypted as such. Also, we all know that the user_id and app_id are prerequesites to generate the token in the first place.
However, let's assume you stored your token(s) in a database and lost the associated user_id and app_id. In that case, it is a valid question on how to retrieve them having only the token at hand. If your token is still valid, this is possible. If it is expired, you're out of luck.
To retrieve the user_id, make a call to:
https://graph.facebook.com/me?fields=id&access_token=xxx
To retrieve the app_id, make a call to:
https://graph.facebook.com/app?fields=id&access_token=xxx
In both cases, the associated id's will be part of the JSON response, regardless of the access_token being an encrypted or unencrypted one.
Let's illustrate this with an example. Let's assume Mark Zuckerberg uses the Graph API Explorer to generate an access_token. Calling the /me endpoint gives you:
{
"id": "68310606562"
}
and calling the /app endpoint gives you:
{
"id": "145634995501895"
}
The ids you were looking for are part of the response.
Please note that this does not work with the access_token shown on https://developers.facebook.com/apps (not sure if this is a Facebook mistake or intentional). Please use the access_token that your app receives via OAuth.
If the access token is in the encrypted format, there's no programmatic way to determine the User ID and App ID.
I struggle to think of a legitimate way you could have come across an access token without already having those two pieces of information since presumably you know your own App ID and the User ID you stored the access token against.
Nevertheless, assuming there's a legitimate use-case for this:
a call to /me?fields=id will return the user ID and/or you can use the debug tool at https://developers.facebook.com/tools/debug to debug the other properties of the access token
Generally you use the access_token to access other data from your application. So for example, your application would authenticate the user then use the access token to access other functions in FB's API, such as the graph:
https://graph.facebook.com/me?access_token=<access_token>
There is no public way of decrypting an access token to get the user id and app id. This is also very likely a massive breach of Facebook policy.
To get an access token in the first place you have to have access to the user and the app id anyway so you shouldn't need to do this. If you don't actually have access to the user or the app id then my guess would be you probably shouldn't even have their access token and have probably obtained it 'illegally'...
This is actually a very simple task, if you look closely at the access token itself. It consists of 3 segments, separated by a pipe character, |:
APP_ID|SOME_STUFF.NUMBER-USERID|SOME_MORE_STUFF
I'm not sure what SOME_STUFF, NUMBER, and SOME_MORE_STUFF are; presumably timestamps, signatures, or other encoded data that facebook uses to keep track of the access_token's validity and so on.
Unless you've obtained the acces_token in question by fowl means, I don't see a problem with being able to access the APP_ID and USER_ID from them (and neither does Facebook, apparently). So all I'll say on that is be responsible :)
The other thing to keep in mind is that this isn't a standard or anything, and is subject to change. So, watch out for that, too.
you don't have to decrypt accesstoken
As far as AppID is concerned,you should get it from facebook whn you make an app there,its your id to connect to facebook.
facebook sends the userId along with accesstoken..
just check your cookies in browser or in oauth case check entire string returned when request for acess token.
Are you sure you're talking about the Access Token here and not the signed request?
When you're Facebook Application is loaded you have a signed request object, which has the information I believe you are looking for (however if the user has not authorized your application their user ID will not be in the signed request, Facebook security)
The only way currently available is to use to Facebook Access Token Lint Tool.
You can consider to automate the process.