我知道有很多关于这个主题的信息,但我找不到任何最新的信息.我看到这样的主题与rails和android身份验证相关但我看到 TokenAuthenticatable
现在已经从设计中删除了.
我的问题很简单:有没有一种很好的方法来使用Rails 4从本机Android和iPhone应用程序验证用户?有没有人知道提供解决方案的好教程或文章?
Adam Waite添加赏金:
我刚刚在这个问题上开了一个500赏金,因为我找不到在任何地方从iOS应用程序到Rails API验证用户的正确做法.这是我考虑做的事情,但不知道它是否安全?!:
我们假设我们有一个User
记录.用户已注册已User
在数据库中使用email
列和password_digest
列创建记录的帐户.
当用户登录时,我希望该用户在移动应用程序上保持身份验证,直到明确注销.
我想我们将需要基于令牌的身份验证.我可能会在创建时创建一个ApiKey记录,User
并将其保存为User
记录中的关联.
当用户登录/注册时,响应将包含一个API令牌(类似于SecureRandom.hex
),它将保存在iOS Keychain中,并与所有后续请求一起使用,通过将其传递到标头并使用以下内容进行验证来验证用户:
before_filter :restrict_access private def restrict_access authenticate_or_request_with_http_token do |token, options| ApiKey.exists?(access_token: token) end
这样安全吗?我是否应该使用每个请求刷新令牌并将其包含在响应中?
我还有其他选择吗?Facebook,Twitter和Pinterest的喜欢做什么?
我知道OAuth2.0,但这不是用于授予外部应用程序吗?
有没有一个宝石可以管理这些?
对不起,这里完全不确定.
500到最佳答案.
您处于正确的轨道上,但用户的令牌应仅用于识别发出请求的用户.您仍然需要某种身份验证,因为当您推测在每个请求上更改令牌时,黑客可以拦截数据流,获取令牌,然后在后续请求中"成为"该用户.
通过更改每个请求上的令牌,您可以消除拦截问题,但是一旦有人拦截了令牌,他们就可以通过继续拦截它甚至修改响应来进一步利用系统.对此的一个解决方案是使用HMAC(由Amazon Web Services使用).它是一种算法,为您的请求提供签名(哈希),该签名对于每个请求都是唯一的,不需要更改密钥,也不能为将来的请求进行预测.
有一个用于rails的ruby gem,它在服务器端实现HMAC,用于签署HMAC请求,以及在进行服务器到服务器通信时生成它们.对于客户端到服务器请求,您需要在iOS或Android端生成签名并在服务器上对其进行身份验证.
考虑ApiAuth gem在服务器端进行工作.在iOS客户端,考虑HBHMAC库以生成签名.看看ApiAuth的具体实现,因为它为数据添加时间戳以防止重放攻击,因此您可能需要在将数据传递给HBHMAC之前为其添加字段.
总之,使用HMAC身份验证将通过利用单向散列算法避免人员处于中间攻击和重放攻击,该算法可防止攻击者生成真实请求,即使他们能够拦截有效请求.
我遇到过这个问题,我是一名API开发人员.您可以通过令牌和自定义授权来完成这项工作,但我会告诉您我们如何处理我们的应用程序,该应用程序以六位数的形式为用户提供服务.
至少对于iOS,设备将为您处理会话,这意味着如果iOS应用程序上的用户/users/sign_in
使用参数发出POST请求
user: { password: 'mypassword', email: 'testuser@example.com', remember_me: true # optional }
iOS设备将安全,持久地为您存储会话.
现在,如果你想进入OAuth 2路线,我实际上为rails 4维护了一个名为OAuth 2 providable的gem,我添加了一个非常酷的功能,允许你让用户通过"授权"屏幕,因为很明显如果您开发了该软件,则无需用户确认他们信任您.
如果您决定使用OAuth 2,则需要使用调用隐式访问令牌的内容. 这是漫长而非常的枯燥的的OAuth2规范
rails 4项目可以在github上找到 https://github.com/bwheeler96/devise-oauth2-provider-rails4
如果您不在rails 4上,则可以使用原始gem https://github.com/socialcast/devise_oauth2_providable
顺便说一句,宝石需要工作,所以如果有人读这个想要帮助它做得更好的人,请务必分叉这个存储库
我的研究解决方案的要点.随意编辑,更正,无效等.
SessionsController < ApplicationController skip_before_filter :authenticate_user, :only => [:create] def create user = User.where("username = ? OR email = ?", params[:username_or_email], params[:username_or_email]).first if user && user.authenticate(params[:password]) api_key = user.find_api_key if !api_key.secret_key || api_key.is_expired? api_key.set_expiry_date api_key.generate_secret_key end api_key.save render json: api_key, status: 201 else status: 401 end end
注意ApiAuth.authentic?方法和请求对象.该请求必须在客户端上使用HMAC算法进行签名.
ApplicationController < ActionController::Base respond_to :json force_ssl protect_from_forgery with: :null_session before_filter :authenticate_user private def authenticate_user if authenticate_user_from_secret_key return true else head :unauthorized end end def authenticate_user_from_secret_key userid = ApiAuth.access_id(request) currentuser = userid && User.find_by_id(userid) if ApiAuth.authentic?(request, currentuser.find_api_key.secret_key) return true else return false end false end
用户创建/注册
UsersController < ApplicationController skip_before_filter :authenticate_user, :only => [:create] def create user = User.create(user_params) if !user.new_record? render json: user.find_apit_key, status: 201 else # error end end
Api关键模型.与#352 railscast中的api密钥模型类似,唯一的区别是ApiAuth密钥生成.
class ApiKey < ActiveRecord::Base before_create :generate_secret_key, :set_expiry_date belongs_to :user def generate_secret_key begin self.secret_key = ApiAuth.generate_secret_key end while self.class.exists?(secret_key: secret_key) end
用户模型.
class User < ActiveRecord::Base has_secure_password before_save :ensure_api_key has_many :api_keys def find_api_key self.api_keys.active.ios.first_or_create end
在客户端,必须使用HMAC算法对请求进行签名.
代码来自:[SHA1 HMAC密钥生成/认证] https://github.com/mgomes/api_auth [控制器和型号] https://github.com/danahartweg/authenticatable_rest_api