0
votes

I have currently configured Devise,Doorkeeper and grape in my rails application. Devise and Doorkeeper are configured so that I can register and login with Devise on the website and Doorkeeper provides oAuth endpoints that can create tokens.

How can I add a token to a HttpRequest and protect the grape API with it?

Edit: So I tried to implement the Winebouncer implementation Tom Hert suggested. I followed the instructions on https://github.com/antek-drzewiecki/wine_bouncer

I have installed the gem. I have defined config/initializers/wine_bouncer.rb as the following.

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

I have registered Winebouncer as middleware in grape in my base api controller. app\controllers\api\base.rb

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

I mounted my projects controller in my V1 base controller app\controllers\api\v1\base.rb

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

And this is my projectscontroller 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

To be honest I don't yet know how the ", auth: { scopes: [] }" in the description is suppossed to work. And how to add the token to a request, but I would expect my request but be blocked when no token is added. But the the request is still producing the json data.

2

2 Answers

2
votes

I found quite interesting code here: https://github.com/fuCtor/grape-doorkeeper It seems to be still maintained. But I think this is good just to get the idea of what is going on there.

I would recommend this: https://github.com/antek-drzewiecki/wine_bouncer As said on the page:

Protect your precious Grape API with Doorkeeper. WineBouncer uses minimal modification, to make the magic happen.

0
votes

obedeijn, i just noticed your question on stackoverflow. WineBouncer works just like doorkeeper, it looks for the Authorizations header with a "Bearer x" where x is the token.