【发布时间】:2014-01-11 19:23:27
【问题描述】:
我的 Rails 应用使用 Devise 进行身份验证。它有一个姊妹 iOS 应用程序,用户可以使用与 Web 应用程序相同的凭据登录到 iOS 应用程序。所以我需要某种 API 来进行身份验证。
这里有很多类似的问题指向this tutorial,但它似乎已经过时了,因为token_authenticatable 模块已经从Devise 中删除,并且一些行会引发错误。 (我使用的是 Devise 3.2.2。)我尝试根据该教程(和this one)推出自己的教程,但我对它不是 100% 有信心 - 我觉得我可能有一些东西被误解或错过了。
首先,根据this gist 的建议,我在users 表中添加了authentication_token 文本属性,并将以下内容添加到user.rb:
before_save :ensure_authentication_token
def ensure_authentication_token
if authentication_token.blank?
self.authentication_token = generate_authentication_token
end
end
private
def generate_authentication_token
loop do
token = Devise.friendly_token
break token unless User.find_by(authentication_token: token)
end
end
然后我有以下控制器:
api_controller.rb
class ApiController < ApplicationController
respond_to :json
skip_before_filter :authenticate_user!
protected
def user_params
params[:user].permit(:email, :password, :password_confirmation)
end
end
(请注意,我的application_controller 有before_filter :authenticate_user! 这一行。)
api/sessions_controller.rb
class Api::SessionsController < Devise::RegistrationsController
prepend_before_filter :require_no_authentication, :only => [:create ]
before_filter :ensure_params_exist
respond_to :json
skip_before_filter :verify_authenticity_token
def create
build_resource
resource = User.find_for_database_authentication(
email: params[:user][:email]
)
return invalid_login_attempt unless resource
if resource.valid_password?(params[:user][:password])
sign_in("user", resource)
render json: {
success: true,
auth_token: resource.authentication_token,
email: resource.email
}
return
end
invalid_login_attempt
end
def destroy
sign_out(resource_name)
end
protected
def ensure_params_exist
return unless params[:user].blank?
render json: {
success: false,
message: "missing user parameter"
}, status: 422
end
def invalid_login_attempt
warden.custom_failure!
render json: {
success: false,
message: "Error with your login or password"
}, status: 401
end
end
api/registrations_controller.rb
class Api::RegistrationsController < ApiController
skip_before_filter :verify_authenticity_token
def create
user = User.new(user_params)
if user.save
render(
json: Jbuilder.encode do |j|
j.success true
j.email user.email
j.auth_token user.authentication_token
end,
status: 201
)
return
else
warden.custom_failure!
render json: user.errors, status: 422
end
end
end
在 config/routes.rb 中:
namespace :api, defaults: { format: "json" } do
devise_for :users
end
我有点不知所措,但我敢肯定,我未来的自己会在这里回顾某些事情并畏缩(通常是这样)。一些不确定的部分:
首先,您会注意到 Api::SessionsController 继承自 Devise::RegistrationsController 而 Api::RegistrationsController 继承自 ApiController(我还有一些其他控制器,例如 Api::EventsController < ApiController 可以处理更多我的其他模型的标准 REST 东西,并且与 Devise 没有太多联系。)这是一个非常丑陋的安排,但我想不出另一种方法来访问我在 Api::RegistrationsController 中需要的方法。我在上面链接的教程有 include Devise::Controllers::InternalHelpers 行,但这个模块似乎已在较新版本的 Devise 中被删除。
其次,我已经通过skip_before_filter :verify_authentication_token 行禁用了CSRF 保护。我怀疑这是否是一个好主意——我看到很多关于 JSON API 是否容易受到 CSRF 攻击的conflicting 或hard to understand 建议——但添加该行是我能得到该死的东西的唯一方法工作。
第三,我想确保了解用户登录后身份验证的工作原理。假设我有一个 API 调用 GET /api/friends,它返回当前用户的朋友列表。据我了解,iOS应用程序必须从数据库中获取用户的authentication_token(对于每个用户来说这是一个永远不会改变的固定值??),然后将其作为参数与每个请求一起提交,例如GET /api/friends?authentication_token=abcdefgh1234,然后我的Api::FriendsController 可以执行User.find_by(authentication_token: params[:authentication_token]) 之类的操作来获取current_user。真的这么简单,还是我遗漏了什么?
因此,对于任何设法一直阅读到这个庞大问题结尾的人,感谢您抽出宝贵的时间!总结一下:
- 这个登录系统安全吗? 或者有什么我忽略或误解的东西,例如当涉及到 CSRF 攻击?
- 我对用户登录后如何验证请求的理解是否正确?(请参阅上面的“第三...”。)
-
有什么办法可以清理或改进这段代码吗? 特别是丑陋的设计,让一个控制器从
Devise::RegistrationsController继承而其他控制器从ApiController继承。
谢谢!
【问题讨论】:
-
您的
Api::SessionsController是从Devise::RegistrationsController扩展而来的..
标签: ruby-on-rails json api security devise