多个模型的门卫资源所有者凭证流

时间:2014-09-14 14:38:57

标签: ruby-on-rails doorkeeper

我遇到的问题是我使用门卫resource owner credential flow来验证来自iOS应用的用户。我的项目有两个独立的用户模型(让我们称之为用户和管理员)。我的代码如下所示:

resource_owner_from_credentials do |routes|
  user = User.find_for_database_authentication(:email => params[:username])
  user if user && user.valid_password?(params[:password])
end

它有效,但我如何检查管理员呢?换句话说,我不知道登录的人是用户还是管理员 - 我如何检查这两者?

2 个答案:

答案 0 :(得分:9)

门卫为此目的提供范围(参见https://github.com/doorkeeper-gem/doorkeeper/wiki/Using-Scopes

所以在你的doorkeeper.rb文件中你可以这样做:

resource_owner_from_credentials do |routes|
  if params[:scope].present?
    case params[:scope]
      when "user"
        u = User.find_for_database_authentication(:email => params[:email])
      when "admin"
        u = Admin.find_for_database_authentication(:email => params[:email])
    end
  else 
    #default auth
    u = User.find_for_database_authentication(:email => params[:email])
  end
  u if u && u.valid_password?(params[:password])
end  

然后,为了验证您的资源(例如管理员),您可以提出类似的请求:

POST $HOST/oauth/token
{
  email: john@doe.com
  password: johndoe
  grant_type: password
  scope: admin
}

答案 1 :(得分:1)

您可以通过传入一个额外的参数,然后根据该参数选择要进行身份验证的模型来完成此操作。这与gdonato的答案类似,但是在守门员中的范围更好地用于管理向经过身份验证的应用程序授予的权限(即“授予此应用程序读取X并代表您写入Y的权限”)。

这是我正在使用的

resource_owner_from_credentials do |routes|
  if params[:user_type].present?
    case params[:user_type]
    when 'user'
      u = User.find_for_database_authentication(email: params[:email])
    when 'employer'
      u = Employer.find_for_database_authentication(email: params[:email])
    when 'admin'
      u = Administrator.find_for_database_authentication(email: params[:email])
    end
  end # I don't want a default auth user_type, so no 'else' block for me
  user if user && user.valid_password?(params[:password])
end

请注意,如果您使用范围而不是守门人尚未使用其他内容的参数来执行此操作,则必须配置范围如下:

# These are found in doorkeeper.rb, but they're commented by default.
# You would use whatever scopes you're wanting to check
default_scopes  :public
optional_scopes :write, :update

使用范围作为参数来区分用户和管理员可能无需在doorkeeper.rb中跳转default_scopes或optional_scopes,但仅作为门卫所期望的范围的副作用。

相关问题