使用AuthlogicvalidationAPI

我在我的应用程序中使用Authlogic进行身份validation,使用标准的User和UserSession模型。 我正在为我的应用程序构建API,并且我希望使用单个访问令牌对API访问进行身份validation。 在我的应用程序中,每个用户都belongs_to一个公司,其中有很多用户。 API用于访问属于公司的资源,因此我想为整个公司使用一个访问令牌。

我最初的想法是向公司添加一个虚拟用户,该用户只有访问API的权限,该公司将使用该访问令牌来授予对API的访问权限。 我似乎无法使用AuthLogic将用户的电子邮件和密码设置为空白,因此这不是平移。 我的下一个想法也许是我可以将acts_as_authentic添加到公司本身,但我不确定这是如何工作的。

我真的想使用Authlogic作为解决方案,因为它与我的ACL很好地集成,并且似乎具有我主要内置的function。

是否有可能有两个act_as_authentic模型? Authlogic中是否有一种我没想到的更简单的方法? 有没有办法可以使用虚拟用户的API密钥? 我该怎么做?

我这样做的方式是:

 class Something acts_as_authentic do |m| # API keys are auto generated (See +regenerate_api_key+.) # The password is not used for authentication (its just an api_key lookup), so a dummy field is used m.login_field = :api_key m.validate_login_field = false m.validate_email_field = false m.crypted_password_field = :api_key_hash m.require_password_confirmation = false m.validate_password_field = false m.crypto_provider = ApiKeyCrypto end end class ApiKeyCrypto def self.encrypt(*tokens) 'X' end def self.matches?(crypted, *tokens) crypted == 'X' end end #application_controller.rb def current_session return @current_session if defined?(@current_session) ... format.any(*api_formats) do @current_session = SomethingSession.find end end @current_session end def api_formats [:xml, :json] end 

这适用于ActiveResource FYI。

当然,你可以有两个模型acts_as_authentic 。 使用最小的Authlogic数据库字段设置Company ,并使用它的single_access_token进行API访问。 请注意,您的API不会知道哪个User正在使用系统,只知道Company