简体   繁体   English

使用devise_token_auth时如何通过Rails API验证用户是否拥有请求的资源

[英]How to validate that a user owns the requested resource through Rails API when using devise_token_auth

I am building an API-only (for now) Rails app to serve as the back end for an Android app I'm building. 我正在构建一个仅API(目前)的Rails应用程序,以作为我正在构建的Android应用程序的后端。 I was previously using Firebase but wanted to do more processing on application data and I didn't want to bloat the mobile client with all the logic, so I am moving away from Firebase's real-time database and backing the application data using Rails. 我以前使用过Firebase,但想对应用程序数据进行更多处理,并且我不想让所有逻辑都使移动客户端膨胀,因此我不再使用Firebase的实时数据库,而是使用Rails支持应用程序数据。 I was also using Firebase's authentication which is very straightforward. 我还使用了非常简单的Firebase身份验证。 But it seems less complex for the overall system to keep all of this functionality in one place, so I'd like to perform auth and user management in the Rails app as well. 但是,将整个系统的所有功能都集中在一个地方似乎并不那么复杂,因此我也想在Rails应用程序中执行身份验证和用户管理。

I have installed devise_token_auth (seen here ) and finished the basic configuration. 我已经安装了devise_token_auth (在此处看到)并完成了基本配置。 The /auth route works correctly if params are provided and creates a user. 如果提供了参数并创建了一个用户,则/auth路由可以正常工作。 sign_in and sign_out both successfully create sessions and return some header information. sign_insign_out均可成功创建会话并返回一些头信息。 The important parts returned are client , access-token , and uid , which I need to use in future calls to the API. 返回的重要部分是clientaccess-tokenuid ,我需要在以后的API调用中使用它们。 I believe these are invalidated and replaced with each subsequent call. 我相信这些都是无效的,并在以后的每次调用中都被替换。 At this part of the flow is where I'm not sure how to proceed. 在流程的这一部分,我不确定如何进行。 I don't understand how the data in these headers is associated with the user who signed in and how I can validate that they own a resource they request. 我不了解这些标头中的数据如何与登录的用户相关联,以及如何验证他们拥有他们请求的资源。 To summarize the question another way: 用另一种方式总结问题:

How can I sign a user into the API and then validate which user is making subsequent API calls? 如何将用户登录到API,然后验证哪个用户在进行后续的API调用?

My Android app is a task manager, so I need to be able to validate for example that if user 1 requests task 3 , that they own that resource. 我的Android应用程序是一个任务管理器,因此我需要能够验证例如,如果user 1请求task 3 ,那么他们拥有该资源。 I'm also unsure how to direct index calls using the API. 我也不确定如何使用API​​定向索引调用。 That is, when the index endpoint is hit ( /tasks ), how can I identify from the data in the API headers which user's tasks I should retrieve. 也就是说,当命中索引端点( /tasks )时,如何从API标头中的数据中识别出应该检索哪些用户任务。

I haven't been able to find any solid tutorials or samples using devise_token_auth so I'm having trouble stitching together the interaction between the pieces I have now. 我无法使用devise_token_auth找到任何可靠的教程或示例,因此我很难将现在各部分之间的交互联系在一起。 I know this is a meaty question - thanks in advance for any guidance. 我知道这是一个棘手的问题-在此先感谢您的指导。

How can I [...] validate which user is making subsequent API calls? 我如何验证哪个用户正在进行后续的API调用?

With the current_user method. 使用current_user方法。 This is a built-in feature to the devise_token_auth gem. 这是devise_token_auth gem的内置功能

I need to be able to validate for example that if user 1 requests task 3, that they own that resource 我需要能够验证例如,如果用户1请求任务3,那么他们拥有该资源

There are many different approaches you could take. 您可以采用许多不同的方法。 You could just write some custom logic in each controller action, using the current_user method (and return 403 forbidden if necessary). 可以使用current_user方法在每个控制器操作中编写一些自定义逻辑(并在必要时返回403 forbidden )。

Or, you could use a popular "framework" solution for this such as CanCanCan or Pundit . 或者,您可以为此使用流行的“框架”解决方案,例如CanCanCanPundit I, and probably most of the modern community, would recommend Pundit . 我以及大多数现代社区会推荐Pundit

I highly advise you to read that library's README, as it's extremely helpful. 强烈建议您阅读该库的自述文件,因为它非常有帮助。 But for the example above, you could write something like this: 但是对于上面的示例,您可以编写如下内容:

class TasksController
  def show
    task = Task.find(params[:id])
    authorize(task) # !!!
    render task
  end
end

# app/policies/task_policy.rb
class TaskPolicy
  def show?
    record.user == user
  end
end

(Note that by default, the " user " in Pundit policies calls the method: current_user . This is all explained in the project's README.) (请注意,默认情况下,Pundit策略中的“ user ”调用方法: current_user 。这在项目的自述文件中都有详细说明。)

when the index endpoint is hit ( /tasks ), how can I identify from the data in the API headers which user's tasks I should retrieve 当索引端点被命中( /tasks )时,如何从API标头中的数据中识别出应该检索哪些用户任务

Again, this is all handled as part of Pundit 's standard features. 同样,所有这些都作为Pundit的标准功能的一部分进行处理。 You just need to define a TaskPolicy::Scope and call policy_scope(Task) in the controller - as explained here . 你只需要定义一个TaskPolicy::Scope ,并呼吁policy_scope(Task)控制器-作为解释在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用devise_token_auth rails gem正确验证用户身份? - how to properly authenticate user with devise_token_auth rails gem? 如何使用devise_token_auth在Rails中获取current_user? - How to get current_user in Rails using devise_token_auth? Rails:devise_token_auth - Rails: devise_token_auth 如何通过在Rails应用程序中直接访问API网址来获取current_user? 我正在使用ng-auth-token和devise_token_auth进行身份验证 - How can I get current_user by directly visiting an API url in rails app? I am using ng-auth-token and devise_token_auth for authentication Rails-devise_token_auth-在约束中访问当前用户 - Rails - devise_token_auth - Accessing current user in constraint 在 Rails 上的 Ruby 中带有 devise_token_auth 的“不允许的参数::用户,:会话” - "Unpermitted parameters: :user, :session" with devise_token_auth in Ruby on Rails 使用devise_token_auth的Rails Api给出了“ ActiveModel :: ForbiddenAttributesError: - Rails Api with devise_token_auth gives "ActiveModel::ForbiddenAttributesError: devise_token_auth如何通过令牌识别用户 - devise_token_auth how to identify a user by token 将devise_token_auth与表单身份验证一起使用 - Using devise_token_auth with form auth devise_token_auth和rails 5.2.0兼容性 - devise_token_auth and rails 5.2.0 Compatibility
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM