0

我目前在我的 Rails 应用程序中配置了 Devise、Doorkeeper 和葡萄。配置了 Devise 和 Doorkeeper,以便我可以在网站上注册和登录 Devise,并且 Doorkeeper 提供可以创建令牌的 oAuth 端点。

如何将令牌添加到 HttpRequest 并用它保护葡萄 API?

编辑:所以我尝试实现 Tom Hert 建议的 Winebouncer 实现。我按照https://github.com/antek-drzewiecki/wine_bouncer上的说明进行操作

我已经安装了宝石。我已将 config/initializers/wine_bouncer.rb 定义如下。

WineBouncer.configure do |config|
  config.auth_strategy = :default

  config.define_resource_owner do
    User.find(doorkeeper_access_token.resource_owner_id) if doorkeeper_access_token
  end
end

我已在我的基本 api 控制器中将 Winebouncer 注册为葡萄中的中间件。应用\控制器\api\base.rb

module API
  class Base < Grape::API
    mount API::V1::Base
    use ::WineBouncer::OAuth2
  end
end

我将我的项目控制器安装在我的 V1 基本控制器 app\controllers\api\v1\base.rb

module API
  module V1
    class Base < Grape::API
      mount API::V1::Projects
    end
  end
end

这是我的项目控制器 app\controllers\api\v1\projects.rb

module API
  module V1
    class Projects < Grape::API
      version 'v1'
      format :json

      resource :projects do
        desc "Return list of projects" , auth: { scopes: [] }
        get do
          Project.all
        end
      end
    end
  end
end

老实说,我还不知道描述中的“, auth: { scopes: [] }” 应该如何工作。以及如何将令牌添加到请求中,但我希望我的请求在没有添加令牌时被阻止。但请求仍在产生 json 数据。

4

2 回答 2

2

我在这里发现了相当有趣的代码:https ://github.com/fuCtor/grape-doorkeeper 似乎仍然在维护。但我认为这很好,只是为了了解那里发生了什么。

我会推荐这个:https ://github.com/antek-drzewiecki/wine_bouncer 正如页面上所说:

使用 Doorkeeper 保护您宝贵的 Grape API。WineBouncer 使用最小的修改,使魔术发生。

于 2014-10-20T18:37:30.293 回答
0

obedeijn,我刚刚注意到你关于stackoverflow的问题。WineBouncer 就像看门人一样工作,它查找带有“Bearer x”的 Authorizations 标头,其中 x 是令牌。

于 2014-11-28T10:07:02.870 回答