简体   繁体   English

通过PHP SDK验证Facebook API访问令牌和用户ID?

[英]Verify Facebook API Access Token & UserID via PHP SDK?

I'm using the Facebook PHP SDK (v3.2.3, not v4.0)( https://developers.facebook.com/docs/reference/php/3.2.3 ) on my server, and the Phonegap Facebook Plugin (Master)( https://github.com/phonegap/phonegap-facebook-plugin ) manually installed in my Phonegap v3.3.0 iOS app. 我在我的服务器上使用Facebook PHP SDK(v3.2.3,而不是v4.0)( https://developers.facebook.com/docs/reference/php/3.2.3 )和Phonegap Facebook插件(Master )( https://github.com/phonegap/phonegap-facebook-plugin )手动安装在我的Phonegap v3.3.0 iOS应用程序中。

I've got everything set up nicely, the app displays a Facebook access token and the "userID" of the authenticating user. 我已经很好地设置了所有内容,应用程序显示了Facebook访问令牌和身份验证用户的“userID”。 Problem is, Facebook tokens only last about 2 months. 问题是,Facebook令牌只持续约2个月。 To fix this, I created a table which houses every FB Access Token that Facebook has given for all of my FB-based users. 为了解决这个问题,我创建了一个表,其中包含了Facebook为所有基于FB的用户提供的每个FB访问令牌。 This works great with my website's "Log in with Facebook" button! 这适用于我的网站“使用Facebook登录”按钮!

...But because this Phonegap plugin leaves the device as a middleman between Facebook and my database, I need my server to double check with Facebook directly to verify the user ID and access token supplied by the user are genuine. ...但是因为这个Phonegap插件让设备成为Facebook和我的数据库之间的中间人,我需要我的服务器直接用Facebook仔细检查以验证用户提供的用户ID和访问令牌是真的。 I've seen that I could query https://graph.facebook.com/app?access_token=TOKEN or do something like: 我已经看到我可以查询https://graph.facebook.com/app?access_token=TOKEN或执行以下操作:

GET /debug_token?
     input_token={input-token}&
     access_token={access-token}

... and apparently get back everything I need, but I get the feeling an attacker could just keep hitting my PHP script that checks with Facebook over and over again until they find success - then they would have the credentials necessary to access my app on behalf of that user whose token they guessed. ...并且显然得到了我需要的一切,但我感觉攻击者可以继续点击我的PHP脚本,一遍又一遍地检查Facebook,直到他们找到成功 - 然后他们将拥有访问我的应用程序所需的凭据代表他们猜对象的那个用户。

If the idea of an attacker guessing any Facebook access token over the period of a few weeks is ridiculous, let me know. 如果攻击者在几周内猜测任何Facebook访问令牌的想法是荒谬的,请告诉我。 But I was hoping to narrow it down and force the attacker to also know which user they're guessing the access token of - THAT should be near impossible to crack. 但我希望将其缩小范围并迫使攻击者也知道他们猜测哪个用户访问令牌 - 这几乎不可能破解。 So how can I verify a user using BOTH the user access token & the user's numerical ID? 那么如何验证用户使用用户访问令牌和用户的数字ID呢?

Get the users Facebook ID from Facebook anytime you get a token and use the user's ID from Facebook to look up your user. 每当您获得令牌并从Facebook使用用户ID查找您的用户时,从Facebook获取用户的Facebook ID。

Even if your app requires offline access and you use a saved token, validate the token before you use it. 即使您的应用需要离线访问并且您使用已保存的令牌,也请在使用之前验证令牌。

If you do not validate the token, your app may still function if the user logged out of Facebook or perhaps unauthorized your app. 如果您未验证令牌,则如果用户退出Facebook或未经授权您的应用,您的应用仍可能正常运行。

$facebook = new Facebook(array(
    'appId'  => <FACEBOOK_ID>,
    'secret' => <FACEBOOK_SECRET>,
));

$facebook->setAccessToken($_REQUEST['access_token']);

if (($userId = $facebook->getUser())) {
    // $_REQUEST['access_token'] is valid token for user with id $userId
}

You shouldn't worry about attacker guessing the access token. 您不应该担心攻击者猜测访问令牌。 Access token is 211 characters long consisting about 62 kind of characters(based on my token). 访问令牌长度为211个字符,包含大约62种字符(基于我的令牌)。 That's 62^211 unique access token. 这是62 ^ 211唯一访问令牌。 There's only 7 billion human in the world, so it's practically impossible to brute force an access token. 世界上只有70亿人,所以几乎不可能强行使用访问令牌。 Nevertheless this kind of attack is FB responsibility to generate a better access_token. 然而,这种攻击是FB负责生成更好的access_token。

Let say the access token is leaked to a criminal: If you are suggesting to compare to the user's FB numerical id -- dont! 假设访问令牌泄露给犯罪分子:如果您建议与用户的FB数字ID进行比较 - 不要! The id is accessible with the access token. 可以使用访问令牌访问id。 Instead, assign your own unique id/username to a user when they sign up for the first time or something. 相反,当用户第一次注册时,会为用户分配您自己的唯一ID /用户名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM