-
Notifications
You must be signed in to change notification settings - Fork 240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Grape integration? #138
Comments
The article seems to be implementing same José Valim gist which this gem packages. The idea behind Simple Token Authentication is precisely to make the implementation easier and less prone to errors, and you idea sound very well to me. In order to make possible to your If you want to see how adapters are built, you can take a look at #131 (the Rails::API adapter) and #124 (where I described step-by-step the adapter implementation). If you want to move quickly, just do fork and I'll help you with the adapter definition, pulling back the commits afterwards is easy. |
Hi @gonzalomelov, Would you give a try to a Grape adapter for Simple Token Authentication? # Gemfile
gem 'simple_token_authentication', git: 'git@github.com:gonzalo-bulnes/simple_token_authentication.git', branch: 'spike-add-grape-support' Hi @u2, for what I've seen you too may find that feature interesting ; ) Once the gem installed, you should be able to call Regards! Edit: Fixed missing |
Hi @gonzalo-bulnes
My controller for Grape looks like the following
Am I missing something? Thanks a lot |
Hi @ianchen06, Yes, indeed, the First of all, I'll disable the Once that done, for Grape users, an additional step will be necessary to enable the token authentication handling feature for each endpoint (I don't know how the I have some refactoring to do, however, to make sure the fallback option usage is straightforward, and I think it would be better if I created a new method called module V1
class Users < Grape::API
format :json
version 'v1', using: :path
acts_as_token_authentication_handler_for User, fallback: :none
desc 'Get all reports'
get '/reports' do
authenticate_user_from_token_or_fallback
# At this point, if the users credentials (email, authentication_token) were correct,
# then `current_user` is set.
#
# If the users credentials were missing or incorrect, the fallback was triggered:
# - with `fallback: :exception`, an exception was raised
# - with `fallback: :devise` (the default), Devise attempted to authenticate, and
# either `current_user` was set or an exception was raised by Devise
# - with `fallback: :none`, `current_user` is `nil`
#
# And you can do whatever you have to do with `current_user`.
report = Report.where(author: current_user) # for example
end
end
end (Note: I changed a little bit your example so we don't get confused by the different usages of What do you think? Does that sound reasonable? I mean: would that usage be something you would expect using Grape? |
Hi @ianchen06, I updated the the code with the improvements described above. That's an exploratory move, but you can already use the brand new # Gemfile
gem 'simple_token_authentication', git: 'git@github.com:gonzalo-bulnes/simple_token_authentication.git', branch: 'spike-add-grape-support-2' I look forward to your feedback! Edit: see the |
Hi @gonzalo-bulnes I have a issue related with this thread, I got an This is how I defined my Grapre Endpoint module Pools
class Data < Grape::API
acts_as_token_authentication_handler_for User, fallback: :exception
resource :pools do
params do
requires :id, type: Integer, desc: "Pool ID"
end
desc "Post payment to pool"
post ":id/payments" do
authenticate_user_from_token_or_fallback
byebug
end
end
end
end This is my Gemfile gem 'grape', '~> 0.10.0'
gem 'simple_token_authentication', github: 'gonzalo-bulnes/simple_token_authentication', branch: 'spike-add-grape-support-2' Can you give me a hint about whats happening or there is a bug? |
Hi @Kentverger, I think I know what's going on: I'm not sure if the right thing to do is defining the method for Do you know what is the relation between a given instance of # ...
post ':id/payments' do
pools_data_api.authenticate_user_from_token_or_fallback
end
# ... What do you think? |
I don't know whats is the relation between module Pools
class Data < Grape::API
acts_as_token_authentication_handler_for User, fallback: :exception
params do
requires :id, type: Integer, desc: "Pool ID"
end
desc "Post payment to pool"
post ":id/payments" do
authenticate_user_from_token_or_fallback
end
end
end What could be? |
Hi @Kentverger, The Grape adapter is intended to be used just as you did. When I wrote it, I added the corresponding callbacks to (Note: The adapters role is to define which class should represent the token authentication handler in each framework, there is one adapter for Rails, another for Rails::API, and the one for Grape. Despite their big "adapter" name they are pretty simple pieces of code indeed.) Specifically, what the Grape adapter does (and matters to us now) is defining that any # ...
post ":id/payments" do
# This piece of code defines a Grape::Endpoint instance.
# That's to say anything in this block is executed in the scope of that instance,
# not in the scope of the Grape::API instance,
# and authenticate_user_from_token_or_fallback is not defined there...
end
# ... I consider that as a bug in the adapter, I'm not sure how to handle it right now, but I'll explore it and keep you updated next week. |
TL;DR: If you're a Grape user and would like to use it with Simple Token Authentication, please let me know here. Some work has been done, but I need help defining an API which would make sense to Grape users, see #194. |
Hey @gonzalo-bulnes, would love to get Grape Integration to work. I forked the gem and merged master to the grape branch https://github.com/Juko/simple_token_authentication/tree/spike-add-grape-support-2, but this has broken |
Hello @Juko ! Merging The updated spike should make things easier to debug. Could you give it a try? I'm definitely interested in progressing on the Grape support, and you help is very much appreciated. : ) |
Hi, I have a question.
How can I integrate this gem with Grape?
I think that I have to set acts_as_token_authentication_handler_for on the root class of Grape API, as it is used here
What do you think?
Thanks
The text was updated successfully, but these errors were encountered: