这是使用API​​密钥进行HTTP BASIC身份验证的合理方法吗?

时间:2009-01-17 23:04:26

标签: ruby-on-rails api authentication

我刚刚开始在rails应用程序上添加REST API,因为我只想暴露一些控制器/动作,所以我向ApplicationController添加了一个方法:

  def http_basic_authentication
    if request.format == Mime::XML
      authenticate_or_request_with_http_basic do |username, api_key|
        self.current_user = User.find(:first, :from => 'users, accounts', :conditions => ["accounts.id = users.account_id AND accounts.api_key = ?", api_key])
      end
    end
  end

然后我可以在我想要公开的单个控制器/操作上使用before_filter。有没有人有任何反馈,代码审查或更好的方法?

2 个答案:

答案 0 :(得分:1)

这可能会更清洁:

self.current_user = Account.find_by_api_key(api_key).user

答案 1 :(得分:0)

您可能会发现此处详述的方法很有用http://www.compulsivoco.com/2009/05/rails-api-authentication-using-restful-authentication/

这与常见的restful_authentication插件集成。

相关问题