确保一个REST API从Android的访问 [英] securing a REST API accessible from Android

查看:131
本文介绍了确保一个REST API从Android的访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在建立一个游戏的Andr​​oid,它需要访问网络服务 - 让我们写我们自己的服务器上运行PHP中的REST的API。什么是API提供的是:创建用户,登录,下载游戏,获取游戏排行榜,提交成绩... 的等。现在我在想,如果一些有经验的用户得到的 URL格式的API - 他/她就能垃圾系统在许多方面:

We're building a game for Android, which needs access to web services - so we wrote a RESTful API in PHP that runs on our own server. What the API offers is: creating user, logging in, downloading games, retrieving game list, submitting score... etc. Now I'm thinking, if some experienced user gets the URL format of the API - s/he will be able to trash the system in many ways:

  • 创建一个脚本和放大器;运行它来自动创建用户 - 我想我可以用$ CAPTCHA p $ pvent,或成才之类的。但同样,验证码会惹恼游戏玩家。
  • 在使用他的浏览器,下载游戏和放大器恶意用户登录;然后提交分数,因为他希望 - 所有这些都通过简单地从他的浏览器中键入它调用API。我认为恶意用户以某种方式知道API的网址叫 - 通过在应用程序中进行HTTP请求的嗅探
  • 我需要确保请求只能从安装游戏Android设备制作。 (本场比赛将是免费的)

现在我如何prevent这种侵权行为?

Now How do I prevent such abuses?

推荐答案

我想你将永远无法隐藏的网址被称为应用程序 (如果我运行一个根版Android手机,我应该能够窥探所有网络通信)

I think you will never be able to hide the urls being called by the application (if I am running a root-ed android phone, I should be able to spy on all network traffic)

但你的真正的问题是,你需要以某种方式验证您的API。

But your real problem is that you need to authenticate your api in some way.

一个办法是实施OAUTH,但也许this'd矫枉过正。

One way would be to implement OAUTH, but maybe this'd be overkill.

如果你想有一个简单的机制,这个怎么样;

If you want a simple mechanism, how about this;

  1. 创建一个密钥
  2. 构建API请求(如:<一href="https://my.example.com/users/23?fields=name,email">https://my.example.com/users/23?fields=name,email)
  3. 在哈希这个请求路径+加你的密钥(例如MD5(URL + SECRET_KEY)==a3c2fe167)
  4. 在添加此哈希你的要求(现在是<一个href="https://.....?fields=name,email&hash=a3c2fe167">https://.....?fields=name,email&hash=a3c2fe167)
  5. 在API端,做同样的转换(删除散列参数)
  6. 检查URL的MD5和密钥
  1. create a secret key
  2. build the api request (eg. https://my.example.com/users/23?fields=name,email)
  3. hash this request path + plus your secret key (eg. md5(url+secret_key) == "a3c2fe167")
  4. add this hash to your request (now it is https://.....?fields=name,email&hash=a3c2fe167)
  5. on the api end, do the same conversion (remove the hash param)
  6. check the md5 of the url and the secret key

只要秘密仍然是秘密,没有人可以伪造您的要求。

As long as the secret remains secret, no one can forge your requests.

例(在伪code):

Android的一面:

Android side:

SECRET_KEY = "abc123"

def call_api_with_secret(url, params)
  # create the hash to sign the request
  hash = MD5.hash(SECRET_KEY, url, params)

  # call the api with the added hash
  call_api(url+"&hash=#{hash}", params)
end

服务器端:

SECRET_KEY = "abc123"

def receive_from_api(url, params)
  # retrieve the hash
  url_without_hash, received_hash = retrieve_and_remove_hash(url)

  # check the hash
  expected_hash = MD5.hash(SECRET_KEY, url_without_hash, params)

  if (expected_hash != received_hash)
    raise our exception!
  end

  # now do the usual stuff
end

这篇关于确保一个REST API从Android的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆