我目前在我的 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 数据。