I made an API with Rails that authenticates using authenticate_with_http_token
method. This is working great.
The API uses tus-ruby-server for resumable file uploads. Tus::Server
is a Roda app. I can use hooks to add authentication. It is working inside the Rails app (its endpoints are mounted in routes.rb
).
Rails already provides authenticate_with_http_token
which is a very well crafted implementation to authenticate via tokens. Is it possible to use this method somehow in the Roda app inside these hooks?
So far I couldn't succeed in doing so and I ended implementing a simpler way to read the header and parse the token that leaved me with a feeling that I over-implemented something already provided by Rails and that the solution I did is much less robust of what Rails already provides.
I tried to include the Rails method this way.
# config/initializers/tus.rb
Tus::Server.before
extend ActionController::HttpAuthentication::Token::ControllerMethods
authenticate_with_http_token { |token| User.find_by(token: token) }
end
But ended up with this error.
NoMethodError: undefined method `authorization' for #<Tus::Server::RodaRequest:0x00007f9fe3b48450>
The roda request does not understands the ActionDispatch::Request#authorization
called here. I got stuck here as I don't understand how it may be possible to add ActionDispatch methods in the RodaRequest
object.
I may be missing something obvious but so far I'm not seeing it.
Am I missing something more fundamental to get the authenticate_with_http_token
method working inside the Tus::Server
hook?