适用于 iOS 的安全 API,无需用户帐户 [英] Secure API for iOS without user account

查看:44
本文介绍了适用于 iOS 的安全 API,无需用户帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个很受欢迎的问题,但我找不到任何其他可以满足我特定需求的问题.

I know this is a popular type of question, but I haven't been able to find any other questions that address my specific needs.

  • 我有一个 iOS 应用,可以从我的 Ruby on Rails 开发的 Web API 中检索数据.
  • 我想将我的 API 私有化,以便其他来源无法使用来自我的 API 的数据(即,其他人开发的应用访问我的 API 网址并为他们的客户使用数据)
  • (硬)私有 API,以便只有经过授权的客户端(仅来自 iOS 应用)才能访问 API 中的数据.
  • (硬)用户不必创建用户/密码帐户.
  • (SOFT)我了解到,在尝试让 Apple 批准应用程序时,SSL 可能会成为一场噩梦.由于这是一个小型应用程序(截至目前),我更喜欢依赖 SSL.但是,如果您能就在 iOS 上的所有 API 流量中轻松使用 SSL 为我指明正确的方向,我会全力以赴).
  • (HARD) Private API so that only authorized clients (coming ONLY from iOS app) can access the data from the API.
  • (HARD) Users must not have to create user/password accounts.
  • (SOFT) I have read that SSL can become a nightmare when trying to get apps approved by Apple. Since this is a small-time app (as of now), I prefer to not depend on SSL. However, if you can point me in the right direction regarding the ease of using SSL with all API traffic on iOS, I am all ears).

如果您失去兴趣,请跳到问题的最后:)

! Skip to the end for the question if you are losing interest :) !

想法 1:

  1. iOS 从网络请求令牌,发送一些 UUID
  2. WEB 响应 API_Token 和 Token_Expiry
  3. WEB 将 UUID、API_Token 和 Token_Expiry 存储在数据库中
  4. iOS 在本地存储 API_Token、Token_Expiry
  5. iOS 通过发送 UUID 和 API_Token 请求数据
  6. WEB 验证 UUID 和 API_Token,以数据响应
  7. 重复步骤 5-6,直到 API_Token 过期,然后从步骤 1 开始重复

*想法 2:(一次性使用 API_Token)*

*Idea 2: (single use API_Token)*

  1. iOS 从网络请求令牌,发送一些 UUID
  2. WEB 使用 API_Token 响应
  3. WEB 在数据库中存储 UUID 和 API_Token
  4. iOS 在本地存储 API_Token
  5. iOS 通过发送 UUID 和 API_Token 请求数据
  6. WEB 验证 UUID 和 API_Token,以数据和新令牌响应
  7. iOS 获取数据并在本地保存 NEW TOKEN
  8. 无限期地重复步骤 5-7

这些想法的问题

我相信不再有适用于 iOS 的完美 UUID 解决方案.如果 UUID 可以随时间变化(或者如果用户有多个 iOS 设备),则可能会出现身份验证问题.

Problems with these ideas

I believe there is no perfect UUID solution for iOS any longer. If the UUID can change over time (or if the user has multiple iOS devices), an authentication problem can occur.

如果黑客获得 API 密钥,我不希望他们能够访问数据(因此产生了过期或新令牌的想法).

If a hacker gets an API key, I don't want them to be able to access the data (hence the expiry or new token idea).

对于在 Rails 和 iOS 之间创建安全的 API,您有什么建议?

What suggestion do you have to create a secure API between Rails and iOS?

编辑 1:

我仍然很惊讶这不是一直出现的事情.一定有大量的应用程序与 API 进行通信,但不会强制用户注册.如果 SSL 或 OAuth 是唯一正确的解决方案,请进行辩护.我全神贯注.

I'm still surprised this isn't something that comes up all the time. There must be a ton of apps out there that communicate to an API but do not force user's to sign up. If SSL or OAuth is the only proper solution, please defend. I'm all ears.

推荐答案

我最终根据我在网上找到的一些建议(请参阅最后的参考链接)推出了自己的解决方案.

I ended up rolling my own solution based on a few suggestions that I found on the web (see reference links at the end).

  1. iOS 检查它是否有 auth_token.如果否,请继续执行步骤 2,否则继续执行步骤 4.
  2. iOS 通过发送只有我的 iOS 应用和服务器知道如何生成的特殊签名来请求 auth_token.
  3. WEB 验证特殊签名并创建一个唯一的 auth_token,该令牌保存在数据库中并发送回 iOS 应用.
  4. iOS 通过发送 auth_token 和生成的签名(同样只有我的 iOS 和服务器知道如何生成的签名)来请求数据.
  5. WEB 验证 auth_token 是否存在于数据库中.然后它会生成一个 auth_signature 并验证请求是否来自我的 iOS 应用.
  6. WEB 使用数据和新生成的 auth_token 进行响应.
  7. WEB 从数据库中删除以前的 auth_token.
  8. iOS 在本地保存新的 auth_token 并使用数据.
  9. 重复步骤 4-8;如果响应是 401 未授权,则从步骤 1 重新开始.

参考:

@keighl 的这个 GitHub Gist 是我的开始,这是迄今为止我发现的最好的例子:https://gist.github.com/4336694

Railscast:保护 API:http://railscasts.com/episodes/352-securing-an-api

Railscast: Securing an API: http://railscasts.com/episodes/352-securing-an-api

这篇关于适用于 iOS 的安全 API,无需用户帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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